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

Function relax_check_gradients

tests/python/relax/test_op_gradient_numeric.py:35–204  ·  view source on GitHub ↗

Generate the forward and the gradient module. Then run them and check numeric gradients. Parameters ---------- op_func : Callable The forward operator function. Should be a function in package relax.op. inputs_numpy : List[np.array] The np array inputs for op_func.

(
    op_func: Callable,
    inputs_numpy: list[np.array],
    target: str | tvm.target.Target,
    dev: tvm.runtime.Device,
    tuple_input: bool = False,
    ignore_grads: list[int] = [],
    **kwargs,  # attr for operators
)

Source from the content-addressed store, hash-verified

33
34
35def relax_check_gradients(
36 op_func: Callable,
37 inputs_numpy: list[np.array],
38 target: str | tvm.target.Target,
39 dev: tvm.runtime.Device,
40 tuple_input: bool = False,
41 ignore_grads: list[int] = [],
42 **kwargs, # attr for operators
43):
44 """Generate the forward and the gradient module. Then run them and check numeric gradients.
45
46 Parameters
47 ----------
48 op_func : Callable
49 The forward operator function. Should be a function in package relax.op.
50
51 inputs_numpy : List[np.array]
52 The np array inputs for op_func. inputs_numpy will be transformed into TVM Tensor inside
53 this function.
54
55 If op_func takes a tuple of tensors as input, you can set tuple_input as True, and pass the
56 tuple input (or list) as inputs_numpy. See test_concat().
57
58 target : Union[str, tvm.target.Target]
59 The building target.
60
61 dev : tvm.runtime.Device
62 The device to deploy the module.
63
64 tuple_input : bool
65 Whether the operator accepts a tuple as input. If true, operator will accept exactly one
66 tuple of tensors as input; otherwise, operator accept one or more tensors as input. See
67 test_concat(). Default: False.
68
69 ignore_grads: List[int]
70 Specifies which input we do not need to find gradient.
71
72 Sometimes the input is not differentiable, such as shape, boolean values, positions, etc.
73 We can specify the index of these inputs to check the gradient of them is no_grad, and
74 prevent computing numeric gradient.
75
76 kwargs : Any
77 The keyword arguments for the op_func. Will be passed to op_func directly.
78 """
79
80 func_name = "main"
81
82 # Helper functions
83 def _numpy_to_sinfo(data):
84 if isinstance(data, list):
85 return relax.TupleStructInfo([_numpy_to_sinfo(d) for d in data])
86 return relax.TensorStructInfo(data.shape, str(data.dtype))
87
88 def _numpy_to_tvm(data):
89 if isinstance(data, list):
90 return [_numpy_to_tvm(d) for d in data]
91 return tvm.runtime.tensor(data)
92

Callers 15

test_unaryFunction · 0.85
test_binary_arithFunction · 0.85
test_binary_minmaxFunction · 0.85
test_binary_cmpFunction · 0.85
test_ones_zeros_likeFunction · 0.85
test_full_likeFunction · 0.85
test_ones_zerosFunction · 0.85
test_triuFunction · 0.85
test_sumFunction · 0.85
test_sum_with_axisFunction · 0.85
test_sum_keepdimsFunction · 0.85
test_meanFunction · 0.85

Calls 15

functionMethod · 0.95
dataflowMethod · 0.95
emit_outputMethod · 0.95
emit_func_outputMethod · 0.95
getMethod · 0.95
emitMethod · 0.95
check_numerical_gradsFunction · 0.90
strFunction · 0.85
_numpy_to_sinfoFunction · 0.85
_gen_weightsFunction · 0.85
_is_call_no_gradFunction · 0.85
_numpy_to_tvmFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…