Skip to main content

Confidence-Gated Gradient Routing for Efficient Transformer Training

Project description

Confidence-Gated Gradient Routing (CGGR)

CGGR is a PyTorch library that implements Multi-Stage Gradient Routing for Transformer models. It selectively stops gradient backpropagation for tokens based on their confidence and entropy, effectively reducing the compute required for the backward pass.

Mechanism

Gradient Routing

The library identifies tokens as "easy" or "hard" based on a difficulty score derived from the model's output logits. $$D_t = \text{Entropy}(P_t) - \text{Confidence}(P_t)$$

  • Hard Tokens: Gradients propagate through all layers.
  • Easy Tokens: Gradients are blocked at earlier layers (e.g., top 25% or 50% of the model).

This is achieved by registering backward hooks on specific layers that multiply gradients by a binary mask (0 or 1).

Multi-Stage Bucketing

Tokens are sorted by difficulty and assigned to "buckets" which correspond to different gradient depths.

Bucket Difficulty Gradient Depth
0 Highest 100% (All Layers)
1 Medium-High 75%
2 Medium-Low 50%
3 Lowest 25%

Curriculum Learning

To ensure training stability, the system uses a linear warmup. Initially, all tokens are processed at full depth. Over a specified number of warmup_steps, the system gradually enables the shallower gradient paths.

Usage

The CGGRWrapper class wraps an existing PyTorch module. It automatically detects the Transformer layer stack and injects the necessary hooks.

import torch
from cggr import CGGRWrapper
from my_model import MyTransformer

# 1. Initialize your standard model
model = MyTransformer(...)

# 2. Wrap with CGGR
# num_buckets: Number of depth tiers (e.g., 4)
# warmup_steps: Number of steps to reach target efficiency
model = CGGRWrapper(model, num_buckets=4, warmup_steps=1000)

# 3. Training Loop
optimizer = torch.optim.Adam(model.parameters())

for batch in dataloader:
    # Forward pass
    logits = model(input_ids) 
    loss = criterion(logits, targets)
    
    # Backward pass with dynamic routing
    loss.backward() 
    
    optimizer.step()
    
    # Update curriculum state
    model.step()

Configuration Options

  • num_buckets: Number of efficiency tiers.
  • warmup_steps: Length of the curriculum warmup phase.
  • leak_rate: (Optional) Allows a fraction of gradients to pass through stopped paths. Default is 0.0.

Persistence

The wrapper registers a buffer for the step count, so the curriculum state is automatically saved and loaded with model.state_dict().

Triton Acceleration

CGGR uses fused Triton kernels for maximum performance:

  • 3x faster difficulty scoring: Single kernel for softmax → entropy → confidence
  • O(n) bucket assignment: Percentile-based instead of O(n log n) sort
  • Fused gradient masking: Custom autograd with Triton backward
pip install cggr

[!IMPORTANT] Triton is required. CGGR is designed for CUDA training only.

Compatibility

CGGR is designed for Transformer-based LLMs (Llama, Mistral, GPT), but it was specifically engineered to work best with SRDE (Sparse Routed Delta Experts).

  • SRDE Optimization: When combined with SRDE, CGGR enables "Double Sparsity", sparsifying both the forward pass (via MoE routing) and the backward pass (via Gradient Routing). This combination yields the theoretical maximum training efficiency.
  • Dense Models: Works out of the box for standard Transformers.
  • Requirements: CUDA GPU + Triton. CPU training not supported.

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.2.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

cggr-0.2.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file cggr-0.2.0.tar.gz.

File metadata

  • Download URL: cggr-0.2.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for cggr-0.2.0.tar.gz
Algorithm Hash digest
SHA256 beb85471c71f5c3b40e0077319b46484943358c308decd8de35e6c8e6c7f0b33
MD5 3497c91807b65503dced607419ef262b
BLAKE2b-256 75a07024b52befc0bd2ca031b209bdfaef121ff75910ccbd7955a0d968f7e1ca

See more details on using hashes here.

File details

Details for the file cggr-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cggr-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for cggr-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2c9f88b07a4d32045d1a22af0e06aaf6a9803158d829bf5f500bc77fa883563
MD5 b7e6c1fed146fb4a0d603a31be1d9b1a
BLAKE2b-256 10e7d809227fb9dd61024455d0bae81be2fe32c98a9255d1caa46f203913d134

See more details on using hashes here.

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