Instructions to use tysgydke/Granite-1B-LoopAttnRes with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tysgydke/Granite-1B-LoopAttnRes with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="tysgydke/Granite-1B-LoopAttnRes")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("tysgydke/Granite-1B-LoopAttnRes", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use tysgydke/Granite-1B-LoopAttnRes with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "tysgydke/Granite-1B-LoopAttnRes" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tysgydke/Granite-1B-LoopAttnRes", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/tysgydke/Granite-1B-LoopAttnRes
- SGLang
How to use tysgydke/Granite-1B-LoopAttnRes with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "tysgydke/Granite-1B-LoopAttnRes" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tysgydke/Granite-1B-LoopAttnRes", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "tysgydke/Granite-1B-LoopAttnRes" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tysgydke/Granite-1B-LoopAttnRes", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use tysgydke/Granite-1B-LoopAttnRes with Docker Model Runner:
docker model run hf.co/tysgydke/Granite-1B-LoopAttnRes
Granite-1B-LoopAttnRes
An ablation set of six fine-tuned checkpoints built on IBM granite-3.0-1b-a400m-base, each adding a weight-shared loop over the 24-layer block and, optionally, attention-residual (AttnRes) reads across rounds. Every subdirectory (loop-g0 … loop-g3-r2) is a complete checkpoint (config.json, generation_config.json, model.safetensors) that cannot be loaded by stock Transformers: it needs the bundled modeling_granite_loop.py (GraniteLoopForCausalLM). The author's reported finding is that cross-round AttnRes (loop-g3) keeps inter-round representation activity alive where a plain loop collapses, while downstream task scores at this training scale stay near the random baseline.
这是什么
- 一组基于 IBM
ibm-granite/granite-3.0-1b-a400m-base的对照微调模型(原 README 表述:MoE,总参 1.33B / 激活约 400M)。 - 研究目的是验证「跨轮 AttnRes」结构:循环计算的后续轮次中,注意力残差不仅能看到本轮正在演算的块,也能定向提取之前轮次的块。原 README 给出的思想来源为月之暗面 AttnRes(2025.03)与字节跳动 Ouro 的循环架构。
- 6 个子目录构成消融矩阵:R=1 对照、R=4 纯循环、R=4 + 轮内 AttnRes、R=4 + 跨轮 AttnRes,外加一个续训版和一个被作者标为失败品的废弃版(见下表)。
base_model的取证:原 README 的 front matter 写作base_model: [ibm-granite/granite-3.0-1b-a400m-base],正文亦写明「基于 IBM granite-3.0-1b-a400m-base」。本卡片已将该底座官方config.json与仓库内config.json逐字段比对:hidden_size 1024、num_hidden_layers 24、num_local_experts 32、num_experts_per_tok 8、intermediate_size 512、vocab_size 49152、num_attention_heads 16、num_key_value_heads 8、embedding_multiplier 12.0、residual_multiplier 0.22、attention_multiplier 0.015625、logits_scaling 6.0、rope_theta 10000、tie_word_embeddings true完全一致,仅新增了loop_rounds/attnres/cross_round/attnres_blocks四个开关,并把architectures由GraniteMoeForCausalLM改为GraniteLoopForCausalLM。因此「基于哪个 Granite、哪个 1B 版本」有硬证据,不是猜测。
仓库内容
6 个权重子目录
下表左右两侧互相独立取证:左侧「配置」列直接读各目录的 config.json,右侧「原 README 说明」列引原 README 表格。
| 目录 | config.json 开关 |
原 README 说明 | 单文件大小(bf16) |
|---|---|---|---|
loop-g0 |
loop_rounds=1, attnres=false, cross_round=false |
R=1 无循环对照(single-pass baseline),LR 1e-5,正式对照 | 2,669,276,952 B |
loop-g1 |
loop_rounds=4, attnres=false, cross_round=false |
R=4 纯循环,LR 1e-5 | 2,669,276,952 B |
loop-g2 |
loop_rounds=4, attnres=true, cross_round=false |
R=4 + 轮内 AttnRes(intra-round),LR 1e-5 | 2,669,678,456 B |
loop-g3 |
loop_rounds=4, attnres=true, cross_round=true |
R=4 + 跨轮 AttnRes(cross-round,本文方案),LR 1e-5 | 2,669,678,456 B |
loop-g3-r2 |
同 loop-g3 |
loop-g3 的同配置续训版(+800 步 / +52.4M token),训练量演化分析用 |
2,669,678,456 B |
loop-g0-lr2e5-deprecated |
loop_rounds=1, attnres=false, cross_round=false |
R=1,LR 2e-5(配置失误版本),失败品,仅供审计 | 2,669,276,952 B |
attnres_blocks 各目录均为 8(24 层 × 2 子层 = 48 子层 / 8 块 = 每块 6 子层、3 层)。所有目录共享同一份底座超参(见上节),model_type 为 granitemoe,dtype 为 bfloat16,transformers_version 记录为 5.14.1。
原 README 特别提示:loop-g0-lr2e5-deprecated 的学习率是其他组的 2 倍,其 val/GSM8K 优势大半来自学习率而非结构,正式结论一律以 loop-g0(1e-5)为准。
其他文件
| 文件 | 大小 | 作用 |
|---|---|---|
modeling_granite_loop.py |
22,622 B | 自定义建模代码,GraniteLoopForCausalLM(GraniteMoeForCausalLM);实现循环、AttnRes、checkpoint 分段、greedy_generate(手动 KV cache 贪心) |
configuration.json |
48 B | {"framework":"Pytorch","task":"text-generation"} |
README.md |
3,349 B | 原作者说明(含训练配置、评测表、许可声明) |
.gitattributes |
2,530 B | Git LFS 规则,并把 6 个 */model.safetensors 显式列入 LFS;文件内还残留 blog/index.html、blog/en.html 两条规则,但仓库中不存在这两个文件 |
loop-*/config.json |
≈1 KB ×6 | 上述开关与底座超参 |
loop-*/generation_config.json |
132 B ×6 | bos/eos/pad_token_id 均为 0,_from_model_config: true |
仓库内没有 tokenizer 文件(tokenizer.json / vocab.json / merges.txt 均无)。许可为 Apache-2.0,仓库已随附 LICENSE 全文。
用法
原 README 给出的加载方式(权重不能直接用 transformers 加载,需要自定义建模代码):
from modeling_granite_loop import GraniteLoopForCausalLM
model = GraniteLoopForCausalLM.from_pretrained("loop-g3")
from_pretrained 的 kwargs 会直接传给 __init__,因此可以用 loop_rounds= / attnres= / cross_round= / attnres_blocks= 覆盖 config 中的开关(代码注释:kwargs 优先,否则读 config)。推理路径见 modeling_granite_loop.py:
forward(input_ids=..., labels=...)是训练前向,不支持 KV cache;- 推理用
model.greedy_generate(input_ids, max_new_tokens=64, eos_token_id=None)(手动贪心 + 按(round, layer)缓存 K/V),仅支持 batch=1; labels=None时forward返回最终轮的 logits。
加载后若发现 checkpoint 中缺少 pseudo_queries,需要调用 model.reset_pseudo_queries() —— 代码注释说明:transformers 5 的加载管线会把 checkpoint 中缺失的参数重置为未初始化内存(可能含 NaN),而 reset_pseudo_queries() 可恢复论文要求的零初始化(初始 = 等权平均)。
本卡片未下载权重(单文件 ≈2.67 GB)。由文件大小差可推断 AttnRes 组应该已包含
pseudo_queries:4 轮 × 49 × 1024 个参数 = 200,704 个 bf16 数值 ≈ +401 KB,而loop-g2/g3相对loop-g0/g1的实际差值恰为 401,504 B。但这只是算术推断,未经验证。
技术要点
以下均取自 modeling_granite_loop.py 与原作者 README。
- 循环:24 层 × R 轮,权重跨轮硬共享;每轮输出经共享
lm_head算 CE,取各轮均值作为 loss;MoE 的 router aux loss(router_aux_loss_coef=0.001)照常加入。 - AttnRes(G2+):每个子层(attn / MoE 各算一个)配一个零初始化可学习伪查询,按轮独立(
pseudo_queries形状(轮数, 48+1, d),+1是每轮末尾的终读出查询);key = value = 子层输出,核为exp(q·RMSNorm(k))(RMSNorm 无可学习参数),softmax 在源维度上归一化。 - 残差缩放折进 value:
μ = residual_multiplier = 0.22,即v_i = μ·f_i;块内部分和按标准残差累加,块边界把块摘要加入源集合。 - 每轮末尾的终读出:对
[b0(embedding)] + (跨轮时的历史块摘要) + 本轮各块摘要再做一次注意力,而非只取最后一个块的部分和(代码注释:否则最终 hidden 丢掉b0主干,初始 loss 会崩到随机水平)。 - 跨轮(G3)源集合:
[b0] + [之前所有轮次的块摘要] + [本轮已完成块] + [本块部分和]。 - 显存优化:
torch.utils.checkpoint的粒度从「整轮」细化到「单层 / 单块」,4 轮 CE 也单独成段,避免 logits 与 32 个块的源堆叠同时常驻;聚合侧用q·RMSNorm(k) = (q·k)·inv_rms把归一化折成标量乘法(代码注释称数学上与原式等价,仅浮点次序不同)。 - 分析钩子:
alpha_recorder/round_recorder在设为 list 时记录各源注意力权重与每轮结束后的 hidden(训练时恒为None,零开销),是原 README「表示几何」结论的观测手段。 - 训练配置(原 README 声明,本卡片未复核):语料 98.3M token(75% 中文 + 25% 英文/代码),块长 2048;1500 步,有效 batch 32 块(≈65.5K token/步),种子 42;AdamW (0.9, 0.95),wd 0.1,grad clip 1.0,warmup 100 步,余弦退火至 LR/10;
loop-g3-r2为 +800 步 / +52.4M token 的续训。 - 参数量交叉核对:单文件 2,669,276,952 B(bf16)÷ 2 ≈ 1.335B 参数,与原 README「总参 1.33B」一致;
loop-g0与loop-g1文件大小完全相同,与「权重跨轮共享」的描述一致。
原 README 报告的结果
以下数字全部来自原作者 README(本卡片未复核、未复跑)。
| 模型 | val loss | GSM8K-200 | CMMLU-2000 | LogiQA-651 | passkey-60 |
|---|---|---|---|---|---|
| 底座 | 1.5905 | — | — | 25.7% | — |
| loop-g0 | 1.4147 | 15.5% | 23.9% | 22.1% | 7/60 |
| loop-g1 | 1.5527 | 8.5% | 26.6% | 25.3% | 0/60 |
| loop-g2 | 1.4133 | 16.5% | 24.2% | 21.7% | 5/60 |
| loop-g3 | 1.4517 | 13.0% | 24.1% | 25.0% | 5/60 |
| loop-g3-r2 | 1.4239 | — | — | — | — |
原 README 补充说明:在 98M token 量级,四组的下游任务表现都在随机线附近,核心证据在表示几何 —— 轮间 hidden 余弦距离 G2 ≈0.001(循环坍缩空转)对 G3 0.039/0.006/0.007(活性 6~7 倍);G3 跨轮注意力偏好提取旧轮次的浅-中深度块(中间计算过程),而非「抄结论」;passkey 首 token logprob 探针上 G3 逐轮单调改善、G1 恶化、G2 不动;坍缩(G2)与活性(G3)的结构在前 500 步内定型。loop-g3-r2 续训后 val 固定子集 1.4517 → 1.4239,跨轮提取签名强化(读旧轮时中间过程块偏好峰值 5.05% vs 结论块 1.76%),passkey 首 token logprob 整体上移约 0.77 nats。
已知限制
- 不能直接用 transformers 加载:必须配合仓库内的
modeling_granite_loop.py(原 README 明说)。 - 训练前向不支持 KV cache;推理只有手写贪心
greedy_generate,且仅支持 batch=1,没有采样/beam search 等生成接口。 - 仓库不含 tokenizer 文件:仅有 config / generation_config / 权重,推理时需自行提供与底座一致的 tokenizer。
- 依赖版本未声明:各
config.json记录transformers_version: 5.14.1,代码从transformers.masking_utils导入create_causal_mask(5.x 布局),并依赖GraniteMoeConfig、GraniteMoeForCausalLM、load_balancing_loss_func;但仓库 README 未给出依赖要求,也未给出安装命令。 loop-g0-lr2e5-deprecated是作者标记的失败品(学习率配置失误),仅供审计,不应作为结论依据。- 未说明的信息:训练日期、硬件与显存配置、底座权重的初始化方式、语料的具体来源与配比细节、
DESIGN.md(代码注释称「依据 DESIGN.md 与两篇论文」,该文件不在仓库内)、「配套 GitHub 仓库」的地址(README 提到两次但未给链接)、引用格式。评测脚本与训练代码均不在本仓库。 - 原 README 提到的「月之暗面 AttnRes(2025.03)」「字节 Ouro」只给了名称与年份,没有给出论文标题或链接。
许可
- 原 README 的 front matter 与正文均声明 Apache License 2.0(「底座 IBM granite-3.0-1b-a400m-base 为 Apache 2.0,本组权重随同 Apache 2.0 发布」),故本卡片按 HF 规范写作
license: apache-2.0。 - 仓库已随附 Apache-2.0 许可全文(根目录
LICENSE,11,343 字节)。许可依据是原作者的声明与底座许可;底座ibm-granite/granite-3.0-1b-a400m-base的官方 model card 标为apache-2.0。版权署名:Copyright 2026 我本是我沐。
引用
- 魔搭(原仓库):https://modelscope.cn/models/tysgydk/Granite-1B-LoopAttnRes
- Hugging Face:
- 底座:https://huggingface.co/ibm-granite/granite-3.0-1b-a400m-base
- 原 README 的致谢名单:月之暗面 AttnRes 论文(注意力残差新构)、字节跳动 Ouro(衔尾蛇循环架构)、IBM Granite 团队(底座模型)。
- 原 README 未给出 BibTeX 或其他引用格式;本卡片不代为编写引用条目。
Model tree for tysgydke/Granite-1B-LoopAttnRes
Base model
ibm-granite/granite-3.0-1b-a400m-base