Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / quantization / tensor / csa2_cache_tensor.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-12-05 05:12 +0800

1# Copyright 2026 Huawei Technologies Co., Ltd 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================ 

15"""Floating logical cache wrapper whose physical storage contains only bytes. 

16 

17Autograd attaches to this wrapper; codecs and transport Functions access its 

18packed bytes. The wrapper allocates no dense floating storage and imports no 

19codec, keeping the storage type independent of quantization. 

20""" 

21 

22from collections.abc import Callable 

23from typing import Literal 

24 

25import torch 

26from torch import Tensor 

27 

28from hyper_parallel.components.quantization.tensor.quantized_tensor import QuantizedTensor 

29 

30CSA2CacheFormat = Literal["indexer_k", "main_kv", "swa_kv"] 

31CSA2ScaleFormat = Literal["e8m0", "e4m3", "e8m7"] 

32 

33 

34class CSA2CacheTensor(QuantizedTensor): 

35 """Logical floating tensor whose only physical storage is a packed byte row.""" 

36 

37 def __new__(cls, packed: Tensor, logical_shape: tuple[int, ...], logical_dtype: torch.dtype, 

38 cache_format: CSA2CacheFormat, scale_format: CSA2ScaleFormat, 

39 requires_grad: bool = False) -> "CSA2CacheTensor": 

40 """Expose logical shape and dtype without allocating dense storage.""" 

41 return QuantizedTensor.__new__( 

42 cls, logical_shape, logical_dtype, device=packed.device, requires_grad=requires_grad, 

43 ) 

44 

45 def __init__(self, packed: Tensor, logical_shape: tuple[int, ...], logical_dtype: torch.dtype, 

46 cache_format: CSA2CacheFormat, scale_format: CSA2ScaleFormat, 

47 requires_grad: bool = False) -> None: 

48 """Attach the packed bytes and their format metadata.""" 

49 super().__init__() 

50 del logical_shape, logical_dtype, requires_grad 

51 self.packed = packed 

52 self.cache_format = cache_format 

53 self.scale_format = scale_format 

54 

55 def _new_with_transformed_data(self, transform: Callable[[Tensor], Tensor], *, 

56 requires_grad: bool) -> "CSA2CacheTensor": 

57 """Preserve metadata while detach, clone or device-copy transforms the bytes.""" 

58 return type(self)(transform(self.packed), tuple(self.shape), self.dtype, 

59 self.cache_format, self.scale_format, requires_grad) 

60 

61 @property 

62 def packed_nbytes(self) -> int: 

63 """Physical data bytes, including quantization scales.""" 

64 return self.packed.numel() 

65 

66 

67__all__ = ["CSA2CacheTensor"]