Ejemplo n.º 1
0
def test_const_dtype():
    strides = (1, 1)
    np_array = np.array(strides).astype("int32")
    strides = _op.const(np_array, dtype="int64")

    # strides needs to be autoconverted to int64 on Windows
    assert infer_type(strides).checked_type.dtype == np.dtype(np.int64)

    a = tvm.nd.array(np.random.randint(0, high=255, size=(2, 3),
                                       dtype="uint8"))
    a = _op.const(a, dtype="uint8")
    aa = a.data.numpy()
    assert aa.dtype == np.dtype(np.uint8)

    b = _op.const(1, dtype="int8")
    bb = b.data.numpy()
    assert bb.dtype == np.dtype(np.int8)

    kshape = (3, 10, 3, 3)
    w = relay.const(np.zeros(kshape, dtype="float32"))
    assert w.data.numpy().dtype == np.dtype(np.float32)
Ejemplo n.º 2
0
    def _calculate_qparam(inp):
        # reference ATen/native/quantized/cpu/qlinear_dynamic.cpp
        # ChooseQuantizationParams function
        mn = _op.min(inp)
        mx = _op.max(inp)

        # Ensure that the interval contains 0
        mn = _op.minimum(mn, _op.const(0.0, dtype="float32"))
        mx = _op.maximum(mx, _op.const(0.0, dtype="float32"))

        qmax = 255

        # reduce_range became True in v1.6
        if is_version_greater_than("1.5.1"):
            qmax = 127

        scale = (mx - mn) / _expr.const(qmax, dtype="float32")

        zero_point_from_min = -(mn / scale)
        zero_point = _op.cast(_op.round(_op.clip(zero_point_from_min, 0.0, qmax)), "int32")

        return scale, zero_point