MCPcopy Create free account
hub / github.com/microsoft/BitNet / interleave_weight_int8

Function interleave_weight_int8

gpu/pack_weight.py:56–72  ·  view source on GitHub ↗
(qweight, nbits=2)

Source from the content-addressed store, hash-verified

54
55
56def interleave_weight_int8(qweight, nbits=2):\
57 # reinterpret the data type of qweight to int32
58 # shift = [ 0, 8, 16, 24, 2, 10, 18, 26, 4, 12, 20, 28, 6, 14, 22, 30]
59 # index: [ 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15]
60 qweight = qweight.view(np.int32)
61 new_qweight = np.zeros_like(qweight)
62 bits_stride = 8
63 mask = (1 << nbits) - 1 # for 4bit the val is 0x0000000f
64 num_groups = 32 // bits_stride # 4
65 elems_per_group = bits_stride // nbits # 4
66 for i in range(num_groups):
67 for j in range(elems_per_group):
68 offset = i * elems_per_group + j
69 shift = (offset % num_groups) * bits_stride + (offset // num_groups) * nbits
70
71 new_qweight |= ((qweight >> (nbits * offset)) & mask) << shift
72 return new_qweight.view(np.int8)
73
74
75

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected