class_docstr: Represents structured linear-chain CRFs, generalizing HMMs smoothing, tagging models,
  and anything with chain-like dynamics.
docstr: Compute forward pass by linear scan
docstr_tok: ["Compute","forward","pass","by","linear","scan"]
code: def logpartition(
      self, log_potentials, lengths=None, force_grad=False
  ):
      "Compute forward pass by linear scan"
      # Setup
      semiring = self.semiring
      ssize = semiring.size()
      log_potentials, batch, N, C, lengths = self._check_potentials(
          log_potentials, lengths
      )
      log_N, bin_N = self._bin_length(N - 1)
      chart = self._chart(
          (batch, bin_N, C, C), log_potentials, force_grad
      )
  
      # Init
      init = torch.zeros_like(chart).bool()
      init.diagonal(0, 3, 4).fill_(True)
      chart = semiring.fill(chart, init, semiring.one)
  
      # Length mask
      big = torch.zeros(
          ssize,
          batch,
          bin_N,
          C,
          C,
          dtype=log_potentials.dtype,
          device=log_potentials.device,
      )
      big[:, :, : N - 1] = log_potentials
      c = chart[:, :, :].view(ssize, batch * bin_N, C, C)
      lp = big[:, :, :].view(ssize, batch * bin_N, C, C)
      mask = (
          torch.arange(bin_N)
          .view(1, bin_N)
          .expand(batch, bin_N)
          .type_as(c)
      )
      mask = mask >= (lengths - 1).view(batch, 1)
      mask = mask.view(batch * bin_N, 1, 1).to(lp.device)
      lp.data[:] = semiring.fill(lp.data, mask, semiring.zero)
      c.data[:] = semiring.fill(c.data, ~mask, semiring.zero)
  
      c[:] = semiring.sum(torch.stack([c.data, lp], dim=-1))
  
      # Scan
      for n in range(1, log_N + 1):
          chart = semiring.matmul(chart[:, :, 1::2], chart[:, :, 0::2])
      v = semiring.sum(semiring.sum(chart[:, :, 0].contiguous()))
      return v, [log_potentials]
  
file_path: pytorch-struct/torch_struct/linearchain.py
imports: import torch
  from .helpers import _Struct
label_desc: Use image node_id 2 for calling the LinearChain obj's underlying member method code with example usage: obj.logpartition(log_potentials, lengths, force_grad) and returns: v,  list