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().

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.1.0.tar.gz (5.4 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.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cggr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4a2b12795674f4b37e49df159b5d0d8d383722e1dbb465c7c459968dc38f6416
MD5 291e374bbe83b41d9cba7be31d57c7ba
BLAKE2b-256 39dce8e617e5041fdb2939e775e7c049b1141819c35e0782ba1c95666244808e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cggr-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 97712b1b90ce141c08a889ac88a5bc78a7271afcd155b893976198dfd8223303
MD5 0e1cdbccaeb2e4873d21829737c66eb8
BLAKE2b-256 c5fb4cdd5e918823336fafdffd38ebc43f0c79e5308ee621b1f9baf4b43e4865

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