code: def test_session_margin_loss(self, n, k, m, gc, dc):
      y = np.random.rand(n * m).astype(np.float32)
      r = np.random.randint(k, size=n * m).astype(np.float32)
      # m sessions of length n
      session_lengths = np.repeat(n, m).astype(np.int32)
      ref_loss = np.empty(0)
      ref_scale_loss = np.empty(0)
      ref_dy = np.empty(0)
      ref_scale_dy = np.empty(0)
      for i in range(m):
          r_loss, r_dy = self.ref_margin_loss(
              y[(i) * n : (i + 1) * n], r[(i) * n : (i + 1) * n], 0.06
          )
          r_scale_loss, r_scale_dy = self.ref_margin_loss(
              y[(i) * n : (i + 1) * n], r[(i) * n : (i + 1) * n], 0.04
          )
          ref_loss = np.append(ref_loss, r_loss)
          ref_dy = np.append(ref_dy, r_dy)
          ref_scale_loss = np.append(ref_scale_loss, r_scale_loss)
          ref_scale_dy = np.append(ref_scale_dy, r_scale_dy)
  
      dloss = np.random.random(m).astype(np.float32)
  
      workspace.blobs["pred"] = y
      workspace.blobs["label"] = r
      workspace.blobs["session_lengths"] = session_lengths
      workspace.blobs["dloss"] = dloss
  
      # Test scale = 1
      op = core.CreateOperator(
          "SessionMarginLoss",
          ["pred", "label", "session_lengths"],
          ["loss", "dpred"],
          margin=0.06,
      )
      workspace.RunOperatorOnce(op)
      loss = workspace.blobs["loss"]
      dy = workspace.blobs["dpred"]
      np.testing.assert_allclose(loss, ref_loss, rtol=1e-5, atol=1e-6)
      np.testing.assert_allclose(dy, ref_dy, rtol=1e-5, atol=1e-6)
      name = op.output[0]
      arr = workspace.FetchBlob(name)
      self.assertGradientChecks(
          gc,
          op,
          [y, r, session_lengths],
          0,
          [0],
          stepsize=1e-3,
          threshold=2e-1,
      )
  
      # Test scale > 1
      op = core.CreateOperator(
          "SessionMarginLoss",
          ["pred", "label", "session_lengths"],
          ["loss", "dpred"],
          margin=0.04,
      )
      workspace.RunOperatorOnce(op)
      loss = workspace.blobs["loss"]
      dy = workspace.blobs["dpred"]
      np.testing.assert_allclose(
          loss, ref_scale_loss, rtol=1e-5, atol=1e-6
      )
      np.testing.assert_allclose(dy, ref_scale_dy, rtol=1e-5, atol=1e-6)
      self.assertGradientChecks(
          gc,
          op,
          [y, r, session_lengths],
          0,
          [0],
          stepsize=1e-3,
          threshold=2e-1,
      )
  
file_path: pytorch/caffe2/python/operator_test/margin_loss_l2r_operator_test.py
imports: import caffe2.python.hypothesis_test_util
  import hypothesis.strategies
  import numpy
  from caffe2.python import core, workspace
  from hypothesis import given
label_desc: Use image node_id 2 for calling the TestMarginLossL2rOps obj's underlying member method code with example usage: obj.test_session_margin_loss(n, k, m, gc, dc) without return types