比较2个Relay expr的结构图/数据流图是否一致

analysis.alpha_equal
analysis.graph_equal
定义文件:python/tvm/relay/analysis.py

  1. # file : python/tvm/relay/analysis.py
  2. def alpha_equal(lhs, rhs):
  3. """
  4. 比较两个Relay expr的结构等效(alpha等效)。
  5. """
  6. return bool(_make._alpha_equal(lhs, rhs))
  7. def assert_alpha_equal(lhs, rhs):
  8. _make._assert_alpha_equal(lhs, rhs)
  9. def graph_equal(lhs, rhs):
  10. """
  11. 比较两个Relay expr的数据流等效。
  12. 这个和alpha_equal的区别在于它不要求变量lhs和rhs匹配,它们被视为源并相互映射。
  13. """
  14. return bool(_make._graph_equal(lhs, rhs))
  15. def assert_graph_equal(lhs, rhs):
  16. _make._assert_graph_equal(lhs, rhs)

比较2个tensor值是否一致

tvm.testing.assert_allclose
定义文件:python/tvm/testing.py

  1. def assert_allclose(actual, desired, rtol=1e-7, atol=1e-7):
  2. """ Version of np.testing.assert_allclose with `atol` and `rtol` fields set
  3. in reasonable defaults.
  4. Arguments `actual` and `desired` are not interchangable, since the function
  5. compares the `abs(actual-desired)` with `atol+rtol*abs(desired)`. Since we
  6. often allow `desired` to be close to zero, we generally want non-zero `atol`.
  7. """
  8. np.testing.assert_allclose(actual, desired, rtol=rtol, atol=atol, verbose=True)

它和np.testing.assert_allclose等效,其中带有“ atol”和“ rtol”字段的设置为合理的默认值。
numpy中还有其它的比较函数,如下:

  1. # numpy.testing.assert_allclose(actual, desired, rtol=1e-07, atol=0, equal_nan=True, err_msg='', verbose=True)
  2. # Raises an AssertionError if two objects are not equal up to desired tolerance.
  3. # The test is equivalent to allclose(actual, desired, rtol, atol) (note that allclose has different default values). It compares the difference between actual and desired to atol + rtol * abs(desired).
  4. x = [1e-5, 1e-3, 1e-1]
  5. y = np.arccos(np.cos(x))
  6. np.testing.assert_allclose(x, y, rtol=1e-5, atol=0)
  7. # numpy.testing.assert_array_almost_equal_nulp(x, y, nulp=1)
  8. # Compare two arrays relatively to their spacing.
  9. # This is a relatively robust method to compare two arrays whose amplitude is variable.
  10. x = np.array([1., 1e-10, 1e-20])
  11. eps = np.finfo(x.dtype).eps
  12. np.testing.assert_array_almost_equal_nulp(x, x*eps/2 + x)
  13. # numpy.testing.assert_array_max_ulp(a, b, maxulp=1, dtype=None)
  14. # Check that all items of arrays differ in at most N Units in the Last Place.
  15. a = np.linspace(0., 1., 100)
  16. res = np.testing.assert_array_max_ulp(a, np.arcsin(np.sin(a)))
  17. # numpy.testing.assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True)[source]
  18. # Raises an AssertionError if two objects are not equal up to desired precision.

assert_allclose的示例:

  1. def test_any_concat():
  2. x = relay.var('x', shape=(relay.Any(), 2), dtype="float32")
  3. y = relay.var('y', shape=(1, 2), dtype="float32")
  4. z = relay.op.concatenate([x, y], axis=0)
  5. mod = relay.module.Module()
  6. mod["main"] = relay.Function([x, y], z)
  7. x_np = np.random.uniform(size=(3, 2)).astype('float32')
  8. y_np = np.random.uniform(size=(1, 2)).astype('float32')
  9. ref = np.concatenate([x_np, y_np], axis=0)
  10. for kind in ["debug", "vm"]:
  11. ex = relay.create_executor(kind, mod=mod, ctx=tvm.cpu(), target="llvm")
  12. result = ex.evaluate()(x_np, y_np)
  13. tvm.testing.assert_allclose(result.asnumpy(), ref)
  1. import numpy as np
  2. import tvm
  3. from tvm import relay
  4. from tvm.contrib.nvcc import have_fp16
  5. def test_basic_build():
  6. tgt = "llvm"
  7. ctx = tvm.cpu()
  8. # func
  9. a = relay.var("a", dtype="float32", shape=(16, 8))
  10. b = relay.var("b", dtype="float32", shape=(8, 8))
  11. c = relay.var("c", dtype="float32", shape=(16, 8))
  12. x = relay.nn.dense(a, b)
  13. y = relay.nn.relu(x)
  14. z = y + c
  15. func = relay.Function([a, b, c], z)
  16. A = tvm.nd.array(np.random.uniform(-1, 1, (16, 8)).astype("float32"), ctx=ctx)
  17. B = tvm.nd.array(np.random.uniform(-1, 1, (8, 8)).astype("float32"), ctx=ctx)
  18. C = tvm.nd.array(np.random.uniform(-1, 1, (16, 8)).astype("float32"), ctx=ctx)
  19. params = {
  20. "b" : B,
  21. "c" : C
  22. }
  23. # build
  24. targets = {
  25. tvm.expr.IntImm("int32", ctx.device_type): tgt
  26. }
  27. g_json, mmod, params = relay.build(relay.Module.from_expr(func), targets, "llvm", params=params)
  28. # test
  29. rt = tvm.contrib.graph_runtime.create(g_json, mmod, ctx)
  30. rt.set_input("a", A)
  31. rt.load_params(relay.save_param_dict(params))
  32. rt.run()
  33. out = rt.get_output(0)
  34. np.testing.assert_allclose(out.asnumpy(), np.maximum(np.dot(A.asnumpy(),B.asnumpy().T),0) + C.asnumpy(),atol=1e-5, rtol=1e-5)