opacus/autograd_grad_sample.py
def _capture_backprops(
    layer: nn.Module,
    inputs: Tuple[torch.Tensor],
    outputs: Tuple[torch.Tensor],
    loss_reduction: str,
    batch_first: bool,
):
    r"""Backward hook handler captures backpropagated gradients during
    backward pass, and computes and stores per-sample gradients.

    Args:
        layer: Layer to capture gradients in.
        inputs: Gradients of the tensor on which ``.backward()`` is called with
            respect to the inputs to the ``layer``.
        outputs: Gradients of the tensor on which ``.backward()`` is called with
            respect to the outputs of the ``layer``.
        loss_reduction: Indicates if the loss reduction (for aggregating the gradients)
            is a sum or a mean operation. Can take values ``sum`` or ``mean``.
        batch_first: Flag to indicate if the input tensor to the corresponding module
            has the first dimension represent the batch, for example of shape
            ``[batch_size, ..., ...]``. Set to True if batch appears in first
            dimension else set to False (``batch_first=False`` implies that the
            batch is always in the second dimension).
    """

    if _hooks_disabled:
        return

    backprops = outputs[0].detach()
    _compute_grad_sample(layer, backprops, loss_reduction, batch_first)
r"""Backward hook handler captures backpropagated gradients during
    backward pass, and computes and stores per-sample gradients.

    Args:
        layer: Layer to capture gradients in.
        inputs: Gradients of the tensor on which ``.backward()`` is called with
            respect to the inputs to the ``layer``.
        outputs: Gradients of the tensor on which ``.backward()`` is called with
            respect to the outputs of the ``layer``.
        loss_reduction: Indicates if the loss reduction (for aggregating the gradients)
            is a sum or a mean operation. Can take values ``sum`` or ``mean``.
        batch_first: Flag to indicate if the input tensor to the corresponding module
            has the first dimension represent the batch, for example of shape
            ``[batch_size, ..., ...]``. Set to True if batch appears in first
            dimension else set to False (``batch_first=False`` implies that the
            batch is always in the second dimension).
    
r"""Backward hook handler captures backpropagated gradients during
backward pass, and computes and stores per-sample gradients.
r " " " Backward hook handler captures backpropagated gradients during backward pass and computes and stores per - sample gradients .
def _capture_backprops(
    layer: nn.Module,
    inputs: Tuple[torch.Tensor],
    outputs: Tuple[torch.Tensor],
    loss_reduction: str,
    batch_first: bool,
):
    if _hooks_disabled:
        return
    backprops = outputs[0].detach()
    _compute_grad_sample(layer, backprops, loss_reduction, batch_first)
def _capture_backprops ( layer : nn . Module , inputs : Tuple [ torch . Tensor ] , outputs : Tuple [ torch . Tensor ] , loss_reduction : str , batch_first : bool , ) : if _hooks_disabled : return backprops = outputs [ 0 ] . detach ( ) _compute_grad_sample ( layer , backprops , loss_reduction , batch_first )
r"""Backward hook handler captures backpropagated gradients during
backward pass, and computes and stores per-sample gradients.
r " " " Backward hook handler captures backpropagated gradients during backward pass and computes and stores per - sample gradients .
r"""Backward hook handler captures backpropagated gradients during
    backward pass, and computes and stores per-sample gradients.

    Args:
        layer: Layer to capture gradients in.
        inputs: Gradients of the tensor on which ``.backward()`` is called with
            respect to the inputs to the ``layer``.
        outputs: Gradients of the tensor on which ``.backward()`` is called with
            respect to the outputs of the ``layer``.
        loss_reduction: Indicates if the loss reduction (for aggregating the gradients)
            is a sum or a mean operation. Can take values ``sum`` or ``mean``.
        batch_first: Flag to indicate if the input tensor to the corresponding module
            has the first dimension represent the batch, for example of shape
            ``[batch_size, ..., ...]``. Set to True if batch appears in first
            dimension else set to False (``batch_first=False`` implies that the
            batch is always in the second dimension).
    """
layer
nn.Module
Layer to capture gradients in.
Layer to capture gradients in .
inputs
Tuple[torch.Tensor]
Gradients of the tensor on which ``.backward()`` is called with
respect to the inputs to the ``layer``.
Gradients of the tensor on which ` ` . backward () ` ` is called with respect to the inputs to the ` ` layer ` ` .
outputs
Tuple[torch.Tensor]
Gradients of the tensor on which ``.backward()`` is called with
respect to the outputs of the ``layer``.
Gradients of the tensor on which ` ` . backward () ` ` is called with respect to the outputs of the ` ` layer ` ` .
loss_reduction
str
Indicates if the loss reduction (for aggregating the gradients)
is a sum or a mean operation. Can take values ``sum`` or ``mean``.
Indicates if the loss reduction ( for aggregating the gradients ) is a sum or a mean operation . Can take values ` ` sum ` ` or ` ` mean ` ` .
batch_first
bool
Flag to indicate if the input tensor to the corresponding module
has the first dimension represent the batch, for example of shape
``[batch_size, ..., ...]``. Set to True if batch appears in first
dimension else set to False (``batch_first=False`` implies that the
batch is always in the second dimension).
Flag to indicate if the input tensor to the corresponding module has the first dimension represent the batch for example of shape ` ` [ batch_size ... ... ] ` ` . Set to True if batch appears in first dimension else set to False ( ` ` batch_first = False ` ` implies that the batch is always in the second dimension ) .