Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Tensor Container

Tensor containers for PyTorch with PyTree compatibility and torch.compile optimization

Python 3.9+ License: MIT PyTorch

⚠️ Academic Research Project: This project exists solely for academic purposes to explore and learn PyTorch internals. For production use, please use the official, well-maintained torch/tensordict library.

Tensor Container provides efficient, type-safe tensor container implementations for PyTorch workflows. It includes PyTree integration and torch.compile optimization for batched tensor operations.

The library includes tensor containers, probabilistic distributions, and batch/event dimension semantics for machine learning workflows.

Table of Contents

Installation

From Source (Development)

# Clone the repository
git clone https://github.com/mctigger/tensor-container.git
cd tensor-container

# Install in development mode
pip install -e .

# Install with development dependencies
pip install -e .[dev]

Requirements

  • Python 3.9+
  • PyTorch 2.0+

Quick Start

TensorDict: Dictionary-Style Containers

import torch
from tensorcontainer import TensorDict

# Create a TensorDict with batch semantics
data = TensorDict({
    'observations': torch.randn(32, 128),
    'actions': torch.randn(32, 4),
    'rewards': torch.randn(32, 1)
}, shape=(32,), device='cpu')

# Dictionary-like access
obs = data['observations']
data['new_field'] = torch.zeros(32, 10)

# Batch operations work seamlessly
stacked_data = torch.stack([data, data])  # Shape: (2, 32)

TensorDataClass: Type-Safe Containers

import torch
from tensorcontainer import TensorDataClass

class RLData(TensorDataClass):
    observations: torch.Tensor
    actions: torch.Tensor
    rewards: torch.Tensor

# Create with full type safety and IDE support
data = RLData(
    observations=torch.randn(32, 128),
    actions=torch.randn(32, 4),
    rewards=torch.randn(32, 1),
    shape=(32,),
    device='cpu'
)

# Type-safe field access with autocomplete
obs = data.observations
data.actions = torch.randn(32, 8)  # Type-checked assignment

TensorDistribution: Probabilistic Containers

import torch
from tensorcontainer import TensorDistribution

# Built-in distribution types
from tensorcontainer.tensor_distribution import (
    TensorNormal, TensorBernoulli, TensorCategorical,
    TensorTruncatedNormal, TensorTanhNormal
)

# Create probabilistic tensor containers
normal_dist = TensorNormal(
    loc=torch.zeros(32, 4),
    scale=torch.ones(32, 4),
    shape=(32,),
    device='cpu'
)

# Sample and compute probabilities
samples = normal_dist.sample()  # Shape: (32, 4)
log_probs = normal_dist.log_prob(samples)
entropy = normal_dist.entropy()

# Categorical distributions for discrete actions
categorical = TensorCategorical(
    logits=torch.randn(32, 6),  # 6 possible actions
    shape=(32,),
    device='cpu'
)

PyTree Operations

# All containers work seamlessly with PyTree operations
import torch.utils._pytree as pytree

# Transform all tensors in the container
doubled_data = pytree.tree_map(lambda x: x * 2, data)

# Combine multiple containers
combined = pytree.tree_map(lambda x, y: x + y, data1, data2)

Features

  • torch.compile Optimized: Compatible with PyTorch's JIT compiler
  • PyTree Support: Integration with torch.utils._pytree for tree operations
  • Zero-Copy Operations: Efficient tensor sharing and manipulation
  • Type Safety: Static typing support with IDE autocomplete and type checking
  • Batch Semantics: Consistent batch/event dimension handling
  • Shape Validation: Automatic validation of tensor shapes and device consistency
  • Multiple Container Types: Different container types for different use cases
  • Probabilistic Support: Distribution containers for probabilistic modeling
  • Comprehensive Testing: Extensive test suite with compile compatibility verification
  • Memory Efficient: Optimized memory usage with slots-based dataclasses

API Overview

Core Components

  • TensorContainer: Base class providing core tensor manipulation operations with batch/event dimension semantics
  • TensorDict: Dictionary-like container for dynamic tensor collections with nested structure support
  • TensorDataClass: DataClass-based container for static, typed tensor structures
  • TensorDistribution: Distribution wrapper for probabilistic tensor operations

Key Concepts

  • Batch Dimensions: Leading dimensions defined by the shape parameter, consistent across all tensors
  • Event Dimensions: Trailing dimensions beyond batch shape, can vary per tensor
  • PyTree Integration: All containers are registered PyTree nodes for seamless tree operations
  • Device Consistency: Automatic validation ensures all tensors reside on compatible devices
  • Unsafe Construction: Context manager for performance-critical scenarios with validation bypass

torch.compile Compatibility

Tensor Container is designed for torch.compile compatibility:

@torch.compile
def process_batch(data: TensorDict) -> TensorDict:
    # PyTree operations compile efficiently
    return TensorContainer._tree_map(lambda x: torch.relu(x), data)

@torch.compile
def sample_and_score(dist: TensorNormal, actions: torch.Tensor) -> torch.Tensor:
    # Distribution operations are compile-safe
    return dist.log_prob(actions)

# All operations compile efficiently with minimal graph breaks
compiled_result = process_batch(tensor_dict)
log_probs = sample_and_score(normal_dist, action_tensor)

The testing framework includes compile compatibility verification to ensure operations work efficiently under JIT compilation, including:

  • Graph break detection and minimization
  • Recompilation tracking
  • Memory leak prevention
  • Performance benchmarking

Contributing

Contributions are welcome! Tensor Container is a learning project for exploring PyTorch internals and tensor container implementations.

Development Setup

# Clone and install in development mode
git clone https://github.com/mctigger/tensor-container.git
cd tensor-container
pip install -e .[dev]

Running Tests

# Run all tests with coverage
pytest --strict-markers --cov=src

# Run specific test modules
pytest tests/tensor_dict/test_compile.py
pytest tests/tensor_dataclass/
pytest tests/tensor_distribution/

# Run compile-specific tests
pytest tests/tensor_dict/test_graph_breaks.py
pytest tests/tensor_dict/test_recompilations.py

Development Guidelines

  • All new features must maintain torch.compile compatibility
  • Comprehensive tests required, including compile compatibility verification
  • Follow existing code patterns and typing conventions
  • Distribution implementations must support KL divergence registration
  • Memory efficiency considerations for large-scale tensor operations
  • Unsafe construction patterns for performance-critical paths

Contribution Process

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes with appropriate tests
  4. Ensure all tests pass and maintain coverage
  5. Submit a pull request with a clear description

Documentation

The project includes documentation:

  • docs/compatibility.md: Python version compatibility guide and best practices
  • docs/testing.md: Testing philosophy, standards, and guidelines
  • Source Code Documentation: Extensive docstrings and type annotations throughout the codebase
  • Test Coverage: 643+ tests covering all major functionality with 86% code coverage

License

This project is licensed under the MIT License - see the LICENSE file for details.

Authors

Contact and Support


Tensor Container is an academic research project for learning PyTorch internals and tensor container patterns. For production applications, we strongly recommend using the official torch/tensordict library, which is actively maintained by the PyTorch team.

Metadata

Release files for tensorcontainer 0.5.1.dev20250719184726

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tensorcontainer 0.5.1.dev20250719184726
File Size Uploaded Provenance Actions
tensorcontainer-0.5.1.dev20250719184726.tar.gz 31.4 kB With provenance Details for tensorcontainer-0.5.1.dev20250719184726.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tensorcontainer 0.5.1.dev20250719184726
File Interpreter ABI Platform Actions
tensorcontainer-0.5.1.dev20250719184726-py3-none-any.whl Python 3 none any Details for tensorcontainer-0.5.1.dev20250719184726-py3-none-any.whl

Total release size: 61.2 kB

Release files / tensorcontainer-0.5.1.dev20250719184726.tar.gz

Download URL tensorcontainer-0.5.1.dev20250719184726.tar.gz
Size 31.4 kB
Tags Source
SHA-256 checksum
How to use checksums
11a4afe52a3d005da26e4fb774f20a950c2139dae9a17a3dc0c88f65f62dacec
BLAKE2b-256 checksum
How to use checksums
253382dc929b9e28967bf741db87098532d91f84f4eb187518a43fe9af8f520f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 19, 2025.

Transparency log

Release files / tensorcontainer-0.5.1.dev20250719184726-py3-none-any.whl

Download URL tensorcontainer-0.5.1.dev20250719184726-py3-none-any.whl
Size 29.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
177eb9dc9630fac13dff1eaed41aeed54833f179b5d4dbc74407a8fca3a20231
BLAKE2b-256 checksum
How to use checksums
cd91ac96b09915a5895c1d06b3c7f444b4af370b8060c9b74e46ab21e3e4cb30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 19, 2025.

Transparency log

Security for tensorcontainer 0.5.1.dev20250719184726

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tensorcontainer-0.5.1.dev20250719184726.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 19, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page