def verify_any_topk(data_shape, kval, np_dshape, dtype, const_k=False): mod = tvm.IRModule() data = relay.var('data', shape=data_shape, dtype=dtype) np_data = np.random.uniform(size=np_dshape).astype(dtype) if const_k: k = relay.const(kval) args = [data] in_vals = [np_data] else: k = relay.var('k', shape=(), dtype="int32") args = [data, k] in_vals = [np_data, kval] out = relay.topk(data, k, ret_type="indices") mod["main"] = relay.Function(args, out) sorted = np.argsort(-np_data) if len(np_dshape) == 2: ref_out = sorted[:, 0:kval] else: ref_out = sorted[0:kval] for kind in ["debug", "vm"]: ex = relay.create_executor(kind, mod=mod, ctx=tvm.cpu(), target="llvm") result = ex.evaluate()(*in_vals) tvm.testing.assert_allclose(result.asnumpy(), ref_out)
def test_fake_quantize_topk(k, axis, is_ascend, dtype): x = relay.var("x", shape=[20, 100], dtype=dtype) zero = relay.const(0) x = relay.qnn.op.dequantize(x, relay.const(2.0), zero) op = relay.topk(x, k, axis, "values", is_ascend, "float32") op = relay.qnn.op.quantize(op, relay.const(2.0), zero, out_dtype=dtype) x_np = np.random.randint(0, 127, size=[20, 100], dtype=dtype) compare_fq_to_int(op, [x_np])
def verify_topk(k, axis, ret_type, is_ascend, dtype): shape = (20, 100) x = relay.var("x", relay.TensorType(shape, "float32")) k_var = relay.const(k) out = relay.topk(x, k_var, axis, ret_type, is_ascend, dtype) if isinstance(out, relay.expr.TupleWrapper): out = out.astuple() func = relay.Function([x], out) np_data = np.random.uniform(size=shape).astype("float32") if is_ascend: np_indices = np.argsort(np_data, axis=axis) else: np_indices = np.argsort(-np_data, axis=axis) kk = k if k >= 1 else shape[axis] if axis == 0: np_indices = np_indices[:kk, :] np_values = np.zeros(np_indices.shape).astype("float32") for i in range(shape[1]): np_values[:, i] = np_data[np_indices[:, i], i] else: np_indices = np_indices[:, :kk] np_values = np.zeros(np_indices.shape).astype("float32") for i in range(shape[0]): np_values[i, :] = np_data[i, np_indices[i, :]] np_indices = np_indices.astype(dtype) func2 = run_opt_pass(run_opt_pass(func, transform.DynamicToStatic()), transform.InferType()) zz = func2.body assert isinstance(zz, relay.Call) assert zz.op == relay.op.get("topk") for target, ctx in tvm.testing.enabled_targets(): if "llvm" not in target: continue for kind in ["graph", "vm", "debug"]: mod = tvm.ir.IRModule.from_expr(func2) intrp = relay.create_executor(kind, mod=mod, ctx=ctx, target=target) op_res = intrp.evaluate()(np_data) if ret_type == "both": tvm.testing.assert_allclose(op_res[0].asnumpy(), np_values) tvm.testing.assert_allclose(op_res[1].asnumpy(), np_indices) elif ret_type == "values": tvm.testing.assert_allclose(op_res.asnumpy(), np_values) else: tvm.testing.assert_allclose(op_res.asnumpy(), np_indices)
def verify_topk(k, axis, ret_type, is_ascend, dtype): shape = (20, 100) x = relay.var("x", relay.TensorType(shape, "float32")) k_var = relay.var("x", relay.TensorType((1, ), "float32")) out = relay.topk(x, k_var, axis, ret_type, is_ascend, dtype) if isinstance(out, relay.expr.TupleWrapper): out = out.astuple() func = relay.Function([x, k_var], out) np_data = np.random.uniform(size=shape).astype("float32") if is_ascend: np_indices = np.argsort(np_data, axis=axis) else: np_indices = np.argsort(-np_data, axis=axis) kk = k if k >= 1 else shape[axis] if axis == 0: np_indices = np_indices[:kk, :] np_values = np.zeros(np_indices.shape).astype("float32") for i in range(shape[1]): np_values[:, i] = np_data[np_indices[:, i], i] else: np_indices = np_indices[:, :kk] np_values = np.zeros(np_indices.shape).astype("float32") for i in range(shape[0]): np_values[i, :] = np_data[i, np_indices[i, :]] np_indices = np_indices.astype(dtype) for target, ctx in ctx_list(): if "llvm" not in target: continue for kind in ["vm", "debug"]: mod = tvm.ir.IRModule.from_expr(func) intrp = relay.create_executor(kind, mod=mod, ctx=ctx, target=target) op_res = intrp.evaluate()(np_data, np.array([k]).astype("float32")) if ret_type == "both": tvm.testing.assert_allclose(op_res[0].asnumpy(), np_values) tvm.testing.assert_allclose(op_res[1].asnumpy(), np_indices) elif ret_type == "values": tvm.testing.assert_allclose(op_res.asnumpy(), np_values) else: tvm.testing.assert_allclose(op_res.asnumpy(), np_indices)
def verify_topk(k, axis, ret_type, is_ascend, dtype): shape = (20, 100) x = relay.var("x", relay.TensorType(shape, "float32")) out = relay.topk(x, k, axis, ret_type, is_ascend, dtype) if isinstance(out, relay.expr.TupleWrapper): out = out.astuple() func = relay.Function([x], out) np_data = np.random.uniform(size=shape).astype("float32") if is_ascend: np_indices = np.argsort(np_data, axis=axis) else: np_indices = np.argsort(-np_data, axis=axis) kk = k if k >= 1 else shape[axis] if axis == 0: np_indices = np_indices[:kk, :] np_values = np.zeros(np_indices.shape).astype("float32") for i in range(shape[1]): np_values[:, i] = np_data[np_indices[:, i], i] else: np_indices = np_indices[:, :kk] np_values = np.zeros(np_indices.shape).astype("float32") for i in range(shape[0]): np_values[i, :] = np_data[i, np_indices[i, :]] np_indices = np_indices.astype(dtype) for target, ctx in tvm.testing.enabled_targets(): for kind in ["graph", "debug"]: intrp = relay.create_executor(kind, ctx=ctx, target=target) op_res = intrp.evaluate(func)(np_data) if ret_type == "both": tvm.testing.assert_allclose(op_res[0].asnumpy(), np_values) tvm.testing.assert_allclose(op_res[1].asnumpy(), np_indices) elif ret_type == "values": tvm.testing.assert_allclose(op_res.asnumpy(), np_values) else: tvm.testing.assert_allclose(op_res.asnumpy(), np_indices)