File: resize_nearest_dnnlowp_op_test.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (58 lines) | stat: -rw-r--r-- 1,994 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58


import caffe2.python.hypothesis_test_util as hu
import hypothesis.strategies as st
import numpy as np
from caffe2.python import core, dyndep, workspace
from hypothesis import given, settings


dyndep.InitOpsLibrary("//caffe2/caffe2/quantization/server:dnnlowp_ops")
workspace.GlobalInit(["caffe2", "--caffe2_omp_num_threads=11"])


class DNNLowPResizeNearestOpTest(hu.HypothesisTestCase):
    @given(
        N=st.integers(0, 3),
        H=st.integers(10, 300),
        W=st.integers(10, 300),
        C=st.integers(1, 32),
        scale_w=st.floats(0.25, 4.0) | st.just(2.0),
        scale_h=st.floats(0.25, 4.0) | st.just(2.0),
        **hu.gcs_cpu_only
    )
    @settings(deadline=None, max_examples=50)
    def test_resize_nearest(self, N, H, W, C, scale_w, scale_h, gc, dc):
        X = np.round(np.random.rand(N, H, W, C) * 255).astype(np.float32)

        quantize = core.CreateOperator("Quantize", ["X"], ["X_q"], engine="DNNLOWP")
        resize_nearest = core.CreateOperator(
            "Int8ResizeNearest",
            ["X_q"],
            ["Y_q"],
            width_scale=scale_w,
            height_scale=scale_h,
            engine="DNNLOWP",
        )

        net = core.Net("test_net")
        net.Proto().op.extend([quantize, resize_nearest])

        workspace.FeedBlob("X", X)
        workspace.RunNetOnce(net)
        X_q = workspace.FetchInt8Blob("X_q").data
        Y_q = workspace.FetchInt8Blob("Y_q").data

        def resize_nearest_ref(X):
            outH = np.int32(H * scale_h)
            outW = np.int32(W * scale_w)
            outH_idxs, outW_idxs = np.meshgrid(
                np.arange(outH), np.arange(outW), indexing="ij"
            )
            inH_idxs = np.minimum(outH_idxs / scale_h, H - 1).astype(np.int32)
            inW_idxs = np.minimum(outW_idxs / scale_w, W - 1).astype(np.int32)
            Y = X[:, inH_idxs, inW_idxs, :]
            return Y

        Y_q_ref = resize_nearest_ref(X_q)
        np.testing.assert_allclose(Y_q, Y_q_ref)