code: def test_generic_lengths(model_test, data):
      gen = Gen(model_test, data, LogSemiring)
      model, struct, vals, N, batch = (
          gen.model,
          gen.struct,
          gen.vals,
          gen.N,
          gen.batch,
      )
      lengths = torch.tensor(
          [
              data.draw(integers(min_value=2, max_value=N))
              for b in range(batch - 1)
          ]
          + [N]
      )
  
      m = model(MaxSemiring).marginals(vals, lengths=lengths)
      maxes = struct.score(vals, m)
      part = model().sum(vals, lengths=lengths)
  
      # Check that max is correct
      assert (maxes <= part + 1e-3).all()
      m_part = model(MaxSemiring).sum(vals, lengths=lengths)
      assert (torch.isclose(maxes, m_part)).all(), maxes - m_part
  
      if model == CKY:
          return
  
      seqs, extra = struct.from_parts(m)
      full = struct.to_parts(seqs, extra, lengths=lengths)
  
      assert (full == m.type_as(full)).all(), "%s %s %s" % (
          full.shape,
          m.shape,
          (full - m.type_as(full)).nonzero(),
      )
  
imports: from torch_struct import CKY, CKY_CRF, DepTree, LinearChain, SemiMarkov, Alignment, deptree_nonproj, deptree_part
  from torch_struct import LogSemiring, CheckpointSemiring, CheckpointShardSemiring, KMaxSemiring, SparseMaxSemiring, MaxSemiring, StdSemiring, EntropySemiring
  from .extensions import LinearChainTest, SemiMarkovTest, DepTreeTest, CKYTest, CKY_CRFTest, test_lookup
  import torch
  from hypothesis import given
  from hypothesis.strategies import integers, data, sampled_from
  import pytest
  from hypothesis import settings
label_desc: Use image node_id 11 for calling a global function with example usage: test_generic_lengths(model_test, data) without return types