MCPcopy Create free account
hub / github.com/apache/tvm / Gradient

Function Gradient

python/tvm/relax/transform/transform.py:55–229  ·  view source on GitHub ↗

Reverse-mode automatic differentiation. This pass will differentiate one function in the IRModule. Now the input function must have only one dataflow block (ConvertToDataflow may need to be called first). For a given function specified by `func_name`, it generates a new function with t

(
    func_name: str, require_grads: Var | list[Var] | None = None, target_index: int = 0
)

Source from the content-addressed store, hash-verified

53
54
55def Gradient(
56 func_name: str, require_grads: Var | list[Var] | None = None, target_index: int = 0
57) -> tvm.ir.transform.Pass:
58 """Reverse-mode automatic differentiation.
59
60 This pass will differentiate one function in the IRModule. Now the input function must have only
61 one dataflow block (ConvertToDataflow may need to be called first).
62
63 For a given function specified by `func_name`, it generates a new function with the name
64 `func_name + "_adjoint"`. The new function computes the gradient of the **differentiation
65 target** with respect to the arguments specified by `require_grads` of the original function.
66
67 If the function has only one return value, the return value will be specified as target. If the
68 function has more than one return values, the target will be specified as the target_index-th
69 return value. The target must be a scalar (0-dim tensor).
70
71 The new function will be like:
72
73 .. code-block:: python
74
75 @R.function
76 def main_adjoint(original_parameters):
77 with R.dataflow():
78 # the bindings of the original function
79 ...
80 # calculating the gradients
81 ...
82 R.output(original_outputs, grad_1, grad_2, ...)
83 return (original_return_value, (grad_1, grad_2, ...))
84
85 This AD pass also supports checkpointing as described in
86 "Training deep nets with sublinear memory cost." - Chen, Tianqi, et al. (2016).
87 See tvm.relax.testing.nn.checkpoint for more details.
88
89 Parameters
90 ----------
91 func_name : str
92 The name of the specific function.
93
94 require_grads : Optional[Union[relax.Var, List[relax.Var]]]
95 The relax variables whose adjoints is needed. Must be parameters of the given function and
96 should not be duplicate. If it is not specified, adjoints of all parameters would be
97 computed.
98
99 target_index : int
100 If the specified function has more than one return values, specify the index of the return
101 value as the target. If it is not specified, the first return value will be the target.
102
103 Returns
104 -------
105 ret : tvm.ir.transform.Pass
106 The Pass.
107
108 Examples
109 --------
110 The following code shows how to use this pass:
111
112 .. code-block:: python

Callers 6

test_emit_teFunction · 0.90
test_call_tirFunction · 0.90
test_emit_te_kwargsFunction · 0.90
test_call_tir_kwargsFunction · 0.90
test_tir_varFunction · 0.90
transform_moduleMethod · 0.85

Calls

no outgoing calls

Tested by 5

test_emit_teFunction · 0.72
test_call_tirFunction · 0.72
test_emit_te_kwargsFunction · 0.72
test_call_tir_kwargsFunction · 0.72
test_tir_varFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…