yobx.torch.in_transformers.flatten_class#
Flatten and unflatten functions for transformers cache types.
Enables registration of transformers cache classes with
pytree so that torch.export.export()
can handle them as inputs and outputs.
- yobx.torch.in_transformers.flatten_class.flatten_dynamic_cache(dynamic_cache: DynamicCache) Tuple[List[Any], Any][source]#
Serializes a
transformers.cache_utils.DynamicCachewith python objects.Note
Mixed-layer-type encoding (e.g.
DynamicSlidingWindowLayer) is only supported whenDynamicCacheexposes alayersattribute, which was introduced intransformers >= 4.50. On older versions the cache is serialized with plainkey_<i>/value_<i>keys and no per-layer type information is preserved.
- yobx.torch.in_transformers.flatten_class.flatten_encoder_decoder_cache(ec_cache: EncoderDecoderCache) Tuple[List[Any], Any][source]#
Serializes a
transformers.cache_utils.EncoderDecoderCachewith python objects.
- yobx.torch.in_transformers.flatten_class.flatten_static_cache(cache: StaticCache) Tuple[List[Any], Any][source]#
Serializes a
transformers.cache_utils.StaticCachewith python objects.
- yobx.torch.in_transformers.flatten_class.flatten_with_keys_dynamic_cache(dynamic_cache: DynamicCache) Tuple[List[Any], Any][source]#
Serializes a
transformers.cache_utils.DynamicCachewith python objects.Note
Mixed-layer-type encoding (e.g.
DynamicSlidingWindowLayer) is only supported whenDynamicCacheexposes alayersattribute, which was introduced intransformers >= 4.50. On older versions the cache is serialized with plainkey_<i>/value_<i>keys and no per-layer type information is preserved.
- yobx.torch.in_transformers.flatten_class.flatten_with_keys_encoder_decoder_cache(ec_cache: EncoderDecoderCache) Tuple[List[Any], Any][source]#
Serializes a
transformers.cache_utils.EncoderDecoderCachewith python objects.
- yobx.torch.in_transformers.flatten_class.flatten_with_keys_static_cache(cache: StaticCache) Tuple[List[Any], Any][source]#
Serializes a
transformers.cache_utils.StaticCachewith python objects.
- yobx.torch.in_transformers.flatten_class.unflatten_dynamic_cache(values: List[Any], context: Any, output_type=None) DynamicCache[source]#
Restores a
transformers.cache_utils.DynamicCachefrom python objects.Note
Reconstruction of mixed layer types requires
DynamicCacheto expose alayersattribute (transformers >= 4.50). On older versions only homogeneousDynamicLayercaches can be restored.