class_docstr: This module produces sinusoidal positional embeddings of any length.
class_docstr_tok: ["This","module","produces","sinusoidal","positional","embeddings","of","any","length","."]
docstr: Build sinusoidal embeddings.
  
  This matches the implementation in tensor2tensor, but differs slightly from the description in Section 3.5 of
  "Attention Is All You Need".
docstr_tok: ["Build","sinusoidal","embeddings",".","This","matches","the","implementation","in","tensor2tensor",",","but","differs","slightly","from","the","description","in","Section","3.5","of","''","Attention","Is","All","You","Need","''","."]
code: def get_embedding(
      num_embeddings: int,
      embedding_dim: int,
      padding_idx: Optional[int] = None,
  ):
      """
      Build sinusoidal embeddings.
  
      This matches the implementation in tensor2tensor, but differs slightly from the description in Section 3.5 of
      "Attention Is All You Need".
      """
      half_dim = embedding_dim // 2
      emb = math.log(10000) / (half_dim - 1)
      emb = torch.exp(torch.arange(half_dim, dtype=torch.float) * -emb)
      emb = torch.arange(num_embeddings, dtype=torch.float).unsqueeze(
          1
      ) * emb.unsqueeze(0)
      emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1).view(
          num_embeddings, -1
      )
      if embedding_dim % 2 == 1:
          # zero pad
          emb = torch.cat([emb, torch.zeros(num_embeddings, 1)], dim=1)
      if padding_idx is not None:
          emb[padding_idx, :] = 0
  
      return emb.to(torch.get_default_dtype())
  
imports: import math
  from typing import List, Optional, Tuple, Union
  import torch
  import torch.utils.checkpoint
  from torch import nn
  from torch.nn import CrossEntropyLoss
  from ...activations import ACT2FN
  from ...modeling_attn_mask_utils import _prepare_4d_attention_mask, _prepare_4d_causal_attention_mask
  from ...modeling_outputs import BaseModelOutputWithPastAndCrossAttentions, CausalLMOutputWithCrossAttentions
  from ...modeling_utils import PreTrainedModel
  from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
  from .configuration_xglm import XGLMConfig
label_desc: Use image node_id 3 for calling the XGLMSinusoidalPositionalEmbedding obj's underlying member method code with example usage: obj.get_embedding(num_embeddings, embedding_dim, padding_idx) and returns: emb