MCPcopy
hub / github.com/tinygrad/tinygrad / _test_one_input_function

Method _test_one_input_function

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

Source from the content-addressed store, hash-verified

12 self.assertAlmostEqual(x, y, places=5)
13
14 def _test_one_input_function(self, f:Callable, jf:Callable|None=None):
15 if jf is None: jf = f
16 x = UOp.variable('x', -math.inf, math.inf, dtype=dtypes.float)
17 gx = compute_gradient(f(x), UOp.const(dtypes.float, 1.0), set([x]))[x]
18
19 for val in [-5., -2.0, 0.0, 2.0, 5.]:
20 tg_out = gx.substitute({x: x.const_like(val)}).ssimplify()
21 tx = torch.tensor([val], dtype=torch.float, requires_grad=True)
22 torch_out = torch.autograd.grad(jf(tx), tx)[0].item()
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

Callers 6

test_recipMethod · 0.95
test_sinMethod · 0.95
test_sqrtMethod · 0.95
test_log2Method · 0.95
test_exp2Method · 0.95
test_chainMethod · 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