|
16 | 16 | # |
17 | 17 |
|
18 | 18 | import unittest |
| 19 | +from copy import copy |
19 | 20 | from dataclasses import replace |
20 | 21 | from types import SimpleNamespace |
21 | 22 | from unittest.mock import MagicMock |
22 | 23 |
|
| 24 | +import numpy as np |
| 25 | + |
23 | 26 | import tests.ut.distributed.ascend_store._mock_deps # noqa: F401, E402 |
24 | 27 |
|
25 | 28 | # isort: split |
@@ -60,7 +63,13 @@ def test_sharded_mamba_groups_exclude_tp_replicas(self): |
60 | 63 | specs = [ |
61 | 64 | FullAttentionSpec(block_size=16, num_kv_heads=1, head_size=1, dtype=torch.float32), |
62 | 65 | mamba_spec, |
63 | | - replace(mamba_spec, tp_replicated=True), |
| 66 | + MambaSpec( |
| 67 | + shapes=((4,),), |
| 68 | + dtypes=(torch.float32,), |
| 69 | + block_size=16, |
| 70 | + mamba_cache_mode="align", |
| 71 | + tp_replicated=True, |
| 72 | + ), |
64 | 73 | UniformTypeKVCacheSpecs.from_specs({"layer.3": mamba_spec}), |
65 | 74 | ] |
66 | 75 | config = SimpleNamespace( |
@@ -431,6 +440,30 @@ def test_prepare_value_layer(self): |
431 | 440 | self.assertEqual(addr[0], 1000 + 5 * 160) |
432 | 441 | self.assertEqual(addr[1], 2000 + 5 * 320) |
433 | 442 |
|
| 443 | + def test_prepare_value_layer_selects_group_with_segmented_partial_range(self): |
| 444 | + db = ChunkedTokenDatabase([self.meta, self.meta], [16, 16], None) |
| 445 | + db.set_group_buffers( |
| 446 | + {1: [1000, 2000, 3000]}, |
| 447 | + {1: [80, 80, 160]}, |
| 448 | + {1: [100, 100, 200]}, |
| 449 | + group_num_layers={1: 2}, |
| 450 | + group_layer_cache_entry_offsets={1: [0, 2, 3]}, |
| 451 | + group_cache_entry_segments={1: [(0, 2), (1, 2), (0, 1)]}, |
| 452 | + ) |
| 453 | + self.assertEqual(db.prepare_value_layer(4, 12, [2], 0, kv_cache_group_id=1), ([1240, 2200], [40, 40], 2)) |
| 454 | + self.assertEqual(db.prepare_value_layer(4, 12, [2], 1, kv_cache_group_id=1), ([3440], [80], 2)) |
| 455 | + self.assertEqual(db.prepare_value(4, 12, [2], kv_cache_group_id=1), ([1240, 2200, 3440], [40, 40, 80], 2)) |
| 456 | + |
| 457 | + def test_prepare_value_layer_broadcasts_descriptor_lengths_and_strides(self): |
| 458 | + db = ChunkedTokenDatabase([self.meta, self.meta], [16, 16], None) |
| 459 | + db.set_group_buffers( |
| 460 | + {1: [1000, 2000, 3000, 4000]}, |
| 461 | + {1: [160]}, |
| 462 | + {1: [200]}, |
| 463 | + group_num_layers={1: 2}, |
| 464 | + ) |
| 465 | + self.assertEqual(db.prepare_value_layer(0, 8, [2], 1, kv_cache_group_id=1), ([3400, 4400], [80, 80], 2)) |
| 466 | + |
434 | 467 | def test_decode_adaptor_prefill_pp_no_partitions(self): |
435 | 468 | key, addr, size = self.db.decode_adaptor_prefill_pp(["k1"], [[1, 2]], [[10, 20]]) |
436 | 469 | self.assertEqual(key, ["k1"]) |
@@ -569,6 +602,67 @@ def test_update_mamba_mtp_with_tuple_chunk8(self): |
569 | 602 |
|
570 | 603 |
|
571 | 604 | class TestReqMeta(unittest.TestCase): |
| 605 | + def test_save_decision_does_not_advance_tracker(self): |
| 606 | + tracker = RequestTracker("r1", 256, allocated_block_ids=[1, 2], num_saved_tokens=0) |
| 607 | + decision = tracker.plan_save(128, block_hash_count=1, save_partial_block=True) |
| 608 | + self.assertEqual(decision.save_end_token, 128) |
| 609 | + self.assertEqual(decision.target_token_len, 256) |
| 610 | + self.assertTrue(decision.should_save_partial_block) |
| 611 | + self.assertEqual(tracker.num_saved_tokens, 0) |
| 612 | + |
| 613 | + def test_transfer_preparation_is_independent_for_copied_views(self): |
| 614 | + original = ReqMeta("r1", block_ids_np=np.array([1]), block_gvas_np=np.array([100])) |
| 615 | + original.transfer.load_keys = ["original"] |
| 616 | + view = copy(original) |
| 617 | + view.transfer.load_keys = ["view"] |
| 618 | + view.transfer.block_gvas_np = np.array([200]) |
| 619 | + other = ReqMeta("r2") |
| 620 | + other.transfer.load_keys.append("other") |
| 621 | + self.assertEqual(original.transfer.load_keys, ["original"]) |
| 622 | + self.assertEqual(view.transfer.load_keys, ["view"]) |
| 623 | + np.testing.assert_array_equal(original.get_transfer_arrays(0)[1], [100]) |
| 624 | + np.testing.assert_array_equal(view.get_transfer_arrays(0)[1], [200]) |
| 625 | + self.assertEqual(ReqMeta("r3").transfer.load_keys, []) |
| 626 | + |
| 627 | + def test_group_arrays_prefer_selected_group(self): |
| 628 | + meta = ReqMeta( |
| 629 | + "r1", |
| 630 | + block_ids_np=np.array([99]), |
| 631 | + block_ids_by_group_np=[np.array([10]), np.array([20, 21])], |
| 632 | + block_gvas_np=np.array([999]), |
| 633 | + block_gvas_by_group_np=[np.array([100]), np.array([200, 210])], |
| 634 | + load_block_gvas_np=np.array([888]), |
| 635 | + load_block_gvas_by_group_np=[np.array([300]), np.array([400, 410])], |
| 636 | + ) |
| 637 | + np.testing.assert_array_equal(meta.get_block_ids_array(1), [20, 21]) |
| 638 | + ids, gvas = meta.get_transfer_arrays(1) |
| 639 | + np.testing.assert_array_equal(ids, [20, 21]) |
| 640 | + np.testing.assert_array_equal(gvas, [200, 210]) |
| 641 | + ids, gvas = meta.get_transfer_arrays(1, is_save=False) |
| 642 | + np.testing.assert_array_equal(ids, [20, 21]) |
| 643 | + np.testing.assert_array_equal(gvas, [400, 410]) |
| 644 | + |
| 645 | + def test_transfer_arrays_fall_back_as_a_pair(self): |
| 646 | + meta = ReqMeta( |
| 647 | + "r1", |
| 648 | + block_ids_np=np.array([99]), |
| 649 | + block_ids_by_group_np=[np.array([10]), np.array([20])], |
| 650 | + block_gvas_np=np.array([999]), |
| 651 | + block_gvas_by_group_np=[np.array([100])], |
| 652 | + load_block_gvas_np=np.array([888]), |
| 653 | + ) |
| 654 | + for is_save, expected in ((True, [999]), (False, [888])): |
| 655 | + with self.subTest(is_save=is_save): |
| 656 | + ids, gvas = meta.get_transfer_arrays(1, is_save=is_save) |
| 657 | + np.testing.assert_array_equal(ids, [99]) |
| 658 | + np.testing.assert_array_equal(gvas, expected) |
| 659 | + |
| 660 | + def test_group_arrays_preserve_empty_groups_and_missing_data(self): |
| 661 | + meta = ReqMeta("r1", block_ids_np=np.array([99]), block_ids_by_group_np=[np.array([], dtype=np.int64)]) |
| 662 | + np.testing.assert_array_equal(meta.get_block_ids_array(0), []) |
| 663 | + np.testing.assert_array_equal(meta.get_block_ids_array(1), [99]) |
| 664 | + self.assertEqual(ReqMeta("empty").get_transfer_arrays(0), (None, None)) |
| 665 | + |
572 | 666 | def test_from_request_tracker_basic_save(self): |
573 | 667 | tracker = RequestTracker( |
574 | 668 | req_id="r1", |
|
0 commit comments