(self, f:Callable, jf:Callable|None=None)
| 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) |
no test coverage detected