def test_dc_get_symbol_called_twice(): a = mx.np.arange(10) dc.set_variable(a, mx.sym.var('a')) with dc.context(): b = a + 1 sym1 = dc.get_symbol(b) sym2 = dc.get_symbol(b) assert sym1.list_inputs() == ['a'] assert sym2.list_inputs() == ['a']
def _assert_dc(setup, compute, mode='all', setup_is_deterministic=True, numpy=True): """Compare results of deferred compute and normal imperative mode. Parameters ---------- setup : callable Setup function computing inputs for compute function. Always called outside of deferred compute. compute : callable Compute function. We compare the output between normal computation and deferred compute. mode : {'all', 'symbolic', 'imperative', 'imperativewithnondccompute'} Compare deferred compute outputs triggered via imperative computation (eg. asnumpy() conversion) or obtained from the exported symbol or both. setup_is_deterministic : bool If True, setup function may be called multiple times. If False, will only be called once. numpy : bool If True, use mx.np. Otherwise mx.nd. """ try: nd = mx.np if numpy else mx.nd if numpy: mx.npx.set_np() xs = setup(nd=nd) ys = compute(*xs, nd=nd) ys_np = [y.asnumpy() for y in ys] if setup_is_deterministic: xs = setup(nd=nd) xs_names = list(map(str, range(len(xs)))) symbol_inputs = [ mx.symbol.var(name).as_np_ndarray() if numpy else mx.symbol.var(name) for arg, name in zip(xs, xs_names) ] dc.set_variable(xs, symbol_inputs) with dc.context(): ys_dc = compute(*xs, nd=nd) assert mode in ('all', 'symbolic', 'imperative', 'imperativewithnondccompute') if mode in ('all', 'imperativewithnondccompute'): ys_dc_np = [(y + 0).asnumpy() for y in ys_dc] _all_same(ys_np, ys_dc_np) if mode in ('all', 'imperative'): ys_dc_np = [y.asnumpy() for y in ys_dc] _all_same(ys_np, ys_dc_np) if mode in ('all', 'symbolic'): sym = dc.get_symbol(ys_dc, sym_cls=mx.sym.np._Symbol if numpy else mx.sym.Symbol) if setup_is_deterministic: xs = setup(nd=nd) args = {name: x for name, x in zip(xs_names, xs)} ys_sym = sym._bind(mx.context.current_context(), args=args).forward() ys_sym_np = [y.asnumpy() for y in ys_sym] _all_same(ys_np, ys_sym_np) finally: if numpy: mx.npx.reset_np()
def test_dc_set_variable_called_twice_error(): a = mx.np.arange(10) dc.set_variable(a, mx.sym.var('a')) with pytest.raises(MXNetError): # TODO(leezu): Should raise ValueError https://github.com/apache/incubator-mxnet/issues/17522 dc.set_variable(a, mx.sym.var('b'))
def test_dc_input_part_of_output(): a = mx.np.arange(10) dc.set_variable(a, mx.sym.var('a')) with dc.context(): b = a + 1 dc.get_symbol([a, b])
def test_dc_set_variable_called_twice(): a = mx.np.arange(10) dc.set_variable(a, mx.sym.var('a')) dc.set_variable(a, mx.sym.var('b'))