Views
No views yet
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4MODEL_PATH = "Intel/GLM-4.7-int4-mixed-AutoRound"
5messages = [{"role": "user", "content": "Give me a short introduction to large language model."}]
6tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
7inputs = tokenizer.apply_chat_template(
8 messages,
9 tokenize=True,
10 add_generation_prompt=True,
11 return_dict=True,
12 return_tensors="pt",
13)
14model = AutoModelForCausalLM.from_pretrained(
15 pretrained_model_name_or_path=MODEL_PATH,
16 torch_dtype=torch.bfloat16,
17 device_map="auto",
18)
19inputs = inputs.to(model.device)
20inputs.pop("token_type_ids")
21generated_ids = model.generate(**inputs, max_new_tokens=512, do_sample=False)
22output_text = tokenizer.decode(generated_ids[0][inputs.input_ids.shape[1] :])
23print(output_text)
24"""
251. **Analyze the Request:**
26 * **Topic:** Large Language Models (LLMs).
27 * **Format:** Short introduction.
28 * **Goal:** Explain what they are, how they work (briefly), and why they matter, in a concise and accessible way.
29
302. **Identify Key Concepts to Cover:**
31 * Definition: What is an LLM? (AI, text processing).
32 * Mechanism: How does it work? (Training on massive data, predicting the next word).
33 * Capabilities: What can it do? (Writing, coding, translating, summarizing).
34 * Examples: ChatGPT, Claude, Llama (to make it concrete).
35 * Significance: Why is it important? (Versatility, human-like interaction).
36
373. **Drafting - Section by Section:**
38
39 * *The Hook/Definition:* Start with a simple definition. It's a type of AI trained on text.
40 * *The "How":* Explain the core mechanism simply. It's not magic; it's probability. "Next token prediction."
41 * *The "Scale":* Mention the "Large" part. Huge datasets, huge parameters.
42 * *The "What can it do":* List common use cases (chatbots, writing, coding).
43 * *The "Why it matters":* It's a shift in how we interact with computers (natural language).
44
454. **Refining and Condensing (Iterative Process):**
46
47 * *Draft 1 (Mental Outline):* A Large Language Model is an AI that reads a lot of text. It uses deep learning to understand patterns. It predicts the next word in a sentence. Because it's trained on the internet, it knows a lot. It can write essays, code, and answer questions. Examples are GPT-4. It changes how we use computers because we can talk to them normally.
48
49 * *Draft 2 (Polishing for flow and tone):* A Large Language Model (LLM) is a deep learning algorithm that can perform a variety of natural language processing tasks. It is trained on massive amounts of text data from the internet. The fundamental way it works is by predicting the next word in a sequence based on context. This allows it to generate human-like text, translate languages, and write code. Popular examples include ChatGPT and Claude. They represent a
50"""1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3import transformers
4from auto_round import AutoRound
5from auto_round.utils import llm_load_model
6
7model_name = "zai-org/GLM-4.7"
8model, tokenizer = llm_load_model(model_name, device="cpu")
9
10layer_config = {}
11for n, m in model.named_modules():
12 if isinstance(m, torch.nn.Linear):
13 if "expert" in n and "shared_experts" not in n:
14 layer_config[n] = {"bits": 4}
15 print(n, 4)
16 elif n != "lm_head":
17 layer_config[n] = {"bits": 8}
18 print(n, 8)
19
20autoround = AutoRound(model, tokenizer, iters=0, layer_config=layer_config, disable_opt_rtn=True)
21autoround.quantize_and_save(format="auto_round", output_dir="tmp_autoround")
22