code: def backward(ctx, grad_output):
      input, weight = ctx.input, ctx.weight
      padding_idx, scale_grad_by_freq, sparse = (
          ctx.padding_idx,
          ctx.scale_grad_by_freq,
          ctx.sparse,
      )
  
      def weight_per_sample_grad(weight):
          batch_size = input.shape[0]
          embedding_dim = weight.shape[1]
          index = (
              input.unsqueeze(-1)
              .expand(*input.shape, embedding_dim)
              .reshape(batch_size, -1, embedding_dim)
          )
          grad_sample = torch.zeros(
              batch_size,
              *weight.shape,
              device=weight.device,
              dtype=grad_output.dtype,
          )
          return grad_sample.scatter_add_(
              1,
              index,
              grad_output.reshape(batch_size, -1, embedding_dim),
          )
  
      results: List[Optional[torch.Tensor]] = []
      results.append(None)  # for kwarg names
      results.append(None)  # for op reference
  
      if input.requires_grad:
          bw_fn = torch.ops.aten.embedding_backward
          results.append(
              bw_fn(
                  grad_output,
                  input,
                  weight.shape[0],
                  padding_idx,
                  scale_grad_by_freq,
                  sparse,
              )
          )
      else:
          results.append(None)
  
      # weight doesn't compute batched gradients; no other arguments are differentiable (2 not saved from forward)
      results = results + [None] * 6
  
      # set grad_sample field for weight with per sample gradients
      set_grad_sample_if_exists(weight, weight_per_sample_grad)
      return tuple(results)
  
file_path: pytorch/torch/nn/utils/_expanded_weights/embedding_expanded_weights.py
imports: import torch
  import torch.nn.functional
  from .expanded_weights_impl import implements_per_sample_grads
  from .expanded_weights_utils import standard_kwargs, forward_helper, set_grad_sample_if_exists
  from typing import List, Optional
label_desc: Use image node_id 2 for calling the EmbeddingPerSampleGrad obj's underlying member method code with example usage: obj.backward(ctx, grad_output) and returns: tuple, grad_sample