def test_as_type(): x = TensorWrapper([1, 2, 3], dtype=np.float32) y = x.astype(qint8(0.1)) np.testing.assert_almost_equal(get_scale(y.dtype), 0.1) z = y.astype(qint8(0.2)) np.testing.assert_almost_equal(get_scale(z.dtype), 0.2) a = z.astype(quint8(0.3, 127)) np.testing.assert_almost_equal(get_scale(a.dtype), 0.3) np.testing.assert_equal(get_zero_point(a.dtype), 127) b = a.astype(quint8(0.3, 128)) np.testing.assert_almost_equal(get_scale(b.dtype), 0.3) np.testing.assert_equal(get_zero_point(b.dtype), 128)
def test_dtype_quint8(): with pytest.raises(ValueError): blah = quint8(0.05, 0.233) with pytest.raises(ValueError): blah = quint8(0.02, 777) with pytest.raises(ValueError): blah = quint8(0.02, -1) dt = quint8(0.01, 135) assert isinstance(dt, np.dtype) assert "mgb_dtype" in dt.metadata np.testing.assert_allclose(dt.metadata["mgb_dtype"]["scale"], 0.01) np.testing.assert_equal(dt.metadata["mgb_dtype"]["zero_point"], 135) assert is_quantize(dt) np.testing.assert_allclose(get_scale(dt), 0.01) np.testing.assert_equal(get_zero_point(dt), 135)
def test_quint8_typecvt(): device = "xpux" shape = (3, 3, 3) data = np.random.random(shape).astype(np.float32) * 5 - 1 def typecvt(x, dt=None): (y, ) = G.apply_normal_varnode(ops.TypeCvt(dtype=dt), x) return y # convert to quint8 dtype = quint8(0.01, 135) oup = _get_compiled_result(data, np.float32, shape, device, calc_func=partial(typecvt, dt=dtype)) _check_result_attr(oup, dtype, "quint8") np.testing.assert_equal(oup, convert_to_quint8(data, dtype)) # convert from quint8 to float32 oup_float = _get_compiled_result(oup, dtype, shape, device, calc_func=partial(typecvt, dt=np.float32)) assert oup_float.dtype == np.float32 np.testing.assert_equal( oup_float, convert_from_quint8(convert_to_quint8(data, dtype)))
def test_as_type(is_varnode): if is_varnode: network = Network() else: network = None x_np = np.array([1, 2, 3], dtype=np.float32) x = make_tensor(x_np, network) y = x.astype(qint8(0.1)) np.testing.assert_almost_equal(get_scale(y.dtype), 0.1) z = y.astype(qint8(0.2)) np.testing.assert_almost_equal(get_scale(z.dtype), 0.2) a = z.astype(quint8(0.3, 127)) np.testing.assert_almost_equal(get_scale(a.dtype), 0.3) np.testing.assert_equal(get_zero_point(a.dtype), 127) b = a.astype(quint8(0.3, 128)) np.testing.assert_almost_equal(get_scale(b.dtype), 0.3) np.testing.assert_equal(get_zero_point(b.dtype), 128)
def test_dtype_int8_ffi_handle(): device = "xpux" shape = (3, 3, 3) data = np.random.random(shape).astype(np.float32) * 5 - 1 def identity(x): return x dtype = quint8(0.01, 127) inp = convert_to_quint8(data, dtype) oup = _get_compiled_result(inp, dtype, shape, device, calc_func=identity) _check_result_attr(oup, dtype, "quint8") np.testing.assert_allclose(convert_from_quint8(oup), convert_from_quint8(inp)) dtype = qint8(0.01) inp = convert_to_qint8(data, dtype) oup = _get_compiled_result(inp, dtype, shape, device, calc_func=identity) _check_result_attr(oup, dtype, "qint8", is_unsigned=False) np.testing.assert_allclose(convert_from_qint8(oup), convert_from_qint8(inp))
def test_snpe_model_8f(): model = "8w16f_backbone.tm" net = mge.load(model) print(net.flatten().graph) inp_dtype = dtype.quint8(16.0 / 128.0, 128) inps = get_qat_inputs_quint8(inp_dtype, num_inp=2, shape=(1, 16, 384, 512)) tm_result = dict(zip(net.graph.outputs, net(*inps))) _test_convert_result( inps, net, tm_result, max_err, input_data_type="quint8", input_scales=inps[0].qparams.scale, input_zero_points=inps[0].qparams.zero_point, require_quantize=False, param_fake_quant=True, split_conv_relu=True, input_name=["inp", "prev"], )
def test_dtype_qint4(): dt = qint4(0.01) assert isinstance(dt, np.dtype) assert "mgb_dtype" in dt.metadata np.testing.assert_allclose(dt.metadata["mgb_dtype"]["scale"], 0.01) assert is_quantize(dt) np.testing.assert_allclose(get_scale(dt), 0.01) @pytest.mark.parametrize( "dtype, dtype_name", [ (quint4(0.01, 5), "quint4"), (qint4(0.01), "qint4"), (quint8(0.01, 135), "quint8"), (qint8(0.01), "qint8"), ], ) def test_dtype_qint_mgb_ffi_handle(dtype, dtype_name): def identity(x): return x convert_to_dtype = eval("convert_to_%s" % dtype_name) convert_from_dtype = eval("convert_from_%s" % dtype_name) device = "xpux" shape = (3, 3, 3) data = np.random.random(shape).astype(np.float32) * 5 - 1 inp = convert_to_dtype(data, dtype) oup = _get_compiled_result(inp, dtype, shape, device, calc_func=identity)