-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(glm_asr): honor sampling params in vLLM generate() #2997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SuperMarioYL
wants to merge
1
commit into
modelscope:main
Choose a base branch
from
SuperMarioYL:fix/glm-asr-vllm-sampling-params
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+163
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,119 @@ | ||||||||||||||||||
| """Unit tests for GLM-ASR vLLM sampling-parameter handling. | ||||||||||||||||||
|
|
||||||||||||||||||
| These tests exercise ``GLMASRVLLMEngine.generate`` without a GPU or a real | ||||||||||||||||||
| vLLM installation: the vLLM entry points are stubbed in ``sys.modules`` and the | ||||||||||||||||||
| audio/encoder/engine collaborators are mocked, so only the sampling-parameter | ||||||||||||||||||
| wiring is under test. | ||||||||||||||||||
| """ | ||||||||||||||||||
|
|
||||||||||||||||||
| import re | ||||||||||||||||||
| import sys | ||||||||||||||||||
| import types | ||||||||||||||||||
| import unittest | ||||||||||||||||||
| from unittest import mock | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _install_vllm_stub(): | ||||||||||||||||||
| """Install a minimal ``vllm`` stub whose SamplingParams records kwargs.""" | ||||||||||||||||||
|
|
||||||||||||||||||
| captured = {} | ||||||||||||||||||
|
|
||||||||||||||||||
| class _RecordingSamplingParams: | ||||||||||||||||||
| def __init__(self, **kwargs): | ||||||||||||||||||
| captured.clear() | ||||||||||||||||||
| captured.update(kwargs) | ||||||||||||||||||
|
|
||||||||||||||||||
| class _EmbedsPrompt: | ||||||||||||||||||
| def __init__(self, **kwargs): | ||||||||||||||||||
| self.kwargs = kwargs | ||||||||||||||||||
|
|
||||||||||||||||||
| vllm_mod = types.ModuleType("vllm") | ||||||||||||||||||
| vllm_mod.SamplingParams = _RecordingSamplingParams | ||||||||||||||||||
| vllm_mod.LLM = object | ||||||||||||||||||
| inputs_mod = types.ModuleType("vllm.inputs") | ||||||||||||||||||
| inputs_mod.EmbedsPrompt = _EmbedsPrompt | ||||||||||||||||||
| data_mod = types.ModuleType("vllm.inputs.data") | ||||||||||||||||||
| data_mod.EmbedsPrompt = _EmbedsPrompt | ||||||||||||||||||
|
|
||||||||||||||||||
| sys.modules["vllm"] = vllm_mod | ||||||||||||||||||
| sys.modules["vllm.inputs"] = inputs_mod | ||||||||||||||||||
| sys.modules["vllm.inputs.data"] = data_mod | ||||||||||||||||||
| return captured | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| class GLMASRSamplingParamsTest(unittest.TestCase): | ||||||||||||||||||
| def setUp(self): | ||||||||||||||||||
| self.captured = _install_vllm_stub() | ||||||||||||||||||
| from funasr.models.glm_asr.inference_vllm import GLMASRVLLMEngine | ||||||||||||||||||
|
|
||||||||||||||||||
| # Build an engine without running __init__ (no model load / GPU needed). | ||||||||||||||||||
| engine = GLMASRVLLMEngine.__new__(GLMASRVLLMEngine) | ||||||||||||||||||
| engine.device = "cpu" | ||||||||||||||||||
| engine._encode_audio = mock.Mock(return_value="audio_embeds") | ||||||||||||||||||
| engine._build_prompt_embeds = mock.Mock( | ||||||||||||||||||
| return_value=mock.Mock(float=lambda: "embeds") | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| token_out = types.SimpleNamespace(token_ids=[1, 2, 3]) | ||||||||||||||||||
| vllm_output = types.SimpleNamespace(outputs=[token_out]) | ||||||||||||||||||
| engine.vllm_engine = mock.Mock() | ||||||||||||||||||
| engine.vllm_engine.generate = mock.Mock(return_value=[vllm_output]) | ||||||||||||||||||
| engine.tokenizer = mock.Mock() | ||||||||||||||||||
| engine.tokenizer.decode = mock.Mock(return_value="hello world") | ||||||||||||||||||
| self.engine = engine | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_defaults_preserve_greedy_behavior(self): | ||||||||||||||||||
| results = self.engine.generate("a.wav") | ||||||||||||||||||
| self.assertEqual(results, [{"key": "a", "text": "hello world"}]) | ||||||||||||||||||
| self.assertEqual(self.captured["temperature"], 0.0) | ||||||||||||||||||
| self.assertEqual(self.captured["top_p"], 1.0) | ||||||||||||||||||
| self.assertEqual(self.captured["top_k"], -1) | ||||||||||||||||||
| self.assertEqual(self.captured["repetition_penalty"], 1.0) | ||||||||||||||||||
| self.assertEqual(self.captured["max_tokens"], 500) | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_caller_sampling_params_are_forwarded(self): | ||||||||||||||||||
| self.engine.generate( | ||||||||||||||||||
| "a.wav", max_new_tokens=128, temperature=0.7, top_p=0.9, top_k=20 | ||||||||||||||||||
| ) | ||||||||||||||||||
| self.assertEqual(self.captured["max_tokens"], 128) | ||||||||||||||||||
| self.assertEqual(self.captured["temperature"], 0.7) | ||||||||||||||||||
| self.assertEqual(self.captured["top_p"], 0.9) | ||||||||||||||||||
| self.assertEqual(self.captured["top_k"], 20) | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_non_positive_top_k_is_normalized_to_disabled(self): | ||||||||||||||||||
| self.engine.generate("a.wav", top_k=0) | ||||||||||||||||||
| self.assertEqual(self.captured["top_k"], -1) | ||||||||||||||||||
|
Comment on lines
+83
to
+85
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also test the case where
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| def test_repetition_penalty_is_forced_neutral_in_prompt_embeds_mode(self): | ||||||||||||||||||
| # A non-neutral repetition_penalty would crash vLLM prompt-embeds mode | ||||||||||||||||||
| # (issue #2948), so it must be coerced back to 1.0 rather than forwarded. | ||||||||||||||||||
| self.engine.generate("a.wav", repetition_penalty=1.3) | ||||||||||||||||||
| self.assertEqual(self.captured["repetition_penalty"], 1.0) | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_neutral_repetition_penalty_passes_through(self): | ||||||||||||||||||
| self.engine.generate("a.wav", repetition_penalty=1.0) | ||||||||||||||||||
| self.assertEqual(self.captured["repetition_penalty"], 1.0) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| class SafeRepetitionPenaltyTest(unittest.TestCase): | ||||||||||||||||||
| def setUp(self): | ||||||||||||||||||
| _install_vllm_stub() | ||||||||||||||||||
| import funasr.models.glm_asr.inference_vllm as mod | ||||||||||||||||||
|
|
||||||||||||||||||
| self.mod = mod | ||||||||||||||||||
| # Reset the process-wide warn-once flag between tests. | ||||||||||||||||||
| mod._warned_rep_penalty = False | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_neutral_and_none_map_to_one(self): | ||||||||||||||||||
| self.assertEqual(self.mod._safe_repetition_penalty(1.0), 1.0) | ||||||||||||||||||
| self.assertEqual(self.mod._safe_repetition_penalty(None), 1.0) | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_non_neutral_is_coerced_and_warns_once(self): | ||||||||||||||||||
| with self.assertLogs(self.mod.logger, level="WARNING") as cm: | ||||||||||||||||||
| self.assertEqual(self.mod._safe_repetition_penalty(1.5), 1.0) | ||||||||||||||||||
| self.assertTrue(any("2948" in line for line in cm.output)) | ||||||||||||||||||
| self.assertTrue(self.mod._warned_rep_penalty) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||
| unittest.main() | ||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
top_kis passed asNone(which is common when passing optional configuration dictionaries), the comparisontop_k > 0will raise aTypeError: '>' not supported between instances of 'NoneType' and 'int'. We should explicitly check iftop_kis notNonebefore comparing it.