def piecewise_mult(params, in_tensors, qrec: MultQuantizationRecord, details=None): del details in_tensors = qrec.prepare_inputs(params, in_tensors, ktype="symmetric") func = PIECEWISE_OPS[params.__class__] op = func['op'] if func['is_mult']: qrec.set_scale(in_idx=(0, 1), out_idx=0) i1 = in_tensors[0].astype(np.int32) i2 = in_tensors[1].astype(np.int32) res = qrec.scale_mul_biases_q.apply_scales(op(i1, i2, np.int32)) else: # larger scale should be scaled qrec.set_add_scale() if qrec.scaled_idx: i1 = in_tensors[0].astype(np.int32) i2 = qrec.scale_in_mul_biases_q.apply_scales(in_tensors[1]) else: i1 = qrec.scale_in_mul_biases_q.apply_scales(in_tensors[0]) i2 = in_tensors[1].astype(np.int32) res = qrec.scale_mul_biases_q.apply_scales(op(i1, i2, None)) return qrec.get_outputs(params, [qrec.out_qs[0].clip(res)], ktype="symmetric")
def hsigmoid_mult(params, in_tensors, qrec: MultQuantizationRecord, details=None): del details in_tensor = qrec.prepare_inputs(params, in_tensors, ktype="symmetric")[0] fac_1, upper_bound, lower_bound = hsigmoid_mult_gen_factors(params, qrec) in_tensor = in_tensor.astype(np.int32) in_tensor_relued = np.minimum(np.maximum(in_tensor + fac_1, lower_bound), upper_bound) in_tensor = qrec.scale_mul_biases_q.apply_scales(in_tensor_relued) return qrec.get_outputs(params, [in_tensor], ktype="symmetric")
def average_execute_mult(cls, params, in_tensors, qrec: MultQuantizationRecord): # Prepare the quantization levels in_tensor = qrec.prepare_inputs(params, in_tensors, ktype="symmetric")[0] out_dims = params.out_dims[0] qrec.set_scale(in_idx=0, out_idx=0) sum_by_chan = np.sum(in_tensor, dtype=np.int32, axis=tuple( params.axis), keepdims=params.keep_dims) sz = reduce(lambda x, y: x * y, [i for idx, i in enumerate(in_tensor.shape) if idx in params.axis]) res = at_norm(((sum_by_chan << 7) / sz).astype(np.int32), 7) res = out_tensor = qrec.scale_mul_biases_q.apply_scales(res) return qrec.get_outputs(params, [out_tensor.reshape(out_dims.shape)], ktype="symmetric")
def average_execute(cls, params, in_tensors, qrec: MultQuantizationRecord): # Prepare the quantization levels in_tensor = qrec.prepare_inputs(params, in_tensors, ktype="symmetric")[0] out_dims = params.out_dims[0] sum_by_chan = np.sum(in_tensor, dtype=np.int32, axis=tuple( params.axis), keepdims=params.keep_dims) norm = (np.array([31], dtype=np.int32) - gap_clb(sum_by_chan.flatten())).astype(np.int32) sz = reduce(lambda x, y: x * y, [i for idx, i in enumerate(in_tensor.shape) if idx in params.axis]) inv_wh = ((1 << norm) // sz).reshape(sum_by_chan.shape) out_tensor = at_norm((inv_wh * sum_by_chan), norm.reshape(sum_by_chan.shape)) return qrec.get_outputs(params, [qrec.out_qs[0].clip(out_tensor).reshape(out_dims.shape)], ktype="symmetric")
def av_global_pool_mult(params, in_tensors, qrec: MultQuantizationRecord, details=None): # Prepare the quantization levels in_tensor = qrec.prepare_inputs(params, in_tensors, ktype="symmetric")[0] in_dims = params.in_dims[0] out_dims = params.out_dims[0] qrec.set_scale(in_idx=0, out_idx=0) sum_by_chan = np.sum(in_tensor, dtype=np.int32, axis=(in_dims.get_order_idx('w'), in_dims.get_order_idx('h'))) res = at_norm((sum_by_chan << 7) // (in_dims.h * in_dims.w), 7) res = out_tensor = qrec.scale_mul_biases_q.apply_scales(res) return qrec.get_outputs(params, [out_tensor.reshape(out_dims.shape)], ktype="symmetric")