yujiepan commited on
Commit
fe15d36
·
verified ·
1 Parent(s): 7635095

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +94 -0
README.md ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ pipeline_tag: text-generation
4
+ inference: true
5
+ widget:
6
+ - text: Hello!
7
+ example_title: Hello world
8
+ group: Python
9
+ ---
10
+
11
+ This model is for debugging. It is randomly initialized with the config from [Qwen/QwQ-32B-Preview](https://huggingface.co/Qwen/QwQ-32B-Preview) but is of smaller size.
12
+
13
+ Codes:
14
+ ```python
15
+ from transformers import AutoModelForCausalLM, AutoTokenizer
16
+ import transformers
17
+ import torch
18
+ import os
19
+ from huggingface_hub import create_repo, upload_folder
20
+ import accelerate
21
+
22
+ model_id = 'Qwen/QwQ-32B-Preview'
23
+ save_path = '/tmp/yujiepan/QwQ-tiny-random'
24
+ repo_id = 'yujiepan/QwQ-tiny-random'
25
+
26
+ os.system(f'rm -rf {save_path}')
27
+
28
+ config = transformers.AutoConfig.from_pretrained(
29
+ model_id,
30
+ trust_remote_code=True,
31
+ )
32
+ config._name_or_path = model_id
33
+ config.hidden_size = 8
34
+ config.intermediate_size = 16
35
+ config.num_key_value_heads = 1
36
+ config.num_attention_heads = 2
37
+ config.num_hidden_layers = 2
38
+ config.max_window_layers = 1
39
+
40
+ model = transformers.AutoModelForCausalLM.from_config(
41
+ config,
42
+ trust_remote_code=True,
43
+ )
44
+ model.generation_config = transformers.GenerationConfig.from_pretrained(
45
+ model_id)
46
+ model = model.to(torch.bfloat16)
47
+
48
+ transformers.set_seed(42)
49
+ num_params = 0
50
+ with torch.no_grad():
51
+ for name, p in sorted(model.named_parameters()):
52
+ print(name, p.shape)
53
+ torch.nn.init.uniform_(p, -0.5, 0.5)
54
+ num_params += p.numel()
55
+ print("Total number of parameters:", num_params)
56
+ model.save_pretrained(save_path)
57
+
58
+ tokenizer = transformers.AutoTokenizer.from_pretrained(
59
+ model_id,
60
+ trust_remote_code=True,
61
+ )
62
+ tokenizer.save_pretrained(save_path)
63
+
64
+ os.system(f'ls -alh {save_path}')
65
+ create_repo(repo_id, exist_ok=True)
66
+ upload_folder(repo_id=repo_id, folder_path=save_path)
67
+
68
+
69
+ def try_example(model, tokenizer):
70
+ prompt = "How many r in strawberry."
71
+ messages = [
72
+ {"role": "system", "content": "You are a helpful and harmless assistant. You should think step-by-step."},
73
+ {"role": "user", "content": prompt}
74
+ ]
75
+ text = tokenizer.apply_chat_template(
76
+ messages,
77
+ tokenize=False,
78
+ add_generation_prompt=True
79
+ )
80
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
81
+
82
+ generated_ids = model.generate(
83
+ **model_inputs,
84
+ max_new_tokens=32
85
+ )
86
+ generated_ids = [
87
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
88
+ ]
89
+
90
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
91
+ print(response)
92
+
93
+ try_example(model, tokenizer)
94
+ ```