def test_variable(): fname = os.path.join( this_file_dir, 'runtime_analysis/{}/gradients/variable.txt'.format(DIM)) if os.path.exists(fname): os.remove(fname) for lib, call in [ (l, c) for l, c in helpers.calls if c not in [helpers.tf_graph_call, helpers.mx_graph_call] ]: time_lib = LIB_DICT[lib] append_to_file(fname, '{}'.format(lib)) x0 = ivy_gen.array([random.uniform(0, 1) for _ in range(DIM)], f=lib) ivy_grad.variable(x0, f=lib) ivy_grad_w_time.variable(x0, f=time_lib) TIMES_DICT.clear() for _ in range(100): log_time(fname, 'tb0') ivy_grad_w_time.variable(x0, f=time_lib) log_time(fname, 'tb4', time_at_start=True) log_time(fname, 'tt0') ivy_grad.variable(x0, f=lib) log_time(fname, 'tt1', time_at_start=True) write_times() append_to_file(fname, 'end of analysis')
def test_execute_with_gradients(): for lib, call in helpers.calls: if call is helpers.mx_graph_call: # mxnet symbolic does not support ivy gradient functions continue # func with single return val func = lambda xs_in: (xs_in[0] * xs_in[0])[0] xs = [ivy_grad.variable(ivy_gen.array([3.], f=lib))] y, dydxs = call(ivy_grad.execute_with_gradients, func, xs, f=lib) assert np.allclose(y, np.array(9.)) if call is helpers.np_call: # numpy doesn't support autodiff assert dydxs is None else: assert np.allclose(dydxs[0], np.array([6.])) # func with multi return vals func = lambda xs_in: ((xs_in[0] * xs_in[0])[0], xs_in[0] * 1.5) xs = [ivy_grad.variable(ivy_gen.array([3.], f=lib))] y, dydxs, extra_out = call(ivy_grad.execute_with_gradients, func, xs, f=lib) assert np.allclose(y, np.array(9.)) assert np.allclose(extra_out, np.array([4.5])) if call is helpers.np_call: # numpy doesn't support autodiff assert dydxs is None else: assert np.allclose(dydxs[0], np.array([6.])) # func with multi weights vals func = lambda xs_in: (xs_in[0] * xs_in[1])[0] xs = [ ivy_grad.variable(ivy_gen.array([3.], f=lib)), ivy_grad.variable(ivy_gen.array([5.], f=lib)) ] y, dydxs = call(ivy_grad.execute_with_gradients, func, xs, f=lib) assert np.allclose(y, np.array(15.)) if call is helpers.np_call: # numpy doesn't support autodiff assert dydxs is None else: assert np.allclose(dydxs[0], np.array([5.])) assert np.allclose(dydxs[1], np.array([3.]))
def test_gradient_descent_update(): for lib, call in helpers.calls: if call is helpers.mx_graph_call: # mxnet symbolic does not support ivy gradient functions continue ws = [ivy_grad.variable(ivy_gen.array([3.], f=lib))] dcdws = [ivy_gen.array([6.], f=lib)] w_new = ivy_gen.array(ivy_grad.gradient_descent_update(ws, dcdws, 0.1, f=lib)[0], f=lib) assert np.allclose(ivy_gen.to_numpy(w_new), np.array([2.4]))
def test_linspace(): for lib, call in helpers.calls: if call is helpers.mx_graph_call: # mxnet symbolic does not support linspace continue assert np.allclose(call(ivy_gen.linspace, 1, 10, 100, f=lib), np.linspace(1, 10, 100), atol=1e-6) start = ivy_gen.array([[0., 1., 2.]], f=lib) stop = ivy_gen.array([[1., 2., 3.]], f=lib) assert np.allclose(call(ivy_gen.linspace, start, stop, 100, f=lib), np.linspace(np.array([[0., 1., 2.]]), np.array([[1., 2., 3.]]), 100, axis=-1), atol=1e-6) start = ivy_gen.array([[[-0.1471, 0.4477, 0.2214]]], f=lib) stop = ivy_gen.array([[[-0.3048, 0.3308, 0.2721]]], f=lib) res = np.array([[[[-0.1471, 0.4477, 0.2214], [-0.1786, 0.4243, 0.2316], [-0.2102, 0.4009, 0.2417], [-0.2417, 0.3776, 0.2518], [-0.2732, 0.3542, 0.2620], [-0.3048, 0.3308, 0.2721]]]]) assert np.allclose(call(ivy_gen.linspace, start, stop, 6, axis=-2, f=lib), res, atol=1e-4) if call is helpers.torch_call: start = ivy_grad.variable(start) stop = ivy_grad.variable(stop) res = ivy_grad.variable(res) assert np.allclose(ivy_gen.linspace(start, stop, 6, axis=-2, f=lib).detach().numpy(), res, atol=1e-4)
def test_adam_update(): for lib, call in helpers.calls: if call is helpers.mx_graph_call: # mxnet symbolic does not support ivy gradient functions continue ws = Container({'w': ivy_grad.variable(ivy_gen.array([3.], f=lib))}) dcdws = Container({'w': ivy_gen.array([6.], f=lib)}) mw = dcdws vw = dcdws.map(lambda x, _: x**2) w_new = ivy_gen.array(ivy_grad.adam_update(ws, dcdws, 0.1, mw, vw, lib.array(1), f=lib)[0]['w'], f=lib) assert np.allclose(ivy_gen.to_numpy(w_new), np.array([2.96837726]))