Skip to main content

pyt-crf

A modern linear-chain CRF (Conditional Random Field) layer for PyTorch.

  • forward() returns the negative log-likelihood directly (ready for .backward())
  • decode() runs batched Viterbi decoding
  • forward_and_decode() computes both in one fused sweep (for eval during training), with separate loss/decode masks
  • Batch-first (batch, seq_len, num_tags) by default

See docs/explanation.md for how the CRF math maps to the code.

Requirements

  • Python >= 3.12
  • PyTorch >= 2.10

Usage

import torch
from pyt_crf import CRF

crf = CRF(num_tags=5)
emissions = torch.randn(2, 7, 5)          # (batch, seq_len, num_tags)
tags = torch.randint(5, (2, 7))           # (batch, seq_len)
mask = torch.ones(2, 7, dtype=torch.bool) # True = real token, False = padding

loss = crf(emissions, tags, mask)  # scalar NLL loss
loss.backward()

best_paths = crf.decode(emissions, mask)  # List[List[int]]

# eval during training: loss + decode in one fused sweep,
# with separate masks for the loss (-100 labels) and decoding (attention mask)
loss, best_paths = crf.forward_and_decode(
    emissions, tags, loss_mask=mask, decode_mask=mask
)

Examples

Build (wheel)

uv build

# or, without uv:
pip install build
python -m build

Both commands produce dist/pyt_crf-<version>-py3-none-any.whl (plus the sdist dist/pyt_crf-<version>.tar.gz). The version comes from pyproject.toml.

Install

# from the built wheel
pip install dist/pyt_crf-<version>-py3-none-any.whl

# or editable, for development
pip install -e .

Test

Tests live in tests/ and run with pytest. pyproject.toml already puts src/ on the import path for pytest, so no install is needed beforehand.

conda activate py312
pip install pytest

pytest            # run the whole suite
pytest -v         # per-test names
pytest -k fused   # only tests matching a keyword

# or with uv (pytest comes from the dev dependency group):
uv run pytest

The suite checks forward/decode against brute-force enumeration of all tag paths (including masks with mid-sequence holes), verifies forward_and_decode returns exactly the same loss/paths/gradients as the separate calls, and covers input validation.

Changelog

  • 0.1.2 — forward_and_decode (fused loss + Viterbi), pytest suite, docs, publish workflow.
  • 0.1.1 — initial release; full list of differences from pytorch-crf.

Acknowledgements

Metadata

Release files for pyt-crf 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyt-crf 0.1.2
File Size Uploaded
pyt_crf-0.1.2.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyt-crf 0.1.2
File Interpreter ABI Platform
pyt_crf-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 18.4 kB

Release files / pyt_crf-0.1.2.tar.gz

Download URL pyt_crf-0.1.2.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d948da3ed10d35df4e080564443c023a8763a84109af39d95f4f65d2a4b7a335
BLAKE2b-256 checksum
How to use checksums
921cdcc9791f3faed671db83055061e507614a4ed355f30b806d6f51392e947d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 10, 2026.

Transparency log

Release files / pyt_crf-0.1.2-py3-none-any.whl

Download URL pyt_crf-0.1.2-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
25e9c032944a81e103b590ceefaa3dc0bda416c2cda74bbe0b722bd4f936aa4e
BLAKE2b-256 checksum
How to use checksums
f418c9e7ab45a93489114c5091666f9850e292137241571fe0c3f43e1618c5b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 10, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page