compressed-tensors 0.10.3a20250703__py3-none-any.whl → 0.10.3a20250708__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- from typing import Optional
15
+ from typing import Optional, Union
16
16
 
17
17
  import torch
18
18
  from compressed_tensors.transform import TransformArgs, TransformScheme
@@ -22,7 +22,7 @@ from compressed_tensors.transform.utils.utils import (
22
22
  apply_transform_weight,
23
23
  get_matrix_size,
24
24
  )
25
- from compressed_tensors.utils import get_offloaded_device
25
+ from compressed_tensors.utils import get_execution_device, get_offloaded_device
26
26
  from compressed_tensors.utils.helpers import ParameterizedDefaultDict
27
27
  from torch import Tensor, device, dtype
28
28
  from torch.nn import Linear, Module, Parameter
@@ -41,6 +41,7 @@ class HadamardFactory(TransformFactory):
41
41
  def __init__(self, name: str, scheme: TransformScheme, seed: Optional[int] = None):
42
42
  super().__init__(name, scheme, seed)
43
43
  self.weights = ParameterizedDefaultDict(self._create_weight)
44
+ self.perms = ParameterizedDefaultDict(self._create_permutation)
44
45
 
45
46
  def create_transform(self, module: Module, args: TransformArgs):
46
47
  """
@@ -54,26 +55,46 @@ class HadamardFactory(TransformFactory):
54
55
  size = get_matrix_size(module, args.location)
55
56
  dtype = module.weight.dtype
56
57
  device = get_offloaded_device(module)
58
+ exec_device = get_execution_device(module)
57
59
 
58
- weight = self.weights[size, dtype, device]
59
- return HadamardTransform(weight, args)
60
+ factory_kwargs = {"construct_device": exec_device}
61
+ weight = self.weights.get(size, dtype, device, factory_kwargs=factory_kwargs)
62
+ perm = self.perms[weight] if self.scheme.randomize else None
63
+ return HadamardTransform(weight, perm, args)
60
64
 
61
- def _create_weight(self, size: int, dtype: dtype, device: device) -> Parameter:
62
- data = deterministic_hadamard_matrix(size, dtype, device)
63
- data = data.to(dtype=dtype, device=device)
65
+ def _create_weight(
66
+ self,
67
+ size: int,
68
+ dtype: dtype,
69
+ device: device,
70
+ construct_device: device,
71
+ ) -> Parameter:
72
+ # construct on execution device, cache on offload device
73
+ data = deterministic_hadamard_matrix(size, dtype, construct_device)
74
+ data = data.to(device=device)
64
75
  return Parameter(data, requires_grad=self.scheme.requires_grad)
65
76
 
77
+ def _create_permutation(self, weight: Parameter) -> Parameter:
78
+ data = torch.randperm(weight.size(0), generator=self.generator)
79
+ return Parameter(data, requires_grad=False)
80
+
66
81
 
67
82
  class HadamardTransform(TransformBase):
68
- def __init__(self, weight: Parameter, args: TransformArgs):
83
+ def __init__(
84
+ self, weight: Parameter, perm: Union[Parameter, None], args: TransformArgs
85
+ ):
69
86
  super().__init__()
70
87
  self.weight = weight
88
+ self.perm = perm
71
89
  self.args = args
72
90
 
73
91
  def forward(self, value: Tensor) -> Tensor:
74
- if not self.args.inverse:
75
- weight = self.weight
76
- else:
77
- weight = self.weight.T
92
+ weight = self.weight
93
+
94
+ if self.perm is not None:
95
+ weight = weight[self.perm][:, self.perm]
96
+
97
+ if self.args.inverse:
98
+ weight = weight.T
78
99
 
79
100
  return apply_transform_weight(weight, value, self.args.location)
@@ -62,6 +62,7 @@ class RandomMatrixFactory(TransformFactory):
62
62
  return RandomMatrixTransform(weight, args)
63
63
 
64
64
  def _create_weight(self, size: int, dtype: dtype, device: device) -> Parameter:
65
+ # TODO: verify that weight is invertible (has non-zero determinant)
65
66
  data = torch.rand(
66
67
  (size, size), generator=self.generator, dtype=dtype, device=device
67
68
  )
@@ -28,7 +28,14 @@ class RandomHadamardFactory(HadamardFactory):
28
28
  :param seed: random seed used to transform weight randomization
29
29
  """
30
30
 
31
- def _create_weight(self, size: int, dtype: dtype, device: device) -> Parameter:
32
- data = random_hadamard_matrix(size, dtype, device, self.generator)
33
- data = data.to(dtype=dtype, device=device)
31
+ def _create_weight(
32
+ self,
33
+ size: int,
34
+ dtype: dtype,
35
+ device: device,
36
+ construct_device: device,
37
+ ) -> Parameter:
38
+ # construct on execution device, cache on offload device
39
+ data = random_hadamard_matrix(size, dtype, construct_device, self.generator)
40
+ data = data.to(device=device)
34
41
  return Parameter(data, requires_grad=self.scheme.requires_grad)
@@ -49,7 +49,7 @@ QUIP = TransformConfig(
49
49
  inverse=True,
50
50
  ),
51
51
  ],
52
- randomize_modules=True,
52
+ randomize=True,
53
53
  ),
54
54
  "u": TransformScheme(
55
55
  type="hadamard",
@@ -62,7 +62,7 @@ QUIP = TransformConfig(
62
62
  targets=["Linear"], location="output", inverse=True # non-mergable
63
63
  ),
64
64
  ],
65
- randomize_modules=True,
65
+ randomize=True,
66
66
  ),
67
67
  }
68
68
  )
@@ -31,13 +31,12 @@ class TransformScheme(BaseModel):
31
31
  (see `Transforms.registered_names()`)
32
32
  :param apply: list of TransformationArgs containing the information about the
33
33
  modules that should be targeted by the specified transform
34
- :param randomize_modules: True if unique transforms should be applied to each
35
- unique module targeted by `apply`, otherwise reuse transform weights where
36
- applicable
34
+ :param randomize: True if uniquely randomized transform weights should be used,
35
+ otherwise use identical transform weights where applicable
37
36
  :param requires_grad: True if weights include gradients for training
38
37
  """
39
38
 
40
39
  type: str
41
40
  apply: List[TransformArgs] = Field(default_factory=list)
42
- randomize_modules: bool = Field(default=False)
41
+ randomize: bool = Field(default=False)
43
42
  requires_grad: bool = Field(default=False)
@@ -15,10 +15,11 @@
15
15
  import contextlib
16
16
  import warnings
17
17
  from functools import wraps
18
- from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
18
+ from typing import TYPE_CHECKING, Any, Callable, Dict, List, Mapping, Optional
19
19
 
20
20
  import numpy
21
21
  import torch
22
+ from frozendict import frozendict
22
23
  from transformers import AutoConfig
23
24
 
24
25
 
@@ -373,11 +374,23 @@ class ParameterizedDefaultDict(dict):
373
374
 
374
375
  def __init__(self, default_factory: Callable[[Any], Any]):
375
376
  self.default_factory = default_factory
377
+ self._factory_kwargs = frozendict()
376
378
 
377
- def __missing__(self, key):
379
+ def __missing__(self, key: Any) -> Any:
378
380
  if isinstance(key, tuple):
379
- value = self.default_factory(*key)
381
+ value = self.default_factory(*key, **self._factory_kwargs)
380
382
  else:
381
- value = self.default_factory(key)
383
+ value = self.default_factory(key, **self._factory_kwargs)
382
384
  self[key] = value
383
385
  return value
386
+
387
+ def get(self, *args, factory_kwargs: Mapping = frozendict()) -> Any:
388
+ """
389
+ Similar to `__getitem__`, but allows passing kwargs to factory function
390
+
391
+ :param \\*args: args whose tuple will value will be treated as key
392
+ :param factory_kwargs: keyword arguments to pass to `default_factory`
393
+ :return: dictionary entry for given key
394
+ """
395
+ with patch_attr(self, "_factory_kwargs", factory_kwargs):
396
+ return self[args]
@@ -17,5 +17,5 @@ __version__: str
17
17
  __version_tuple__: VERSION_TUPLE
18
18
  version_tuple: VERSION_TUPLE
19
19
 
20
- __version__ = version = '0.10.3.a20250703'
20
+ __version__ = version = '0.10.3.a20250708'
21
21
  __version_tuple__ = version_tuple = (0, 10, 3)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: compressed-tensors
3
- Version: 0.10.3a20250703
3
+ Version: 0.10.3a20250708
4
4
  Summary: Library for utilization of compressed safetensors of neural network models
5
5
  Home-page: https://github.com/neuralmagic/compressed-tensors
6
6
  Author: Neuralmagic, Inc.
@@ -11,6 +11,7 @@ License-File: LICENSE
11
11
  Requires-Dist: torch>=1.7.0
12
12
  Requires-Dist: transformers
13
13
  Requires-Dist: pydantic>=2.0
14
+ Requires-Dist: frozendict
14
15
  Provides-Extra: dev
15
16
  Requires-Dist: black==22.12.0; extra == "dev"
16
17
  Requires-Dist: isort==5.8.0; extra == "dev"
@@ -1,6 +1,6 @@
1
1
  compressed_tensors/__init__.py,sha256=UtKmifNeBCSE2TZSAfduVNNzHY-3V7bLjZ7n7RuXLOE,812
2
2
  compressed_tensors/base.py,sha256=73HYH7HY7O2roC89yG_piPFnZwrBfn_i7HmKl90SKc0,875
3
- compressed_tensors/version.py,sha256=04rxXoVqmA3MUpLQgR38io8vaMTr_WfSI4-0of2By0w,523
3
+ compressed_tensors/version.py,sha256=rt9C5dMk9h9d8wWYD1c12HvB1Cl7FNRVoxT2irO1UxE,523
4
4
  compressed_tensors/compressors/__init__.py,sha256=smSygTSfcfuujRrAXDc6uZm4L_ccV1tWZewqVnOb4lM,825
5
5
  compressed_tensors/compressors/base.py,sha256=nvWsv4xEw1Tkxkxth6TmHplDYXfBeP22xWxOsZERyDY,7204
6
6
  compressed_tensors/compressors/helpers.py,sha256=OK6qxX9j3bHwF9JfIYSGMgBJe2PWjlTA3byXKCJaTIQ,5431
@@ -41,26 +41,26 @@ compressed_tensors/registry/__init__.py,sha256=FwLSNYqfIrb5JD_6OK_MT4_svvKTN_nEh
41
41
  compressed_tensors/registry/registry.py,sha256=0s15BxdGgzBv8RL4kUJCYcuDOFUh_KZYvNvLEeRqWTc,11956
42
42
  compressed_tensors/transform/__init__.py,sha256=mtUOzwq-H7fXGi7sMmfe7zj83fjMg_LAu4DjTZ5vaHk,886
43
43
  compressed_tensors/transform/transform_args.py,sha256=8-Ab5_dFfdObfwVCgrWrEWcoVRzXmMBSDSUxjftI-Ss,3177
44
- compressed_tensors/transform/transform_config.py,sha256=6JA8VFcoz4EGHOev6thj51OuB7K2gKUUazWjrVPYDLc,2144
45
- compressed_tensors/transform/transform_scheme.py,sha256=c7NAuLDL0itFgUfBMNShegMI9bzKL7s4LR3QJTHsXLs,1733
44
+ compressed_tensors/transform/transform_config.py,sha256=A3RuLNDqBNEByQNeu40Kg7sItwE6kWgnX18Umg1uONI,2128
45
+ compressed_tensors/transform/transform_scheme.py,sha256=JAFQoCiNLg04diXG5KsynRGcLIB0Y0tC5s8U7HoDM7c,1692
46
46
  compressed_tensors/transform/factory/__init__.py,sha256=fH6rjBYAxuwrTzBTlTjTgCYNyh6TCvCqajCz4Im4YrA,617
47
47
  compressed_tensors/transform/factory/base.py,sha256=yVrYWEnrr2RFWE5AjSNeXzO9aXc443dTNMVSxuLztz8,5940
48
- compressed_tensors/transform/factory/hadamard.py,sha256=zkq6w8uJXRLokUXajAkFb2fJrH0K3SL6qrR2dARrAr8,3139
49
- compressed_tensors/transform/factory/matrix_multiply.py,sha256=0g4sYC_tOmCjOomae2gl54UTXiFdl0mCCkmbqIRX8yw,3613
50
- compressed_tensors/transform/factory/random_hadamard.py,sha256=TFInxbHslqREOFFiy_mpR88eEYXQnslxXmyh-ZbN-MU,1499
48
+ compressed_tensors/transform/factory/hadamard.py,sha256=oLdDUu1p82lgD7li-sHMSvXZxz1SDjLeYf-EfXqNzvk,3918
49
+ compressed_tensors/transform/factory/matrix_multiply.py,sha256=KYiQRGFSU33TpPWkGTKwNADTmYoU0E3hjQypOMclHbg,3689
50
+ compressed_tensors/transform/factory/random_hadamard.py,sha256=nUhTlFa4ikSpcl4Umme71pnjMPgwYoGlwjKlU27UHZ4,1634
51
51
  compressed_tensors/transform/utils/__init__.py,sha256=fH6rjBYAxuwrTzBTlTjTgCYNyh6TCvCqajCz4Im4YrA,617
52
52
  compressed_tensors/transform/utils/hadamard.py,sha256=U27Kvo-eDebKcVt8oXTSIAaQ5DvPQj9tDv2hdXHCPPQ,5584
53
53
  compressed_tensors/transform/utils/hadamards.safetensors,sha256=mFd1GzNodGG-ifA1IoH-0nHYzfraCOvrq_dX2zFI1B4,1436901
54
54
  compressed_tensors/transform/utils/utils.py,sha256=PRPTYwPs2nnNaQMq2GEbC4QYKHFKlZwaRyPgdDhl66g,2992
55
55
  compressed_tensors/utils/__init__.py,sha256=gS4gSU2pwcAbsKj-6YMaqhm25udFy6ISYaWBf-myRSM,808
56
- compressed_tensors/utils/helpers.py,sha256=cPg-ikdeA92aIGwBONg8GmPNvcGlFhozyJVwsRiXBTA,11981
56
+ compressed_tensors/utils/helpers.py,sha256=Q3iRAa2XSdmmn4vSpUplnvKOmWwn4Clao9ZkPBHXtpI,12604
57
57
  compressed_tensors/utils/offload.py,sha256=3XiBuWbUkBAt8v1t5i57qDcbB3VJQs_FDeayi-JzIWg,23896
58
58
  compressed_tensors/utils/permutations_24.py,sha256=kx6fsfDHebx94zsSzhXGyCyuC9sVyah6BUUir_StT28,2530
59
59
  compressed_tensors/utils/permute.py,sha256=V6tJLKo3Syccj-viv4F7ZKZgJeCB-hl-dK8RKI_kBwI,2355
60
60
  compressed_tensors/utils/safetensors_load.py,sha256=DMfZBuUbA6qp_BG_zIWT3ckiEE33K9ob34s-OgzReO4,12057
61
61
  compressed_tensors/utils/semi_structured_conversions.py,sha256=XKNffPum54kPASgqKzgKvyeqWPAkair2XEQXjkp7ho8,13489
62
- compressed_tensors-0.10.3a20250703.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
63
- compressed_tensors-0.10.3a20250703.dist-info/METADATA,sha256=302O-3Co7eWCGmk4dPjPW2trRYnQnhPfR8lZoPXTEmI,7005
64
- compressed_tensors-0.10.3a20250703.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
65
- compressed_tensors-0.10.3a20250703.dist-info/top_level.txt,sha256=w2i-GyPs2s1UwVxvutSvN_lM22SXC2hQFBmoMcPnV7Y,19
66
- compressed_tensors-0.10.3a20250703.dist-info/RECORD,,
62
+ compressed_tensors-0.10.3a20250708.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
63
+ compressed_tensors-0.10.3a20250708.dist-info/METADATA,sha256=eY_wXSsGo1nsV1y993HgKBKU9KCbQDoi9VdSFEAkRes,7031
64
+ compressed_tensors-0.10.3a20250708.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
65
+ compressed_tensors-0.10.3a20250708.dist-info/top_level.txt,sha256=w2i-GyPs2s1UwVxvutSvN_lM22SXC2hQFBmoMcPnV7Y,19
66
+ compressed_tensors-0.10.3a20250708.dist-info/RECORD,,