def test_fully_connected_inference(): n = 1024 l = 128 m = 235 bias = tvm.var('bias', dtype=tvm.float32) A = tvm.placeholder((l, ), name='A') B = tvm.placeholder((m, l), name='B') C = nnpack.fully_connected_inference(A, B) D = tvm.compute(C.shape, lambda i: C[i] + bias, name="D") s = tvm.create_schedule(D.op) def verify(target="llvm"): if not tvm.module.enabled(target): print("skip because %s is not enabled..." % target) return if not tvm.get_global_func( "tvm.contrib.nnpack.fully_connected_inference", True): print("skip because extern function is not available") return if not nnpack.is_available(): return ctx = tvm.cpu(0) f = tvm.build(s, [A, B, D, bias], target) a = tvm.nd.array(np.random.uniform(size=(l)).astype(A.dtype), ctx) b = tvm.nd.array(np.random.uniform(size=(m, l)).astype(B.dtype), ctx) d = tvm.nd.array(np.zeros((m, ), dtype=D.dtype), ctx) bb = 10.0 f(a, b, d, bb) tvm.testing.assert_allclose(d.asnumpy(), np.dot(a.asnumpy(), b.asnumpy().T) + bb, rtol=1e-5) verify()
def test_fully_connected_inference(): n = 1024 l = 128 m = 235 bias = te.var("bias", dtype="float32") A = te.placeholder((l,), name="A") B = te.placeholder((m, l), name="B") C = nnpack.fully_connected_inference(A, B) D = te.compute(C.shape, lambda i: C[i] + bias, name="D") s = te.create_schedule(D.op) def verify(target="llvm"): if not tvm.get_global_func("tvm.contrib.nnpack.fully_connected_inference", True): pytest.skip("extern function is not available") if not nnpack.is_available(): pytest.skip("nnpack is not available") ctx = tvm.cpu(0) f = tvm.build(s, [A, B, D, bias], target) a = tvm.nd.array(np.random.uniform(size=(l)).astype(A.dtype), ctx) b = tvm.nd.array(np.random.uniform(size=(m, l)).astype(B.dtype), ctx) d = tvm.nd.array(np.zeros((m,), dtype=D.dtype), ctx) bb = 10.0 f(a, b, d, bb) tvm.testing.assert_allclose(d.asnumpy(), np.dot(a.asnumpy(), b.asnumpy().T) + bb, rtol=1e-5) verify()
def test_fully_connected_inference(): n = 1024 l = 128 m = 235 bias = tvm.var('bias', dtype=tvm.float32) A = tvm.placeholder((l, ), name='A') B = tvm.placeholder((m, l), name='B') C = nnpack.fully_connected_inference(A, B) D = tvm.compute(C.shape, lambda i: C[i] + bias, name="D") s = tvm.create_schedule(D.op) def verify(target="llvm"): if not tvm.module.enabled(target): print("skip because %s is not enabled..." % target) return if not tvm.get_global_func("tvm.contrib.nnpack.fully_connected_inference", True): print("skip because extern function is not available") return if not nnpack.is_available(): return ctx = tvm.cpu(0) f = tvm.build(s, [A, B, D, bias], target) a = tvm.nd.array(np.random.uniform(size=(l)).astype(A.dtype), ctx) b = tvm.nd.array(np.random.uniform(size=(m, l)).astype(B.dtype), ctx) d = tvm.nd.array(np.zeros((m, ), dtype=D.dtype), ctx) bb = 10.0 f(a, b, d, bb) tvm.testing.assert_allclose( d.asnumpy(), np.dot(a.asnumpy(), b.asnumpy().T) + bb, rtol=1e-5) verify()