|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2022-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Unit tests for trtllm::inplace_slice_copy. |
| 16 | +
|
| 17 | +Verifies that the cudaMemcpy2DAsync-backed op produces the same result as a |
| 18 | +reference Python slice + Tensor.copy_, for the row-prefix / column-slice |
| 19 | +write pattern used in EAGLE3 hidden-state capture. |
| 20 | +""" |
| 21 | + |
| 22 | +import pytest |
| 23 | +import torch |
| 24 | + |
| 25 | +import tensorrt_llm # noqa: F401 |
| 26 | + |
| 27 | + |
| 28 | +def _reference(dest_shape, src, dim1_start, dim1_end, dtype): |
| 29 | + dest = torch.zeros(dest_shape, dtype=dtype, device="cuda") |
| 30 | + num_tokens = src.shape[0] |
| 31 | + dest[:num_tokens, dim1_start:dim1_end].copy_(src) |
| 32 | + return dest |
| 33 | + |
| 34 | + |
| 35 | +def _run(dest_shape, src, dim1_start, dim1_end, dtype): |
| 36 | + dest = torch.zeros(dest_shape, dtype=dtype, device="cuda") |
| 37 | + torch.ops.trtllm.inplace_slice_copy(dest, src, dim1_start, dim1_end) |
| 38 | + return dest |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float16, torch.float32]) |
| 42 | +def test_full_dest_full_width(dtype): |
| 43 | + """num_tokens == dest.size(0) and slice == full dest width.""" |
| 44 | + dest_shape = (16, 64) |
| 45 | + src = torch.randn(16, 64, dtype=dtype, device="cuda") |
| 46 | + out = _run(dest_shape, src, 0, 64, dtype) |
| 47 | + ref = _reference(dest_shape, src, 0, 64, dtype) |
| 48 | + torch.testing.assert_close(out, ref) |
| 49 | + |
| 50 | + |
| 51 | +def test_partial_rows(): |
| 52 | + """num_tokens < dest.size(0): trailing rows must stay zero.""" |
| 53 | + dtype = torch.bfloat16 |
| 54 | + dest_shape = (32, 64) |
| 55 | + src = torch.randn(8, 64, dtype=dtype, device="cuda") |
| 56 | + out = _run(dest_shape, src, 0, 64, dtype) |
| 57 | + ref = _reference(dest_shape, src, 0, 64, dtype) |
| 58 | + torch.testing.assert_close(out, ref) |
| 59 | + assert torch.all(out[8:] == 0) |
| 60 | + |
| 61 | + |
| 62 | +def test_column_slice_middle(): |
| 63 | + """Write to a middle column band; flanking columns must stay zero.""" |
| 64 | + dtype = torch.bfloat16 |
| 65 | + dest_shape = (16, 96) |
| 66 | + src = torch.randn(16, 32, dtype=dtype, device="cuda") |
| 67 | + out = _run(dest_shape, src, 32, 64, dtype) |
| 68 | + ref = _reference(dest_shape, src, 32, 64, dtype) |
| 69 | + torch.testing.assert_close(out, ref) |
| 70 | + assert torch.all(out[:, :32] == 0) |
| 71 | + assert torch.all(out[:, 64:] == 0) |
| 72 | + |
| 73 | + |
| 74 | +def test_layered_capture_pattern(): |
| 75 | + """Mimic EAGLE3 hidden-state capture: write each layer into its band.""" |
| 76 | + dtype = torch.bfloat16 |
| 77 | + num_tokens, hidden_size, num_layers = 12, 48, 3 |
| 78 | + dest_shape = (24, hidden_size * num_layers) |
| 79 | + srcs = [ |
| 80 | + torch.randn(num_tokens, hidden_size, dtype=dtype, device="cuda") for _ in range(num_layers) |
| 81 | + ] |
| 82 | + |
| 83 | + out = torch.zeros(dest_shape, dtype=dtype, device="cuda") |
| 84 | + for i, s in enumerate(srcs): |
| 85 | + torch.ops.trtllm.inplace_slice_copy(out, s, i * hidden_size, (i + 1) * hidden_size) |
| 86 | + |
| 87 | + ref = torch.zeros(dest_shape, dtype=dtype, device="cuda") |
| 88 | + for i, s in enumerate(srcs): |
| 89 | + ref[:num_tokens, i * hidden_size : (i + 1) * hidden_size].copy_(s) |
| 90 | + |
| 91 | + torch.testing.assert_close(out, ref) |
| 92 | + |
| 93 | + |
| 94 | +def test_empty_src_is_noop(): |
| 95 | + """num_tokens == 0 must not modify dest and must not raise.""" |
| 96 | + dtype = torch.bfloat16 |
| 97 | + dest_shape = (16, 64) |
| 98 | + dest = torch.full(dest_shape, 7, dtype=dtype, device="cuda") |
| 99 | + src = torch.empty(0, 32, dtype=dtype, device="cuda") |
| 100 | + torch.ops.trtllm.inplace_slice_copy(dest, src, 16, 48) |
| 101 | + assert torch.all(dest == 7) |
| 102 | + |
| 103 | + |
| 104 | +def test_dtype_mismatch_raises(): |
| 105 | + dest = torch.zeros(8, 32, dtype=torch.bfloat16, device="cuda") |
| 106 | + src = torch.randn(8, 32, dtype=torch.float16, device="cuda") |
| 107 | + with pytest.raises(RuntimeError): |
| 108 | + torch.ops.trtllm.inplace_slice_copy(dest, src, 0, 32) |
| 109 | + |
| 110 | + |
| 111 | +def test_out_of_bounds_raises(): |
| 112 | + dtype = torch.bfloat16 |
| 113 | + dest = torch.zeros(8, 32, dtype=dtype, device="cuda") |
| 114 | + src = torch.randn(8, 8, dtype=dtype, device="cuda") |
| 115 | + with pytest.raises(RuntimeError): |
| 116 | + torch.ops.trtllm.inplace_slice_copy(dest, src, 28, 36) |
| 117 | + |
| 118 | + |
| 119 | +def test_negative_dim1_start_raises(): |
| 120 | + """A negative dim1_start would underflow the dest pointer.""" |
| 121 | + dtype = torch.bfloat16 |
| 122 | + dest = torch.zeros(8, 32, dtype=dtype, device="cuda") |
| 123 | + src = torch.randn(8, 8, dtype=dtype, device="cuda") |
| 124 | + with pytest.raises(RuntimeError): |
| 125 | + torch.ops.trtllm.inplace_slice_copy(dest, src, -8, 0) |
| 126 | + |
| 127 | + |
| 128 | +def test_device_mismatch_raises(): |
| 129 | + """dest and src on different CUDA devices must be rejected.""" |
| 130 | + if torch.cuda.device_count() < 2: |
| 131 | + pytest.skip("requires >= 2 CUDA devices") |
| 132 | + dtype = torch.bfloat16 |
| 133 | + dest = torch.zeros(8, 32, dtype=dtype, device="cuda:0") |
| 134 | + src = torch.randn(8, 32, dtype=dtype, device="cuda:1") |
| 135 | + with pytest.raises(RuntimeError): |
| 136 | + torch.ops.trtllm.inplace_slice_copy(dest, src, 0, 32) |
0 commit comments