File size: 10,730 Bytes
12aaacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
---
library_name: transformers
base_model:
- thinkingmachines/Inkling
---

This tiny model is intended for debugging. It is randomly initialized using the configuration adapted from [thinkingmachines/Inkling](https://huggingface.co/thinkingmachines/Inkling).

| File path | Size |
|------|------|
| model.safetensors | 7.3MB |


### Example usage:

```python
import numpy as np
import torch
from PIL import Image
from transformers import AutoModelForMultimodalLM, AutoProcessor

model_id = "tiny-random/inkling"
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForMultimodalLM.from_pretrained(
    model_id,
    dtype=torch.bfloat16,
    device_map="cuda" if torch.cuda.is_available() else "cpu",
)

# Synthetic multimodal inputs — no network fetch.
image = Image.fromarray(np.random.randint(0, 255, (80, 80, 3), dtype=np.uint8))
sampling_rate = processor.feature_extractor.sampling_rate
t = np.linspace(0, 0.2, int(sampling_rate * 0.2), endpoint=False)
audio = (0.1 * np.sin(2 * np.pi * 440 * t)).astype(np.float32)

messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": image},
            {"type": "audio", "audio": audio},
            {"type": "text", "text": "Describe the image and audio briefly."},
        ],
    },
]
inputs = processor.apply_chat_template(
    messages,
    add_generation_prompt=True,
    tokenize=True,
    return_dict=True,
    return_tensors="pt",
    reasoning_effort="none",
    processor_kwargs={"sampling_rate": sampling_rate},
).to(model.device, dtype=model.dtype)
input_len = inputs["input_ids"].shape[-1]
outputs = model.generate(**inputs, max_new_tokens=16)
print(processor.decode(outputs[0], skip_special_tokens=False))
```

### Codes to create this repo:

<details><summary>Click to expand</summary>

```python
import json
from pathlib import Path

import torch
from huggingface_hub import file_exists, hf_hub_download
from safetensors.torch import load_file, save_file
from transformers import (
    AutoConfig,
    AutoProcessor,
    GenerationConfig,
    InklingForConditionalGeneration,
    set_seed,
)

source_model_id = "thinkingmachines/Inkling"
save_folder = "/tmp/tiny-random/inkling"

processor = AutoProcessor.from_pretrained(source_model_id)
processor.save_pretrained(save_folder)

with open(hf_hub_download(source_model_id, filename='config.json', repo_type='model'), 'r', encoding='utf-8') as f:
    config_json = json.load(f)

# Only shrink size-critical dims. Keep kernel-sensitive knobs (d_rel, rel_extent,
# sliding_window_size, num_experts_per_tok, n_shared_experts, ...) as upstream.
hidden_size = 8
num_mtp_layers = 1
config_json['text_config'].update({
    'hidden_size': hidden_size,
    'num_hidden_layers': 2,
    'num_attention_heads': 8,
    'num_key_value_heads': 4,
    'head_dim': 32,
    'swa_num_attention_heads': 8,
    'swa_num_key_value_heads': 4,
    'swa_head_dim': 32,
    'local_layer_ids': [0],  # keep 1 sliding + 1 global with 2 layers
    'dense_mlp_idx': 1,  # 1 dense + 1 sparse
    'dense_intermediate_size': 32,
    'intermediate_size': 32,
    'moe_intermediate_size': 32,
})
config_json['vision_config'].update({
    'decoder_dmodel': hidden_size,
    'n_layers': 2,
})
config_json['audio_config'].update({
    'decoder_dmodel': hidden_size,
})
config_json['mtp_config'].update({
    'num_nextn_predict_layers': num_mtp_layers,
    'local_layer_ids': [0],
})

with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
    json.dump(config_json, f, indent=2)

config = AutoConfig.from_pretrained(save_folder)
print(config)
torch.set_default_dtype(torch.bfloat16)
model = InklingForConditionalGeneration(config)
torch.set_default_dtype(torch.float32)
if file_exists(filename="generation_config.json", repo_id=source_model_id, repo_type='model'):
    model.generation_config = GenerationConfig.from_pretrained(
        source_model_id, trust_remote_code=True,
    )
set_seed(42)
model = model.cpu()
num_params = sum(p.numel() for p in model.parameters())
with torch.no_grad():
    for name, p in sorted(model.named_parameters()):
        torch.nn.init.normal_(p, 0, 0.2)
        print(name, p.shape, f'{p.numel() / num_params:.2%}', f'{p.numel() * p.element_size() / 1024**2:.2f}MB')
# Upstream MoE gate bias / global_scale are F32; sconv stays BF16 in the checkpoint.
for name, module in model.named_modules():
    if hasattr(module, "e_score_correction_bias"):
        module.e_score_correction_bias = torch.nn.Parameter(
            module.e_score_correction_bias.detach().float()
        )
    if name.endswith(".mlp.gate") and hasattr(module, "global_scale"):
        module.global_scale = torch.nn.Parameter(module.global_scale.detach().float())
model.save_pretrained(save_folder)

# HF ignores `model.mtp.*` on main load; write them with original checkpoint naming.
set_seed(42)
path = Path(save_folder) / "model.safetensors"
state = load_file(str(path))
dense_prefix = "model.llm.layers.0."  # MTP blocks are dense
dense_keys = {k: v for k, v in state.items() if k.startswith(dense_prefix)}
for i in range(num_mtp_layers):
    block_prefix = f"model.mtp.layers.{i}.transformer_block."
    for src_key, tensor in dense_keys.items():
        dst_key = block_prefix + src_key[len(dense_prefix):]
        state[dst_key] = torch.empty_like(tensor)
        torch.nn.init.normal_(state[dst_key], 0, 0.2)
        print(dst_key, tuple(state[dst_key].shape))
    for name, shape in (
        (f"model.mtp.layers.{i}.embed_norm.weight", (hidden_size,)),
        (f"model.mtp.layers.{i}.hidden_norm.weight", (hidden_size,)),
        (f"model.mtp.layers.{i}.input_proj.weight", (hidden_size, hidden_size * 2)),
    ):
        state[name] = torch.empty(shape, dtype=torch.bfloat16)
        torch.nn.init.normal_(state[name], 0, 0.2)
        print(name, shape)
# Keep checkpoint key dtypes aligned even if save_pretrained downcasts.
for key, tensor in list(state.items()):
    if key.endswith(".mlp.gate.bias") or key.endswith(".mlp.gate.global_scale"):
        state[key] = tensor.float()
save_file(state, str(path))
```

</details>

### Printing the model:

<details><summary>Click to expand</summary>

```text
InklingForConditionalGeneration(
  (model): InklingModel(
    (language_model): InklingTextModel(
      (embed_tokens): Embedding(201024, 8)
      (layers): ModuleList(
        (0): InklingDecoderLayer(
          (self_attn): InklingAttention(
            (q_proj): Linear(in_features=8, out_features=256, bias=False)
            (k_proj): Linear(in_features=8, out_features=128, bias=False)
            (v_proj): Linear(in_features=8, out_features=128, bias=False)
            (r_proj): Linear(in_features=8, out_features=128, bias=False)
            (o_proj): Linear(in_features=256, out_features=8, bias=False)
            (k_sconv): InklingShortConvolution(
              (conv1d): Conv1d(128, 128, kernel_size=(4,), stride=(1,), padding=(3,), groups=128, bias=False)
            )
            (v_sconv): InklingShortConvolution(
              (conv1d): Conv1d(128, 128, kernel_size=(4,), stride=(1,), padding=(3,), groups=128, bias=False)
            )
            (q_norm): InklingRMSNorm((32,), eps=1e-06)
            (k_norm): InklingRMSNorm((32,), eps=1e-06)
            (rel_logits_proj): InklingRelativeLogits()
          )
          (mlp): InklingMLP(
            (gate_proj): Linear(in_features=8, out_features=32, bias=False)
            (up_proj): Linear(in_features=8, out_features=32, bias=False)
            (down_proj): Linear(in_features=32, out_features=8, bias=False)
            (act_fn): SiLUActivation()
          )
          (input_layernorm): InklingRMSNorm((8,), eps=1e-06)
          (post_attention_layernorm): InklingRMSNorm((8,), eps=1e-06)
          (attn_sconv): InklingShortConvolution(
            (conv1d): Conv1d(8, 8, kernel_size=(4,), stride=(1,), padding=(3,), groups=8, bias=False)
          )
          (mlp_sconv): InklingShortConvolution(
            (conv1d): Conv1d(8, 8, kernel_size=(4,), stride=(1,), padding=(3,), groups=8, bias=False)
          )
        )
        (1): InklingDecoderLayer(
          (self_attn): InklingAttention(
            (q_proj): Linear(in_features=8, out_features=256, bias=False)
            (k_proj): Linear(in_features=8, out_features=128, bias=False)
            (v_proj): Linear(in_features=8, out_features=128, bias=False)
            (r_proj): Linear(in_features=8, out_features=128, bias=False)
            (o_proj): Linear(in_features=256, out_features=8, bias=False)
            (k_sconv): InklingShortConvolution(
              (conv1d): Conv1d(128, 128, kernel_size=(4,), stride=(1,), padding=(3,), groups=128, bias=False)
            )
            (v_sconv): InklingShortConvolution(
              (conv1d): Conv1d(128, 128, kernel_size=(4,), stride=(1,), padding=(3,), groups=128, bias=False)
            )
            (q_norm): InklingRMSNorm((32,), eps=1e-06)
            (k_norm): InklingRMSNorm((32,), eps=1e-06)
            (rel_logits_proj): InklingRelativeLogits()
          )
          (mlp): InklingMoE(
            (gate): InklingTopkRouter()
            (experts): InklingExperts(
              (act_fn): SiLUActivation()
            )
            (shared_experts): InklingSharedExperts(
              (act_fn): SiLUActivation()
            )
          )
          (input_layernorm): InklingRMSNorm((8,), eps=1e-06)
          (post_attention_layernorm): InklingRMSNorm((8,), eps=1e-06)
          (attn_sconv): InklingShortConvolution(
            (conv1d): Conv1d(8, 8, kernel_size=(4,), stride=(1,), padding=(3,), groups=8, bias=False)
          )
          (mlp_sconv): InklingShortConvolution(
            (conv1d): Conv1d(8, 8, kernel_size=(4,), stride=(1,), padding=(3,), groups=8, bias=False)
          )
        )
      )
      (norm): InklingRMSNorm((8,), eps=1e-06)
      (embed_norm): InklingRMSNorm((8,), eps=1e-06)
    )
    (audio_tower): InklingAudioModel(
      (embed_audio_tokens): InklingAudioModelEmbeddings(
        (embed_audio_tokens): Embedding(1280, 8)
      )
      (norm): InklingRMSNorm((8,), eps=1e-06)
    )
    (vision_tower): InklingVisionModel(
      (encoder_layers): ModuleList(
        (0): InklingVisionEncoderLayer(
          (projection): Linear(in_features=300, out_features=320, bias=False)
          (layer_norm): InklingRMSNorm((320,), eps=1e-06)
        )
        (1): InklingVisionEncoderLayer(
          (projection): Linear(in_features=10240, out_features=8, bias=False)
        )
      )
      (final_norm): InklingRMSNorm((8,), eps=1e-06)
    )
  )
  (lm_head): Linear(in_features=8, out_features=201024, bias=False)
)
```

</details>

### Test environment:

- torch: 2.11.0+cu128
- transformers: 5.15.0.dev0