code: def test_embedding_padding_idx(self, device, dtype):
      embedding = nn.Embedding(10, 20, padding_idx=0).to(device, dtype)
      input = torch.tensor(
          [[0, 2, 4, 5], [4, 3, 0, 9]], dtype=torch.long
      ).to(device)
      output = embedding(input)
      self.assertEqual(output[0][0].sum(), 0)
      self.assertEqual(output[1][2].sum(), 0)
  
      embedding = nn.Embedding(10, 20, padding_idx=0, sparse=True).to(
          device, dtype
      )
      input = torch.tensor(
          [[0, 2, 4, 5], [4, 3, 0, 9]], dtype=torch.long
      ).to(device)
      output = embedding(input)
      self.assertEqual(output[0][0].sum(), 0)
      self.assertEqual(output[1][2].sum(), 0)
  
      # negative indexing check for padding_idx
      # padding_idx=-2, num_embeddings=10 ==> index 8 padded
      embedding = nn.Embedding(10, 20, padding_idx=-2).to(device, dtype)
      input = torch.tensor(
          [[0, 2, 8, 5], [4, 8, 0, 9]], dtype=torch.long
      ).to(device)
      output = embedding(input)
      self.assertEqual(output[0][2].sum(), 0)
      self.assertEqual(output[1][1].sum(), 0)
  
      embedding = nn.Embedding(10, 20, padding_idx=-2, sparse=True).to(
          device, dtype
      )
      input = torch.tensor(
          [[0, 2, 8, 5], [4, 8, 0, 9]], dtype=torch.long
      ).to(device)
      output = embedding(input)
      self.assertEqual(output[0][2].sum(), 0)
      self.assertEqual(output[1][1].sum(), 0)
  
      # change padding vector
      padding_vector = torch.ones(20, dtype=dtype, device=device)
      embedding = nn.Embedding(10, 20, padding_idx=2, sparse=True).to(
          device, dtype
      )
      with torch.no_grad():
          embedding.weight[2] = padding_vector
      input = torch.tensor([0, 2], dtype=torch.long).to(device)
      output = embedding(input)
      self.assertEqual(output[1], padding_vector)
  
      # out of bounds check for padding_idx
      self.assertRaises(
          AssertionError,
          nn.Embedding,
          num_embeddings=10,
          embedding_dim=20,
          padding_idx=25,
      )
      self.assertRaises(
          AssertionError,
          nn.Embedding,
          num_embeddings=10,
          embedding_dim=20,
          padding_idx=-25,
      )
  
      padding_idx = 0
      embedding = nn.Embedding(5, 2, padding_idx=padding_idx).to(
          device, dtype
      )
      for n in (
          1,
          2,
          1000,
      ):  # Need large N to trigger all the methods we have implemented
          for other_indices in ([], [1, 3], [2]):
              indices = torch.tensor(
                  other_indices + [padding_idx] * n, dtype=torch.long
              ).to(device)
              pre = embedding.weight[padding_idx].clone()
              embedding(indices).sum().backward()
              after = (embedding.weight + embedding.weight.grad)[
                  padding_idx
              ]
              embedding.zero_grad()
              self.assertEqual(after, pre)
  
              # test double backward
              emb_sum = embedding(indices).sum()
              emb_grad = torch.autograd.grad(
                  outputs=emb_sum,
                  inputs=list(embedding.parameters()),
                  retain_graph=True,
              )
              scalar = emb_grad[0].sum() + emb_sum
              scalar.backward()
              after = (embedding.weight + embedding.weight.grad)[
                  padding_idx
              ]
              embedding.zero_grad()
              self.assertEqual(after, pre)
  
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 6 for calling the TestEmbeddingNNDeviceType obj's underlying member method code with example usage: obj.test_embedding_padding_idx(device, dtype) without return types