sxjscience commented on a change in pull request #16876: [Numpy] Implementation npx.{sample}_n URL: https://github.com/apache/incubator-mxnet/pull/16876#discussion_r349255827
########## File path: tests/python/unittest/test_numpy_op.py ########## @@ -2669,6 +2669,45 @@ def hybrid_forward(self, F, loc, scale): assert_almost_equal(loc.grad.asnumpy().sum(), _np.ones(out_shape).sum(), rtol=1e-3, atol=1e-5) +@with_seed() +@use_np +def test_npx_sample_n(): + def shape_formatter(s): + if s is None: + return () + if isinstance(s, tuple): + return s + # scalar case + return (s,) + + class TestSampleN(HybridBlock): + def __init__(self, shape, op_name): + super(TestSampleN, self).__init__() + self._shape = shape + self._op_name = op_name + + def hybrid_forward(self, F, param1, param2): + op = getattr(F.npx.random, self._op_name, None) + assert op is not None + # return param1 + param2 + op(batch_shape=self._shape) + return op(param1, param2, batch_shape=self._shape) + + batch_shapes = [(10,), (2, 3), 6, (), None] + event_shapes = [(), (2,), (2,2)] + dtypes = ['float16', 'float32', 'float64'] + op_names = ['uniform_n', 'normal_n'] + + for bshape, eshape, dtype, op in itertools.product(batch_shapes, event_shapes, dtypes, op_names): + for hybridize in [True, False]: + net = TestSampleN(bshape, op) + if hybridize: + net.hybridize() + expected_shape = (shape_formatter(bshape) + + shape_formatter(eshape)) + out = net(np.ones(shape=eshape), np.ones(shape=eshape)) + assert out.shape == expected_shape Review comment: We need some randomness tests by calling https://github.com/apache/incubator-mxnet/blob/4da14a22385622c35e9a5c9c3e8a17c07f718cad/python/mxnet/test_utils.py#L2199-L2245 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services