code: def init_lstm_model(self, T, num_layers, forward_only, use_loss=True):
      workspace.FeedBlob(
          "seq_lengths", np.array([T] * self.batch_size, dtype=np.int32)
      )
      workspace.FeedBlob(
          "target",
          np.random.rand(T, self.batch_size, self.hidden_dim).astype(
              np.float32
          ),
      )
      workspace.FeedBlob(
          "hidden_init",
          np.zeros(
              [1, self.batch_size, self.hidden_dim], dtype=np.float32
          ),
      )
      workspace.FeedBlob(
          "cell_init",
          np.zeros(
              [1, self.batch_size, self.hidden_dim], dtype=np.float32
          ),
      )
  
      model = model_helper.ModelHelper(name="lstm")
      model.net.AddExternalInputs(["input"])
  
      init_blobs = []
      for i in range(num_layers):
          hidden_init, cell_init = model.net.AddExternalInputs(
              "hidden_init_{}".format(i), "cell_init_{}".format(i)
          )
          init_blobs.extend([hidden_init, cell_init])
  
      output, last_hidden, _, last_state = rnn_cell.LSTM(
          model=model,
          input_blob="input",
          seq_lengths="seq_lengths",
          initial_states=init_blobs,
          dim_in=self.input_dim,
          dim_out=[self.hidden_dim] * num_layers,
          scope="",
          drop_states=True,
          forward_only=forward_only,
          return_last_layer_only=True,
      )
  
      if use_loss:
          loss = model.AveragedLoss(
              model.SquaredL2Distance([output, "target"], "dist"),
              "loss",
          )
          # Add gradient ops
          if not forward_only:
              model.AddGradientOperators([loss])
  
      # init
      for init_blob in init_blobs:
          workspace.FeedBlob(
              init_blob,
              np.zeros(
                  [1, self.batch_size, self.hidden_dim],
                  dtype=np.float32,
              ),
          )
  
      return model, output
  
file_path: pytorch/caffe2/python/operator_test/recurrent_net_executor_test.py
imports: from caffe2.proto import caffe2_pb2
  from caffe2.python import model_helper, workspace, core, rnn_cell, test_util
  from caffe2.python.attention import AttentionType
  import numpy
  import unittest
  import caffe2.python.hypothesis_test_util
  import hypothesis.strategies
  from hypothesis import given, settings
label_desc: Use image node_id 3 for calling the TestRNNExecutor obj's underlying member method code with example usage: obj.init_lstm_model(T, num_layers, forward_only, use_loss) and returns: model,  output