|
| 1 | +"""Unit tests for Fun-ASR-Nano vLLM repetition-penalty handling. |
| 2 | +
|
| 3 | +Regression guard for issue #2948: a repetition penalty other than 1.0 is |
| 4 | +incompatible with vLLM prompt-embeds mode and aborts the engine with a CUDA |
| 5 | +"scatter gather index out of bounds" assertion. The serving paths must never |
| 6 | +forward such a value to ``SamplingParams`` while ``enable_prompt_embeds=True``. |
| 7 | +
|
| 8 | +The helper is dependency-free, so these tests run without a GPU or vLLM. |
| 9 | +""" |
| 10 | + |
| 11 | +import logging |
| 12 | +import unittest |
| 13 | + |
| 14 | +from funasr.models.fun_asr_nano import vllm_utils |
| 15 | +from funasr.models.fun_asr_nano.vllm_utils import ( |
| 16 | + NEUTRAL_REPETITION_PENALTY, |
| 17 | + resolve_repetition_penalty, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class TestResolveRepetitionPenalty(unittest.TestCase): |
| 22 | + def setUp(self): |
| 23 | + # Reset the once-per-process warning flag so each test is independent. |
| 24 | + vllm_utils._warned_prompt_embeds = False |
| 25 | + |
| 26 | + def test_neutral_value_passes_through(self): |
| 27 | + self.assertEqual(resolve_repetition_penalty(1.0), 1.0) |
| 28 | + |
| 29 | + def test_none_maps_to_neutral(self): |
| 30 | + self.assertEqual( |
| 31 | + resolve_repetition_penalty(None), NEUTRAL_REPETITION_PENALTY |
| 32 | + ) |
| 33 | + |
| 34 | + def test_nonneutral_is_clamped_in_prompt_embeds_mode(self): |
| 35 | + # The exact value that triggers the #2948 crash. |
| 36 | + self.assertEqual( |
| 37 | + resolve_repetition_penalty(1.3, prompt_embeds=True), |
| 38 | + NEUTRAL_REPETITION_PENALTY, |
| 39 | + ) |
| 40 | + |
| 41 | + def test_nonneutral_preserved_for_token_prompts(self): |
| 42 | + # Regular token-prompt decoding can safely apply the penalty. |
| 43 | + self.assertEqual( |
| 44 | + resolve_repetition_penalty(1.3, prompt_embeds=False), 1.3 |
| 45 | + ) |
| 46 | + |
| 47 | + def test_warns_once_in_prompt_embeds_mode(self): |
| 48 | + with self.assertLogs(vllm_utils.logger, level=logging.WARNING) as cm: |
| 49 | + resolve_repetition_penalty(1.3, prompt_embeds=True) |
| 50 | + # Subsequent clamps must not emit additional warnings. |
| 51 | + resolve_repetition_penalty(1.5, prompt_embeds=True) |
| 52 | + self.assertEqual(len(cm.records), 1) |
| 53 | + self.assertIn("2948", cm.output[0]) |
| 54 | + |
| 55 | + def test_no_warning_when_value_is_safe(self): |
| 56 | + # Capture records directly (assertNoLogs is only available on 3.10+). |
| 57 | + records = [] |
| 58 | + |
| 59 | + class _Collect(logging.Handler): |
| 60 | + def emit(self, record): |
| 61 | + records.append(record) |
| 62 | + |
| 63 | + handler = _Collect(level=logging.WARNING) |
| 64 | + vllm_utils.logger.addHandler(handler) |
| 65 | + try: |
| 66 | + resolve_repetition_penalty(1.0, prompt_embeds=True) |
| 67 | + resolve_repetition_penalty(1.3, prompt_embeds=False) |
| 68 | + finally: |
| 69 | + vllm_utils.logger.removeHandler(handler) |
| 70 | + self.assertEqual(records, []) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + unittest.main() |
0 commit comments