MCPcopy Create free account
hub / github.com/pytorch/pytorch / _get_grad

Function _get_grad

caffe2/python/gradient_checker.py:40–58  ·  view source on GitHub ↗
(net, outputs, outputs_with_grad, input_values, inputs_with_grads)

Source from the content-addressed store, hash-verified

38
39
40def _get_grad(net, outputs, outputs_with_grad, input_values, inputs_with_grads):
41 grad_net = net.Clone(net.Name() + "_copy")
42 grad_map = grad_net.AddGradientOperators(outputs_with_grad)
43
44 for name, value in (input_values or {}).items():
45 workspace.blobs[name] = value
46
47 for input_to_check in inputs_with_grads:
48 assert input_to_check in grad_map, (
49 '{} has no gradient, cannot check net gradient.'.format(
50 input_to_check))
51 assert str(input_to_check) in workspace.blobs
52
53 workspace.RunNetOnce(grad_net)
54 forward_results = [(output, workspace.blobs[output]) for output in outputs]
55 grads = {input_to_check: _get_grad_blob(grad_map, input_to_check)
56 for input_to_check in inputs_with_grads}
57
58 return forward_results, grads, grad_net
59
60
61def _assert_close(value1, value2, threshold, err_msg=''):

Callers 2

CompareNetsMethod · 0.85
CheckMethod · 0.85

Calls 6

_get_grad_blobFunction · 0.85
CloneMethod · 0.80
NameMethod · 0.45
AddGradientOperatorsMethod · 0.45
itemsMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…