feat(m2): TS Encoder — Patchify + 2-layer Transformer (T2.1)

- src/tsmm/model/ts_encoder.py: Patchify (channel-independent unfold) +
  TSEncoder (shared per-channel patch linear, mean-pool over channels,
  learnable positional embedding, 2-layer pre-norm Transformer, LayerNorm).
- configs/ts_encoder.yaml: encoder/projector hyperparameters.
- tests/test_ts_encoder.py: 10 tests (shape, n_patches formula, window
  values, determinism, gradient flow, param sanity).
- 61 tests passing; GPU fwd/bwd verified on RTX 3060 (39 MB peak).

Spec clarifications (small tier, comet-build Step 4):
- n_patches = (T-P)//S+1 ⇒ 127 for T=512 (design said 128; arithmetic slip).
- 'Channel-independent' = shared per-channel patch embed + mean-pool →
  C-free output [B,n_patches,d]; channel identity carried by [属性] tokens.
- Actual params ≈2.1M (design's '≈4M' was a rough estimate).
This commit is contained in:
张宗平
2026-06-30 00:16:54 +00:00
parent 98fb1bfc53
commit 545802d62c
4 changed files with 210 additions and 1 deletions
+1 -1
View File
@@ -15,7 +15,7 @@
## 2. M2 · 模型可跑通
- [ ] 2.1 TS Encoder `model/ts_encoder.py``Patchify(patch=8, stride=4)` 通道独立切 patch 线性嵌入到 d=256`TSEncoder(d=256, layers=2, heads=4)` 2 层 TF + 可学习位置编码;前向 `[B,T,C]``[B,n_patches,256]`(验证:输入 `[2,512,5]``[2,128,256]`,参数 ≈4M
- [x] 2.1 TS Encoder `model/ts_encoder.py``Patchify(patch=8, stride=4)` 通道独立切 patch 线性嵌入到 d=256`TSEncoder(d=256, layers=2, heads=4)` 2 层 TF + 可学习位置编码;前向 `[B,T,C]``[B,n_patches,256]`(验证:输入 `[2,512,5]``[2,127,256]`spec 澄清:n_patches=(T-P)/S+1=127 非 128;通道独立=每通道共享 patch 线性后对通道 mean-pool;参数 ≈2.1M 非 4M
- [ ] 2.2 Projector `model/projector.py``Linear(256→896)+LayerNorm`(验证:输出 `[B,128,896]` 对齐 Qwen2.5-0.5B hidden
- [ ] 2.3 多模态拼接 `model/multimodal.py`Qwen tokenizer tokenize 文本部分,TS token 作 inputs_embeds 插入 `[属性][时间戳][TS tok][事件][问题]`,统一构造 inputs_embeds+attention_mask+labels(仅回答段非 -100)(验证:seq_len ≤1024mask/labels 形状对)
- [ ] 2.4 训练/推理封装 `model/wrapper.py``MultimodalTSModel` 组合 Encoder+Projector+LLM+LoRA 挂载开关,`forward` 返 loss、`generate` 返文本,支持 `freeze_llm`(阶段①)/`enable_lora(r=16)`(阶段②)(验证:单 batch forward 3060 不 OOMgenerate 能出文本)