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
)
| 33 | |
| 34 | |
| 35 | def 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 |
no test coverage detected
searching dependent graphs…