{"id":24723,"library":"torch-struct","title":"Torch-Struct","description":"A library for structured prediction (e.g., parsing, sequence labeling) with PyTorch. Provides differentiable implementations of dynamic programming algorithms like CYK, Inside-Outside, and Viterbi. Current version 0.5, last updated 2022. Low release cadence.","status":"active","version":"0.5","language":"python","source_language":"en","source_url":"https://github.com/harvardnlp/pytorch-struct","tags":["structured-prediction","pytorch","crf","parsing","dynamic-programming"],"install":[{"cmd":"pip install torch-struct","lang":"bash","label":"PyPI install"}],"dependencies":[{"reason":"Core dependency — requires PyTorch.","package":"torch","optional":false},{"reason":"Was a dependency in v0.4, dropped in v0.5.","package":"genbmm","optional":true}],"imports":[{"note":"Standard import path; not from torch import struct.","symbol":"torch_struct","correct":"import torch_struct"},{"note":"Common mistake: models submodule does not exist in v0.5.","wrong":"from torch_struct.models import LineCRF","symbol":"LineCRF","correct":"from torch_struct import LineCRF"},{"note":"TorchStruct is a class within the package, not a submodule.","wrong":"import torch_struct.TorchStruct","symbol":"TorchStruct","correct":"from torch_struct import TorchStruct"}],"quickstart":{"code":"import torch\nimport torch_struct\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nbatch, N, C = 2, 5, 3\nlog_potentials = torch.randn(batch, N, C, device=device)\ndist = torch_struct.LineCRF(log_potentials)\nlog_partition = dist.partition\nprint(f\"Log partition: {log_partition}\")","lang":"python","description":"Creates a linear-chain CRF from random potentials and computes the log-partition function."},"warnings":[{"fix":"Remove `genbmm` imports; use torch.bmm or einsum instead. Replace removed models with custom implementations.","message":"In v0.5, the `genbmm` dependency was dropped and some lesser-used features were removed (e.g., NeuralPottsModel). If you upgrade from v0.4, code relying on those features will break.","severity":"breaking","affected_versions":"0.4 -> 0.5"},{"fix":"Replace `Semiring` usage with `StructDistribution` and log-potential pattern.","message":"The `Semiring` API is deprecated in favor of `StructDistribution`. Old code using `Semiring` will still work but generate deprecation warnings.","severity":"deprecated","affected_versions":">=0.4"},{"fix":"Ensure inputs are log-space (e.g., use torch.log(probs) if you have probabilities).","message":"Potentials must be in log-space (logits). The library does not check for this; using raw probabilities leads to incorrect results.","severity":"gotcha","affected_versions":"all"},{"fix":"Pass a `lengths` tensor of shape (batch,) to the constructor: `dist = LineCRF(log_potentials, lengths=lengths)`.","message":"Many struct classes require the length of each sequence (mask) for variable-length sequences. Without a mask, the algorithms assume fully-observed sequences of length N, which can produce incorrect gradients.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use `StructDistribution` instead: `from torch_struct import StructDistribution`.","cause":"Semiring was removed in v0.5 or used via incorrect import.","error":"AttributeError: module 'torch_struct' has no attribute 'Semiring'"},{"fix":"Check the expected input shape for the struct class. For LineCRF, use (batch, N, C). For TreeCRF, use (batch, N, N, C).","cause":"Potentials tensor shape (batch, N, C) does not match the expected shape for the chosen struct (e.g., for Trees, you need (batch, N, N, C) or similar).","error":"RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 2"},{"fix":"Ensure batch size > 1 or set model.eval() during inference.","cause":"Batch normalization layer in the model receiving too few samples; not directly a torch-struct issue but common when integrating.","error":"ValueError: Expected more than 1 value per channel when training, got input size ..."}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}