code: def compute_embeddingbag_gradsampler(layer, inputs, backprops):
      index, offset = inputs
      batch_size = offset.shape[0]
      gsm = torch.zeros(
          batch_size, layer.num_embeddings, layer.embedding_dim
      )
  
      for i in range(batch_size):
          begin = offset[i]
          if i < batch_size - 1:
              end = offset[i + 1]
          else:
              end = index.shape[0]
  
          if layer.mode == "sum":
              gsm[i][index[begin:end]] += backprops[i]
          elif layer.mode == "mean":
              gsm[i][index[begin:end]] += backprops[i] / (end - begin)
  
      ret = {}
      ret[layer.weight] = gsm
  
      return ret
  
imports: from typing import Dict
  import torch
  import torch.nn
  from .utils import register_grad_sampler
label_desc: Use image node_id 2 for calling a global function with example usage: compute_embeddingbag_gradsampler(layer, inputs, backprops) and returns: ret