MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / f

Function f

imperative/python/test/unit/amp/test_grad_scaler.py:17–31  ·  view source on GitHub ↗
(idx, data, calc)

Source from the content-addressed store, hash-verified

15 scaler = GradScaler()
16
17 def f(idx, data, calc):
18 x = mge.tensor(data, no_cache=True)
19 y = mge.tensor(data, no_cache=True)
20
21 if is_trace:
22 calc = trace(calc)
23
24 gm.attach([x, y])
25 with gm:
26 loss = calc(x, y)
27 scaler.backward(gm, loss, unscale_grad=False)
28 np.testing.assert_equal(x.grad.numpy(), 2 * scaler.scale_factor)
29 scaler.unscale(filter(lambda t: t.grad is not None, gm.attached_tensors()))
30 # scaler.unscale(gm.attached_tensors())
31 np.testing.assert_equal(x.grad.numpy(), 2)
32
33 def double_variables(x, y):
34 z = x + 2 * y

Callers 1

test_grad_scalerFunction · 0.70

Calls 9

traceClass · 0.90
filterFunction · 0.85
tensorMethod · 0.80
assert_equalMethod · 0.80
unscaleMethod · 0.80
attached_tensorsMethod · 0.80
attachMethod · 0.45
backwardMethod · 0.45
numpyMethod · 0.45

Tested by

no test coverage detected