(
N,
IC,
OC,
IH,
IW,
KH,
KW,
PH,
PW,
SH,
SW,
has_bias=True,
nonlinear_mode="identity",
)
| 791 | out_dtype = dtype.qint8(outp_scale) |
| 792 | |
| 793 | def run( |
| 794 | N, |
| 795 | IC, |
| 796 | OC, |
| 797 | IH, |
| 798 | IW, |
| 799 | KH, |
| 800 | KW, |
| 801 | PH, |
| 802 | PW, |
| 803 | SH, |
| 804 | SW, |
| 805 | has_bias=True, |
| 806 | nonlinear_mode="identity", |
| 807 | ): |
| 808 | inp_v = np.random.normal(size=(N, IC, IH, IW)) |
| 809 | w_v = np.random.normal(size=(OC, IC, KH, KW)) |
| 810 | b_v = np.random.normal(size=(1, OC, 1, 1)) |
| 811 | inp_scale = dtype.get_scale(inp_dtype) |
| 812 | w_scale = dtype.get_scale(w_dtype) |
| 813 | b_scale = dtype.get_scale(b_dtype) |
| 814 | |
| 815 | inpv = dtype.convert_to_qint8(inp_v * inp_scale, inp_dtype) |
| 816 | wv = dtype.convert_to_qint8(w_v * w_scale, w_dtype) |
| 817 | bv = dtype.convert_to_qint32(b_v * b_scale, b_dtype) |
| 818 | |
| 819 | inp_int8 = tensor(inpv, dtype=inp_dtype) |
| 820 | w_int8 = Parameter(wv, dtype=w_dtype) |
| 821 | b_int32 = Parameter(bv, dtype=b_dtype) |
| 822 | |
| 823 | inp_fp32 = inp_int8.astype("float32") |
| 824 | w_fp32 = w_int8.astype("float32") |
| 825 | b_fp32 = b_int32.astype("float32") |
| 826 | |
| 827 | def convert_to_nchw4(var): |
| 828 | var = F.reshape( |
| 829 | var, (var.shape[0], var.shape[1] // 4, 4, var.shape[2], var.shape[3]) |
| 830 | ) |
| 831 | var = F.transpose(var, (0, 1, 3, 4, 2)) |
| 832 | return var |
| 833 | |
| 834 | def run_conv2d(inp, w, b): |
| 835 | O = F.conv2d( |
| 836 | inp, w, b if has_bias else None, stride=(SH, SW), padding=(PH, PW), |
| 837 | ) |
| 838 | if nonlinear_mode == "relu": |
| 839 | return F.relu(O) |
| 840 | else: |
| 841 | return O |
| 842 | |
| 843 | def run_conv_bias(inp, w, b, format="NCHW"): |
| 844 | b = b if has_bias else Parameter(np.zeros_like(b.numpy())) |
| 845 | if format == "NCHW4": |
| 846 | inp = convert_to_nchw4(inp) |
| 847 | w = convert_to_nchw4(w) |
| 848 | b = convert_to_nchw4(b) |
| 849 | return F.quantized.conv_bias_activation( |
| 850 | inp, |
no test coverage detected