code: def test_EmbeddingBag_empty_per_sample_weights_and_offsets(
      self, device, dtypes
  ):
      # Test empty input and per sample weight, and backward pass. There was a CUDA
      # invalid configuration bug (more context in #46572)
      def test_per_sample_weights(mode, trainable_scale):
          es = nn.EmbeddingBag(5, 2, mode=mode).to(
              dtype=dtypes[2], device=device
          )
          es.weight.data.copy_(
              torch.arange(1, 11, device=device)
              .view_as(es.weight)
              .to(dtypes[2])
          )
          input = torch.tensor([], device=device, dtype=dtypes[0])
          offsets = torch.tensor(
              [0, 0, 0, 0, 0], device=device, dtype=dtypes[1]
          )
          per_sample_weights = torch.randn_like(
              input, dtype=dtypes[2]
          ).requires_grad_(trainable_scale)
          ref_per_sample_weights = (
              per_sample_weights.detach().requires_grad_(
                  trainable_scale
              )
          )
          reference_weights = es.weight.detach().requires_grad_()
  
          expected = self._embedding_bag_reference_impl(
              input,
              reference_weights,
              offsets,
              mode,
              ref_per_sample_weights,
          )
          result = es(input, offsets, per_sample_weights)
          self.assertEqual(
              result,
              expected,
              atol=dtype2prec_DONTUSE[dtypes[2]],
              rtol=0,
          )
  
          grad = torch.randn_like(expected)
          result.backward(grad)
          # the reference impl doesn't have grad fn for empty input; but the grad should
          # simply be a zero tensor
          ref_weights_grad = torch.zeros_like(es.weight)
          self.assertEqual(
              es.weight.grad,
              ref_weights_grad,
              atol=dtype2prec_DONTUSE[dtypes[2]],
              rtol=0,
          )
          if trainable_scale:
              ref_per_sample_weights_grad = torch.empty_like(
                  per_sample_weights
              )
              self.assertEqual(
                  per_sample_weights.grad,
                  ref_per_sample_weights_grad,
                  atol=dtype2prec_DONTUSE[dtypes[2]],
                  rtol=0,
              )
  
      modes = ("sum",)
      trainable_scale = (True, False)
      for mode, trainable in itertools.product(modes, trainable_scale):
          test_per_sample_weights(mode, trainable)
  
imports: import unittest
  import random
  import itertools
  from itertools import product
  import torch
  from torch.testing._internal.common_utils import run_tests, set_default_dtype, skipIfTorchDynamo, instantiate_parametrized_tests, parametrize, _assertGradAndGradgradChecks, IS_JETSON
  from torch.testing._internal.common_cuda import TEST_CUDA
  from torch.testing._internal.common_nn import NNTestCase
  from torch.testing._internal.common_device_type import onlyNativeDeviceTypes, dtypes, instantiate_device_type_tests, dtypesIfCUDA, onlyCUDA, TEST_WITH_ROCM, skipCUDAIf, skipMeta
  import torch.nn.functional
  import torch.nn
  from torch.testing._internal.common_utils import dtype2prec_DONTUSE
label_desc: Use image node_id 15 for calling the TestEmbeddingNNDeviceType obj's underlying member method code with example usage: obj.test_EmbeddingBag_empty_per_sample_weights_and_offsets(device, dtypes) without return types