Skip to content

#1920 activation sparsity + compression #2076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ved1beta
Copy link

Activation Compression Module for PyTorch AO

Overview

This PR introduces a new activation compression module for PyTorch AO that enables efficient training of large neural networks by compressing sparse activations. The module provides significant memory savings while maintaining model accuracy.

Key Features

  • Multiple compression methods:
    • simple: Basic compression storing non-zero values and indices
    • block: Block-based compression for better cache efficiency
    • run_length: Run-length encoding for sequences of zeros
  • Memory-efficient training with automatic compression/decompression
  • Comprehensive error handling and validation
  • Memory usage tracking and statistics
  • Seamless integration with existing PyTorch models

How It Works

Core Components

  1. ActivationCompressor

    • Handles the compression and decompression of sparse tensors
    • Supports multiple compression methods
    • Tracks memory usage
    • Validates input tensors and compression methods
  2. CompressedActivation

    • PyTorch module that manages activation compression during training
    • Automatically compresses activations in forward pass
    • Handles proper gradient computation in backward pass
    • Maintains sparsity patterns in gradients

Usage Example

import torch
from ao.sparsity import CompressedActivation

# Create a compressed activation layer
compressed_layer = CompressedActivation(compression_method='block')

# Use in your model
class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(512, 2048)
        self.compressed = CompressedActivation()
        self.fc2 = nn.Linear(2048, 512)
    
    def forward(self, x):
        x = self.fc1(x)
        x = self.compressed(x)  # Activations are automatically compressed
        x = self.fc2(x)
        return x

Compression Methods

  1. Simple Compression

    • Stores non-zero values and their indices
    • Best for general use cases
    • Minimal overhead
  2. Block Compression

    • Groups values into blocks for better cache efficiency
    • Ideal for structured sparsity patterns
    • Configurable block size
  3. Run-Length Encoding

    • Efficient for sequences with many zeros
    • Best for highly sparse activations
    • Minimal memory overhead

Performance Benefits

  • Significant memory savings during training
  • Minimal impact on model accuracy
  • Efficient compression/decompression operations
  • Automatic sparsity-aware gradient computation

Testing

The module includes comprehensive tests for:

  • Compression/decompression correctness
  • Memory usage tracking
  • Gradient computation
  • Different compression methods
  • Edge cases and error handling

Documentation

  • Detailed docstrings for all classes and methods
  • Usage examples and best practices
  • Performance benchmarks
  • Integration guidelines

Dependencies

  • PyTorch >= 1.8.0
  • No additional dependencies required

Checklist

  • Code follows the project's style guidelines
  • Tests have been added/updated
  • Documentation has been updated
  • Performance benchmarks included
  • No breaking changes introduced

Copy link

pytorch-bot bot commented Apr 18, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/2076

Note: Links to docs will display an error until the docs builds have been completed.

❌ 8 New Failures

As of commit 93785f4 with merge base a61e302 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 18, 2025
@supriyar
Copy link
Contributor

cc @jcaip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants