code: def _embedding_bag_reference_impl(
      self,
      input,
      weight,
      offsets=None,
      mode="sum",
      per_sample_weights=None,
      include_last_offset=False,
  ):
      assert mode == "sum" or per_sample_weights is None
      assert offsets is not None
      if per_sample_weights is None:
          per_sample_weights = torch.ones(input.size()).to(
              dtype=weight.dtype, device=weight.device
          )
      assert input.numel() == per_sample_weights.numel()
  
      bags = []
      long_input = input.to(torch.long)
      embeddings = weight.index_select(
          0, long_input
      ) * per_sample_weights.unsqueeze(1)
      if include_last_offset:
          for index in range(len(offsets) - 1):
              offset = offsets[index]
              next_offset = offsets[index + 1]
              length = next_offset - offset
              if length == 0:
                  bags.append(
                      torch.tensor([0] * weight.size(1)).to(
                          dtype=embeddings.dtype,
                          device=embeddings.device,
                      )
                  )
              else:
                  if mode == "sum":
                      bags.append(
                          embeddings.narrow(0, offset, length).sum(0)
                      )
                  elif mode == "mean":
                      bags.append(
                          embeddings.narrow(0, offset, length)
                          .sum(0)
                          .div(length)
                      )
                  else:
                      assert mode == "max"
                      bags.append(
                          embeddings.narrow(0, offset, length).max(0)[0]
                      )
      else:
          for index, offset in enumerate(offsets):
              if index + 1 < len(offsets):
                  next_offset = offsets[index + 1]
              else:
                  next_offset = len(long_input)
              length = next_offset - offset
              if length == 0:
                  bags.append(
                      torch.tensor([0] * weight.size(1)).to(
                          dtype=embeddings.dtype,
                          device=embeddings.device,
                      )
                  )
              else:
                  if mode == "sum":
                      bags.append(
                          embeddings.narrow(0, offset, length).sum(0)
                      )
                  elif mode == "mean":
                      bags.append(
                          embeddings.narrow(0, offset, length)
                          .sum(0)
                          .div(length)
                      )
                  else:
                      assert mode == "max"
                      bags.append(
                          embeddings.narrow(0, offset, length).max(0)[0]
                      )
      return torch.stack(bags)
  
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 14 for calling the TestEmbeddingNNDeviceType obj's underlying member method code with example usage: obj._embedding_bag_reference_impl(input, weight, offsets, mode, per_sample_weights, include_last_offset) and returns: torch