Skip to content

fix(loss): upsample InfoNCE hard negatives from the negatives only - #336

Merged
tastelikefeet merged 1 commit into
modelscope:mainfrom
flyingdoubleG:fix/infonce-negative-upsampling
Oct 26, 2026
Merged

tastelikefeet merged 1 commit into
modelscope:mainfrom
flyingdoubleG:fix/infonce-negative-upsampling

Conversation

@flyingdoubleG

Copy link
Copy Markdown
Contributor

PR type

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

PR information

With hard_negatives set, _parse_multi_negative_sentences pads groups that have fewer negatives:

# upsample negatives with replacement; skip index 0 (positive)
selected = np.random.choice(list(range(negatives)), size=hard_negatives - negatives, replace=True) + 1

A group is laid out as [anchor, positive, negative_0, ..., negative_{n-1}], as both the InfonceLoss docstring and the labels layout in cookbook/client/twinkle/embedding.py describe. Index 0 is the anchor, not the positive, so + 1 samples indices 1..n:

  • it can pick the positive (index 1);
  • it can never pick the last negative.

For a group with a single negative, every padded slot is a copy of the positive. The positive then also appears as its own negative, so the loss is bounded below by log(1 + copies), and the gradient pushes the anchor away from its positive.

ms-swift's _parse_multi_negative_sentences has the same code with selected += 2 # skip anchor and positive. This PR uses the same offset.

Experiment results

New tests/loss/test_infonce.py (there were no InfoNCE tests). It covers two things:

  • padded slots only ever contain that group's negatives;
  • with one negative padded to three, the loss equals CE over [positive, neg, neg, neg].

Both tests fail on main and pass with the fix (CPU).

🤖 Generated with Claude Code

With hard_negatives set, groups with fewer negatives are padded by
sampling `choice(range(negatives)) + 1`. A group is laid out as
[anchor, positive, negatives...], so that offset samples indices
1..negatives: it can pick the positive and never picks the last
negative. A group with a single negative is always padded with copies
of the positive, which then appears as its own negative and caps the
loss below at log(1 + copies). Offset by 2, as ms-swift does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@tastelikefeet
tastelikefeet merged commit ac559f2 into modelscope:main Oct 26, 2026
2 of 3 checks passed
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