MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / run

Function run

imperative/python/test/unit/quantization/test_op.py:75–152  ·  view source on GitHub ↗
(
        N,
        IC,
        OC,
        IH,
        IW,
        KH,
        KW,
        PH,
        PW,
        SH,
        SW,
        has_bias=True,
        nonlinear_mode="identity",
    )

Source from the content-addressed store, hash-verified

73 out_dtype = dtype.qint8(outp_scale)
74
75 def run(
76 N,
77 IC,
78 OC,
79 IH,
80 IW,
81 KH,
82 KW,
83 PH,
84 PW,
85 SH,
86 SW,
87 has_bias=True,
88 nonlinear_mode="identity",
89 ):
90 inp_v = np.random.normal(size=(N, IC, IH, IW))
91 w_v = np.random.normal(size=(OC, IC, KH, KW))
92 b_v = np.random.normal(size=(1, OC, 1, 1))
93 inp_scale = dtype.get_scale(inp_dtype)
94 w_scale = dtype.get_scale(w_dtype)
95 b_scale = dtype.get_scale(b_dtype)
96
97 inpv = dtype.convert_to_qint8(inp_v * inp_scale, inp_dtype)
98 wv = dtype.convert_to_qint8(w_v * w_scale, w_dtype)
99 bv = dtype.convert_to_qint32(b_v * b_scale, b_dtype)
100
101 inp_int8 = mge.tensor(inpv, dtype=inp_dtype)
102 w_int8 = mge.Parameter(wv, dtype=w_dtype)
103 b_int32 = mge.Parameter(bv, dtype=b_dtype)
104
105 inp_fp32 = inp_int8.astype("float32")
106 w_fp32 = w_int8.astype("float32")
107 b_fp32 = b_int32.astype("float32")
108
109 def convert_to_nchw4(var):
110 var = F.reshape(
111 var, (var.shape[0], var.shape[1] // 4, 4, var.shape[2], var.shape[3])
112 )
113 var = F.transpose(var, (0, 1, 3, 4, 2))
114 return var
115
116 def run_conv2d(inp, w, b):
117 O = F.conv2d(
118 inp, w, b if has_bias else None, stride=(SH, SW), padding=(PH, PW),
119 )
120 if nonlinear_mode == "relu":
121 return F.relu(O)
122 else:
123 return O
124
125 def run_conv_bias(inp, w, b, format="NCHW"):
126 b = b if has_bias else mge.Parameter(np.zeros_like(b.numpy()))
127 if format == "NCHW4":
128 inp = convert_to_nchw4(inp)
129 w = convert_to_nchw4(w)
130 b = convert_to_nchw4(b)
131 return F.quantized.conv_bias_activation(
132 inp,

Callers 2

test_conv_biasFunction · 0.70
test_conv_bias_int4Function · 0.70

Calls 10

normalMethod · 0.80
tensorMethod · 0.80
run_conv2dFunction · 0.70
run_conv_biasFunction · 0.70
get_scaleMethod · 0.45
astypeMethod · 0.45
transposeMethod · 0.45
flattenMethod · 0.45
numpyMethod · 0.45
TensorMethod · 0.45

Tested by

no test coverage detected