Skip to content

fix: detach probability weights in DFT loss - #291

Merged
tastelikefeet merged 1 commit into
modelscope:mainfrom
taking-lying-flat:fix/dft-probability-detach
Nov 21, 2026
Merged

tastelikefeet merged 1 commit into
modelscope:mainfrom
taking-lying-flat:fix/dft-probability-detach

Conversation

@taking-lying-flat

Copy link
Copy Markdown
Contributor

PR type

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

PR information

DFT requires a stop-gradient probability weight: -stop_gradient(p_y) * log(p_y), as specified in the paper's Equation 9. Differentiating the weight instead multiplies the intended gradient by 1 + log(p_y), reversing its direction when p_y < exp(-1).

Detach the probability weight at all four expressions: standard CE, chunked CE forward, chunked backward recomputation, and the chunked precomputed-logps path. This changes four lines across two production files. Forward loss values and ordinary CE behavior remain unchanged.

Experiment results

For p_y=0.1, one SGD step with learning rate 0.1 gives the same results across both losses and their logits/logps input paths:

Before After
Forward loss 0.230259 0.230259
Target-logit gradient +0.117233 -0.090000
Updated target probability 0.097910 0.101632

Validated locally with PyTorch 2.13.0 on CPU:

  • An out-of-tree harness checked forward values and full analytic gradients for the baseline and edited implementations: 384 checks covering float32/float64, mean/sum, masked and all-ignored labels, three chunk sizes, upstream gradient scaling, and ordinary CE.
  • tests/loss/test_ce_mse.py: 18 passed.
  • uvx pre-commit run --all-files: passed.

@tastelikefeet
tastelikefeet merged commit f15189e into modelscope:main Nov 21, 2026
@taking-lying-flat
taking-lying-flat deleted the fix/dft-probability-detach branch November 25, 2026 11:04
tastelikefeet added a commit that referenced this pull request Nov 28, 2026
main carried 17 commits (#311..#291) over merge-base 96dab68. v5 had already ported
everything up to #312 (a4ddc3b) and moved server/client to twinkle_cs, so:

- server/client (src/twinkle/server, twinkle_cs/*, tests/server, tests/twinkle_client):
  kept the v5 tree; the legacy src/twinkle/server path and the old ci_container_test.sh
  stay removed on v5.
- loss: #291 detached DFT weights, #335 Liger CE contract (delegates micro_batch_scale /
  batch_normalizer to CrossEntropyLoss), #334 chunked CE comments, #336 InfoNCE tests.
- model: #343 Torch.empty_cache (auto-merged), #385 fused_lm_rl ported onto the v5 task
  registry (processor/tasks.py + model/tasks.py FusedRL + transformers postprocess),
  #386 NPU fused CE fast path in selective_log_softmax.
- twinkle_agentic: #345 check kinds (hidden_python / sql_query / answer_one_of /
  answer_number / web_receipt / lean) adapted to the episode-scoped Env interface;
  AgenticChallenger gains the _verify_check / _proposal_reward hooks.

Known: cookbook/rsi and tests/cookbook/rsi (#345) are written against main's stateless
Env / AgenticChallenger API and do not run on v5 yet.
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.

2 participants