Files
tokenspeed/test/runtime/sampling/test_sampling_params.py
a3594e64bc refactor(ascend): switch DSA CP prefill transport to native flash_ops adapters (#2834)
## Summary

- Unify Ascend DSA/MLA prefill transport through official HCCL
collectives and the `flash_ops` adapter, gathering referenced cache
pages into temporary Tensor buffers.
- Create cache-owner process groups through the runtime manager before
forward execution and graph capture. Preserve temporary buffer ownership
through the final queued consumer.
- Preserve prepared cache storage identity, int64 write slots and DSA
indexer scaling; normalize empty MLA partial states and preserve sampler
length buffers during single-token verify.
- Default Ascend Events to timed completion while preserving explicit
arguments. Run Python exit hooks before native teardown and retain the
GIL through native destructors.

## Validation

- Includes regression coverage for prefill buffers, cache contracts,
empty MLA states, Event defaults and interpreter shutdown.
- `git diff --check` passed.
- Hardware and full-suite tests were not rerun on this rebased branch.

---------

Signed-off-by: SolaAir <41902620+chenhongluo@users.noreply.github.com>
Signed-off-by: pangyoki <pangyoki@126.com>
Co-authored-by: longcat-ia-team <312376714+longcat-ia-team@users.noreply.github.com>
Co-authored-by: pangyoki <pangyoki@126.com>
Co-authored-by: longcat-ia-team <longcat_ia_team@meituan.com>
2026-11-03 21:35:07 +08:00

88 lines
3.7 KiB
Python

# Copyright (c) 2026 LightSeek Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""``SamplingParams.verify`` range checks.
Each check has to match its own message: an inclusive bound that the message
excludes is how a bad value reaches the penalty kernels and corrupts a
distribution. ``repetition_penalty`` divides positive logits by the penalty, so
``0.0`` would turn every repeated token into ``+inf`` at sample time instead of
being rejected at the request boundary.
"""
import pytest
from tokenspeed.runtime.sampling.sampling_params import SamplingParams
def test_repetition_penalty_rejects_zero():
params = SamplingParams(repetition_penalty=0.0)
with pytest.raises(ValueError, match=r"repetition_penalty must be in \(0, 2\]"):
params.verify(vocab_size=32)
def test_repetition_penalty_rejects_negative():
params = SamplingParams(repetition_penalty=-0.5)
with pytest.raises(ValueError, match=r"repetition_penalty must be in \(0, 2\]"):
params.verify(vocab_size=32)
def test_repetition_penalty_accepts_positive_and_upper_bound():
SamplingParams(repetition_penalty=1.0).verify(vocab_size=32)
SamplingParams(repetition_penalty=2.0).verify(vocab_size=32)
def test_repetition_penalty_rejects_above_upper_bound():
params = SamplingParams(repetition_penalty=2.5)
with pytest.raises(ValueError, match=r"repetition_penalty must be in \(0, 2\]"):
params.verify(vocab_size=32)
def test_min_p_stays_inclusive_at_zero():
# Guards the neighbouring check against a blanket flip of every bound in
# this file: min_p=0 is valid and must keep passing.
SamplingParams(min_p=0.0).verify(vocab_size=32)
SamplingParams(min_p=1.0).verify(vocab_size=32)
def test_temperature_rejects_negative():
# Coercing it to greedy would also drop the caller's top_k.
params = SamplingParams(temperature=-0.5, top_k=40)
with pytest.raises(ValueError, match=r"temperature must be non-negative"):
params.verify(vocab_size=32)
def test_temperature_zero_collapses_to_greedy():
# Greedy pins the seed and forces top_k=1.
params = SamplingParams(temperature=0.0, top_k=40, seed=7)
assert (params.temperature, params.top_k, params.seed) == (1.0, 1, 0)
params.verify(vocab_size=32)
@pytest.mark.parametrize("top_k", [1, 127, -1])
def test_default_top_k_validation_preserves_gpu_range(top_k):
SamplingParams(temperature=1.0, top_k=top_k).verify(vocab_size=163840)
@pytest.mark.parametrize("top_k", [128, 4096, 4097, 1 << 40])
def test_default_top_k_validation_keeps_rejecting_gpu_overflow(top_k):
with pytest.raises(ValueError, match="top_k must be < 128"):
SamplingParams(temperature=1.0, top_k=top_k).verify(vocab_size=163840)