code: def test_sinusoidal_positional_embedding():
      def _gt_sinusoidal_embedding(np_data, units):
          half_dim = units // 2
          emb = np.log(10000) / (half_dim - 1)
          emb = np.exp(np.arange(half_dim, dtype=np.float32) * -emb)
          emb = (
              np.expand_dims(np_data.astype(np.float32), axis=-1) * emb
          )
          emb = np.concatenate([np.sin(emb), np.cos(emb)], axis=-1)
          if units % 2 == 1:
              # zero pad
              emb = np.concatenate(
                  [
                      emb,
                      np.expand_dims(np.zeros_like(np_data), axis=-1),
                  ],
                  axis=-1,
              )
          return emb
  
      for units in [31, 16]:
          for hybridize in [False, True]:
              pos_embed = SinusoidalPositionalEmbedding(
                  units=units, dtype=np.float32
              )
              pos_embed.initialize(mx.init.Normal(0.01))
              if hybridize:
                  pos_embed.hybridize()
              in_data = mx.np.array([100, 5, 20], dtype=np.int32)
              out = pos_embed(in_data)
              gt_out = _gt_sinusoidal_embedding(
                  in_data.asnumpy(), units=units
              )
              assert_allclose(gt_out, out.asnumpy(), 1e-5, 1e-5)
  
file_path: gluon-nlp/tests/test_layers.py
imports: import pytest
  import numpy
  from numpy.testing import assert_allclose
  import mxnet
  from gluonnlp.layers import PositionalEmbedding, SinusoidalPositionalEmbedding, LearnedPositionalEmbedding, BucketPositionalEmbedding, AdaptiveEmbedding, ProjectedAdaptiveLogSoftmaxWithLoss, get_activation, get_norm_layer
  from gluonnlp.op import relative_position_bucket
label_desc: Use image node_id 1 for calling a global function with example usage: test_sinusoidal_positional_embedding() and returns: emb