Skip to content

fix(inferencer): use raw backend logits in SpeculativeInferencer - #979

Open
AyhamJo7 wants to merge 1 commit into
OptimalScale:mainfrom
AyhamJo7:fix/920-speculative-inferencer-raw-logits
Open

AyhamJo7 wants to merge 1 commit into
OptimalScale:mainfrom
AyhamJo7:fix/920-speculative-inferencer-raw-logits

Conversation

@AyhamJo7

Copy link
Copy Markdown

Summary

Fixes #920.

SpeculativeInferencer.autoregressive_sampling previously called self.predict_next_token, which invoked model.inference(..., output_scores=True, do_sample=True) (HuggingFace generate()). In transformers, generate()'s scores are already warped by the model's generation_config (such as default temperature, top_p, top_k), and autoregressive_sampling then passed those processed scores into score_to_prob a second time, distorting the draft probability distribution relative to the target model's raw logits.

Changes

  • SpeculativeInferencer.autoregressive_sampling (src/lmflow/pipeline/inferencer.py): Compute next-token raw logits directly from model.get_backend_model()(input_ids=sequence, return_dict=True).logits[:, -1, :] under torch.no_grad(), matching STEP 2's target-model forward pass and avoiding HF generate() logits warping.
  • SpeculativeInferencer.score_to_prob: Use out-of-place scores = scores / temperature instead of in-place scores /= temperature, preventing double temperature scaling when score_to_prob is called twice on the same output.logits slice during rejection/adjustment.
  • SpeculativeInferencer.inference: Forward temperature=temperature to autoregressive_sampling and wrap STEP 2's forward pass in torch.no_grad().
  • Added offline CPU unit tests in tests/pipeline/test_speculative_inferencer.py.

Verification

  • Reproduced warped draft probabilities when model.inference() returned generation-config-modified scores before the fix.
  • PYTHONPATH=src pytest -v --strict-markers tests/pipeline/test_speculative_inferencer.py -> 3 passed.
  • ruff check src/lmflow/pipeline/inferencer.py tests/pipeline/test_speculative_inferencer.py --select E4,E7,E9,F --ignore E731 -> All checks passed.

This branch has not been deployed

No deployments
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.

[BUG] Do not use generation of hf model in inferencers

1 participant