def test_as_type(): x = Tensor([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_qint_new_tensor(dtype, dtype_name): convert_to_dtype = eval("convert_to_%s" % dtype_name) convert_from_dtype = eval("convert_from_%s" % dtype_name) shape = (3, 3, 3) data = np.random.random(shape).astype(np.float32) * 5 - 1 # create a new Tensor with quint8 dtype inp = Tensor(convert_to_dtype(data, dtype), dtype=dtype) _check_result_attr(inp, dtype, dtype_name, dtype_name.startswith("qu")) np.testing.assert_equal(inp.numpy(), convert_to_dtype(data, dtype)) # convert from quint8 to float32 inp_float = inp.astype("float32") assert inp_float.dtype == np.float32 np.testing.assert_equal(inp_float.numpy(), convert_from_dtype(convert_to_dtype(data, dtype)))
def test_qint_astype(dtype, dtype_name): convert_to_dtype = eval("convert_to_%s" % dtype_name) convert_from_dtype = eval("convert_from_%s" % dtype_name) shape = (3, 3, 3) data = np.random.random(shape).astype(np.float32) * 5 - 1 inp = Tensor(data, dtype="float32") # convert to quint4 oup = inp.astype(dtype) _check_result_attr(oup, dtype, dtype_name, dtype_name.startswith("qu")) np.testing.assert_equal(oup.numpy(), convert_to_dtype(data, dtype)) # convert from quint4 to float32 oup_float = oup.astype("float32") assert oup_float.dtype == np.float32 np.testing.assert_equal(oup_float.numpy(), convert_from_dtype(convert_to_dtype(data, dtype)))