The repository or configuration that created this release differ from a previous release. View details.
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
⚠️ 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.
What is TensorContainer?
TensorContainer transforms how you work with structured tensor data in PyTorch by providing tensor-like operations for entire data structures. Instead of manually managing individual tensors across devices, batch dimensions, and nested hierarchies, TensorContainer lets you treat complex data as unified entities that behave just like regular tensors.
🚀 Unified Operations Across Data Types
Apply tensor operations like view(), permute(), detach(), and device transfers to entire data structures—no matter how complex:
# Single operation transforms entire distribution
distribution = distribution.view(2, 3, 4).permute(1, 0, 2).detach()
# Works seamlessly across TensorDict, TensorDataClass, and TensorDistribution
data = data.to('cuda').reshape(batch_size, -1).clone()
🔄 Drop-in Compatibility with PyTorch
TensorContainer integrates seamlessly with existing PyTorch workflows:
- torch.distributions compatibility: TensorDistribution is API-compatible with
torch.distributionswhile adding tensor-like operations - PyTree support: All containers work with
torch.utils._pytreeoperations andtorch.compile - Zero learning curve: If you know PyTorch tensors, you already know TensorContainer
⚡ Eliminates Boilerplate Code
Compare the complexity difference:
With torch.distributions (manual parameter handling):
# Requires type-specific parameter extraction and reconstruction
if isinstance(dist, Normal):
detached = Normal(loc=dist.loc.detach(), scale=dist.scale.detach())
elif isinstance(dist, Categorical):
detached = Categorical(logits=dist.logits.detach())
# ... more type checks needed
With TensorDistribution (unified interface):
# Works for any distribution type
detached = dist.detach()
🏗️ Structured Data Made Simple
Handle complex, nested tensor structures with the same ease as single tensors:
- Batch semantics: Consistent shape handling across all nested tensors
- Device management: Move entire structures between CPU/GPU with single operations
- Shape validation: Automatic verification of tensor compatibility
- Type safety: Full IDE support with static typing and autocomplete
TensorContainer doesn't just store your data—it makes working with structured tensors as intuitive as working with individual tensors, while maintaining full compatibility with the PyTorch ecosystem you already know.
Table of Contents
- Installation
- Quick Start
- Features
- API Overview
- torch.compile Compatibility
- Contributing
- Documentation
- License
- Authors
- Contact and Support
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._pytreefor 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 semanticsTensorDict: Dictionary-like container for dynamic tensor collections with nested structure supportTensorDataClass: DataClass-based container for static, typed tensor structuresTensorDistribution: Distribution wrapper for probabilistic tensor operations
Key Concepts
- Batch Dimensions: Leading dimensions defined by the
shapeparameter, 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.compilecompatibility - 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
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes with appropriate tests
- Ensure all tests pass and maintain coverage
- Submit a pull request with a clear description
Documentation
The project includes documentation:
docs/compatibility.md: Python version compatibility guide and best practicesdocs/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
- Tim Joseph - mctigger
Contact and Support
- Issues: Report bugs and request features on GitHub Issues
- Discussions: Join conversations on GitHub Discussions
- Email: For direct inquiries, contact tim@mctigger.com
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.6.3.dev20250723205723
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Change of provenance
The repository or configuration that created these release files differ from that of a previous release.
We recommend inspecting the repository and configuration before proceeding. View details.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tensorcontainer-0.6.3.dev20250723205723.tar.gz | 52.5 kB | With provenance | Details for tensorcontainer-0.6.3.dev20250723205723.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tensorcontainer-0.6.3.dev20250723205723-py3-none-any.whl | Python 3 | none | any | Details for tensorcontainer-0.6.3.dev20250723205723-py3-none-any.whl |
Total release size: 127.0 kB
Release files / tensorcontainer-0.6.3.dev20250723205723.tar.gz
Change of provenance
This file belongs to a release with changed provenance: the repository or configuration that created the release files differ from that of a previous release.
We recommend inspecting the repository and configuration before proceeding. View details.
| Download URL | tensorcontainer-0.6.3.dev20250723205723.tar.gz |
|---|---|
| Size | 52.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c000390e3b01d134c3819c2a237b28c9ba5c81ed7e02b13a5b299c46f0a7c87f
|
|
BLAKE2b-256 checksum How to use checksums |
aa1ff1bc689e4420e404f67a82ba0be0c583746a42a77c2d9027c81680bb896a
|
| 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 23, 2025.
Transparency logRelease files / tensorcontainer-0.6.3.dev20250723205723-py3-none-any.whl
Change of provenance
This file belongs to a release with changed provenance: the repository or configuration that created the release files differ from that of a previous release.
We recommend inspecting the repository and configuration before proceeding. View details.
| Download URL | tensorcontainer-0.6.3.dev20250723205723-py3-none-any.whl |
|---|---|
| Size | 74.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
57428231537e2e02a25730373cea2d2fdf9ec2e964f2455b49dfd5932bc4b845
|
|
BLAKE2b-256 checksum How to use checksums |
7e35a3f8a387dd73ef65c731fd4f2dace3e610d4bcfad5244ca882ca9f1786e9
|
| 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 23, 2025.
Transparency logSecurity for tensorcontainer 0.6.3.dev20250723205723
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.
Change of provenance
The configuration that created this release differs from a previous release.
While this could reflect a routine change by the project maintainers, it may also indicate that the release files have been compromised.
Attestation for tensorcontainer-0.6.3.dev20250723205723.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 23, 2025.
Transparency logA 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.