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
examples/conll2003— NER fine-tuning on tner/conll2003 with a Gemma3-based backbone + CRF head.
Changelog
- 0.1.0 — initial release; full list of differences from pytorch-crf.
Acknowledgements
- This package mostly follows implementation of kmkurn/pytorch-crf.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ba3c2fc27ac6ae6a5636fbef8aa3a251f6b02e818d093e08e7dde01e85a6743
|
|
| MD5 |
4c2f923db2a65345bfb20325023a0c76
|
|
| BLAKE2b-256 |
68109f05dd3454e0371ec1eab16530730ca2e53b0c3d09d858647cad7b16df00
|
Provenance
The following attestation bundles were made for pyt_crf-0.1.1.tar.gz:
Publisher:
publish.yml on id4thomas/pyt-crf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyt_crf-0.1.1.tar.gz -
Subject digest:
3ba3c2fc27ac6ae6a5636fbef8aa3a251f6b02e818d093e08e7dde01e85a6743 - Sigstore transparency entry: 2116987023
- Sigstore integration time:
-
Permalink:
id4thomas/pyt-crf@207b4446413d2527531d5fff6a0b063e177b2ff5 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/id4thomas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@207b4446413d2527531d5fff6a0b063e177b2ff5 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2209c3b88f4f3a7a479d19392f4315f5b2d779cac02e6479d4ca75ed15f87dac
|
|
| MD5 |
0eeae073b039f4d1aaa1630a64ecb7e2
|
|
| BLAKE2b-256 |
cc258a182d8dbc28ddfc11a5be060a33e5af2bf40203fba9f07aeaf9fd28cc12
|
Provenance
The following attestation bundles were made for pyt_crf-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on id4thomas/pyt-crf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyt_crf-0.1.1-py3-none-any.whl -
Subject digest:
2209c3b88f4f3a7a479d19392f4315f5b2d779cac02e6479d4ca75ed15f87dac - Sigstore transparency entry: 2116987105
- Sigstore integration time:
-
Permalink:
id4thomas/pyt-crf@207b4446413d2527531d5fff6a0b063e177b2ff5 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/id4thomas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@207b4446413d2527531d5fff6a0b063e177b2ff5 -
Trigger Event:
push
-
Statement type: