MCPcopy Create free account
hub / github.com/bitsandbytes-foundation/bitsandbytes / create_dynamic_map

Function create_dynamic_map

bitsandbytes/functional.py:296–348  ·  view source on GitHub ↗

Creates the dynamic quantiztion map. The dynamic data type is made up of a dynamic exponent and fraction. As the exponent increase from 0 to -7 the number of bits available for the fraction shrinks. This is a generalization of the dynamic type where a certain number of the

(signed=True, max_exponent_bits=7, total_bits=8)

Source from the content-addressed store, hash-verified

294
295
296def create_dynamic_map(signed=True, max_exponent_bits=7, total_bits=8):
297 """
298 Creates the dynamic quantiztion map.
299
300 The dynamic data type is made up of a dynamic exponent and
301 fraction. As the exponent increase from 0 to -7 the number
302 of bits available for the fraction shrinks.
303
304 This is a generalization of the dynamic type where a certain
305 number of the bits and be reserved for the linear quantization
306 region (the fraction). n determines the maximum number of
307 exponent bits.
308
309 For more details see
310 (8-Bit Approximations for Parallelism in Deep Learning)[https://arxiv.org/abs/1511.04561]
311 """
312
313 data = []
314 # these are additional items that come from the case
315 # where all the exponent bits are zero and no
316 # indicator bit is present
317 non_sign_bits = total_bits - 1
318 additional_items = 2 ** (non_sign_bits - max_exponent_bits) - 1
319 for i in range(max_exponent_bits):
320 fraction_items = int(
321 2 ** (i + non_sign_bits - max_exponent_bits) + 1
322 if signed
323 else 2 ** (i + non_sign_bits - max_exponent_bits + 1) + 1,
324 )
325 boundaries = torch.linspace(0.1, 1, fraction_items, dtype=torch.float32)
326 means = (boundaries[:-1] + boundaries[1:]) / 2.0
327 data += ((10 ** (-(max_exponent_bits - 1) + i)) * means).tolist()
328 if signed:
329 data += (-(10 ** (-(max_exponent_bits - 1) + i)) * means).tolist()
330
331 if additional_items > 0:
332 boundaries = torch.linspace(0.1, 1, additional_items + 1, dtype=torch.float32)
333 means = (boundaries[:-1] + boundaries[1:]) / 2.0
334 data += ((10 ** (-(max_exponent_bits - 1) + i)) * means).tolist()
335 if signed:
336 data += (-(10 ** (-(max_exponent_bits - 1) + i)) * means).tolist()
337
338 data.append(0)
339 data.append(1.0)
340
341 assert len(data) == 2**total_bits
342
343 gap = 256 - len(data)
344 for i in range(gap):
345 data.append(0)
346
347 data.sort()
348 return torch.tensor(data, dtype=torch.float32)
349
350
351def is_on_gpu(tensors: Iterable[Optional[torch.Tensor]]):

Callers 2

quantize_blockwiseFunction · 0.85
dequantize_blockwiseFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected