Delete README.md
Browse files
README.md
DELETED
|
@@ -1,193 +0,0 @@
|
|
| 1 |
-
---
|
| 2 |
-
library_name: transformers
|
| 3 |
-
pipeline_tag: image-text-to-text
|
| 4 |
-
inference: true
|
| 5 |
-
widget:
|
| 6 |
-
- text: Hello!
|
| 7 |
-
example_title: Hello world
|
| 8 |
-
group: Python
|
| 9 |
-
base_model:
|
| 10 |
-
- THUDM/GLM-4.1V-9B-Thinking
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
-
This tiny model is for debugging. It is randomly initialized with the config adapted from [THUDM/GLM-4.1V-9B-Thinking](https://huggingface.co/THUDM/GLM-4.1V-9B-Thinking).
|
| 14 |
-
|
| 15 |
-
### Example usage:
|
| 16 |
-
|
| 17 |
-
```python
|
| 18 |
-
import os
|
| 19 |
-
import re
|
| 20 |
-
|
| 21 |
-
import torch
|
| 22 |
-
|
| 23 |
-
from transformers import AutoProcessor, Glm4vForConditionalGeneration
|
| 24 |
-
|
| 25 |
-
model_id = "yujiepan/glm-4.1v-tiny-random"
|
| 26 |
-
messages = [
|
| 27 |
-
{
|
| 28 |
-
"role": "user",
|
| 29 |
-
"content": [
|
| 30 |
-
{
|
| 31 |
-
"type": "image",
|
| 32 |
-
"url": "https://upload.wikimedia.org/wikipedia/commons/f/fa/Grayscale_8bits_palette_sample_image.png"
|
| 33 |
-
},
|
| 34 |
-
{
|
| 35 |
-
"type": "text",
|
| 36 |
-
"text": "describe this image"
|
| 37 |
-
}
|
| 38 |
-
],
|
| 39 |
-
}
|
| 40 |
-
]
|
| 41 |
-
processor = AutoProcessor.from_pretrained(model_id)
|
| 42 |
-
model = Glm4vForConditionalGeneration.from_pretrained(
|
| 43 |
-
pretrained_model_name_or_path=model_id,
|
| 44 |
-
torch_dtype=torch.bfloat16,
|
| 45 |
-
device_map="auto",
|
| 46 |
-
)
|
| 47 |
-
inputs = processor.apply_chat_template(
|
| 48 |
-
messages,
|
| 49 |
-
tokenize=True,
|
| 50 |
-
add_generation_prompt=True,
|
| 51 |
-
return_dict=True,
|
| 52 |
-
return_tensors="pt"
|
| 53 |
-
).to(model.device)
|
| 54 |
-
generated_ids = model.generate(**inputs, max_new_tokens=16)
|
| 55 |
-
output_text = processor.decode(generated_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=False)
|
| 56 |
-
print(output_text)
|
| 57 |
-
```
|
| 58 |
-
|
| 59 |
-
### Codes to create this repo:
|
| 60 |
-
|
| 61 |
-
```python
|
| 62 |
-
import json
|
| 63 |
-
from pathlib import Path
|
| 64 |
-
|
| 65 |
-
import torch
|
| 66 |
-
|
| 67 |
-
import accelerate
|
| 68 |
-
from huggingface_hub import file_exists, hf_hub_download
|
| 69 |
-
from transformers import (
|
| 70 |
-
AutoConfig,
|
| 71 |
-
AutoModelForCausalLM,
|
| 72 |
-
AutoProcessor,
|
| 73 |
-
GenerationConfig,
|
| 74 |
-
set_seed,
|
| 75 |
-
)
|
| 76 |
-
from transformers import AutoProcessor, Glm4vForConditionalGeneration
|
| 77 |
-
|
| 78 |
-
source_model_id = "THUDM/GLM-4.1V-9B-Thinking"
|
| 79 |
-
save_folder = "/tmp/yujiepan/glm-4.1v-tiny-random"
|
| 80 |
-
|
| 81 |
-
processor = AutoProcessor.from_pretrained(source_model_id, trust_remote_code=True)
|
| 82 |
-
processor.save_pretrained(save_folder)
|
| 83 |
-
|
| 84 |
-
with open(hf_hub_download(source_model_id, filename='config.json', repo_type='model'), 'r', encoding='utf-8') as f:
|
| 85 |
-
config_json = json.load(f)
|
| 86 |
-
config_json['hidden_size'] = 64
|
| 87 |
-
config_json['intermediate_size'] = 128
|
| 88 |
-
config_json['num_attention_heads'] = 2
|
| 89 |
-
config_json['num_hidden_layers'] = 2
|
| 90 |
-
config_json['num_key_value_heads'] = 1
|
| 91 |
-
config_json['tie_word_embeddings'] = True
|
| 92 |
-
config_json['vision_config']['hidden_size'] = 64
|
| 93 |
-
config_json['vision_config']['depth'] = 2
|
| 94 |
-
config_json['vision_config']['num_heads'] = 2
|
| 95 |
-
config_json['vision_config']['intermediate_size'] = 128
|
| 96 |
-
config_json['vision_config']['out_hidden_size'] = 64
|
| 97 |
-
config_json['rope_scaling']['mrope_section'] = [2, 2, 4]
|
| 98 |
-
|
| 99 |
-
with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
|
| 100 |
-
json.dump(config_json, f, indent=2)
|
| 101 |
-
|
| 102 |
-
config = AutoConfig.from_pretrained(
|
| 103 |
-
save_folder,
|
| 104 |
-
trust_remote_code=True,
|
| 105 |
-
)
|
| 106 |
-
print(config)
|
| 107 |
-
torch.set_default_dtype(torch.bfloat16)
|
| 108 |
-
model = Glm4vForConditionalGeneration(config)
|
| 109 |
-
torch.set_default_dtype(torch.float32)
|
| 110 |
-
if file_exists(filename="generation_config.json", repo_id=source_model_id, repo_type='model'):
|
| 111 |
-
model.generation_config = GenerationConfig.from_pretrained(
|
| 112 |
-
source_model_id, trust_remote_code=True,
|
| 113 |
-
)
|
| 114 |
-
set_seed(42)
|
| 115 |
-
model = model.cpu() # cpu is more stable for random initialization across machines
|
| 116 |
-
with torch.no_grad():
|
| 117 |
-
for name, p in sorted(model.named_parameters()):
|
| 118 |
-
torch.nn.init.normal_(p, 0, 0.2)
|
| 119 |
-
print(name, p.shape)
|
| 120 |
-
model.save_pretrained(save_folder)
|
| 121 |
-
print(model)
|
| 122 |
-
```
|
| 123 |
-
|
| 124 |
-
### Printing the model:
|
| 125 |
-
|
| 126 |
-
```text
|
| 127 |
-
Glm4vForConditionalGeneration(
|
| 128 |
-
(model): Glm4vModel(
|
| 129 |
-
(visual): Glm4vVisionModel(
|
| 130 |
-
(embeddings): Glm4vVisionEmbeddings(
|
| 131 |
-
(position_embedding): Embedding(576, 64)
|
| 132 |
-
)
|
| 133 |
-
(patch_embed): Glm4vVisionPatchEmbed(
|
| 134 |
-
(proj): Conv3d(3, 64, kernel_size=(2, 14, 14), stride=(2, 14, 14))
|
| 135 |
-
)
|
| 136 |
-
(rotary_pos_emb): Glm4vVisionRotaryEmbedding()
|
| 137 |
-
(blocks): ModuleList(
|
| 138 |
-
(0-1): 2 x Glm4vVisionBlock(
|
| 139 |
-
(norm1): Glm4vRMSNorm((64,), eps=1e-05)
|
| 140 |
-
(norm2): Glm4vRMSNorm((64,), eps=1e-05)
|
| 141 |
-
(attn): Glm4vVisionAttention(
|
| 142 |
-
(qkv): Linear(in_features=64, out_features=192, bias=False)
|
| 143 |
-
(proj): Linear(in_features=64, out_features=64, bias=False)
|
| 144 |
-
)
|
| 145 |
-
(mlp): Glm4VisionMlp(
|
| 146 |
-
(gate_proj): Linear(in_features=64, out_features=64, bias=False)
|
| 147 |
-
(up_proj): Linear(in_features=64, out_features=64, bias=False)
|
| 148 |
-
(down_proj): Linear(in_features=64, out_features=64, bias=False)
|
| 149 |
-
(act_fn): SiLU()
|
| 150 |
-
)
|
| 151 |
-
)
|
| 152 |
-
)
|
| 153 |
-
(merger): Glm4vVisionPatchMerger(
|
| 154 |
-
(proj): Linear(in_features=64, out_features=64, bias=False)
|
| 155 |
-
(post_projection_norm): LayerNorm((64,), eps=1e-05, elementwise_affine=True)
|
| 156 |
-
(gate_proj): Linear(in_features=64, out_features=128, bias=False)
|
| 157 |
-
(up_proj): Linear(in_features=64, out_features=128, bias=False)
|
| 158 |
-
(down_proj): Linear(in_features=128, out_features=64, bias=False)
|
| 159 |
-
(act1): GELU(approximate='none')
|
| 160 |
-
(act_fn): SiLU()
|
| 161 |
-
)
|
| 162 |
-
(post_conv_layernorm): Glm4vRMSNorm((64,), eps=1e-05)
|
| 163 |
-
(downsample): Conv2d(64, 64, kernel_size=(2, 2), stride=(2, 2))
|
| 164 |
-
(post_layernorm): Glm4vRMSNorm((64,), eps=1e-05)
|
| 165 |
-
)
|
| 166 |
-
(language_model): Glm4vTextModel(
|
| 167 |
-
(embed_tokens): Embedding(151552, 64, padding_idx=151329)
|
| 168 |
-
(layers): ModuleList(
|
| 169 |
-
(0-1): 2 x Glm4vTextDecoderLayer(
|
| 170 |
-
(self_attn): Glm4vTextAttention(
|
| 171 |
-
(q_proj): Linear(in_features=64, out_features=64, bias=True)
|
| 172 |
-
(k_proj): Linear(in_features=64, out_features=32, bias=True)
|
| 173 |
-
(v_proj): Linear(in_features=64, out_features=32, bias=True)
|
| 174 |
-
(o_proj): Linear(in_features=64, out_features=64, bias=False)
|
| 175 |
-
)
|
| 176 |
-
(mlp): Glm4vTextMLP(
|
| 177 |
-
(gate_up_proj): Linear(in_features=64, out_features=256, bias=False)
|
| 178 |
-
(down_proj): Linear(in_features=128, out_features=64, bias=False)
|
| 179 |
-
(activation_fn): SiLU()
|
| 180 |
-
)
|
| 181 |
-
(input_layernorm): Glm4vRMSNorm((64,), eps=1e-05)
|
| 182 |
-
(post_attention_layernorm): Glm4vRMSNorm((64,), eps=1e-05)
|
| 183 |
-
(post_self_attn_layernorm): Glm4vRMSNorm((64,), eps=1e-05)
|
| 184 |
-
(post_mlp_layernorm): Glm4vRMSNorm((64,), eps=1e-05)
|
| 185 |
-
)
|
| 186 |
-
)
|
| 187 |
-
(norm): Glm4vRMSNorm((64,), eps=1e-05)
|
| 188 |
-
(rotary_emb): Glm4vTextRotaryEmbedding()
|
| 189 |
-
)
|
| 190 |
-
)
|
| 191 |
-
(lm_head): Linear(in_features=64, out_features=151552, bias=False)
|
| 192 |
-
)
|
| 193 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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