code: def test_forward_respects_masking(self):
      # seed 1 fails on the old cnn encoder code
      torch.manual_seed(1)
      encoder = CnnEncoder(
          embedding_dim=7,
          num_filters=13,
          ngram_filter_sizes=(1, 2, 3, 4, 5),
      )
      init = Initializer.from_params(
          Params({"type": "normal", "mean": 0.0, "std": 10})
      )
      initializer = InitializerApplicator([(".*", init)])
      initializer(encoder)
      tokens = torch.ones(4, 8, 7)
      padded_tokens = torch.nn.functional.pad(
          tokens.transpose(1, 2), (0, 2), value=5
      ).transpose(1, 2)
      mask = (
          torch.where(
              padded_tokens == 5,
              torch.zeros_like(padded_tokens),
              torch.ones_like(padded_tokens),
          )
          .bool()
          .any(dim=2)
      )
      regular_output = encoder.forward(tokens=tokens, mask=None)
      masked_output = encoder.forward(tokens=padded_tokens, mask=mask)
      assert_almost_equal(
          regular_output.data.numpy(),
          masked_output.data.numpy(),
          decimal=5,
      )
  
file_path: allennlp/tests/modules/seq2vec_encoders/cnn_encoder_test.py
imports: import numpy
  from numpy.testing import assert_almost_equal
  import torch
  from allennlp.common import Params
  from allennlp.modules.seq2vec_encoders import CnnEncoder
  from allennlp.nn import InitializerApplicator, Initializer
  from allennlp.common.testing import AllenNlpTestCase
label_desc: Use image node_id 5 for calling the TestCnnEncoder obj's underlying member method code with example usage: obj.test_forward_respects_masking() without return types