apply_optimizer_in_backward (TorchRec)
This API is an open-source API of the TorchRec and is not an external API of the Rec SDK Torch. This section describes the parameter ranges supported by the TorchRec APIs called when the Rec SDK Torch is used.
Function
Specifies the optimizer used by a table.
Prototype
1 2 3 4 5 | def apply_optimizer_in_backward( optimizer_class: Type[torch.optim.Optimizer], params: Iterable[torch.nn.Parameter], optimizer_kwargs: Dict[str, Any], ) -> None: |
Parameters
Parameter |
Type |
Mandatory/Optional |
Description |
|---|---|---|---|
optimizer_class |
Type[torch.optim.Optimizer] |
Mandatory |
Optimizer type. The options are as follows:
NOTE:
|
params |
Iterable[torch.nn.Parameter] |
Mandatory |
Sets the torch.nn.Parameter object of the optimizer. Pass the parameters of the HashEmbeddingBagCollection object by referring to 5. NOTICE:
For performance purposes, the params parameter cannot be verified. Ensure that the type is correct. |
optimizer_kwargs |
Dict[str, Any] |
Mandatory |
Set this parameter based on the parameters and range of optimizer_class. Ensure that the range of this parameter meets the restrictions of the corresponding optimizer. If the gradient accumulation function is used, optimizer_class must be set to an optimizer with the gradient accumulation function, and use_accumulate (bool type) whose value is True (default value: False) and accumulate_step (int type) whose value is 1 (default value: 1) must be transferred. |
Return Value
- Success: None
- Failure: An exception is thrown.
Example
1 2 3 4 5 6 7 8 | from torchrec.optim.apply_optimizer_in_backward import apply_optimizer_in_backward import torch embedding_optimizer = torch.optim.Adagrad optimizer_kwargs = {"lr": 0.001, "eps": 0.1} apply_optimizer_in_backward( embedding_optimizer, ebc.parameters(), optimizer_kwargs=optimizer_kwargs) |
See Also
For details about the API call sequence and example, see Porting and Training.