def quantize(self, x): max = nd.max(nd.abs(x)) if max != 0: int_len = (nd.ceil(nd.log2(nd.max(nd.abs(x))))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') y = ((x * f)).floor() * (1 / f) return y return x
def quantize_to(x, bits=8): max_v = nd.max(nd.abs(x)) if max_v == 0: return x.astype(np.int8), 8 int_len = nd.ceil(nd.log2(max_v)).asscalar() sb = bits - int_len f = 2**sb y = nd.floor(x * f) y = nd.clip(y, a_min=-2**(bits - 1), a_max=2**(bits - 1) - 1) return y, sb
def int_inference(self, x): max = nd.max(nd.abs(x)) if max != 0: int_len = (nd.ceil(nd.log2(nd.max(nd.abs(x))))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') y = ((x * f)).round() return y.astype('int8'), frac_len return x.astype('int8'), 0
def int_quantize(self, x): max = nd.max(nd.abs(x)) if max != 0: int_len = (nd.ceil(nd.log2(nd.max(nd.abs(x))))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') y = ((x * f)).floor() y = nd.clip(y, a_min=-128, a_max=127) return y, frac_len return x, 0
def forward(self, is_train, req, in_data, out_data, aux): x = in_data[0] y = out_data[0] in_max = nd.max(nd.abs(x)) if in_max != 0: int_len = (nd.ceil(nd.log2(nd.max(nd.abs(x))))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') y = ((x * f)).round() * (1 / f) y = x self.assign(out_data[0], req[0], mx.nd.array(y))
def int_quantize_double(self, x, w): max1 = nd.max(nd.abs(x)) max2 = nd.max(nd.abs(w)) if max1 > max2: max = max1 else: max = max2 if max != 0: int_len = (nd.ceil(nd.log2(max))).astype('float32') num_bit = self.num_bit.as_in_context(x.context) frac_len = num_bit - int_len f = (2**(frac_len)).astype('float32') int_x = ((x * f)).floor() int_w = ((w * f)).floor() return int_x, int_w, frac_len return x, w, 0
def quantize_vector(x, bits=8): """Quantize vertor with precision 'bits' Parameters ---------- x: NDArray shape is (1, n) bits: int vector after quantize preserve bits' precision Returns ------- y, sb: vector after quantization should be left_shift 'sb' bit to backward original value. """ max_v = nd.max(nd.abs(x)) if max_v == 0: return x.astype(np.int8), 8 int_len = nd.ceil(nd.log2(max_v)).asscalar() sb = bits - int_len f = 2**sb y = nd.floor(x * f) y = nd.clip(y, a_min=-2**(bits - 1), a_max=2**(bits - 1) - 1) return y, sb
def check_ceil(): y = nd.ceil(x) # expected ouput for middle 5 values after applying ceil() expected_output = [-1, 0, 0, 1, 1] assert_correctness_of_rounding_ops(y, LARGE_X // 2, expected_output)
def bin_label(z): # Real label to Bin label z = (z + 102) / 3.0 z = nd.ceil(z) return z