scarsty commited on
Commit
490da3c
·
verified ·
1 Parent(s): 7656bef

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -34,3 +34,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  qwen3/tokenizer.json filter=lfs diff=lfs merge=lfs -text
 
 
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  qwen3/tokenizer.json filter=lfs diff=lfs merge=lfs -text
37
+ Hy-MT2/tokenizer.json filter=lfs diff=lfs merge=lfs -text
Hy-MT2/analysis.md ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hy-MT2-7B 在 cccc 上的可行性分析
2
+
3
+ ## 模型概述
4
+
5
+ | 属性 | 值 |
6
+ |------|-----|
7
+ | 来源 | https://huggingface.co/tencent/Hy-MT2-7B |
8
+ | 架构 | `hunyuan_v1_dense` (HunYuanDenseV1ForCausalLM) |
9
+ | 用途 | 多语言翻译模型(36种语言,支持指令翻译) |
10
+ | 大小 | 7B 参数,BF16 约 16 GB,FP16 同等 |
11
+ | 权重文件 | 4 × safetensors(共 ~16 GB),另有 FP8/GGUF 版本 |
12
+
13
+ ---
14
+
15
+ ## 架构参数对比
16
+
17
+ | 参数 | Qwen3-8B(已支持) | Hy-MT2-7B(待支持) |
18
+ |------|-------------------|---------------------|
19
+ | `num_hidden_layers` | 36 | **32** |
20
+ | `hidden_size` (D) | 4096 | 4096 |
21
+ | `intermediate_size` (I) | 12288 | **14336** |
22
+ | `num_attention_heads` (H) | 32 | 32 |
23
+ | `num_key_value_heads` (Hkv) | 8 | 8 |
24
+ | `head_dim` (hd) | 128 | 128 |
25
+ | `vocab_size` (V) | 151936 | **128167** |
26
+ | `rope_theta` | 1,000,000 | **10,000** |
27
+ | `rope_scaling` | 无 | dynamic YaRN (factor=1.0) |
28
+ | `hidden_act` | silu | silu |
29
+ | `norm_type` | rms | rms |
30
+ | QK 归一化 (query/key_layernorm) | ✅ | ✅ |
31
+ | `tie_word_embeddings` | ❌ | **✅ (lm_head = embed)** |
32
+
33
+ ---
34
+
35
+ ## cccc 算子支持情况
36
+
37
+ | 功能 | cccc 算子 | 支持状态 |
38
+ |------|-----------|---------|
39
+ | Token Embedding | `embed` | ✅ |
40
+ | RMSNorm | `rmsNorm` | ✅ |
41
+ | QK Norm | `rmsNorm`(作用于 Q/K) | ✅ |
42
+ | Q/K/V/O 线性投影 | `batchedMul` | ✅ |
43
+ | GQA(K/V 头复制) | `tile` + `reshapeBatch` | ✅ |
44
+ | RoPE 位置编码 | `rope`, `ropeCosTbl`, `ropeSinTbl` | ✅ |
45
+ | KV 缓存 | `kvcache` | ✅ |
46
+ | Scaled Dot-Product Attention | `attention` | ✅ |
47
+ | SwiGLU FFN | `silu` + `elementMul` + `batchedMul` | ✅ |
48
+ | Tied Embeddings (lm_head=embed) | 同一 `MatrixWithName` 复用 | ✅(见 ini) |
49
+
50
+ **结论:架构层面 cccc 完全兼容 Hy-MT2-7B,无需新增算子。**
51
+
52
+ ---
53
+
54
+ ## 需要适配的部分
55
+
56
+ ### 1. 新建 ini 配置(简单,已提供模板)
57
+ - 修改层数 32、FFN 维度 14336、词表 128167、rope_theta 10000.0
58
+ - W_lm_head 复用 W_emb(tied embeddings)
59
+
60
+ ### 2. 权重转换(Python 脚本,已提供 `convert_weights.py`)
61
+ - 从 HuggingFace safetensors 下载权重
62
+ - 转置并重命名矩阵 → cccc 的 `named_weights` 格式
63
+ - 保存为 `INIReaderBin` 格式的 `.bin` 文件
64
+
65
+ **权重名称映射(HF → cccc):**
66
+
67
+ | HuggingFace 名称 | cccc 名称 |
68
+ |-----------------|----------|
69
+ | `model.embed_tokens.weight` [V,D] → 转置 | `W_emb` [D,V] |
70
+ | `lm_head.weight` [V,D] → 转置(与 embed 相同) | `W_lm_head` [D,V] |
71
+ | `model.norm.weight` | `W_rms_final` |
72
+ | `model.layers.{i}.input_layernorm.weight` | `W_rms_attn_{i}` |
73
+ | `model.layers.{i}.self_attn.q_proj.weight` [Dq,D] → 转置 | `W_q_{i}` [D,Dq] |
74
+ | `model.layers.{i}.self_attn.k_proj.weight` [Dkv,D] → 转置 | `W_k_{i}` [D,Dkv] |
75
+ | `model.layers.{i}.self_attn.v_proj.weight` [Dkv,D] → 转置 | `W_v_{i}` [D,Dkv] |
76
+ | `model.layers.{i}.self_attn.o_proj.weight` [D,D] → 转置 | `W_o_{i}` [D,D] |
77
+ | `model.layers.{i}.self_attn.query_layernorm.weight` | `W_qnorm_{i}` |
78
+ | `model.layers.{i}.self_attn.key_layernorm.weight` | `W_knorm_{i}` |
79
+ | `model.layers.{i}.post_attention_layernorm.weight` | `W_rms_ffn_{i}` |
80
+ | `model.layers.{i}.mlp.gate_proj.weight` [I,D] → 转置 | `W_gate_{i}` [D,I] |
81
+ | `model.layers.{i}.mlp.up_proj.weight` [I,D] → 转置 | `W_up_{i}` [D,I] |
82
+ | `model.layers.{i}.mlp.down_proj.weight` [D,I] → 转置 | `W_down_{i}` [I,D] |
83
+
84
+ ### 3. cccc-llm.cpp 适配(需修改 C++ 代码)
85
+
86
+ 这是**唯一需要改 C++ 的地方**,涉及两处硬编码:
87
+
88
+ #### 3a. EOS token 硬编码(`run_generate` 函数)
89
+ ```cpp
90
+ // 当前(Qwen3 硬编码):
91
+ const int EOS_IM_END = 151645;
92
+ const int EOS_EOT = 151643;
93
+
94
+ // Hy-MT2 需要:
95
+ const int EOS = 127960; // <|eos|>
96
+ ```
97
+
98
+ #### 3b. 对话格式(`make_turn` + `llm_init`)
99
+
100
+ **Hy-MT2 Chat Template(来自 chat_template.jinja):**
101
+ ```
102
+ System: <|startoftext|>{content}<|extra_4|>
103
+ User(1st): {content}<|extra_0|> ← 首条 user(system 后)
104
+ User(nth): <|startoftext|>{content}<|extra_0|>
105
+ Assistant: {content}<|eos|> ← 无 start marker,直接输出
106
+ ```
107
+
108
+ **当前 cccc 格式(ChatML):**
109
+ ```
110
+ <|im_start|>system\n{content}<|im_end|>\n
111
+ <|im_start|>user\n{content}<|im_end|>\n
112
+ <|im_start|>assistant\n
113
+ ```
114
+
115
+ 需修改 `make_turn`、`asst_header`、`im_end_nl` 的构造逻辑。
116
+
117
+ > 建议:在 `[llm]` section 里增加 `chat_format=` 参数支持不同格式,避免每次改 C++ 代码。
118
+
119
+ ---
120
+
121
+ ## RoPE Scaling 说明
122
+
123
+ Hy-MT2-7B 的 `rope_scaling.type = "dynamic"`, `factor = 1.0`。
124
+
125
+ - **factor=1.0 时**:YaRN/dynamic NTK 缩放因子为 1,与普通 RoPE 等效
126
+ - **对于 ≤1024 token 的上下文**:用标准 RoPE(theta=10000)完全没有精度损失
127
+ - **如需超长上下文(>32k)**:才需实现 dynamic/YaRN 频率插值
128
+
129
+ 当前 cccc 的 `ropeCosTbl/ropeSinTbl` 使用标准 RoPE,对翻译任务(通常几百 token)完全够用。
130
+
131
+ ---
132
+
133
+ ## 实施路线图
134
+
135
+ ```
136
+ 步骤 1:下载权重
137
+ huggingface-cli download tencent/Hy-MT2-7B --local-dir ./Hy-MT2-weights
138
+ (或直接下载 GGUF 版 Hy-MT2-7B.gguf 用 llama.cpp 验证先)
139
+
140
+ 步骤 2:运行转换脚本
141
+ python convert_weights.py \
142
+ --hf_dir ./Hy-MT2-weights \
143
+ --output hy_mt2_7b_fp16_cccc.bin
144
+
145
+ 步骤 3:复制 tokenizer.json
146
+ cp ./Hy-MT2-weights/tokenizer.json .
147
+
148
+ 步骤 4:修改 cccc-llm.cpp(EOS tokens + chat format)
149
+ → 详见 analysis.md §3
150
+
151
+ 步骤 5:重编 cccc-llm.dll,加载 hy_mt2_7b_fp16.ini 测试
152
+ ```
153
+
154
+ ---
155
+
156
+ ## 预估难度
157
+
158
+ | 工作项 | 难度 | 工时估算 |
159
+ |--------|------|----------|
160
+ | 写 ini 配置 | 低(复制 qwen3 改参数)| 已完成 |
161
+ | 权重转换脚本 | 低(已提供) | 已完成 |
162
+ | cccc-llm.cpp EOS 修改 | 低(改 2 个常量)| 0.5h |
163
+ | cccc-llm.cpp 格式修改 | 中(改对话拼接逻辑)| 1-2h |
164
+ | 下载权重 + 验证运行 | 低 | 依网速 |
Hy-MT2/config.json ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_classification_head": false,
3
+ "architectures": [
4
+ "HunYuanDenseV1ForCausalLM"
5
+ ],
6
+ "attention_bias": false,
7
+ "attention_dropout": 0.1,
8
+ "attention_head_dim": 128,
9
+ "bos_token_id": 127958,
10
+ "cla_share_factor": 2,
11
+ "class_num": 0,
12
+ "dense_list": [
13
+ 4096,
14
+ 0
15
+ ],
16
+ "torch_dtype": "bfloat16",
17
+ "eos_token_id": 127960,
18
+ "head_dim": 128,
19
+ "hidden_act": "silu",
20
+ "hidden_size": 4096,
21
+ "im_end_id": 5,
22
+ "im_newline_id": 11,
23
+ "im_start_id": 4,
24
+ "initializer_range": 0.02,
25
+ "intermediate_size": 14336,
26
+ "mask_init_id": 12,
27
+ "max_position_embeddings": 262144,
28
+ "mlp_bias": false,
29
+ "model_type": "hunyuan_v1_dense",
30
+ "norm_type": "rms",
31
+ "num_attention_heads": 32,
32
+ "num_hidden_layers": 32,
33
+ "num_key_value_heads": 8,
34
+ "org_vocab_size": 128167,
35
+ "pad_id": 127961,
36
+ "pad_token_id": 127961,
37
+ "pool_type": "last",
38
+ "pretraining_tp": 1,
39
+ "rms_norm_eps": 1e-05,
40
+ "rope_scaling": {
41
+ "alpha": 1000.0,
42
+ "beta_fast": 32,
43
+ "beta_slow": 1,
44
+ "factor": 1.0,
45
+ "mscale": 1.0,
46
+ "mscale_all_dim": 1.0,
47
+ "type": "dynamic"
48
+ },
49
+ "rope_theta": 10000.0,
50
+ "sep_token_id": 127962,
51
+ "text_end_id": 7,
52
+ "text_start_id": 6,
53
+ "tie_word_embeddings": true,
54
+ "transformers_version": "4.57.6",
55
+ "use_cache": true,
56
+ "use_cla": false,
57
+ "use_qk_norm": true,
58
+ "use_rotary_pos_emb": true,
59
+ "vocab_size": 128167
60
+ }
Hy-MT2/convert_weights.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ convert_weights.py — 将 Hy-MT2-7B HuggingFace safetensors 转换为 cccc INIReaderBin 格式
3
+
4
+ 用法:
5
+ python convert_weights.py --hf_dir <HF权重目录> --output hy_mt2_7b_fp16_cccc.bin [--dtype half]
6
+
7
+ 依赖:
8
+ pip install safetensors numpy
9
+
10
+ INIReaderBin 格式说明:
11
+ [0..31] 头部 "CFG_BIN INI"(32字节)
12
+ [32..39] INI 索引文本长度(uint64 LE)
13
+ [40..40+N-1] INI 索引文本(每个键对应 "offset,length" 偏移和字节数)
14
+ [40+N..] 二进制数据(各权重依次拼接)
15
+ """
16
+
17
+ import argparse
18
+ import os
19
+ import struct
20
+ import sys
21
+ import numpy as np
22
+
23
+ try:
24
+ import torch
25
+ except ImportError:
26
+ print("请先安装: pip install torch")
27
+ sys.exit(1)
28
+
29
+ try:
30
+ from safetensors import safe_open
31
+ except ImportError:
32
+ print("请先安装: pip install safetensors")
33
+ sys.exit(1)
34
+
35
+
36
+ # ── 权重名称映射:HuggingFace → cccc ─────────────────────────────────────────
37
+ # 格式: (hf_name, cccc_name, needs_transpose)
38
+ # needs_transpose=False:cccc 使用列主序 BLAS(第一维最快),HF [out,in] C-order
39
+ # 在 cccc (in, out, 1, 1) 矩阵中 element(in_i, out_j) 在 in_i + out_j*in_features,
40
+ # 恰好与 HF [out,in] C-order 的 out_j*in + in_i 相同(加法交换律),因此不需要转置。
41
+ def build_weight_map(num_layers=32):
42
+ mapping = []
43
+ # 全局权重
44
+ mapping.append(("model.embed_tokens.weight", "W_emb", False)) # [V,D] 直接存,embed op 按 id*D 取行
45
+ mapping.append(("lm_head.weight", "W_lm_head", False))# tied with embed, 保持一致
46
+ mapping.append(("model.norm.weight", "W_rms_final", False))
47
+
48
+ for i in range(num_layers):
49
+ pfx = f"model.layers.{i}"
50
+ mapping.extend([
51
+ (f"{pfx}.input_layernorm.weight", f"W_rms_attn_{i}", False),
52
+ (f"{pfx}.self_attn.q_proj.weight", f"W_q_{i}", False),
53
+ (f"{pfx}.self_attn.k_proj.weight", f"W_k_{i}", False),
54
+ (f"{pfx}.self_attn.v_proj.weight", f"W_v_{i}", False),
55
+ (f"{pfx}.self_attn.o_proj.weight", f"W_o_{i}", False),
56
+ (f"{pfx}.self_attn.query_layernorm.weight",f"W_qnorm_{i}", False),
57
+ (f"{pfx}.self_attn.key_layernorm.weight", f"W_knorm_{i}", False),
58
+ (f"{pfx}.post_attention_layernorm.weight", f"W_rms_ffn_{i}", False),
59
+ (f"{pfx}.mlp.gate_proj.weight", f"W_gate_{i}", False),
60
+ (f"{pfx}.mlp.up_proj.weight", f"W_up_{i}", False),
61
+ (f"{pfx}.mlp.down_proj.weight", f"W_down_{i}", False),
62
+ ])
63
+ return mapping
64
+
65
+
66
+ # ── INIReaderBin 序列化 ───────────────────────────────────────────────────────
67
+ def build_inireaderbin(named_blobs: dict) -> bytes:
68
+ """
69
+ named_blobs: {key: bytes}
70
+ 返回完整的 INIReaderBin 二进制内容。
71
+ """
72
+ # 1. 拼接所有二进制 blob,记录 (offset, length)
73
+ blob_parts = []
74
+ index_lines = []
75
+ offset = 0
76
+ for key, data in named_blobs.items():
77
+ blob_parts.append(data)
78
+ index_lines.append(f"{key} = {offset},{len(data)}\n")
79
+ offset += len(data)
80
+
81
+ ini_text = "".join(index_lines)
82
+ ini_bytes = ini_text.encode("utf-8")
83
+ binary_content = b"".join(blob_parts)
84
+
85
+ # 2. 构建文件
86
+ header = b"CFG_BIN INI" + b"\x00" * (32 - len("CFG_BIN INI"))
87
+ size_ini = struct.pack("<Q", len(ini_bytes))
88
+ return header + size_ini + ini_bytes + binary_content
89
+
90
+
91
+ def convert(hf_dir: str, output_path: str, dtype_str: str = "bfloat16"):
92
+ mapping = build_weight_map(num_layers=32)
93
+ hf_to_cccc = {hf: (cccc, tp) for hf, cccc, tp in mapping}
94
+
95
+ # 扫描所有 safetensors 文件
96
+ shard_files = sorted(
97
+ f for f in os.listdir(hf_dir) if f.endswith(".safetensors")
98
+ )
99
+ if not shard_files:
100
+ print(f"在 {hf_dir} 中未找到 .safetensors 文件")
101
+ sys.exit(1)
102
+ print(f"找到 {len(shard_files)} 个 safetensors 分片:{shard_files}")
103
+
104
+ # 打开所有分片(lazy load)
105
+ handles = {}
106
+ for sf in shard_files:
107
+ path = os.path.join(hf_dir, sf)
108
+ h = safe_open(path, framework="pt", device="cpu")
109
+ for key in h.keys():
110
+ handles[key] = h
111
+
112
+ named_blobs = {}
113
+ # 元信息(data_type 与 cccc ini 中的 data_type 对应)
114
+ named_blobs["data_type"] = dtype_str.encode()
115
+ named_blobs["named_weights"] = b"1"
116
+
117
+ missing = []
118
+ for hf_name, cccc_name, needs_transpose in mapping:
119
+ if hf_name not in handles:
120
+ missing.append(hf_name)
121
+ continue
122
+
123
+ tensor = handles[hf_name].get_tensor(hf_name) # torch.Tensor, original dtype (may be bfloat16)
124
+
125
+ if needs_transpose and tensor.ndim == 2:
126
+ tensor = tensor.T.contiguous()
127
+
128
+ # 转换到目标精度
129
+ if dtype_str == "half":
130
+ tensor = tensor.to(torch.float16)
131
+ blob = tensor.numpy().tobytes()
132
+ elif dtype_str == "bfloat16":
133
+ tensor = tensor.to(torch.bfloat16)
134
+ # numpy 不支持 bfloat16;用 view(int16) 直接取原始字节
135
+ blob = tensor.view(torch.int16).numpy().tobytes()
136
+ elif dtype_str == "float":
137
+ tensor = tensor.to(torch.float32)
138
+ blob = tensor.numpy().tobytes()
139
+ else:
140
+ raise ValueError(f"不支持的 dtype: {dtype_str}")
141
+ named_blobs[f"weight_{cccc_name}"] = blob
142
+ print(f" {hf_name:60s} → {cccc_name:30s} shape={tensor.shape} {len(blob)//1024}KB")
143
+
144
+ if missing:
145
+ print(f"\n警告:以下权重未找到:{missing}")
146
+
147
+ print(f"\n正在写入 {output_path} ...")
148
+ content = build_inireaderbin(named_blobs)
149
+ with open(output_path, "wb") as f:
150
+ f.write(content)
151
+ print(f"完成!文件大小:{os.path.getsize(output_path) / 1024**3:.2f} GB")
152
+
153
+
154
+ def main():
155
+ parser = argparse.ArgumentParser(description="Hy-MT2-7B → cccc bin 转换工具")
156
+ parser.add_argument("--hf_dir", required=True, help="HuggingFace 权重目录(含 *.safetensors)")
157
+ parser.add_argument("--output", default="hy_mt2_7b_bf16_cccc.bin", help="输出 .bin 文件路径")
158
+ parser.add_argument("--dtype", default="bfloat16", choices=["half", "bfloat16", "float"], help="存储精度(默认 bfloat16,无损)")
159
+ args = parser.parse_args()
160
+
161
+ convert(args.hf_dir, args.output, args.dtype)
162
+
163
+
164
+ if __name__ == "__main__":
165
+ main()
Hy-MT2/hy_mt2_7b_bf16_cccc.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:59f8cebc4f048be063bee5969dfad174b9f1921e28dc41c6a069947645b02b37
3
+ size 16059093959
Hy-MT2/hy_mt2_7b_fp16.ini ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [train]
2
+ load_file = hy_mt2_7b_bf16_cccc.bin
3
+ load_net = 1
4
+ WorkType = 0
5
+ train_epochs = 0
6
+ Batch = 1
7
+ output_net = 1
8
+ gpu = 1
9
+ mp = 1
10
+ mp_device = 0
11
+ named_weights = 1
12
+ data_type = bfloat16
13
+ #disable_activation_memory_reuse=1
14
+
15
+ [llm]
16
+ tokenizer = tokenizer.json
17
+
18
+ # EOS token: <|eos|> = 127960
19
+ eos_tokens = 127960
20
+
21
+ # No thinking support
22
+ think_open_id = -1
23
+ think_close_id = -1
24
+ no_think_str =
25
+
26
+ # Chat format for Hy-MT2-7B
27
+ # System: <|startoftext|>{content}<|extra_4|>
28
+ # User: <|startoftext|>{content}<|extra_0|>
29
+ # Asst: {content}<|eos|>
30
+ sys_prefix = <|startoftext|>
31
+ sys_suffix = <|extra_4|>
32
+ user_prefix = <|startoftext|>
33
+ user_suffix = <|extra_0|>
34
+ asst_prefix =
35
+ asst_suffix = <|eos|>
36
+
37
+ [net]
38
+ net_num = 2
39
+
40
+ # ── prefill net (T tokens at once) ──────────────────────────────────────────
41
+ structure0='
42
+ D = 4096;
43
+ Dq = 4096;
44
+ Dkv = 1024;
45
+ H = 32;
46
+ Hkv = 8;
47
+ hd = 128;
48
+ I = 14336;
49
+ V = 128167;
50
+ T = 1024;
51
+ HB = 32;
52
+ HkvB = 8;
53
+
54
+ W_emb = MatrixWithName("W_emb", D, 1, 1, V);
55
+ token_ids = MatrixF(T, 1, 1, 1);
56
+ X = embed(token_ids, W_emb);
57
+
58
+ cos_tab = ropeCosTbl(T, hd, 10000.0);
59
+ sin_tab = ropeSinTbl(T, hd, 10000.0);
60
+
61
+ for (i = 0; i < 32; i++)
62
+ {
63
+ W_rms_attn = MatrixWithName("W_rms_attn_" + to_string(i), D, 1);
64
+ X_norm = rmsNorm(X, W_rms_attn);
65
+
66
+ W_q = MatrixWithName("W_q_" + to_string(i), D, Dq, 1, 1);
67
+ Q_dq = batchedMul(W_q, X_norm, 1, 0);
68
+ Q_hHT = reshape(Q_dq, {hd, H, T, 1});
69
+ W_qnorm = MatrixWithName("W_qnorm_" + to_string(i), hd, 1);
70
+ Q_qnorm = rmsNorm(Q_hHT, W_qnorm);
71
+ Q_hTH = permute(Q_qnorm, {0, 2, 1, 3});
72
+ Q_hb = reshapeBatch(Q_hTH, {hd, T, 1, HB});
73
+ Q_r = rope(Q_hb, cos_tab, sin_tab);
74
+
75
+ W_k = MatrixWithName("W_k_" + to_string(i), D, Dkv, 1, 1);
76
+ K_dkv = batchedMul(W_k, X_norm, 1, 0);
77
+ K_hHkvT = reshape(K_dkv, {hd, Hkv, T, 1});
78
+ W_knorm = MatrixWithName("W_knorm_" + to_string(i), hd, 1);
79
+ K_knorm = rmsNorm(K_hHkvT, W_knorm);
80
+ K_hTHkv = permute(K_knorm, {0, 2, 1, 3});
81
+ K_hb = reshapeBatch(K_hTHkv, {hd, T, 1, HkvB});
82
+ K_r = rope(K_hb, cos_tab, sin_tab);
83
+
84
+ W_v = MatrixWithName("W_v_" + to_string(i), D, Dkv, 1, 1);
85
+ V_dkv = batchedMul(W_v, X_norm, 1, 0);
86
+ V_hHkvT = reshape(V_dkv, {hd, Hkv, T, 1});
87
+ V_hTHkv = permute(V_hHkvT, {0, 2, 1, 3});
88
+ V_hb = reshapeBatch(V_hTHkv, {hd, T, 1, HkvB});
89
+
90
+ Kcache = MatrixWithName("Kcache_" + to_string(i), hd, T, 1, HkvB);
91
+ setIsWeight(Kcache, 0);
92
+ registerMatrix("Kcache_" + to_string(i), Kcache);
93
+ K_cached = kvcache(K_r, Kcache);
94
+
95
+ Vcache = MatrixWithName("Vcache_" + to_string(i), hd, T, 1, HkvB);
96
+ setIsWeight(Vcache, 0);
97
+ registerMatrix("Vcache_" + to_string(i), Vcache);
98
+ V_cached = kvcache(V_hb, Vcache);
99
+
100
+ K_r2 = reshapeBatch(K_cached, {hd, T, HkvB, 1});
101
+ K_r3 = tile(K_r2, {1, 1, 1, 4});
102
+ K_r4 = permute(K_r3, {0, 1, 3, 2});
103
+ K_tiled = reshapeBatch(K_r4, {hd, T, 1, HB});
104
+
105
+ V_r2 = reshapeBatch(V_cached, {hd, T, HkvB, 1});
106
+ V_r3 = tile(V_r2, {1, 1, 1, 4});
107
+ V_r4 = permute(V_r3, {0, 1, 3, 2});
108
+ V_tiled = reshapeBatch(V_r4, {hd, T, 1, HB});
109
+
110
+ Attn = attention(Q_r, K_tiled, V_tiled, hd, 1);
111
+
112
+ Attn_hTH = reshapeBatch(Attn, {hd, T, H, 1});
113
+ Attn_hHT = permute(Attn_hTH, {0, 2, 1, 3});
114
+ Attn_flat = reshape(Attn_hHT, {Dq, T, 1, 1});
115
+ W_o = MatrixWithName("W_o_" + to_string(i), Dq, D, 1, 1);
116
+ O_out = batchedMul(W_o, Attn_flat, 1, 0);
117
+
118
+ R1 = X + O_out;
119
+
120
+ W_rms_ffn = MatrixWithName("W_rms_ffn_" + to_string(i), D, 1);
121
+ R1_norm = rmsNorm(R1, W_rms_ffn);
122
+
123
+ W_gate = MatrixWithName("W_gate_" + to_string(i), D, I, 1, 1);
124
+ W_up = MatrixWithName("W_up_" + to_string(i), D, I, 1, 1);
125
+ gate_out = silu(batchedMul(W_gate, R1_norm, 1, 0));
126
+ up_out = batchedMul(W_up, R1_norm, 1, 0);
127
+ gated = elementMul(gate_out, up_out);
128
+ W_down = MatrixWithName("W_down_" + to_string(i), I, D, 1, 1);
129
+ ffn_out = batchedMul(W_down, gated, 1, 0);
130
+
131
+ X = R1 + ffn_out;
132
+ }
133
+
134
+ W_rms_final = MatrixWithName("W_rms_final", D, 1);
135
+ X_final = rmsNorm(X, W_rms_final);
136
+ // tie_word_embeddings=true: reshape W_emb to (D, V, 1, 1) view for lm_head
137
+ W_lm_head = reshapeBatch(W_emb, {D, V, 1, 1});
138
+ logits = batchedMul(W_lm_head, X_final, 1, 0);
139
+
140
+ setXY(token_ids, logits);
141
+ '
142
+
143
+ # ── decode net (single token step) ──────────────────────────────────────────
144
+ structure1='
145
+ D = 4096;
146
+ Dq = 4096;
147
+ Dkv = 1024;
148
+ H = 32;
149
+ Hkv = 8;
150
+ hd = 128;
151
+ I = 14336;
152
+ V = 128167;
153
+ T = 1;
154
+ T_kv = 1024;
155
+ HB = 32;
156
+ HkvB = 8;
157
+
158
+ W_emb = MatrixWithName("W_emb", D, 1, 1, V);
159
+ token_ids = MatrixF(T, 1, 1, 1);
160
+ X = embed(token_ids, W_emb);
161
+
162
+ cos_tab = ropeCosTbl(T_kv, hd, 10000.0);
163
+ sin_tab = ropeSinTbl(T_kv, hd, 10000.0);
164
+
165
+ for (i = 0; i < 32; i++)
166
+ {
167
+ W_rms_attn = MatrixWithName("W_rms_attn_" + to_string(i), D, 1);
168
+ X_norm = rmsNorm(X, W_rms_attn);
169
+
170
+ W_q = MatrixWithName("W_q_" + to_string(i), D, Dq, 1, 1);
171
+ Q_dq = batchedMul(W_q, X_norm, 1, 0);
172
+ Q_hHT = reshape(Q_dq, {hd, H, T, 1});
173
+ W_qnorm = MatrixWithName("W_qnorm_" + to_string(i), hd, 1);
174
+ Q_qnorm = rmsNorm(Q_hHT, W_qnorm);
175
+ Q_hTH = permute(Q_qnorm, {0, 2, 1, 3});
176
+ Q_hb = reshapeBatch(Q_hTH, {hd, T, 1, HB});
177
+ Q_r = rope(Q_hb, cos_tab, sin_tab);
178
+
179
+ W_k = MatrixWithName("W_k_" + to_string(i), D, Dkv, 1, 1);
180
+ K_dkv = batchedMul(W_k, X_norm, 1, 0);
181
+ K_hHkvT = reshape(K_dkv, {hd, Hkv, T, 1});
182
+ W_knorm = MatrixWithName("W_knorm_" + to_string(i), hd, 1);
183
+ K_knorm = rmsNorm(K_hHkvT, W_knorm);
184
+ K_hTHkv = permute(K_knorm, {0, 2, 1, 3});
185
+ K_hb = reshapeBatch(K_hTHkv, {hd, T, 1, HkvB});
186
+ K_r = rope(K_hb, cos_tab, sin_tab);
187
+
188
+ W_v = MatrixWithName("W_v_" + to_string(i), D, Dkv, 1, 1);
189
+ V_dkv = batchedMul(W_v, X_norm, 1, 0);
190
+ V_hHkvT = reshape(V_dkv, {hd, Hkv, T, 1});
191
+ V_hTHkv = permute(V_hHkvT, {0, 2, 1, 3});
192
+ V_hb = reshapeBatch(V_hTHkv, {hd, T, 1, HkvB});
193
+
194
+ // 复用 prefill net 预载的共享 KV cache
195
+ Kcache = MatrixWithName("Kcache_" + to_string(i), hd, T_kv, 1, HkvB);
196
+ setIsWeight(Kcache, 0);
197
+ K_cached = kvcache(K_r, Kcache);
198
+
199
+ Vcache = MatrixWithName("Vcache_" + to_string(i), hd, T_kv, 1, HkvB);
200
+ setIsWeight(Vcache, 0);
201
+ V_cached = kvcache(V_hb, Vcache);
202
+
203
+ K_r2 = reshapeBatch(K_cached, {hd, T_kv, HkvB, 1});
204
+ K_r3 = tile(K_r2, {1, 1, 1, 4});
205
+ K_r4 = permute(K_r3, {0, 1, 3, 2});
206
+ K_tiled = reshapeBatch(K_r4, {hd, T_kv, 1, HB});
207
+
208
+ V_r2 = reshapeBatch(V_cached, {hd, T_kv, HkvB, 1});
209
+ V_r3 = tile(V_r2, {1, 1, 1, 4});
210
+ V_r4 = permute(V_r3, {0, 1, 3, 2});
211
+ V_tiled = reshapeBatch(V_r4, {hd, T_kv, 1, HB});
212
+
213
+ Attn = attention(Q_r, K_tiled, V_tiled, hd, 1);
214
+
215
+ Attn_hTH = reshapeBatch(Attn, {hd, T, H, 1});
216
+ Attn_hHT = permute(Attn_hTH, {0, 2, 1, 3});
217
+ Attn_flat = reshape(Attn_hHT, {Dq, T, 1, 1});
218
+ W_o = MatrixWithName("W_o_" + to_string(i), Dq, D, 1, 1);
219
+ O_out = batchedMul(W_o, Attn_flat, 1, 0);
220
+
221
+ R1 = X + O_out;
222
+
223
+ W_rms_ffn = MatrixWithName("W_rms_ffn_" + to_string(i), D, 1);
224
+ R1_norm = rmsNorm(R1, W_rms_ffn);
225
+
226
+ W_gate = MatrixWithName("W_gate_" + to_string(i), D, I, 1, 1);
227
+ W_up = MatrixWithName("W_up_" + to_string(i), D, I, 1, 1);
228
+ gate_out = silu(batchedMul(W_gate, R1_norm, 1, 0));
229
+ up_out = batchedMul(W_up, R1_norm, 1, 0);
230
+ gated = elementMul(gate_out, up_out);
231
+ W_down = MatrixWithName("W_down_" + to_string(i), I, D, 1, 1);
232
+ ffn_out = batchedMul(W_down, gated, 1, 0);
233
+
234
+ X = R1 + ffn_out;
235
+ }
236
+
237
+ W_rms_final = MatrixWithName("W_rms_final", D, 1);
238
+ X_final = rmsNorm(X, W_rms_final);
239
+ // tie_word_embeddings=true: reshape W_emb to (D, V, 1, 1) view for lm_head
240
+ W_lm_head = reshapeBatch(W_emb, {D, V, 1, 1});
241
+ logits = batchedMul(W_lm_head, X_final, 1, 0);
242
+
243
+ setXY(token_ids, logits);
244
+ '
Hy-MT2/tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4185389a69284fdd3ebf737c6120bbeee36cbed9763151fa5bac5986a39e05d4
3
+ size 16388221
qwen3/qwen3_0.5b_fp16.ini CHANGED
@@ -14,6 +14,22 @@ data_type = half
14
  [llm]
15
  tokenizer = tokenizer.json
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  [net]
18
  net_num = 2
19
 
 
14
  [llm]
15
  tokenizer = tokenizer.json
16
 
17
+ # EOS tokens: <|im_end|>=151645, <|endoftext|>=151643
18
+ eos_tokens = 151645,151643
19
+
20
+ # Thinking tokens (Qwen3)
21
+ think_open_id = 151667
22
+ think_close_id = 151668
23
+ no_think_str = <think>\n</think>\n
24
+
25
+ # Chat format (ChatML)
26
+ sys_prefix = <|im_start|>system\n
27
+ sys_suffix = <|im_end|>\n
28
+ user_prefix = <|im_start|>user\n
29
+ user_suffix = <|im_end|>\n
30
+ asst_prefix = <|im_start|>assistant\n
31
+ asst_suffix = <|im_end|>\n
32
+
33
  [net]
34
  net_num = 2
35
 
qwen3/qwen3_8b_fp16.ini CHANGED
@@ -10,10 +10,27 @@ mp = 1
10
  mp_device = 0
11
  named_weights = 1
12
  data_type = half
 
13
 
14
  [llm]
15
  tokenizer = tokenizer.json
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  [net]
18
  net_num = 2
19
 
 
10
  mp_device = 0
11
  named_weights = 1
12
  data_type = half
13
+ #disable_activation_memory_reuse=1
14
 
15
  [llm]
16
  tokenizer = tokenizer.json
17
 
18
+ # EOS tokens: <|im_end|>=151645, <|endoftext|>=151643
19
+ eos_tokens = 151645,151643
20
+
21
+ # Thinking tokens (Qwen3)
22
+ think_open_id = 151667
23
+ think_close_id = 151668
24
+ no_think_str = <think>\n</think>\n
25
+
26
+ # Chat format (ChatML)
27
+ sys_prefix = <|im_start|>system\n
28
+ sys_suffix = <|im_end|>\n
29
+ user_prefix = <|im_start|>user\n
30
+ user_suffix = <|im_end|>\n
31
+ asst_prefix = <|im_start|>assistant\n
32
+ asst_suffix = <|im_end|>\n
33
+
34
  [net]
35
  net_num = 2
36