Skip to main content

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

Project description

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
  • Batch-first (batch, seq_len, num_tags) by default

Requirements

  • Python >= 3.12
  • PyTorch >= 2.10

Build

conda activate py312

# build sdist + wheel into dist/ (uses the uv_build backend)
uv build

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

Install

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

# or editable, for development
pip install -e .

Quick 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]]

[!WARNING] Run the CRF in float32. The forward/Viterbi recursions accumulate logsumexp/max scores over the whole sequence, which is numerically unstable in bf16/fp16. Keep the CRF parameters in fp32 and cast emissions before the layer — crf(emissions.float(), ...) — even when the backbone runs in bf16/autocast.

Examples

Changelog

  • 0.1.0 — initial release; full list of differences from pytorch-crf.

Acknowledgements

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyt_crf-0.1.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyt_crf-0.1.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file pyt_crf-0.1.1.tar.gz.

File metadata

  • Download URL: pyt_crf-0.1.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyt_crf-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3ba3c2fc27ac6ae6a5636fbef8aa3a251f6b02e818d093e08e7dde01e85a6743
MD5 4c2f923db2a65345bfb20325023a0c76
BLAKE2b-256 68109f05dd3454e0371ec1eab16530730ca2e53b0c3d09d858647cad7b16df00

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyt_crf-0.1.1.tar.gz:

Publisher: publish.yml on id4thomas/pyt-crf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyt_crf-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyt_crf-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyt_crf-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2209c3b88f4f3a7a479d19392f4315f5b2d779cac02e6479d4ca75ed15f87dac
MD5 0eeae073b039f4d1aaa1630a64ecb7e2
BLAKE2b-256 cc258a182d8dbc28ddfc11a5be060a33e5af2bf40203fba9f07aeaf9fd28cc12

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyt_crf-0.1.1-py3-none-any.whl:

Publisher: publish.yml on id4thomas/pyt-crf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page