MCPcopy Create free account
hub / github.com/EmbodiedGPT/EmbodiedGPT_Pytorch / compress

Function compress

robohusky/compression.py:144–194  ·  view source on GitHub ↗

Simulate group-wise quantization.

(tensor, config)

Source from the content-addressed store, hash-verified

142
143
144def compress(tensor, config):
145 """Simulate group-wise quantization."""
146 if not config.enabled:
147 return tensor
148
149 group_size, num_bits, group_dim, symmetric = (
150 config.group_size,
151 config.num_bits,
152 config.group_dim,
153 config.symmetric,
154 )
155 assert num_bits <= 8
156
157 original_shape = tensor.shape
158 num_groups = (original_shape[group_dim] + group_size - 1) // group_size
159 new_shape = (
160 original_shape[:group_dim]
161 + (num_groups, group_size)
162 + original_shape[group_dim + 1 :]
163 )
164
165 # Pad
166 pad_len = (group_size - original_shape[group_dim] % group_size) % group_size
167 if pad_len != 0:
168 pad_shape = (
169 original_shape[:group_dim] + (pad_len,) + original_shape[group_dim + 1 :]
170 )
171 tensor = torch.cat(
172 [tensor, torch.zeros(pad_shape, dtype=tensor.dtype, device=tensor.device)],
173 dim=group_dim,
174 )
175 data = tensor.view(new_shape)
176
177 # Quantize
178 if symmetric:
179 B = 2 ** (num_bits - 1) - 1
180 scale = B / torch.max(data.abs(), dim=group_dim + 1, keepdim=True)[0]
181 data = data * scale
182 data = data.clamp_(-B, B).round_().to(torch.int8)
183 return data, scale, original_shape
184 else:
185 B = 2**num_bits - 1
186 mn = torch.min(data, dim=group_dim + 1, keepdim=True)[0]
187 mx = torch.max(data, dim=group_dim + 1, keepdim=True)[0]
188
189 scale = B / (mx - mn)
190 data = data - mn
191 data.mul_(scale)
192
193 data = data.clamp_(0, B).round_().to(torch.uint8)
194 return data, mn, scale, original_shape
195
196
197def decompress(packed_data, config):

Callers 2

__init__Method · 0.85
load_compress_modelFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected