Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
@with_seed()
@use_np
def test_np_xxx():
    class TestXXX(HybridBlock):
        def __init__(self, ...):
            super(TestXXX, self).__init__()
            # necessary initializations
            
        def hybrid_forward(self, F, a, ...):
            return F.np.xxx(a, ...)
    
    shapes = [] # test_shapes, remember to include zero-dim shape and zero-size shapes
    dtypes = [] # remember to include all meaningful data types for the operator
    for hybridize, shape, dtype, param_options in itertools.product([False, True], shape, dtype, more_param_options)
	    # More for-loops for iterating through all other arguments
		# rtol atol values are for reference, may vary for different ops
        rtol = 1e-2 if dtype == np.float16 else 1e-3
        atol = 1e-4 if dtype == np.float16 else 1e-5
	    test_xxx = TestXXX(...)
    	if hybridize:
            test_xxx.hybridize()
	    # here the low and high could vary for different ops
    	x = np.random.uniform(-1.0, 1.0, size=shape).astype(dtype)
        x.attach_grad()
	    np_out = _np.xxx(x.asnumpy(), ...)
    	with mx.autograd.record():
            mx_out = test_xxx(x)
	    assert mx_out.shape == np_out.shape
    	assert_almost_equal(mx_out.asnumpy(), np_out, rtol=rtol, atol=atol)
        mx_out.backward()
	    # Code to get reference backward value
    	# np_backward = ...
		assert_almost_equal(x.grad.asnumpy(), np_backward, rtol=rtol, atol=atol)

	    # Test imperative once again
    	mx_out = np.xxx(x, ...)
        np_out = _np.xxx(x.asnumpy(), ...)
        assert_almost_equal(mx_out.asnumpy(), np_out, rtol=rtol, atol=atol)


Additional Reference

How to implement operators using TVM: https://github.com/hgt312/misc/blob/master/TVMOp%20Tutorial.ipynb