Files
wehub-resource-sync 59a0a3844c
PR Test AMD / cancel-on-close (push) Has been skipped
PR Test NVIDIA ARM / scan (push) Has been skipped
PR Test NVIDIA / cancel-on-close (push) Has been skipped
PR Test AMD / scan (push) Has been skipped
PR Test NVIDIA ARM / cancel-on-close (push) Has been skipped
PR Test NVIDIA / scan (push) Has been skipped
Release Docker Images / build (cu129-torch-2.11.0) (push) Has been skipped
Release Docker Images / build (cu130-torch-2.11.0) (push) Has been skipped
Release PyPI / publish (push) Has been skipped
Scheduler Python Test / test (push) Successful in 27m19s
Docs / build (push) Successful in 28m8s
Scheduler C++ Test / test (push) Successful in 28m19s
Scheduler C++ Test / test-flat (push) Successful in 28m18s
Docs / deploy (push) Has been cancelled
PR Test AMD / finish (push) Has been cancelled
PR Test NVIDIA / finish (push) Has been cancelled
PR Test NVIDIA ARM / finish (push) Has been cancelled
PR Test NVIDIA ARM / ${{ matrix.name }} (${{ matrix.runner }}) (push) Has been cancelled
PR Test AMD / ${{ matrix.name }} (${{ matrix.runner }}) (push) Has been cancelled
PR Test NVIDIA / ${{ matrix.name }} (${{ matrix.runner }}) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:32:31 +08:00

66 lines
2.6 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.
from __future__ import annotations
import pytest
import torch
from tokenspeed.runtime.cache.allocator import KVAllocator
@pytest.mark.skipif(not torch.cuda.is_available(), reason="cuda required")
def test_free_group_end_returns_freed_pages_to_free_slots():
"""free_extra_pages_not_cached + free_group_end should push the freed
page ids back onto free_slots and decrement token_slot_refs."""
page_size = 4
max_pages = 8
total_slots = max_pages * page_size
allocator = KVAllocator(
size=total_slots,
device="cuda",
max_batch_size=2,
max_context_len=total_slots,
page_size=page_size,
)
# Assign known pages [1, 5, 6] to request 0
allocator.req_to_page[0, :3] = torch.tensor(
[1, 5, 6], dtype=torch.int32, device="cuda"
)
for page in (1, 5, 6):
allocator.token_slot_refs[page * page_size : (page + 1) * page_size] = 1
initial_free_count = allocator.free_slots.numel()
# alloced_len=12 covers 3 pages; real_seq_len=4 keeps 1 -> free pages [5, 6]
allocator.free_extra_pages_not_cached(
req_pool_index=0, real_seq_len=page_size, alloced_len=3 * page_size
)
allocator.free_group_end()
assert allocator.free_slots.numel() == initial_free_count + 2
freed_pages = set(allocator.free_slots[initial_free_count:].tolist())
assert freed_pages == {5, 6}
for page in (5, 6):
slot_refs = allocator.token_slot_refs[page * page_size : (page + 1) * page_size]
assert (slot_refs == 0).all()