def test_opaque_func_arg_name_empty(self): of = OpaqueFunc() def py_func_of(of_): def new_py_func(s): assert s == "test2" assert isinstance(of, OpaqueFunc) of_.set_func(new_py_func, [TypeCode.Str]) of_("test2") of2 = OpaqueFunc(py_func_of, [TypeCode.OpaqueFunc]) of2(of) of("test2")
def test_xgraph_arg_name(self): def py_func(xg): assert xg.get_name() == "test" xg.set_name("test2") xgraph = XGraph("test") of = OpaqueFunc(py_func, [TypeCode.XGraph]) of(xgraph) assert xgraph.get_name() == "test2"
def test_str_cont_arg(self): def py_func(str_cont): assert str_cont == "test" str_cont.set_str("2") of = OpaqueFunc(py_func, [TypeCode.StrContainer]) s = StrContainer("test") of(s) assert s == "2"
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))
def test_xgraph_layer_add(self): def py_func(xg): assert xg.get_name() == "test" X = XLayer(name='x1', type=['X1']) xg.add(X) xgraph = XGraph("test") assert len(xgraph) == 0 of = OpaqueFunc(py_func, [TypeCode.XGraph]) of(xgraph) assert xgraph.get_name() == "test" assert len(xgraph) == 1 assert xgraph.get('x1').type == ['X1']
def test_basic_flow(self): def py_func(str_name): assert str_name == "test" of = OpaqueFunc(py_func, [TypeCode.Str]) assert OpaqueFuncRegistry.Size() == 0 ofr = OpaqueFuncRegistry.Register('py_func') ofr.set_func(of) assert OpaqueFuncRegistry.Size() == 1 assert OpaqueFuncRegistry.GetRegisteredFuncs() == ['py_func'] of2 = OpaqueFuncRegistry.Get("py_func") of2("test")
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)
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))
def test_strings_arg(self): def py_func(str_names): assert str_names == ["test1", "test2"] of = OpaqueFunc(py_func, [TypeCode.vStr]) of(["test1", "test2"])
def test_str_arg(self): def py_func(str_name): assert str_name == "test" of = OpaqueFunc(py_func, [TypeCode.Str]) of("test")