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.
Why CGGR?
| Metric | Standard Training | CGGR (Batch Split) | Benefit |
|---|---|---|---|
| Backward Pass | 100% of tokens | 25% of tokens | 4x cheaper backward pass |
| Forward Pass | 1.0x cost | ~1.1x cost (Pass 1 + 2) | Negligible overhead (~9ms) |
| Total Speed | 1.0x (Baseline) | 1.4x - 2.0x faster | Significant training acceleration |
| Data Efficiency | Learns from all tokens | Prioritizes hard tokens | Learns faster from hard examples |
| Memory | High (full graph) | Lower (sparse graph) | Can increase batch size |
Benchmarks
Hardware: RTX 3060
Model: SmolLM-135M (Llama architecture)
Dataset: FineWeb-Edu
| Configuration | Forward (ms) | Backward (ms) | Total Step (ms) | Speedup |
|---|---|---|---|---|
| Standard Training | 118 ms | 185 ms | 309 ms | 1.0x |
| CGGR (Optimized) | 127 ms | 93 ms | 220 ms | 1.40x |
Quick Start
1. Batch Splitting (Recommended)
The most efficient way to use CGGR is via CGGRModel. It uses a lightweight router to score difficulty and only computes gradients for hard tokens.
from cggr import CGGRModel, create_truncated_router
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("...").cuda()
# Create lightweight router (shares weights, 0 extra memory)
router = create_truncated_router(model, num_layers=4)
# Wrap model
cggr_model = CGGRModel(
model,
router=router,
min_tokens_ratio=0.25
)
# Train
loss = cggr_model(input_ids, labels=labels)
loss.backward()
2. Manual Integration (CGGRLoss)
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
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 cggr-0.4.2.tar.gz.
File metadata
- Download URL: cggr-0.4.2.tar.gz
- Upload date:
- Size: 12.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 |
97568301ac3ad214e19b8efaa5b7f2c2d8d37d5c76f26b21962ccdf8b981d45b
|
|
| MD5 |
4b321943a1a50f03718208de0c087209
|
|
| BLAKE2b-256 |
9925cc40679fc7140a2617a453f2faad973cb5fbe64e2c08c73a3497a4552481
|
File details
Details for the file cggr-0.4.2-py3-none-any.whl.
File metadata
- Download URL: cggr-0.4.2-py3-none-any.whl
- Upload date:
- Size: 10.6 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 |
2e48604f02d28685a228b2c891bf2a39b7a2250047d2f4437b9be1f224673d6f
|
|
| MD5 |
c01ccc57743fae2f476a51549edfd7be
|
|
| BLAKE2b-256 |
63b71d38a7fb628edfb17f7b161d41d401369bbed352ef9ec319f3e219afda8e
|