Confidence-Gated Gradient Routing for Efficient Transformer Training
Project description
CGGR - Confidence-Gated Gradient Routing
Selective loss computation for Transformer training. Only hard tokens contribute to loss, providing actual backward pass savings.
Installation
pip install cggr
Requires CUDA + Triton.
Quick Start
from cggr import CGGRLoss
criterion = CGGRLoss(
scoring='combined', # 'entropy', 'margin', 'loss', 'combined'
selection='stratified', # 'topk', 'stratified', 'sequence_aware'
min_tokens_ratio=0.25,
warmup_steps=1000,
)
for batch in dataloader:
logits = model(input_ids)
loss = criterion(logits, targets) # Only hard tokens
loss.backward()
optimizer.step()
criterion.step()
Scoring Strategies
| Strategy | Description | Best For |
|---|---|---|
entropy |
High entropy = hard | General training |
margin |
Small top-2 margin = hard | Classification |
loss |
High loss = hard | Direct optimization |
combined |
All signals combined | Best overall |
Selection Strategies
| Strategy | Description | Benefit |
|---|---|---|
topk |
Top-k hardest tokens | Simple, fast |
stratified |
Sample from difficulty buckets | Prevents forgetting |
sequence_aware |
Ensure coverage per sequence | Preserves structure |
Dynamic Thresholding
Automatically adjusts token ratio based on batch confidence:
- Low confidence → more tokens (model is learning)
- High confidence → fewer tokens (model has converged)
CGGRLoss(dynamic_threshold=True, threshold_sensitivity=0.5)
Full API
CGGRLoss(
# Scoring
scoring='combined',
# Selection
selection='topk',
num_strata=4, # For stratified
min_tokens_per_sequence=1, # For sequence_aware
# Thresholding
dynamic_threshold=True,
threshold_sensitivity=0.5,
# Curriculum
min_tokens_ratio=0.25,
warmup_steps=1000,
)
Performance
| Config | Backward FLOPs | Overhead |
|---|---|---|
| Standard Loss | 100% | 0% |
| CGGR (25% tokens) | ~25% | ~0% |
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
cggr-0.4.0.tar.gz
(8.4 kB
view details)
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
cggr-0.4.0-py3-none-any.whl
(9.0 kB
view details)
File details
Details for the file cggr-0.4.0.tar.gz.
File metadata
- Download URL: cggr-0.4.0.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7b6ad0cd48473c1fde09a45c5c81ab13a6b37b3bcee05e0d8a8eca75a0c2f6a
|
|
| MD5 |
81cfc5555f74a7c6f15d6fb17401b703
|
|
| BLAKE2b-256 |
1a0533c2d9d42c99ea78ff68d22e780f85989f9938133a2a602b9a9cdbc38098
|
File details
Details for the file cggr-0.4.0-py3-none-any.whl.
File metadata
- Download URL: cggr-0.4.0-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f70a8e5664327a9d257d244f63fbbae1229bd75d0103d99cd3d5f32a5d464196
|
|
| MD5 |
bf149fa7ed8136bf76088a7afb66a63d
|
|
| BLAKE2b-256 |
d5b01e5a34fb682dad7891346cf3eb3894c96cd2254dc4b84dc2000122278255
|