|
| 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 | +# pyre-strict |
| 8 | +import unittest |
| 9 | +from unittest.mock import MagicMock |
| 10 | + |
| 11 | +import torch |
| 12 | + |
| 13 | +from executorch.extension.llm.export.builder import DType, LLMEdgeManager |
| 14 | + |
| 15 | + |
| 16 | +class TestLLMEdgeManager(unittest.TestCase): |
| 17 | + def setUp(self) -> None: |
| 18 | + # Create a mock model |
| 19 | + self.mock_model = MagicMock() |
| 20 | + self.modelname = "test_model" |
| 21 | + self.max_seq_len = 2048 |
| 22 | + self.dtype = DType.fp32 |
| 23 | + self.example_inputs = (torch.zeros((1, 10), dtype=torch.long),) |
| 24 | + self.example_kwarg_inputs = {"input_pos": torch.tensor([0])} |
| 25 | + |
| 26 | + def test_get_dynamic_shape_with_preset_dynamic_shapes(self) -> None: |
| 27 | + """Test that _get_dynamic_shape returns preset dynamic_shapes if available.""" |
| 28 | + # Create a manager with preset dynamic_shapes |
| 29 | + preset_dynamic_shapes = {"preset": "shapes"} |
| 30 | + manager = LLMEdgeManager( |
| 31 | + model=self.mock_model, |
| 32 | + modelname=self.modelname, |
| 33 | + max_seq_len=self.max_seq_len, |
| 34 | + dtype=self.dtype, |
| 35 | + use_kv_cache=False, |
| 36 | + example_inputs=self.example_inputs, |
| 37 | + dynamic_shapes=preset_dynamic_shapes, |
| 38 | + ) |
| 39 | + |
| 40 | + # Call _get_dynamic_shape and verify it returns the preset value |
| 41 | + result = manager._get_dynamic_shape() |
| 42 | + self.assertEqual(result, preset_dynamic_shapes) |
| 43 | + |
| 44 | + def test_get_dynamic_shape_with_dynamic_shape_enabled_no_kv_cache(self) -> None: |
| 45 | + """Test _get_dynamic_shape when enable_dynamic_shape=True and use_kv_cache=False.""" |
| 46 | + # Create a manager with enable_dynamic_shape=True and use_kv_cache=False |
| 47 | + manager = LLMEdgeManager( |
| 48 | + model=self.mock_model, |
| 49 | + modelname=self.modelname, |
| 50 | + max_seq_len=self.max_seq_len, |
| 51 | + dtype=self.dtype, |
| 52 | + use_kv_cache=False, |
| 53 | + example_inputs=self.example_inputs, |
| 54 | + enable_dynamic_shape=True, |
| 55 | + ) |
| 56 | + |
| 57 | + # Call _get_dynamic_shape |
| 58 | + result = manager._get_dynamic_shape() |
| 59 | + |
| 60 | + # Verify the result has the expected structure |
| 61 | + self.assertIsInstance(result, tuple) |
| 62 | + self.assertEqual(len(result), 1) |
| 63 | + self.assertIsInstance(result[0], dict) |
| 64 | + self.assertIn(1, result[0]) |
| 65 | + # Check that the value at key 1 is a torch.export.Dim with the correct max value |
| 66 | + self.assertEqual(result[0][1].max, self.max_seq_len - 1) |
| 67 | + |
| 68 | + def test_get_dynamic_shape_with_dynamic_shape_enabled_with_kv_cache(self) -> None: |
| 69 | + """Test _get_dynamic_shape when enable_dynamic_shape=True and use_kv_cache=True.""" |
| 70 | + # Create a manager with enable_dynamic_shape=True and use_kv_cache=True |
| 71 | + manager = LLMEdgeManager( |
| 72 | + model=self.mock_model, |
| 73 | + modelname=self.modelname, |
| 74 | + max_seq_len=self.max_seq_len, |
| 75 | + dtype=self.dtype, |
| 76 | + use_kv_cache=True, |
| 77 | + example_inputs=self.example_inputs, |
| 78 | + enable_dynamic_shape=True, |
| 79 | + ) |
| 80 | + |
| 81 | + # Call _get_dynamic_shape |
| 82 | + result = manager._get_dynamic_shape() |
| 83 | + |
| 84 | + # Verify the result has the expected structure |
| 85 | + self.assertIsInstance(result, tuple) |
| 86 | + self.assertEqual(len(result), 2) |
| 87 | + |
| 88 | + # Check first element (tokens dimension) |
| 89 | + self.assertIsInstance(result[0], dict) |
| 90 | + self.assertIn(1, result[0]) |
| 91 | + self.assertEqual(result[0][1].max, self.max_seq_len - 1) |
| 92 | + |
| 93 | + # Check second element (input_pos dimension) |
| 94 | + self.assertIsInstance(result[1], dict) |
| 95 | + self.assertIn("input_pos", result[1]) |
| 96 | + self.assertIsInstance(result[1]["input_pos"], dict) |
| 97 | + self.assertIn(0, result[1]["input_pos"]) |
| 98 | + self.assertEqual(result[1]["input_pos"][0], 1) |
| 99 | + |
| 100 | + def test_get_dynamic_shape_with_dynamic_shape_disabled(self) -> None: |
| 101 | + """Test _get_dynamic_shape when enable_dynamic_shape=False.""" |
| 102 | + # Create a manager with enable_dynamic_shape=False |
| 103 | + manager = LLMEdgeManager( |
| 104 | + model=self.mock_model, |
| 105 | + modelname=self.modelname, |
| 106 | + max_seq_len=self.max_seq_len, |
| 107 | + dtype=self.dtype, |
| 108 | + use_kv_cache=True, # Doesn't matter for this test |
| 109 | + example_inputs=self.example_inputs, |
| 110 | + enable_dynamic_shape=False, |
| 111 | + ) |
| 112 | + |
| 113 | + # Call _get_dynamic_shape |
| 114 | + result = manager._get_dynamic_shape() |
| 115 | + |
| 116 | + # Verify the result is None |
| 117 | + self.assertIsNone(result) |
0 commit comments