"lib/vscode:/vscode.git/clone" did not exist on "9e9ca3e2ddeadfa6fc96ea69249c6765e099f38b"
test_block_manager.py 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import asyncio

import pytest
import torch

22
23
24
25
26
27
28
# Attempt to import the optional module
try:
    from dynamo.llm import BlockManager
except ImportError:
    pytest.importorskip(
        "optional_module", reason="block-manager feature is not enabled"
    )
29
30
31
32
33
34

pytestmark = pytest.mark.pre_merge


WORKER_ID = 0
NUM_LAYER = 5
35
OUTER_DIM = 2
36
37
38
39
40
41
42
43
PAGE_SIZE = 4
INNER_DIM = 13
DTYPE, TORCH_DTYPE = "FP32", torch.float32
HOST_NUM_BLOCKS = 16
DEVICE_NUM_BLOCKS = 16
DEVICE_ID = 0


44
def new_block_manager():
45
46
47
48
49
50
51
52
53
54
55
56
57
    return BlockManager(
        WORKER_ID,
        NUM_LAYER,
        OUTER_DIM,
        PAGE_SIZE,
        INNER_DIM,
        DTYPE,
        HOST_NUM_BLOCKS,
        DEVICE_NUM_BLOCKS,
        DEVICE_ID,
    )


58
59
60
61
62
@pytest.fixture
def block_manager():
    return new_block_manager()


63
64
65
66
@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA unavailable")
async def test_block_manager_initialization():
    # Python should drop the BlockManager instance as soon as it goes out of scope, but
    # it may not be garbage collected immediately, depending on the garbage collector.
67
68
69
70
71
    BlockManager(WORKER_ID, NUM_LAYER, OUTER_DIM, PAGE_SIZE, INNER_DIM)
    BlockManager(WORKER_ID, NUM_LAYER, OUTER_DIM, PAGE_SIZE, INNER_DIM, DTYPE)
    BlockManager(
        WORKER_ID, NUM_LAYER, OUTER_DIM, PAGE_SIZE, INNER_DIM, DTYPE, HOST_NUM_BLOCKS
    )
72
73
74
    BlockManager(
        WORKER_ID,
        NUM_LAYER,
75
        OUTER_DIM,
76
77
78
79
80
81
82
83
        PAGE_SIZE,
        INNER_DIM,
        DTYPE,
        device_num_blocks=DEVICE_NUM_BLOCKS,
    )
    BlockManager(
        WORKER_ID,
        NUM_LAYER,
84
        OUTER_DIM,
85
86
87
88
89
90
91
92
93
        PAGE_SIZE,
        INNER_DIM,
        DTYPE,
        HOST_NUM_BLOCKS,
        DEVICE_NUM_BLOCKS,
    )
    BlockManager(
        WORKER_ID,
        NUM_LAYER,
94
        OUTER_DIM,
95
96
97
98
99
100
101
102
103
        PAGE_SIZE,
        INNER_DIM,
        DTYPE,
        device_num_blocks=DEVICE_NUM_BLOCKS,
        device_id=DEVICE_ID,
    )
    BlockManager(
        WORKER_ID,
        NUM_LAYER,
104
        OUTER_DIM,
105
106
107
108
109
110
111
112
113
114
        PAGE_SIZE,
        INNER_DIM,
        DTYPE,
        HOST_NUM_BLOCKS,
        DEVICE_NUM_BLOCKS,
        DEVICE_ID,
    )


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA unavailable")
115
async def test_cpu_block_access(block_manager: BlockManager):
116
117
    block_count = 2
    block_list = block_manager.allocate_host_blocks_blocking(block_count)
118
119
120
    blocks = block_list.to_list()
    assert len(blocks) == block_count
    tensors = [torch.from_dlpack(b) for b in blocks]
121
122
    for tensor in tensors:
        assert tensor.get_device() == -1  # CPU
123
        assert tensor.shape == (1, NUM_LAYER, OUTER_DIM, PAGE_SIZE, INNER_DIM)
124
125
126
        assert tensor.dtype == TORCH_DTYPE
    # print(tensors)
    for tensor in tensors:
127
128
        tensor[0][0][0][0][0] = 1.0
        tensor[0][NUM_LAYER - 1][OUTER_DIM - 1][PAGE_SIZE - 1][INNER_DIM - 1] = 1.0
129
    # print(tensors)
130
131
132
133
    blocks_ = block_list.to_list()
    assert blocks is not blocks_
    assert len(blocks) == len(blocks_)
    tensors_ = [torch.from_dlpack(b) for b in blocks_]
134
135
136
137
138
139
140
141
    for tensor, tensor_ in zip(tensors, tensors_):
        assert tensor is not tensor_
        assert tensor.shape == tensor_.shape
        assert tensor.dtype == tensor_.dtype
        assert torch.allclose(tensor, tensor_)


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA unavailable")
142
async def test_gpu_block_access(block_manager: BlockManager):
143
144
    block_count = 6
    block_list = block_manager.allocate_device_blocks_blocking(block_count)
145
146
147
    blocks = block_list.to_list()
    assert len(blocks) == block_count
    tensors = [torch.from_dlpack(b) for b in blocks]
148
149
    for tensor in tensors:
        assert tensor.get_device() == DEVICE_ID  # GPU
150
        assert tensor.shape == (1, NUM_LAYER, OUTER_DIM, PAGE_SIZE, INNER_DIM)
151
152
153
        assert tensor.dtype == TORCH_DTYPE
    # print(tensors)
    for tensor in tensors:
154
155
        tensor[0][0][0][0][0] = 1.0
        tensor[0][NUM_LAYER - 1][OUTER_DIM - 1][PAGE_SIZE - 1][INNER_DIM - 1] = 1.0
156
    # print(tensors)
157
158
159
160
    blocks_ = block_list.to_list()
    assert blocks is not blocks_
    assert len(blocks) == len(blocks_)
    tensors_ = [torch.from_dlpack(b) for b in blocks_]
161
162
163
164
165
166
167
168
    for tensor, tensor_ in zip(tensors, tensors_):
        assert tensor is not tensor_
        assert tensor.shape == tensor_.shape
        assert tensor.dtype == tensor_.dtype
        assert torch.allclose(tensor, tensor_)


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA unavailable")
169
async def test_block_list_iteration(block_manager: BlockManager):
170
    block_count = 4
171
    block_list = await block_manager.allocate_host_blocks(block_count)
172
173
174
175
176
177
    # Test __len__()
    assert len(block_list) == block_count
    # Test __getitem__()
    for i in range(block_count):
        block = block_list[i]
        tensor = torch.from_dlpack(block)
178
        tensor[0][0][0][0][0] = 1.0 + i
179
180
181
182
    # Test __iter__() and __next__()
    idx = 1.0
    for block in block_list:
        tensor = torch.from_dlpack(block)
183
184
        assert tensor[0][0][0][0][0] == idx
        tensor[0][0][0][0][0] += 0.5
185
186
187
188
189
190
        idx += 1.0
    assert idx == 1.0 + block_count
    # Test __iter__() should reset current index
    idx = 1.0
    for block in block_list:
        tensor = torch.from_dlpack(block)
191
        assert tensor[0][0][0][0][0] == idx + 0.5
192
193
194
195
196
        idx += 1.0
    assert idx == 1.0 + block_count


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA unavailable")
197
async def test_block_copy_g1_g2(block_manager: BlockManager):
198
    # Allocate device (G1) and host (G2) block
199
200
    host_block_list = await block_manager.allocate_host_blocks(1)
    device_block_list = await block_manager.allocate_device_blocks(1)
201
202
203
    # Populate host block with unique values
    host_tensor = torch.from_dlpack(host_block_list[0])
    for i in range(NUM_LAYER):
204
205
206
207
208
209
210
211
212
        for j in range(OUTER_DIM):
            for k in range(PAGE_SIZE):
                for w in range(INNER_DIM):
                    host_tensor[0][i][j][k][w] = (
                        i * OUTER_DIM * PAGE_SIZE * INNER_DIM
                        + j * PAGE_SIZE * INNER_DIM
                        + k * INNER_DIM
                        + w
                    )
213
    # Copy host block to device block after permuting
214
    permute_dims = (0, 2, 4, 3, 1)
215
216
217
218
219
    device_tensor_ = torch.from_dlpack(device_block_list[0]).permute(*permute_dims)
    device_tensor_.copy_(host_tensor.permute(*permute_dims))
    # Assert device block is contiguous and updated in block manager
    device_tensor = torch.from_dlpack(device_block_list[0])
    for i in range(NUM_LAYER):
220
221
222
223
224
225
226
227
228
229
        for j in range(OUTER_DIM):
            for k in range(PAGE_SIZE):
                for w in range(INNER_DIM):
                    assert (
                        device_tensor[0][i][j][k][w]
                        == i * OUTER_DIM * PAGE_SIZE * INNER_DIM
                        + j * PAGE_SIZE * INNER_DIM
                        + k * INNER_DIM
                        + w
                    )
230
231
232
233
234
235
236
237
    # Set host block to zero and assert updated in block manager
    host_tensor_ = torch.from_dlpack(host_block_list[0]).permute(*permute_dims)
    host_tensor_.zero_()
    assert torch.all(host_tensor == 0)
    # Copy device block back to host block
    host_tensor_.copy_(device_tensor_)
    # Assert host block is updated in block manager
    for i in range(NUM_LAYER):
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
        for j in range(OUTER_DIM):
            for k in range(PAGE_SIZE):
                for w in range(INNER_DIM):
                    assert (
                        host_tensor[0][i][j][k][w]
                        == i * OUTER_DIM * PAGE_SIZE * INNER_DIM
                        + j * PAGE_SIZE * INNER_DIM
                        + k * INNER_DIM
                        + w
                    )


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA unavailable")
async def test_cpu_layer_access(block_manager: BlockManager):
    block_list = block_manager.allocate_host_blocks_blocking(1)
    block = block_list[0]
    layers = block.to_list()
    assert len(layers) == NUM_LAYER
    tensors = [torch.from_dlpack(bl) for bl in layers]
    for tensor in tensors:
        assert tensor.get_device() == -1  # CPU
        assert tensor.shape == (1, 1, OUTER_DIM, PAGE_SIZE, INNER_DIM)
        assert tensor.dtype == TORCH_DTYPE
    # print(tensors)
    for tensor in tensors:
        tensor[0][0][0][0][0] = 1.0
        tensor[0][0][OUTER_DIM - 1][PAGE_SIZE - 1][INNER_DIM - 1] = 1.0
    # print(tensors)
    layers_ = block.to_list()
    assert layers is not layers_
    assert len(layers) == len(layers_)
    tensors_ = [torch.from_dlpack(bl) for bl in layers_]
    for tensor, tensor_ in zip(tensors, tensors_):
        assert tensor is not tensor_
        assert tensor.shape == tensor_.shape
        assert tensor.dtype == tensor_.dtype
        assert torch.allclose(tensor, tensor_)


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA unavailable")
async def test_gpu_layer_access(block_manager: BlockManager):
    block_list = block_manager.allocate_device_blocks_blocking(1)
    block = block_list[0]
    layers = block.to_list()
    assert len(layers) == NUM_LAYER
    tensors = [torch.from_dlpack(bl) for bl in layers]
    for tensor in tensors:
        assert tensor.get_device() == DEVICE_ID  # GPU
        assert tensor.shape == (1, 1, OUTER_DIM, PAGE_SIZE, INNER_DIM)
        assert tensor.dtype == TORCH_DTYPE
    # print(tensors)
    for tensor in tensors:
        tensor[0][0][0][0][0] = 1.0
        tensor[0][0][OUTER_DIM - 1][PAGE_SIZE - 1][INNER_DIM - 1] = 1.0
    # print(tensors)
    layers_ = block.to_list()
    assert layers is not layers_
    assert len(layers) == len(layers_)
    tensors_ = [torch.from_dlpack(bl) for bl in layers_]
    for tensor, tensor_ in zip(tensors, tensors_):
        assert tensor is not tensor_
        assert tensor.shape == tensor_.shape
        assert tensor.dtype == tensor_.dtype
        assert torch.allclose(tensor, tensor_)


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA unavailable")
async def test_block_iteration(block_manager: BlockManager):
    block = (await block_manager.allocate_host_blocks(1))[0]
    # Test __len__()
    assert len(block) == NUM_LAYER
    # Test __getitem__()
    for i in range(NUM_LAYER):
        layer = block[i]
        tensor = torch.from_dlpack(layer)
        tensor[0][0][0][0][0] = 1.0 + i
    # Test __iter__() and __next__()
    idx = 1.0
    for layer in block:
        tensor = torch.from_dlpack(layer)
        assert tensor[0][0][0][0][0] == idx
        tensor[0][0][0][0][0] += 0.5
        idx += 1.0
    assert idx == 1.0 + NUM_LAYER
    # Test __iter__() should reset current index
    idx = 1.0
    for layer in block:
        tensor = torch.from_dlpack(layer)
        assert tensor[0][0][0][0][0] == idx + 0.5
        idx += 1.0
    assert idx == 1.0 + NUM_LAYER


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA unavailable")
async def test_block_layer_copy_g1_g2(block_manager: BlockManager):
    # Allocate device (G1) and host (G2) block
    host_block = (await block_manager.allocate_host_blocks(1))[0]
    device_block = (await block_manager.allocate_device_blocks(1))[0]
    # Populate host block at layer level with unique values
    host_layer_tensors = [torch.from_dlpack(bl) for bl in host_block]
    for i in range(NUM_LAYER):
        host_layer_tensor = host_layer_tensors[i]
        for j in range(OUTER_DIM):
            for k in range(PAGE_SIZE):
                for w in range(INNER_DIM):
                    host_layer_tensor[0][0][j][k][w] = (
                        i * OUTER_DIM * PAGE_SIZE * INNER_DIM
                        + j * PAGE_SIZE * INNER_DIM
                        + k * INNER_DIM
                        + w
                    )
    # Copy host block to device block after permuting
    permute_dims = (0, 2, 4, 3, 1)
    host_block_tensor_ = torch.from_dlpack(host_block).permute(*permute_dims)
    device_block_tensor_ = torch.from_dlpack(device_block).permute(*permute_dims)
    device_block_tensor_.copy_(host_block_tensor_)
    # Assert device block is contiguous and updated in block manager at layer level
    device_layer_tensors = [torch.from_dlpack(bl) for bl in device_block]
    for i in range(NUM_LAYER):
        device_layer_tensor = device_layer_tensors[i]
        for j in range(OUTER_DIM):
            for k in range(PAGE_SIZE):
                for w in range(INNER_DIM):
                    assert (
                        device_layer_tensor[0][0][j][k][w]
                        == i * OUTER_DIM * PAGE_SIZE * INNER_DIM
                        + j * PAGE_SIZE * INNER_DIM
                        + k * INNER_DIM
                        + w
                    )
    # Set host block to zero and assert updated in block manager
    host_block_tensor = torch.from_dlpack(host_block)
    host_block_tensor.zero_()
    assert torch.all(host_block_tensor_ == 0)
    # Copy device block back to host block
    host_block_tensor_.copy_(device_block_tensor_)
    # Assert host block is updated in block manager
    for i in range(NUM_LAYER):
        for j in range(OUTER_DIM):
            for k in range(PAGE_SIZE):
                for w in range(INNER_DIM):
                    assert (
                        host_block_tensor[0][i][j][k][w]
                        == i * OUTER_DIM * PAGE_SIZE * INNER_DIM
                        + j * PAGE_SIZE * INNER_DIM
                        + k * INNER_DIM
                        + w
                    )
386
387
388
389


async def main():
    await test_block_manager_initialization()
390
391
392
393
394
395
396
397
    await test_cpu_block_access(new_block_manager())
    await test_gpu_block_access(new_block_manager())
    await test_block_list_iteration(new_block_manager())
    await test_block_copy_g1_g2(new_block_manager())
    await test_cpu_layer_access(new_block_manager())
    await test_gpu_layer_access(new_block_manager())
    await test_block_iteration(new_block_manager())
    await test_block_layer_copy_g1_g2(new_block_manager())
398
399
400
401


if __name__ == "__main__":
    asyncio.run(main())