code: def check_layer_bidirectional_varseqlen(size, in_size):
      weight = mx.np.random.uniform(size=(784,))
  
      net = gluon.rnn.LSTM(
          size, bidirectional=True, use_sequence_length=True
      )
      ref_net = gluon.rnn.LSTM(
          size, bidirectional=True, use_sequence_length=False
      )
      net.initialize()
      ref_net.initialize()
      net_params = net.collect_params()
      ref_net_params = ref_net.collect_params()
      net_params["rnn_param"].set_data(weight)
      ref_net_params["rnn_param"].set_data(weight)
  
      batch_size = 10
      num_timesteps = 11
      data = mx.np.random.uniform(
          size=(num_timesteps, batch_size, in_size)
      )
      data_np = data.asnumpy()
  
      sequence_length = mx.np.random.randint(
          1, num_timesteps + 1, size=(batch_size)
      ).astype("int32")
      sequence_length_np = sequence_length.asnumpy().astype("int32")
  
      # Reference net is processing batch elements one at a time, so that it is "perfectly sized"
      # Because of that, we need to accumulate gradients in reference net.
      for p in ref_net.collect_params().values():
          p.grad_req = "add"
  
      ref_net_output = []
      with autograd.record():
          net_output = net(
              data.copy(), sequence_length=sequence_length.copy()
          )
  
          for b in range(batch_size):
              data_slice = mx.np.array(
                  data_np[: sequence_length_np[b], b, :]
              ).reshape(sequence_length_np[b], 1, in_size)
              ref_output_slice = ref_net(data_slice)
              ref_net_output.append(ref_output_slice)
  
      net_output_np = net_output.asnumpy()
  
      # TODO: test state return value as well output
      # Only compare the valid sections for each batch entry
      for b in range(batch_size):
          assert_allclose(
              net_output_np[: sequence_length_np[b], b],
              ref_net_output[b].asnumpy().squeeze(1),
              rtol=1e-2,
              atol=1e-6,
          )
  
      # Now test backward
      net_output.backward()
  
      for ref_output_slice in ref_net_output:
          ref_output_slice.backward()
  
      ref_net_params = ref_net.collect_params()
  
      net_grad = net_params["rnn_param"].grad()
      ref_net_grad = ref_net_params["rnn_param"].grad()
      assert_almost_equal(
          net_grad.asnumpy(),
          ref_net_grad.asnumpy(),
          rtol=1e-2,
          atol=1e-6,
      )
  
imports: import sys
  import os
  import time
  import random
  import mxnet
  import multiprocessing
  from mxnet.test_utils import check_consistency, set_default_device, assert_almost_equal, rand_ndarray, environment
  import numpy
  import math
  from mxnet import autograd
  import pytest
  from common import assert_raises_cudnn_not_satisfied, run_in_spawned_process, random_seed
  from test_gluon import *
  from test_loss import *
  from test_numpy_loss import *
  from test_gluon_rnn import *
label_desc: Use image node_id 7 for calling a global function with example usage: check_layer_bidirectional_varseqlen(size, in_size) without return types