|
7 | 7 | flatten_unflatten_for_dynamic_shapes, |
8 | 8 | make_dynamic_cache, |
9 | 9 | make_encoder_decoder_cache, |
| 10 | + make_hybrid_cache, |
10 | 11 | make_mamba_cache, |
11 | 12 | make_sliding_window_cache, |
12 | 13 | make_static_cache, |
13 | 14 | ) |
| 15 | +from onnx_diagnostic.helpers.torch_helper import torch_deepcopy |
14 | 16 | from onnx_diagnostic.export import CoupleInputsDynamicShapes |
15 | 17 | from onnx_diagnostic.torch_export_patches.patch_inputs import ( |
16 | 18 | convert_dynamic_axes_into_dynamic_shapes, |
@@ -48,6 +50,10 @@ def test_replace_by(self): |
48 | 50 | past_key_values = make_dynamic_cache( |
49 | 51 | [(torch.randn(bsize, nheads, slen, dim), torch.randn(bsize, nheads, slen, dim))] |
50 | 52 | ) |
| 53 | + self.assertEqual( |
| 54 | + "DynamicCache(key_cache=#1[T1s2x4x3x7], value_cache=#1[T1s2x4x3x7])", |
| 55 | + self.string_type(past_key_values, with_shape=True), |
| 56 | + ) |
51 | 57 | kwargs = dict( |
52 | 58 | input_ids=torch.zeros(2, 3), |
53 | 59 | attention_mask=torch.zeros(2, 3), |
@@ -209,6 +215,45 @@ def test_unflatten_flatten_static_cache(self): |
209 | 215 | self.string_type(unflat, with_shape=True), |
210 | 216 | ) |
211 | 217 |
|
| 218 | + def test_make_hybrid_cache(self): |
| 219 | + cache = make_hybrid_cache( |
| 220 | + [ |
| 221 | + (torch.rand((4, 5, 6, 7)), torch.rand((4, 5, 6, 7))), |
| 222 | + (torch.rand((4, 5, 6, 7)), torch.rand((4, 5, 6, 7))), |
| 223 | + (torch.rand((4, 5, 6, 7)), torch.rand((4, 5, 6, 7))), |
| 224 | + ], |
| 225 | + ) |
| 226 | + text = self.string_type(cache, with_shape=True) |
| 227 | + self.assertEqual( |
| 228 | + "HybridCache(key_cache=#3[T1s4x5x6x7,T1s4x5x6x7,T1s4x5x6x7], " |
| 229 | + "value_cache=#3[T1s4x5x6x7,T1s4x5x6x7,T1s4x5x6x7])", |
| 230 | + text, |
| 231 | + ) |
| 232 | + self.assertEqual(0, max_diff(cache, cache)["abs"]) |
| 233 | + self.assertEqual(0, max_diff(cache, torch_deepcopy(cache))["abs"]) |
| 234 | + |
| 235 | + def test_unflatten_flatten_hybrid_cache(self): |
| 236 | + with torch_export_patches(patch_transformers=True): |
| 237 | + c2 = make_hybrid_cache( |
| 238 | + [ |
| 239 | + (torch.rand((4, 5, 6, 7)), torch.rand((4, 5, 6, 7))), |
| 240 | + (torch.rand((4, 5, 6, 7)), torch.rand((4, 5, 6, 7))), |
| 241 | + (torch.rand((4, 5, 6, 7)), torch.rand((4, 5, 6, 7))), |
| 242 | + ], |
| 243 | + ) |
| 244 | + self.assertEqual(0, max_diff(c2, c2)["abs"]) |
| 245 | + self.assertIsInstance(c2, transformers.cache_utils.HybridCache) |
| 246 | + flat, _spec = torch.utils._pytree.tree_flatten(c2) |
| 247 | + self.assertIsInstance(flat, list) |
| 248 | + self.assertEqual(len(flat), 6) |
| 249 | + unflat = flatten_unflatten_for_dynamic_shapes(c2) |
| 250 | + self.assertIsInstance(unflat, list) |
| 251 | + self.assertEqual(len(unflat), 2) |
| 252 | + self.assertEqual( |
| 253 | + "#2[#3[T1s4x5x6x7,T1s4x5x6x7,T1s4x5x6x7],#3[T1s4x5x6x7,T1s4x5x6x7,T1s4x5x6x7]]", |
| 254 | + self.string_type(unflat, with_shape=True), |
| 255 | + ) |
| 256 | + |
212 | 257 |
|
213 | 258 | if __name__ == "__main__": |
214 | 259 | unittest.main(verbosity=2) |
0 commit comments