code: def test_EmbeddingBag_per_sample_weights_and_new_offsets(
      self, device, dtypes
  ):
      def test_per_sample_weights_new_offsets(
          mode, trainable_scale, include_last_offset, has_weight=True
      ):
          es = nn.EmbeddingBag(
              5, 2, mode=mode, include_last_offset=include_last_offset
          ).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(
              [3, 1, 1, 1, 4, 0], device=device, dtype=dtypes[0]
          )
          offsets = torch.tensor(
              [0, 0, 3, 3, 6], device=device, dtype=dtypes[1]
          )
  
          if include_last_offset:
              offsets = torch.cat(
                  (
                      offsets,
                      torch.tensor(
                          [input.size(0)],
                          device=device,
                          dtype=dtypes[1],
                      ),
                  ),
                  0,
              )
  
          if has_weight:
              per_sample_weights = torch.randn_like(
                  input, device=device, dtype=dtypes[2]
              ).requires_grad_(trainable_scale)
              ref_per_sample_weights = (
                  per_sample_weights.detach().requires_grad_(
                      trainable_scale
                  )
              )
          else:
              per_sample_weights = None
              ref_per_sample_weights = None
  
          reference_weights = es.weight.detach().requires_grad_()
  
          expected = self._embedding_bag_reference_impl(
              input,
              reference_weights,
              offsets,
              mode,
              ref_per_sample_weights,
              include_last_offset,
          )
          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)
          expected.backward(grad)
          self.assertEqual(
              es.weight.grad,
              reference_weights.grad,
              atol=dtype2prec_DONTUSE[dtypes[2]],
              rtol=0,
          )
          if has_weight and trainable_scale:
              self.assertEqual(
                  per_sample_weights.grad,
                  ref_per_sample_weights.grad,
                  atol=dtype2prec_DONTUSE[dtypes[2]],
                  rtol=0,
              )
  
      trainable_scale = (True, False)
      include_last_offset_list = (True, False)
      modes = (
          ("sum", False),
          ("sum", True),
          ("max", False),
          ("mean", False),
      )
      for (
          (mode, has_weight),
          trainable,
          include_last_offset,
      ) in itertools.product(
          modes, trainable_scale, include_last_offset_list
      ):
          test_per_sample_weights_new_offsets(
              mode, trainable, include_last_offset, has_weight
          )
  
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 17 for calling the TestEmbeddingNNDeviceType obj's underlying member method code with example usage: obj.test_EmbeddingBag_per_sample_weights_and_new_offsets(device, dtypes) without return types