|
| 1 | +""" |
| 2 | +Copyright (c) 2024 by FlashInfer team. |
| 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 | +""" |
| 16 | + |
| 17 | +import flashinfer |
| 18 | +import numpy as np |
| 19 | +import torch |
| 20 | +import pytest |
| 21 | + |
| 22 | + |
| 23 | +def test_mlc_failed_case(): |
| 24 | + kv_layout = "HND" |
| 25 | + num_pages = 12 |
| 26 | + kv_indptr_1 = torch.tensor([0, 0, 9]).int().to(0) |
| 27 | + kv_indices_1 = torch.tensor([3, 4, 5, 6, 7, 8, 9, 10, 11]).int().to(0) |
| 28 | + kv_last_page_len_1 = torch.tensor([0, 1]).int().to(0) |
| 29 | + num_qo_heads = 32 |
| 30 | + num_kv_heads = 32 |
| 31 | + page_size = 16 |
| 32 | + head_dim = 128 |
| 33 | + q = torch.randn(2, num_qo_heads, head_dim).to(0).half() |
| 34 | + kv_data = torch.randn(12, 2, num_kv_heads, page_size, head_dim).to(0).half() |
| 35 | + |
| 36 | + workspace_buffer = torch.empty(128 * 1024 * 1024, dtype=torch.int8).to(0) |
| 37 | + wrapper = flashinfer.BatchDecodeWithPagedKVCacheWrapper(workspace_buffer, kv_layout) |
| 38 | + wrapper.begin_forward( |
| 39 | + kv_indptr_1, |
| 40 | + kv_indices_1, |
| 41 | + kv_last_page_len_1, |
| 42 | + num_qo_heads, |
| 43 | + num_kv_heads, |
| 44 | + head_dim, |
| 45 | + page_size, |
| 46 | + pos_encoding_mode="NONE", |
| 47 | + data_type=torch.float16, |
| 48 | + q_data_type=torch.float16, |
| 49 | + ) |
| 50 | + o_1, lse_1 = wrapper.forward_return_lse(q, kv_data) |
| 51 | + |
| 52 | + wrapper_tensor_cores = flashinfer.BatchDecodeWithPagedKVCacheWrapper( |
| 53 | + workspace_buffer, kv_layout, use_tensor_cores=True |
| 54 | + ) |
| 55 | + wrapper_tensor_cores.begin_forward( |
| 56 | + kv_indptr_1, |
| 57 | + kv_indices_1, |
| 58 | + kv_last_page_len_1, |
| 59 | + num_qo_heads, |
| 60 | + num_kv_heads, |
| 61 | + head_dim, |
| 62 | + page_size, |
| 63 | + pos_encoding_mode="NONE", |
| 64 | + data_type=torch.float16, |
| 65 | + q_data_type=torch.float16, |
| 66 | + ) |
| 67 | + o_1_tc, lse_1_tc = wrapper_tensor_cores.forward_return_lse( |
| 68 | + q, kv_data |
| 69 | + ) |
| 70 | + |
| 71 | + np.testing.assert_allclose( |
| 72 | + lse_1.cpu().numpy(), lse_1_tc.cpu().numpy(), rtol=1e-3, atol=1e-3 |
| 73 | + ) |
| 74 | + np.testing.assert_allclose( |
| 75 | + o_1.cpu().numpy(), o_1_tc.cpu().numpy(), rtol=1e-3, atol=1e-3 |
| 76 | + ) |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + test_mlc_failed_case() |
0 commit comments