Beispiel #1
0
    def test_xbuffer_getattr(self):
        a = np.array([1., 1.5], dtype=np.float32)
        xb = XBuffer(a)

        assert xb.dtype == 'float32'
        assert xb.shape == (2, )

        a = np.zeros((1, 3, 224, 224), dtype=np.int8)
        xb = XBuffer(a)

        assert xb.dtype == 'int8'
        assert xb.shape == (1, 3, 224, 224)
Beispiel #2
0
    def test_build_rt_opaque_func_cpu_tf(self):
        tf.compat.v1.reset_default_graph()

        W = np.reshape(
            np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=np.float32),
            (2, 1, 2, 2))
        # B = np.array([0, 0], dtype=np.float32)

        iX = px.ops.input('in', [1, 1, 4, 4])
        wX = px.ops.constant('w', W)
        # bX = px.ops.constant('b', B)
        cX = px.ops.conv2d('conv',
                           iX,
                           wX,
                           kernel_size=[2, 2],
                           strides=[1, 1],
                           padding_hw=[0, 0, 0, 0],
                           dilation=[1, 1],
                           groups=1,
                           channels=2,
                           data_layout='NCHW',
                           kernel_layout='OIHW')

        xlayers = [iX, wX, cX]

        xg = TestBase.xg_factory.build_from_xlayer(xlayers)

        of = OpaqueFuncRegistry.Get('pyxir.build_rt')
        rt_of = OpaqueFunc()

        # call opaque func
        of(xg, 'cpu', 'cpu-tf', ['in'], ['conv'], rt_of)

        # By now, the `rt_of` is initialized with the opaque runtime function
        ins = [XBuffer(np.ones((1, 1, 4, 4), dtype=np.float32))]
        outs = [XBuffer(np.empty((1, 2, 3, 3), dtype=np.float32))]

        rt_of(ins, outs)

        assert len(outs) == 1

        expected_outpt = np.array([[[[10., 10., 10.], [10., 10., 10.],
                                     [10., 10., 10.]],
                                    [[26., 26., 26.], [26., 26., 26.],
                                     [26., 26., 26.]]]])

        np.testing.assert_array_equal(outs[0], expected_outpt)
Beispiel #3
0
    def test_xbuffer_operators(self):
        a = np.array([1., 1.5], dtype=np.float32)
        xb = XBuffer(a)

        xb1 = xb + [1, -1]
        assert isinstance(xb1, XBuffer)
        np.testing.assert_equal(xb.to_numpy(), a)
        np.testing.assert_equal(xb1.to_numpy(),
                                np.array([2., .5], dtype=np.float32))
Beispiel #4
0
    def test_xbuffer_arg_name(self):
        def py_func(xb):
            np.testing.assert_equal(xb.to_numpy(),
                                    np.array([1., 2.], dtype=np.float32))
            xb.copy_from(xb * 2)

        xb = XBuffer(np.array([1., 2.], dtype=np.float32))
        of = OpaqueFunc(py_func, [TypeCode.XBuffer])
        of(xb)
        np.testing.assert_equal(xb.to_numpy(),
                                np.array([2., 4.], dtype=np.float32))
Beispiel #5
0
    def test_xbuffer_copy_from(self):
        a = np.array([1., 1.5], dtype=np.float32)
        xb = XBuffer(a)
        b = xb.to_numpy()

        xb.copy_from(xb * np.array([2, -1]))
        assert isinstance(xb, XBuffer)
        np.testing.assert_equal(xb.to_numpy(),
                                np.array([2., -1.5], dtype=np.float32))
        np.testing.assert_equal(a, np.array([1., 1.5], dtype=np.float32))
        np.testing.assert_equal(b, np.array([2., -1.5], dtype=np.float32))
Beispiel #6
0
    def test_vector_xbuffer_arg_name(self):
        def py_func(xbuffers):
            assert len(xbuffers) == 2

            np.testing.assert_equal(xbuffers[0].to_numpy(),
                                    np.array([1., 2.], dtype=np.float32))
            xbuffers[0].copy_from(xbuffers[0] * 2)

            np.testing.assert_equal(xbuffers[1].to_numpy(),
                                    np.array([-1., 0.], dtype=np.float32))
            xbuffers[1].copy_from(xbuffers[1] * 2)

        xb1 = XBuffer(np.array([1., 2.], dtype=np.float32))
        xb2 = XBuffer(np.array([-1., 0.], dtype=np.float32))
        of = OpaqueFunc(py_func, [TypeCode.vXBuffer])
        of([xb1, xb2])

        np.testing.assert_equal(xb1.to_numpy(),
                                np.array([2., 4.], dtype=np.float32))
        np.testing.assert_equal(xb2.to_numpy(),
                                np.array([-2., 0.], dtype=np.float32))
Beispiel #7
0
    def test_xbuffer_construction(self):
        a = np.array([1., 1.5], dtype=np.float32)
        xb = XBuffer(a)

        np.testing.assert_equal(xb.to_numpy(), a)