compressed-tensors 0.10.3a20250805__py3-none-any.whl → 0.10.3a20250806__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.
@@ -13,7 +13,8 @@
13
13
  # limitations under the License.
14
14
 
15
15
  from abc import ABC, abstractmethod
16
- from typing import Optional
16
+ from collections import defaultdict
17
+ from typing import List, Optional, Tuple, Set
17
18
 
18
19
  import torch
19
20
  import torch.nn.utils.parametrize as P
@@ -49,10 +50,13 @@ class TransformFactory(RegistryMixin, ABC):
49
50
  :param seed: random seed used to transform weight randomization
50
51
  """
51
52
 
53
+ transforms: List["TransformBase"]
54
+
52
55
  def __init__(self, name: str, scheme: TransformScheme, seed: Optional[int] = None):
53
56
  self.name = name
54
57
  self.scheme = scheme
55
58
  self.generator = torch.Generator()
59
+ self.transforms = list()
56
60
  if seed is not None:
57
61
  self.generator.manual_seed(seed)
58
62
 
@@ -90,6 +94,8 @@ class TransformFactory(RegistryMixin, ABC):
90
94
  for _, module in match_named_modules(model, arg.targets, arg.ignore):
91
95
  self._apply_to_module(module, arg)
92
96
 
97
+ self._update_tied_weights()
98
+
93
99
  def _apply_to_module(self, module: Module, args: TransformArgs):
94
100
  """
95
101
  Create transforms and apply them to the module
@@ -97,9 +103,17 @@ class TransformFactory(RegistryMixin, ABC):
97
103
  :param module: target module to apply transforms to
98
104
  :param args: defines how the transform will be applied to the target module
99
105
  """
106
+ if has_offloaded_params(module):
107
+ if module._hf_hook.place_submodules:
108
+ raise NotImplementedError(
109
+ "Applying transforms to offloaded submodules with "
110
+ "`place_submodules=True` is not supported"
111
+ )
112
+
100
113
  # create transform as submodule
101
114
  transform_name = f"{self.name}_{args.location}"
102
115
  transform = self.create_transform(module, args)
116
+ self.transforms.append(transform)
103
117
  register_offload_module(module, transform_name, transform)
104
118
 
105
119
  # register input transformation hook
@@ -128,8 +142,9 @@ class TransformFactory(RegistryMixin, ABC):
128
142
  raise ValueError("Offloaded training is not supported")
129
143
  P.register_parametrization(module, "weight", transform)
130
144
 
131
- # transform is no longer needed (unfusing is not supported)
132
- delete_offload_module(module, transform_name)
145
+ else:
146
+ # transform is no longer needed (unfusing is not supported)
147
+ delete_offload_module(module, transform_name)
133
148
 
134
149
  # register output transformation hook
135
150
  elif args.location == TransformLocation.OUTPUT:
@@ -143,6 +158,31 @@ class TransformFactory(RegistryMixin, ABC):
143
158
  else:
144
159
  raise NotImplementedError()
145
160
 
161
+ def _update_tied_weights(self):
162
+ """
163
+ Populate the `_dynamic_tied_weights_keys` attribute of transforms,
164
+ which is used by transformers to detect and remove shared pointers
165
+ during saving
166
+ """
167
+ # map from data_ptrs to keys
168
+ ptr_to_keys: dict[int, List[Tuple[TransformBase, str]]] = defaultdict(list)
169
+ for transform in self.transforms:
170
+ for name, param in transform.named_parameters(recurse=False):
171
+ # NOTE: previously asserted that parent._hf_hook.place_submodules=False
172
+ if has_offloaded_params(transform):
173
+ param = transform._hf_hook.weights_map[name]
174
+ ptr_to_keys[param.data_ptr()].append((transform, name))
175
+
176
+ # populate `_dynamic_tied_weights_keys` if there is more than one key
177
+ # and ensure that they share tensors
178
+ for shared_keys in ptr_to_keys.values():
179
+ if len(shared_keys) > 1:
180
+ tensor = getattr(shared_keys[0][0], shared_keys[0][1])
181
+
182
+ for transform, name in shared_keys:
183
+ transform._dynamic_tied_weights_keys.add(name)
184
+ setattr(transform, name, tensor)
185
+
146
186
 
147
187
  class TransformBase(InternalModule, ABC):
148
188
  """
@@ -151,6 +191,11 @@ class TransformBase(InternalModule, ABC):
151
191
 
152
192
  args: TransformArgs
153
193
  weight: Parameter
194
+ _dynamic_tied_weights_keys: Set[str]
195
+
196
+ def __init__(self):
197
+ super().__init__()
198
+ self._dynamic_tied_weights_keys = set()
154
199
 
155
200
  @abstractmethod
156
201
  def forward(self, value: Tensor) -> Tensor:
@@ -70,6 +70,7 @@ class RandomMatrixFactory(TransformFactory):
70
70
 
71
71
  def _create_inverse(self, weight: Parameter) -> Parameter:
72
72
  data = high_precision_invert(weight.data)
73
+ data = data.contiguous() # ensure proper serialization
73
74
  return Parameter(data, requires_grad=False)
74
75
 
75
76
 
@@ -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.a20250805'
20
+ __version__ = version = '0.10.3.a20250806'
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.3a20250805
3
+ Version: 0.10.3a20250806
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.
@@ -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=UcH3DkUtSV6xgd1l5QTWXLV_iWa7GzNrCWIOpZvkzkE,523
3
+ compressed_tensors/version.py,sha256=AuoKIjSgjjAcZIPZe3HN5zhNJ7enhDAjwQrqUHPg76o,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
@@ -45,9 +45,9 @@ compressed_tensors/transform/transform_args.py,sha256=jJY-Qt996w45LWQ10AHd7tUtNr
45
45
  compressed_tensors/transform/transform_config.py,sha256=A3RuLNDqBNEByQNeu40Kg7sItwE6kWgnX18Umg1uONI,2128
46
46
  compressed_tensors/transform/transform_scheme.py,sha256=uGLC4avdbhrVqNC3-Eo0p7WzNRQK92Fpg0N9hWiuCRQ,1752
47
47
  compressed_tensors/transform/factory/__init__.py,sha256=fH6rjBYAxuwrTzBTlTjTgCYNyh6TCvCqajCz4Im4YrA,617
48
- compressed_tensors/transform/factory/base.py,sha256=Zplf8QO-mFqGwDEhLdYL_afSu7v4nMa79oNhidRNPvY,5880
48
+ compressed_tensors/transform/factory/base.py,sha256=NJ3lI95tJk6gHOeZEVheQ_Ae7NHhhUG_9FHXu613x30,7740
49
49
  compressed_tensors/transform/factory/hadamard.py,sha256=B0BVjbF3y707MO6L2XfEoZJTQU965vU9dUPLOiUSXII,4193
50
- compressed_tensors/transform/factory/matrix_multiply.py,sha256=LdoV2E12HTucmUWcw7UKOpRNnL8QhOOIUnNVlpOpGiI,3925
50
+ compressed_tensors/transform/factory/matrix_multiply.py,sha256=kCB7cfM_PCgJDyyhg2d1rKTEiyuscwzhprXY7VfIx6E,3989
51
51
  compressed_tensors/transform/factory/random_hadamard.py,sha256=nUhTlFa4ikSpcl4Umme71pnjMPgwYoGlwjKlU27UHZ4,1634
52
52
  compressed_tensors/transform/utils/__init__.py,sha256=fH6rjBYAxuwrTzBTlTjTgCYNyh6TCvCqajCz4Im4YrA,617
53
53
  compressed_tensors/transform/utils/hadamard.py,sha256=hDJZC0Gw2fKdxqa3f8TmFc5J0eJqxHtFRxswLU_yVJc,5548
@@ -62,8 +62,8 @@ compressed_tensors/utils/permutations_24.py,sha256=kx6fsfDHebx94zsSzhXGyCyuC9sVy
62
62
  compressed_tensors/utils/permute.py,sha256=V6tJLKo3Syccj-viv4F7ZKZgJeCB-hl-dK8RKI_kBwI,2355
63
63
  compressed_tensors/utils/safetensors_load.py,sha256=DMfZBuUbA6qp_BG_zIWT3ckiEE33K9ob34s-OgzReO4,12057
64
64
  compressed_tensors/utils/semi_structured_conversions.py,sha256=XKNffPum54kPASgqKzgKvyeqWPAkair2XEQXjkp7ho8,13489
65
- compressed_tensors-0.10.3a20250805.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
66
- compressed_tensors-0.10.3a20250805.dist-info/METADATA,sha256=8SpvZ9SNB_DGL6L4I8QrtLczHtxI17ezOlwf6Ew_4R8,7031
67
- compressed_tensors-0.10.3a20250805.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
68
- compressed_tensors-0.10.3a20250805.dist-info/top_level.txt,sha256=w2i-GyPs2s1UwVxvutSvN_lM22SXC2hQFBmoMcPnV7Y,19
69
- compressed_tensors-0.10.3a20250805.dist-info/RECORD,,
65
+ compressed_tensors-0.10.3a20250806.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
66
+ compressed_tensors-0.10.3a20250806.dist-info/METADATA,sha256=e8DIx-6UDn2Wj7fGLEBgVru2k9Tme9dOPgxS_ciZDcw,7031
67
+ compressed_tensors-0.10.3a20250806.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
68
+ compressed_tensors-0.10.3a20250806.dist-info/top_level.txt,sha256=w2i-GyPs2s1UwVxvutSvN_lM22SXC2hQFBmoMcPnV7Y,19
69
+ compressed_tensors-0.10.3a20250806.dist-info/RECORD,,