|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import unittest |
| 8 | +from types import ModuleType |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +import torch |
| 12 | + |
| 13 | +with patch.dict( |
| 14 | + "sys.modules", |
| 15 | + {"executorch.extension.llm.custom_ops.custom_ops": ModuleType("custom_ops")}, |
| 16 | +): |
| 17 | + from executorch.examples.models.voxtral_realtime.model import StandardRingKVCache |
| 18 | + |
| 19 | + |
| 20 | +class StandardRingKVCacheTest(unittest.TestCase): |
| 21 | + def test_additive_mask_uses_finite_negative_values(self): |
| 22 | + cache = StandardRingKVCache(window_size=4, n_heads=1, head_dim=2) |
| 23 | + |
| 24 | + mask = cache.create_causal_mask( |
| 25 | + torch.tensor(0), seq_len=1, dtype=torch.bfloat16 |
| 26 | + ) |
| 27 | + |
| 28 | + self.assertEqual(mask.dtype, torch.bfloat16) |
| 29 | + self.assertTrue(torch.isfinite(mask).all()) |
| 30 | + self.assertEqual(mask[0, 0].item(), 0) |
| 31 | + self.assertLess(mask[0, 1].float().item(), -1e8) |
| 32 | + |
| 33 | + def test_bool_mask_keeps_bool_dtype(self): |
| 34 | + cache = StandardRingKVCache(window_size=4, n_heads=1, head_dim=2) |
| 35 | + |
| 36 | + mask = cache.create_causal_mask(torch.tensor(3), seq_len=2, bool_mask=True) |
| 37 | + |
| 38 | + self.assertEqual(mask.dtype, torch.bool) |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + unittest.main() |
0 commit comments