@@ -26,11 +26,10 @@ def test_first_block_has_correct_content_hash(seed: int, block_size: int,
26
26
token_ids = list (range (num_to_fill ))
27
27
mock_allocator = MagicMock (spec = PrefixCachingBlockAllocator )
28
28
29
- block_with_prev = PrefixCachingBlock (
30
- prev_block = None ,
31
- token_ids = token_ids ,
32
- block_size = block_size ,
33
- prefix_caching_allocator = mock_allocator )
29
+ block_with_prev = PrefixCachingBlock (prev_block = None ,
30
+ token_ids = token_ids ,
31
+ block_size = block_size ,
32
+ allocator = mock_allocator )
34
33
35
34
if is_curr_block_full :
36
35
# Expect hash since block is full.
@@ -71,7 +70,7 @@ def test_nth_block_has_correct_content_hash(seed: int, block_size: int,
71
70
prev_block = previous_block ,
72
71
token_ids = token_ids ,
73
72
block_size = block_size ,
74
- prefix_caching_allocator = mock_allocator ,
73
+ allocator = mock_allocator ,
75
74
)
76
75
77
76
if is_curr_block_full and prev_block_has_hash :
@@ -138,7 +137,7 @@ def create_chain(block_size: int,
138
137
prev_block = prev_block ,
139
138
token_ids = [],
140
139
block_size = block_size ,
141
- prefix_caching_allocator = allocator ,
140
+ allocator = allocator ,
142
141
)
143
142
144
143
tokens_to_append = token_ids [block_number *
@@ -159,11 +158,11 @@ def create_allocate_lambda(allocate_type: str, allocator: BlockAllocator,
159
158
prev_block : Optional [Block ],
160
159
token_ids : List [int ]):
161
160
if allocate_type == "immutable" :
162
- allocate_block = lambda : allocator .allocate_immutable (
161
+ allocate_block = lambda : allocator .allocate_immutable_block (
163
162
prev_block = prev_block , token_ids = token_ids )
164
163
elif allocate_type == "mutable" :
165
- allocate_block = lambda : allocator .allocate_mutable ( prev_block =
166
- prev_block )
164
+ allocate_block = lambda : allocator .allocate_mutable_block (
165
+ prev_block = prev_block )
167
166
else :
168
167
raise ValueError ()
169
168
@@ -233,12 +232,13 @@ def test_allocate_immutable_ooms_many_hash(num_blocks: int,
233
232
234
233
# Expect allocation with unseen hash to fail.
235
234
with pytest .raises (BlockAllocator .NoFreeBlocksError ):
236
- allocator .allocate_immutable (prev_block = chain [- 1 ],
237
- token_ids = list (range (block_size )))
235
+ allocator .allocate_immutable_block (prev_block = chain [- 1 ],
236
+ token_ids = list (
237
+ range (block_size )))
238
238
239
239
# Expect mutable allocation to fail.
240
240
with pytest .raises (BlockAllocator .NoFreeBlocksError ):
241
- allocator .allocate_mutable (prev_block = chain [- 1 ])
241
+ allocator .allocate_mutable_block (prev_block = chain [- 1 ])
242
242
243
243
# Expect allocation of exact same chain to pass.
244
244
second_chain = TestPrefixCachingBlockAllocator .create_immutable_chain (
@@ -270,7 +270,7 @@ def test_free_prevents_oom(num_blocks: int, block_size: int):
270
270
271
271
# Expect mutable allocation to fail.
272
272
with pytest .raises (BlockAllocator .NoFreeBlocksError ):
273
- allocator .allocate_mutable (prev_block = None )
273
+ allocator .allocate_mutable_block (prev_block = None )
274
274
275
275
block_to_free = chain [- 1 ]
276
276
@@ -280,11 +280,11 @@ def test_free_prevents_oom(num_blocks: int, block_size: int):
280
280
allocator .free (block_to_free )
281
281
assert block_to_free .block_id is None , i
282
282
283
- new_block = allocator .allocate_mutable (prev_block = None )
283
+ new_block = allocator .allocate_mutable_block (prev_block = None )
284
284
assert new_block .block_id == block_id , i
285
285
286
286
with pytest .raises (BlockAllocator .NoFreeBlocksError ):
287
- allocator .allocate_mutable (prev_block = None )
287
+ allocator .allocate_mutable_block (prev_block = None )
288
288
289
289
block_to_free = new_block
290
290
@@ -376,17 +376,13 @@ def test_get_common_computed_block_ids(num_blocks: int, block_size: int,
376
376
377
377
# Create token ids that will exhaust all blocks.
378
378
token_ids = list (range (num_blocks_to_consume * block_size ))
379
- blocks = list (range (num_blocks_to_consume ))
380
379
381
380
first_chain = TestPrefixCachingBlockAllocator .create_immutable_chain (
382
381
block_size = block_size ,
383
382
token_ids = token_ids ,
384
383
allocator = allocator ,
385
384
)
386
385
387
- # mark all blocks in first chain as computed
388
- allocator .mark_blocks_as_computed (blocks )
389
-
390
386
# After zero_point, second_chain's token_ids would be set -1, which
391
387
# make it different from here comparing with first_chain
392
388
zero_point = random .randint (1 , len (token_ids ) - 1 )
@@ -424,15 +420,16 @@ def test_alloc_promotion(num_blocks: int, block_size: int, seed: int):
424
420
block_size = block_size )
425
421
token_ids = list (range (block_size ))
426
422
427
- block = allocator .allocate_immutable (prev_block = None ,
428
- token_ids = token_ids )
423
+ block = allocator .allocate_immutable_block (prev_block = None ,
424
+ token_ids = token_ids )
429
425
430
426
assert allocator ._refcounter .get (block .block_id ) == 1
431
- m = allocator .allocate_mutable (prev_block = None )
427
+ m = allocator .allocate_mutable_block (prev_block = None )
432
428
433
429
block_id = m .block_id
434
430
for i in range (block_size ):
435
431
m .append_token_ids ([i ])
432
+
436
433
# After block get promoted to immutable from mutable, if there is
437
434
# already same content hash block, then it shall be released into
438
435
# hashless_allocator
@@ -452,48 +449,79 @@ def test_eviction_alloc_mixed(num_blocks: int, block_size: int, seed: int):
452
449
453
450
all_blocks_list = [i for i in range (num_blocks )]
454
451
zero_ref = {i : 0 for i in range (num_blocks )}
452
+ one_ref = {i : 1 for i in range (num_blocks )}
455
453
allocator = PrefixCachingBlockAllocator (num_blocks = num_blocks ,
456
454
block_size = block_size )
457
455
token_ids = list (range (num_blocks * block_size ))
458
456
459
- # now we have num_blocks free blocks in hashless allocator
460
- # with internal tracking list _blocks _cached_blocks and evictor
461
- # empty and block's ref shall be 0
457
+ # Verify initial/pre-alloc state
458
+
459
+ # Ensure all blocks are free inside hashless allocator
462
460
assert list (allocator ._hashless_allocator ._free_block_indices
463
461
) == all_blocks_list
464
- assert len (allocator ._blocks .keys ()) == 0
462
+ # Ensure no tracked blocks
463
+ assert len (allocator ._block_tracker .keys ()) == num_blocks
464
+ for block_id in range (num_blocks ):
465
+ assert not allocator ._block_tracker [block_id ].active
466
+ # Ensure no cached blocks
465
467
assert len (allocator ._cached_blocks .values ()) == 0
468
+ # Ensure no evicted blocks
466
469
assert len (allocator .evictor .free_table .keys ()) == 0
470
+ # Ensure 0s ref counts for all blocks
467
471
assert allocator ._refcounter ._refcounts == zero_ref
468
472
469
473
# Allocate immutable chains with only one block residuled in
470
474
new_block = []
471
475
for i in range (num_blocks ):
472
- block = allocator .allocate_immutable (
476
+ block = allocator .allocate_immutable_block (
473
477
prev_block = None ,
474
478
token_ids = token_ids [block_size * i :block_size * (i + 1 )])
475
479
new_block .append (block )
476
480
481
+ # Verify post-alloc state
482
+
483
+ # Ensure no blocks are free inside hashless allocator
484
+ assert (len (allocator ._hashless_allocator ._free_block_indices ) == 0 )
485
+ # Ensure all blocks are tracked
486
+ assert len (allocator ._block_tracker .keys ()) == num_blocks
487
+ for block_id in range (num_blocks ):
488
+ assert allocator ._block_tracker [block_id ].active
489
+ # Ensure all blocks are cached (all promoted)
490
+ assert len (allocator ._cached_blocks .values ()) == num_blocks
491
+ # Ensure no evicted blocks
492
+ assert len (allocator .evictor .free_table .keys ()) == 0
493
+ # Ensure 1s ref counts for all blocks
494
+ assert allocator ._refcounter ._refcounts == one_ref
495
+
477
496
# Free all blocks, and now all blocks shall be in the evictor
478
- # there shall be no tracking data left in _blocks
497
+ # there shall be no tracking data left in _block_tracker
479
498
# all blocks shall be tracked in _cached_blocks
480
499
# all blocks' ref shall be zero
481
500
for block in new_block :
482
501
allocator .free (block )
483
502
484
- assert len (allocator ._blocks .keys ()) == 0
503
+ # Verify post-free state
504
+
505
+ # Ensure no tracked blocks
506
+ assert len (allocator ._block_tracker .keys ()) == num_blocks
507
+ for block_id in range (num_blocks ):
508
+ assert not allocator ._block_tracker [block_id ].active
509
+ # Ensure no blocks in hashless allocator (all promoted)
485
510
assert len (allocator ._hashless_allocator ._free_block_indices ) == 0
511
+ # Ensure all blocks are cached
486
512
assert list (allocator ._cached_blocks .values ()) == all_blocks_list
513
+ # Ensure all blocks are inside the evictor
487
514
assert list (allocator .evictor .free_table .keys ()) == all_blocks_list
515
+ # Ensure 0s refcounts
488
516
assert allocator ._refcounter ._refcounts == zero_ref
489
517
490
518
# Allocate a mutable block, and the first block shall be evicted
491
519
# and set its content hash into None, ref to 1
492
- mutable = allocator .allocate_mutable (prev_block = None )
520
+ mutable = allocator .allocate_mutable_block (prev_block = None )
493
521
494
522
assert mutable .block_id == 0
495
523
assert mutable .content_hash is None
496
- assert 0 in allocator ._blocks
524
+ assert allocator ._block_tracker [ 0 ]. active
497
525
assert allocator ._refcounter .get (0 ) == 1
498
526
assert 0 not in allocator ._cached_blocks
499
527
assert 0 not in allocator .evictor
@@ -502,27 +530,27 @@ def test_eviction_alloc_mixed(num_blocks: int, block_size: int, seed: int):
502
530
# hashless allocator
503
531
allocator .free (mutable )
504
532
505
- assert len ( allocator ._blocks . keys ()) == 0
533
+ assert not allocator ._block_tracker [ 0 ]. active
506
534
assert allocator ._refcounter ._refcounts == zero_ref
507
535
assert 0 not in allocator ._cached_blocks
508
536
assert 0 not in allocator .evictor
509
537
assert 0 in allocator ._hashless_allocator ._free_block_indices
510
538
511
- # when allocate immutable with first block_size tokens, we
539
+ # When allocate immutable with first block_size tokens, we
512
540
# shall get free block from hashless allocator, thus no block left
513
541
# in hashless
514
- block = allocator .allocate_immutable ( prev_block = None ,
515
- token_ids = token_ids [:block_size ])
542
+ block = allocator .allocate_immutable_block (
543
+ prev_block = None , token_ids = token_ids [:block_size ])
516
544
517
545
assert block .block_id == 0
518
546
assert len (allocator ._hashless_allocator ._free_block_indices ) == 0
519
- assert 0 in allocator ._blocks
547
+ assert allocator ._block_tracker [ 0 ]. active
520
548
assert 0 in allocator ._cached_blocks .values ()
521
549
assert allocator ._refcounter .get (0 ) == 1
522
550
assert 0 not in allocator .evictor
523
551
524
552
# allocate mutable block again, it shall be popped from evictor
525
- mutable = allocator .allocate_mutable (prev_block = None )
553
+ mutable = allocator .allocate_mutable_block (prev_block = None )
526
554
assert len (allocator ._hashless_allocator ._free_block_indices ) == 0
527
555
assert mutable .block_id not in allocator .evictor .free_table
528
556
assert allocator ._refcounter .get (mutable .block_id ) == 1
@@ -619,7 +647,7 @@ def create_immutable_chain(
619
647
block_token_ids = token_ids [block_number *
620
648
block_size :(block_number + 1 ) *
621
649
block_size ]
622
- prev_block = allocator .allocate_immutable (
650
+ prev_block = allocator .allocate_immutable_block (
623
651
prev_block = prev_block , token_ids = block_token_ids )
624
652
blocks .append (prev_block )
625
653
0 commit comments