mindformers.wrapper.MFTrainOneStepCell¶
- class mindformers.wrapper.MFTrainOneStepCell(network, optimizer, use_clip_grad=False, max_grad_norm=1.0, scale_sense=1.0, **kwargs)[源代码]¶
TrainOneStep For MindFormer. Network training with loss scaling, grad clip, gradient accumulation, exponential moving average and so on.
This is a training step with loss scaling. It takes a network, an optimizer and a scale update Cell(or a Tensor) as args. The loss scale value can be updated in both host side or device side. If you want to update it on host side, using a value of Tensor type as scale_sense, otherwise, using a Cell instance for updating loss scale as scale_sense.
- Args:
network (Cell): The training network. The network only supports single output. optimizer (Cell): Optimizer for updating the network parameters. use_clip_grad (bool): Whether to use the gradient clipping function. Default: False. max_grad_norm (float): Maximum gradient value. Default: 1.0. scale_sense (Union[Tensor, Cell]): If this value is a Cell, it will be called by MFTrainOneStepCell
to update loss scale. If this value is a Tensor, the loss scale can be modified by set_sense_scale, the shape should be \(()\) or \((1,)\).
- Inputs:
(*inputs) (Tuple(Tensor)) - Tuple of input tensors with shape \((N, \ldots)\).
- Outputs:
Tuple of 3 Tensor, the loss, overflow flag and current loss scale value.
loss (Tensor) - A scalar, the loss value.
overflow (Tensor) - A scalar, whether overflow occur or not, the type is bool.
loss scale (Tensor) - The loss scale value, the shape is \(()\) or \((1,)\).
- Raises:
TypeError: If scale_sense is neither Cell nor Tensor. ValueError: If shape of scale_sense is neither (1,) nor ().