Looped-Qwen-pre / README.md
summerMC's picture
Looped GDN: weights + custom code + scripts
51ad11c verified
|
Raw History Blame Contribute Delete
2.84 kB
---
license: apache-2.0
library_name: transformers
base_model:
- j-llm/Qwen3.5-2B-SpeedX2
- Qwen/Qwen3.5-2B
tags: [gated-deltanet, linear-attention, looped-transformer, weight-sharing, distillation, custom_code]
language: [ja, en]
pipeline_tag: text-generation
---
# summerMC/Looped-Qwen-pre
[`j-llm/Qwen3.5-2B-SpeedX2`](https://huggingface.co/j-llm/Qwen3.5-2B-SpeedX2) (全層 Gated DeltaNet) の 24 層を
**空間マージ**して重み共有し、**2 重ループ**にした Looped GDN (反復ステップ毎 LoRA 付き)。教師 [`Qwen/Qwen3.5-2B`](https://huggingface.co/Qwen/Qwen3.5-2B) から蒸留。
```
for t in range(6): # 外側ループ
h = RoPE注入(h) # 位置の再注入 + ループ段埋め込み
for k in range(loops[t]): # 内側ループ (ゲート付き) loops = [4, 4, 4, 4, 4, 4]
h = h + g_k(h) * (GDN_t(h) - h) # GDN_t = 元の 4 層を平均マージした共有ブロック
```
## 使い方 (transformers)
```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
repo = "summerMC/Looped-Qwen-pre"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo, trust_remote_code=True, dtype=torch.bfloat16).cuda().eval()
ids = tok.apply_chat_template([{"role": "user", "content": "日本の首都はどこ?"}], add_generation_prompt=True,
return_tensors="pt", return_dict=True, enable_thinking=False)["input_ids"].cuda()
out = model.generate(ids, max_new_tokens=128, do_sample=True, temperature=0.7, top_p=0.9, repetition_penalty=1.1)
print(tok.decode(out[0, ids.shape[1]:], skip_special_tokens=True))
```
`pip install -U transformers flash-linear-attention` 推奨 (GDN 高速カーネル)。
## ファイル
| ファイル | 内容 |
|---|---|
| `modeling_looped_gdn.py` / `configuration_looped_gdn.py` | transformers 用カスタムコード (`trust_remote_code`) |
| `looped_gdn.py` | モデル本体・空間マージ変換 (`python looped_gdn.py --src j-llm/Qwen3.5-2B-SpeedX2 --groups 6`) |
| `scripts/colab_looped_distill.py` | 蒸留スクリプト (Colab に貼るだけ) |
| `scripts/infer_looped_gdn.py` | 推論 / 対話スクリプト |
| `scripts/colab_ast_transplant.py` | 高度な空間移植 (活性考慮 RegMean + DP 可変長分割 + 活性考慮 LoRA) |
| `scripts/test_looped_gdn.py` | 単体テスト |
| `looped_config.json` | 旧形式の設定 (`LoopedGDNForCausalLM.load` 用) |
## 学習
- Stage 1: グループ単位 hidden 蒸留 (相対 MSE + コサイン) / Stage 2: 教師 logits KL + hidden 損失
- データ: C4 (日本語・英語)
## 注意
蒸留による近似で教師より性能は低下します。ライセンスは元モデル (Qwen/Qwen3.5-2B) に従います。
元モデル: Qwen Team / 全 GDN 化: summerMC, j-llm