|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. |
| 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 | +import torch |
| 17 | + |
| 18 | +from tests.ut.base import TestBase |
| 19 | +from vllm_ascend.attention.attention_mask import AttentionMaskBuilder |
| 20 | + |
| 21 | + |
| 22 | +class TestAttentionMaskBuilder(TestBase): |
| 23 | + |
| 24 | + def test_init_attention_mask_builder(self): |
| 25 | + # generate attention_mask_builder with float16 |
| 26 | + attention_mask_builder = AttentionMaskBuilder(max_seq_len=1024, |
| 27 | + dtype=torch.float16) |
| 28 | + self.assertEqual(attention_mask_builder._seq_len_cached, 1024) |
| 29 | + self.assertEqual(attention_mask_builder.attn_mask_cache.dtype, |
| 30 | + torch.float16) |
| 31 | + self.assertEqual(attention_mask_builder.splitfuse_mask_value, -10000) |
| 32 | + self.assertEqual(attention_mask_builder.attn_mask_cache.shape, |
| 33 | + (1024, 1024)) |
| 34 | + self.assertEqual(attention_mask_builder.attn_mask_cache[0][-1], |
| 35 | + torch.tensor(float("-inf"), dtype=torch.float16)) |
| 36 | + |
| 37 | + # generate attention_mask_builder with int8 |
| 38 | + attention_mask_builder = AttentionMaskBuilder(max_seq_len=512, |
| 39 | + dtype=torch.int8) |
| 40 | + self.assertEqual(attention_mask_builder._seq_len_cached, 512) |
| 41 | + self.assertEqual(attention_mask_builder.attn_mask_cache.dtype, |
| 42 | + torch.int8) |
| 43 | + self.assertEqual(attention_mask_builder.splitfuse_mask_value, -10000) |
| 44 | + self.assertEqual(attention_mask_builder.attn_mask_cache.shape, |
| 45 | + (512, 512)) |
| 46 | + self.assertEqual(attention_mask_builder.attn_mask_cache[0][-1], |
| 47 | + torch.tensor(1, dtype=torch.int8)) |
| 48 | + |
| 49 | + def test_get_attn_mask(self): |
| 50 | + # if the len is less than max_seq_len, the attn_mask_cache will not be updated |
| 51 | + attention_mask_builder = AttentionMaskBuilder(max_seq_len=1024, |
| 52 | + dtype=torch.float16) |
| 53 | + attn_mask = attention_mask_builder.get_attn_mask( |
| 54 | + max_seq_len=512, dtype=torch.float16, device=torch.device("cpu")) |
| 55 | + self.assertEqual(attn_mask.shape, (512, 512)) |
| 56 | + self.assertEqual(attn_mask[0][-1], |
| 57 | + torch.tensor(float("-inf"), dtype=torch.float16)) |
| 58 | + self.assertEqual(attention_mask_builder._seq_len_cached, 1024) |
| 59 | + self.assertEqual(attention_mask_builder.attn_mask_cache.shape, |
| 60 | + (1024, 1024)) |
| 61 | + self.assertEqual(attention_mask_builder.attn_mask_cache[0][-1], |
| 62 | + torch.tensor(float("-inf"), dtype=torch.float16)) |
| 63 | + |
| 64 | + # if the len is greater than max_seq_len, the attn_mask_cache will be updated |
| 65 | + attn_mask = attention_mask_builder.get_attn_mask( |
| 66 | + max_seq_len=2048, dtype=torch.float16, device=torch.device("cpu")) |
| 67 | + self.assertEqual(attn_mask.shape, (2048, 2048)) |
| 68 | + self.assertEqual(attn_mask[0][-1], |
| 69 | + torch.tensor(float("-inf"), dtype=torch.float16)) |
| 70 | + self.assertEqual(attention_mask_builder._seq_len_cached, 2048) |
| 71 | + self.assertEqual(attention_mask_builder.attn_mask_cache.shape, |
| 72 | + (2048, 2048)) |
| 73 | + self.assertEqual(attention_mask_builder.attn_mask_cache[0][-1], |
| 74 | + torch.tensor(float("-inf"), dtype=torch.float16)) |
| 75 | + |
| 76 | + # if the dtype is diff, the attn_mask_cache will be updated |
| 77 | + attn_mask = attention_mask_builder.get_attn_mask( |
| 78 | + max_seq_len=1024, dtype=torch.int8, device=torch.device("cpu")) |
| 79 | + self.assertEqual(attn_mask.shape, (1024, 1024)) |
| 80 | + self.assertEqual(attn_mask[0][-1], torch.tensor(1, dtype=torch.int8)) |
| 81 | + self.assertEqual(attention_mask_builder._seq_len_cached, 1024) |
| 82 | + self.assertEqual(attention_mask_builder.attn_mask_cache.shape, |
| 83 | + (1024, 1024)) |
| 84 | + self.assertEqual(attention_mask_builder.attn_mask_cache[0][-1], |
| 85 | + torch.tensor(1, dtype=torch.int8)) |
| 86 | + |
| 87 | + def test_get_splitfuse_attn_mask(self): |
| 88 | + attention_mask_builder = AttentionMaskBuilder(max_seq_len=1024, |
| 89 | + dtype=torch.float16) |
| 90 | + attn_mask = attention_mask_builder.get_splitfuse_attn_mask( |
| 91 | + seq_lens=[512], |
| 92 | + query_lens=[512], |
| 93 | + position=torch.tensor([0]), |
| 94 | + dtype=torch.float16, |
| 95 | + device=torch.device("cpu"), |
| 96 | + ) |
| 97 | + self.assertEqual(attn_mask.shape, (1, 512)) |
| 98 | + self.assertEqual(attention_mask_builder._seq_len_cached, 1024) |
| 99 | + |
| 100 | + attn_mask = attention_mask_builder.get_splitfuse_attn_mask( |
| 101 | + seq_lens=[2048], |
| 102 | + query_lens=[1024], |
| 103 | + position=torch.tensor([0]), |
| 104 | + dtype=torch.float16, |
| 105 | + device=torch.device("cpu"), |
| 106 | + ) |
| 107 | + self.assertEqual(attn_mask.shape, (1024, 2048)) |
| 108 | + |
| 109 | + attention_mask_builder = AttentionMaskBuilder(max_seq_len=1024, |
| 110 | + dtype=torch.int8) |
| 111 | + attn_mask = attention_mask_builder.get_splitfuse_attn_mask( |
| 112 | + seq_lens=[512], |
| 113 | + query_lens=[512], |
| 114 | + position=torch.tensor([0]), |
| 115 | + dtype=torch.int8, |
| 116 | + device=torch.device("cpu"), |
| 117 | + ) |
| 118 | + self.assertEqual(attn_mask.shape, (1, 512)) |
0 commit comments