Skip to content

fix: preserve Transformers training in safe mode - #304

Closed
Yunnglin wants to merge 5 commits into
mainfrom
fix/safe-mode-training
Closed

Yunnglin wants to merge 5 commits into
mainfrom
fix/safe-mode-training

Conversation

@Yunnglin

@Yunnglin Yunnglin commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

With TWINKLE_TRUST_REMOTE_CODE=0 in Ray mode, Transformers training and forward-only calls fail when a worker passes its own callable model to InputProcessor. The remote decorator rejects these internal arguments before selecting worker-local execution.

Move the processor pipeline into an undecorated process_local method and call it directly from Transformers forward and forward_only. The public __call__ delegates to the same pipeline through its existing remote decorator, retaining safe-mode checks on both dispatch and worker execution. The local method documents its worker-only purpose, rejects remote handles and is not remotely exported. The remote-call framework and deployment configuration are unchanged.

Refs #298 (the Transformers safe-mode training failure).

Experiment results

  • The baseline reproduced the exception in Tinker training, normal training, micro-batching and forward-only inference with Ray mode and trust disabled, including real Ray worker processes.
  • 50 tests passed with no failures, errors or skips: safe-mode regressions, existing micro-batch and processor tests, and Ray dispatch timeout tests. Coverage includes CPU loss/backward, public callable/type rejection, remote-handle rejection and the local entry point's export boundary.
  • All 24 CPU-fixture scenarios passed, including 8 in real Ray worker processes. Loss and gradient norms matched trust-enabled controls exactly.
  • A separate real Ray actor probe confirmed that a worker with trust disabled rejects an incoming callable even when the sending handle has trust enabled.
  • Pre-commit checks and git diff --check passed. Validation does not cover a full HTTP deployment or GPU/PPU training.

Copilot AI balanced review requested due to automatic review settings October 12, 2026 12:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The scoped bypass addresses the reported failure while retaining remote-handle rejection and public safety validation.

0 open findings

What changed in this PR

Fixes Transformers training and inference in Ray safe mode by allowing worker-local processor execution without weakening public remote-call checks.

Changes:

  • Adds scoped worker-local invocation for processor instances.
  • Uses the local processor path in Transformers forward and forward_only.
  • Adds safe-mode, training, custom-processor, cleanup, and remote-handle regression tests.
File Description
src/​twinkle/​infra/​__init__.py Adds context-scoped local execution support.
src/​twinkle/​processor/​base.py Adds the private local processing entry point.
src/​twinkle/​model/​transformers/​transformers.py Routes Transformers processing through the local entry point.
tests/​model/​test_safe_mode.py Covers safe-mode behavior and execution boundaries.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@Yunnglin
Yunnglin marked this pull request as draft October 13, 2026 06:21
@Yunnglin Yunnglin changed the title fix: preserve Transformers training in safe mode fix: check unsafe inputs only before remote dispatch Oct 13, 2026
@Yunnglin Yunnglin changed the title fix: check unsafe inputs only before remote dispatch fix: preserve Transformers training in safe mode Oct 13, 2026
@Yunnglin Yunnglin closed this Oct 21, 2026
@Yunnglin

Copy link
Copy Markdown
Collaborator Author

建议 close 本 PR,作为已被 #309 替代的修复处理;有价值的回归测试可以单独迁移到当前 main。

这次使用独立 worktree 实跑了 main 96dab686dc5eb33d1eb1ddab999e6dc98801be21:Qwen3.5-4B、Transformers、单 PPU、Ray 2.55.1、未打 Twinkle patch 的 Tinker 0.16.1。模型部署设置 TWINKLE_TRUST_REMOTE_CODE=0,并直接在真实模型 actor 内确认该值、确认 lambda 仍被 check_unsafe 拒绝。连续 4 次 forward_backward(data, 'cross_entropy') + optim_step 均成功,loss 为 2.3774 → 2.3145 → 2.0878 → 1.7252,LoRA 参数摘要发生变化,optimizer step 从 0 到 4。forward 和两次命名 save_weights_for_sampler 也成功。

原始触发源是 worker 内部 processor 调用携带 callable model,而装饰器在选择本地执行之前检查参数。已合入的 #309 将 model patch 移到模型准备阶段,processor 不再接收 model 参数,消除了这一触发源。因此本 PR 的额外 process_local 绕过入口不再是修复 #298 原始 Tinker 训练失败的必要条件。

另外,Tinker 的 cross-entropy 路径使用的是字符串 set_loss('CrossEntropyLoss'),不能把“直接传 loss 类被拒绝”当作报告中这条调用链的复现。

源码依据:当前 Tinker loss 设置、已合入 #309。

验证范围限于上述 Transformers 文本 SFT 场景,不宣称完成 Megatron、多卡或完整训练矩阵验证。#298 的只读 CWD 和 stock Tinker checkpoint 下载问题应继续分别跟踪,不能因为关闭本 PR 就认为整个 issue 已解决。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants