yujiepan commited on
Commit
a67ba35
·
verified ·
1 Parent(s): 060e3d6

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 tiny model is for debugging. It is randomly initialized with the config adapted from [openbmb/MiniCPM4-8B](https://huggingface.co/openbmb/MiniCPM4-8B).
12
+
13
+ ### Example usage:
14
+
15
+ ```python
16
+ import torch
17
+
18
+ from transformers import AutoModelForCausalLM, AutoTokenizer
19
+
20
+ model_id = "yujiepan/minicpm4-tiny-random"
21
+
22
+ device = "cuda"
23
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
24
+ model = AutoModelForCausalLM.from_pretrained(
25
+ model_id, torch_dtype=torch.bfloat16, device_map=device, trust_remote_code=True)
26
+
27
+ # User can directly use the chat interface
28
+ # responds, history = model.chat(tokenizer, "Write an article about Artificial Intelligence.", temperature=0.7, top_p=0.7)
29
+ # print(responds)
30
+
31
+ # User can also use the generate interface
32
+ messages = [
33
+ {"role": "user", "content": "Write an article about Artificial Intelligence."},
34
+ ]
35
+ prompt_text = tokenizer.apply_chat_template(
36
+ messages,
37
+ tokenize=False,
38
+ add_generation_prompt=True,
39
+ )
40
+ model_inputs = tokenizer([prompt_text], return_tensors="pt").to(device)
41
+
42
+ model_outputs = model.generate(
43
+ **model_inputs,
44
+ max_new_tokens=32,
45
+ top_p=0.7,
46
+ temperature=0.7
47
+ )
48
+ output_token_ids = [
49
+ model_outputs[i][len(model_inputs[i]):] for i in range(len(model_inputs['input_ids']))
50
+ ]
51
+
52
+ responses = tokenizer.batch_decode(output_token_ids, skip_special_tokens=True)[0]
53
+ print(responses)
54
+ ```
55
+
56
+ ### Codes to create this repo:
57
+
58
+ ```python
59
+ import json
60
+ from pathlib import Path
61
+
62
+ import torch
63
+
64
+ import accelerate
65
+ from huggingface_hub import hf_hub_download
66
+ from transformers import (
67
+ AutoConfig,
68
+ AutoModelForCausalLM,
69
+ AutoTokenizer,
70
+ GenerationConfig,
71
+ set_seed,
72
+ )
73
+
74
+ source_model_id = "openbmb/MiniCPM4-8B"
75
+ save_folder = "/tmp/yujiepan/minicpm4-tiny-random"
76
+
77
+ processor = AutoTokenizer.from_pretrained(source_model_id)
78
+ processor.save_pretrained(save_folder)
79
+
80
+ with open(hf_hub_download(source_model_id, filename='config.json', repo_type='model'), 'r', encoding='utf-8') as f:
81
+ config_json = json.load(f)
82
+ config_json["hidden_size"] = 64
83
+ config_json['intermediate_size'] = 128
84
+ config_json['num_attention_heads'] = 2
85
+ config_json['num_key_value_heads'] = 1
86
+ config_json['dim_model_base'] = 32
87
+ config_json['num_hidden_layers'] = 2
88
+ config_json['tie_word_embeddings'] = True
89
+ for k, v in config_json['auto_map'].items():
90
+ config_json['auto_map'][k] = f'{source_model_id}--{v}'
91
+ automap = config_json['auto_map']
92
+ factor = config_json['rope_scaling']['long_factor']
93
+ config_json['rope_scaling']['long_factor'] = factor[:16]
94
+ config_json['rope_scaling']['short_factor'] = factor[:16]
95
+ with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
96
+ json.dump(config_json, f, indent=2)
97
+
98
+ config = AutoConfig.from_pretrained(
99
+ save_folder,
100
+ trust_remote_code=True,
101
+ )
102
+ print(config)
103
+ torch.set_default_dtype(torch.bfloat16)
104
+ model = AutoModelForCausalLM.from_config(config, trust_remote_code=True)
105
+ torch.set_default_dtype(torch.float32)
106
+ model.generation_config = GenerationConfig.from_pretrained(
107
+ source_model_id, trust_remote_code=True,
108
+ )
109
+ set_seed(42)
110
+ with torch.no_grad():
111
+ for name, p in sorted(model.named_parameters()):
112
+ torch.nn.init.normal_(p, 0, 0.2)
113
+ print(name, p.shape)
114
+ pass
115
+ model.save_pretrained(save_folder)
116
+ with open(f"{save_folder}/config.json", "r", encoding='utf-8') as f:
117
+ config_json = json.load(f)
118
+ config_json['auto_map'] = automap
119
+ with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
120
+ json.dump(config_json, f, indent=2)
121
+ for python_file in Path(save_folder).glob('*.py'):
122
+ python_file.unlink()
123
+ ```
added_tokens.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "<|execute_end|>": 73444,
3
+ "<|execute_start|>": 73443,
4
+ "<|fim_middle|>": 73446,
5
+ "<|fim_prefix|>": 73445,
6
+ "<|fim_suffix|>": 73447,
7
+ "<|im_end|>": 73440,
8
+ "<|im_start|>": 73441,
9
+ "<|tool_call|>": 73442
10
+ }
config.json ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "MiniCPMForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "openbmb/MiniCPM4-8B--configuration_minicpm.MiniCPMConfig",
9
+ "AutoModel": "openbmb/MiniCPM4-8B--modeling_minicpm.MiniCPMModel",
10
+ "AutoModelForCausalLM": "openbmb/MiniCPM4-8B--modeling_minicpm.MiniCPMForCausalLM",
11
+ "AutoModelForSeq2SeqLM": "openbmb/MiniCPM4-8B--modeling_minicpm.MiniCPMForCausalLM",
12
+ "AutoModelForSequenceClassification": "openbmb/MiniCPM4-8B--modeling_minicpm.MiniCPMForSequenceClassification"
13
+ },
14
+ "bos_token_id": 1,
15
+ "dim_model_base": 32,
16
+ "eos_token_id": [
17
+ 2,
18
+ 73440
19
+ ],
20
+ "hidden_act": "silu",
21
+ "hidden_size": 64,
22
+ "initializer_range": 0.1,
23
+ "intermediate_size": 128,
24
+ "max_position_embeddings": 32768,
25
+ "model_type": "minicpm",
26
+ "mup_denominator": 32,
27
+ "num_attention_heads": 2,
28
+ "num_hidden_layers": 2,
29
+ "num_key_value_heads": 1,
30
+ "pad_token_id": 2,
31
+ "pretraining_tp": 1,
32
+ "rms_norm_eps": 1e-06,
33
+ "rope_scaling": {
34
+ "long_factor": [
35
+ 0.9977997200264581,
36
+ 1.014658295992452,
37
+ 1.0349680404997148,
38
+ 1.059429246056193,
39
+ 1.0888815016813513,
40
+ 1.1243301355211495,
41
+ 1.166977103606075,
42
+ 1.2182568066927284,
43
+ 1.2798772354275727,
44
+ 1.3538666751582975,
45
+ 1.4426259039919596,
46
+ 1.5489853358570191,
47
+ 1.6762658237220625,
48
+ 1.8283407612492941,
49
+ 2.0096956085876183,
50
+ 2.225478927469756
51
+ ],
52
+ "original_max_position_embeddings": 32768,
53
+ "rope_type": "longrope",
54
+ "short_factor": [
55
+ 0.9977997200264581,
56
+ 1.014658295992452,
57
+ 1.0349680404997148,
58
+ 1.059429246056193,
59
+ 1.0888815016813513,
60
+ 1.1243301355211495,
61
+ 1.166977103606075,
62
+ 1.2182568066927284,
63
+ 1.2798772354275727,
64
+ 1.3538666751582975,
65
+ 1.4426259039919596,
66
+ 1.5489853358570191,
67
+ 1.6762658237220625,
68
+ 1.8283407612492941,
69
+ 2.0096956085876183,
70
+ 2.225478927469756
71
+ ]
72
+ },
73
+ "rope_theta": 10000.0,
74
+ "scale_depth": 1.4,
75
+ "scale_emb": 12,
76
+ "sparse_config": null,
77
+ "torch_dtype": "bfloat16",
78
+ "transformers_version": "4.51.3",
79
+ "use_cache": true,
80
+ "vocab_size": 73448
81
+ }
generation_config.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 1,
3
+ "do_sample": true,
4
+ "eos_token_id": [
5
+ 2,
6
+ 73440
7
+ ],
8
+ "pad_token_id": 2,
9
+ "temperature": 0.8,
10
+ "top_p": 0.8,
11
+ "transformers_version": "4.51.3",
12
+ "trust_remote_code": true
13
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:146c31f9e7684c662fb314c0132dcb6b71ee3c746aedc864a3739bd99541865b
3
+ size 9551568
special_tokens_map.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_end|>",
4
+ "<|im_start|>",
5
+ "<|tool_call|>",
6
+ "<|execute_start|>",
7
+ "<|execute_end|>",
8
+ "<|fim_prefix|>",
9
+ "<|fim_middle|>",
10
+ "<|fim_suffix|>"
11
+ ],
12
+ "bos_token": {
13
+ "content": "<s>",
14
+ "lstrip": false,
15
+ "normalized": false,
16
+ "rstrip": false,
17
+ "single_word": false
18
+ },
19
+ "eos_token": {
20
+ "content": "<|im_end|>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false
25
+ },
26
+ "unk_token": {
27
+ "content": "<unk>",
28
+ "lstrip": false,
29
+ "normalized": false,
30
+ "rstrip": false,
31
+ "single_word": false
32
+ }
33
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bb74d51116831c3bf65db812c553f94ab0c88dcf97a5bbb37e3504f6d359c530
3
+ size 1181204
tokenizer_config.json ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "add_prefix_space": null,
5
+ "added_tokens_decoder": {
6
+ "0": {
7
+ "content": "<unk>",
8
+ "lstrip": false,
9
+ "normalized": false,
10
+ "rstrip": false,
11
+ "single_word": false,
12
+ "special": true
13
+ },
14
+ "1": {
15
+ "content": "<s>",
16
+ "lstrip": false,
17
+ "normalized": false,
18
+ "rstrip": false,
19
+ "single_word": false,
20
+ "special": true
21
+ },
22
+ "2": {
23
+ "content": "</s>",
24
+ "lstrip": false,
25
+ "normalized": false,
26
+ "rstrip": false,
27
+ "single_word": false,
28
+ "special": true
29
+ },
30
+ "73440": {
31
+ "content": "<|im_end|>",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false,
36
+ "special": true
37
+ },
38
+ "73441": {
39
+ "content": "<|im_start|>",
40
+ "lstrip": false,
41
+ "normalized": false,
42
+ "rstrip": false,
43
+ "single_word": false,
44
+ "special": true
45
+ },
46
+ "73442": {
47
+ "content": "<|tool_call|>",
48
+ "lstrip": false,
49
+ "normalized": false,
50
+ "rstrip": false,
51
+ "single_word": false,
52
+ "special": true
53
+ },
54
+ "73443": {
55
+ "content": "<|execute_start|>",
56
+ "lstrip": false,
57
+ "normalized": false,
58
+ "rstrip": false,
59
+ "single_word": false,
60
+ "special": true
61
+ },
62
+ "73444": {
63
+ "content": "<|execute_end|>",
64
+ "lstrip": false,
65
+ "normalized": false,
66
+ "rstrip": false,
67
+ "single_word": false,
68
+ "special": true
69
+ },
70
+ "73445": {
71
+ "content": "<|fim_prefix|>",
72
+ "lstrip": false,
73
+ "normalized": false,
74
+ "rstrip": false,
75
+ "single_word": false,
76
+ "special": true
77
+ },
78
+ "73446": {
79
+ "content": "<|fim_middle|>",
80
+ "lstrip": false,
81
+ "normalized": false,
82
+ "rstrip": false,
83
+ "single_word": false,
84
+ "special": true
85
+ },
86
+ "73447": {
87
+ "content": "<|fim_suffix|>",
88
+ "lstrip": false,
89
+ "normalized": false,
90
+ "rstrip": false,
91
+ "single_word": false,
92
+ "special": true
93
+ }
94
+ },
95
+ "additional_special_tokens": [
96
+ "<|im_end|>",
97
+ "<|im_start|>",
98
+ "<|tool_call|>",
99
+ "<|execute_start|>",
100
+ "<|execute_end|>",
101
+ "<|fim_prefix|>",
102
+ "<|fim_middle|>",
103
+ "<|fim_suffix|>"
104
+ ],
105
+ "bos_token": "<s>",
106
+ "chat_template": "{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}",
107
+ "clean_up_tokenization_spaces": false,
108
+ "eos_token": "<|im_end|>",
109
+ "extra_special_tokens": {},
110
+ "legacy": true,
111
+ "model_max_length": 1000000000000000019884624838656,
112
+ "pad_token": null,
113
+ "sp_model_kwargs": {},
114
+ "spaces_between_special_tokens": false,
115
+ "tokenizer_class": "LlamaTokenizer",
116
+ "unk_token": "<unk>",
117
+ "use_default_system_prompt": false
118
+ }