MCPcopy
hub / github.com/tinygrad/tinygrad / _test_two_input_function

Method _test_two_input_function

test/null/test_gradient.py:25–45  ·  view source on GitHub ↗
(self, f:Callable, jf:Callable|None=None)

Source from the content-addressed store, hash-verified

23 self._cmp_nan_okay(tg_out, torch_out)
24
25 def _test_two_input_function(self, f:Callable, jf:Callable|None=None):
26 if jf is None: jf = f
27 x = UOp.variable('x', -math.inf, math.inf, dtype=dtypes.float)
28 y = UOp.variable('y', -math.inf, math.inf, dtype=dtypes.float)
29 grads = compute_gradient(f(x, y), UOp.const(dtypes.float, 1.0), set([x, y]))
30 gx, gy = grads[x], grads[y]
31
32 for valx in [-5., -2.0, 0.0, 2.0, 5.]:
33 for valy in [-5., -2.0, 0.0, 2.0, 5.]:
34 # Substitute the values into the gradient expressions
35 substitutions = {x: x.const_like(valx), y: y.const_like(valy)}
36 tg_out_x = gx.substitute(substitutions).ssimplify()
37 tg_out_y = gy.substitute(substitutions).ssimplify()
38
39 tx = torch.tensor([valx], dtype=torch.float, requires_grad=True)
40 ty = torch.tensor([valy], dtype=torch.float, requires_grad=True)
41 torch_grad = torch.autograd.grad(jf(tx, ty), [tx, ty])
42 torch_out_x, torch_out_y = [x.item() for x in torch_grad]
43
44 self._cmp_nan_okay(tg_out_x, torch_out_x)
45 self._cmp_nan_okay(tg_out_y, torch_out_y)
46
47 # unary ops unit
48 def test_recip(self): self._test_one_input_function(lambda x: 1.0/x)

Callers 6

test_addMethod · 0.95
test_mulMethod · 0.95
test_chain_binopMethod · 0.95
test_big_add_sinMethod · 0.95
test_big_chainMethod · 0.95
test_whereMethod · 0.95

Calls 10

_cmp_nan_okayMethod · 0.95
compute_gradientFunction · 0.90
variableMethod · 0.80
ssimplifyMethod · 0.80
substituteMethod · 0.80
tensorMethod · 0.80
itemMethod · 0.80
fFunction · 0.50
constMethod · 0.45
const_likeMethod · 0.45

Tested by

no test coverage detected