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
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 (dict, dataclass) and distributions (torch.distributions equivalent).
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, TensorContainer lets you treat complex data as unified entities that behave just like regular tensors.
Core Benefits
- Unified Operations: Apply tensor operations like
view(),permute(),detach(), and device transfers to entire data structures - Drop-in Compatibility: Seamless integration with existing PyTorch workflows and
torch.compile - Zero Boilerplate: Eliminate manual parameter handling and type-specific operations
- Type Safety: Full IDE support with static typing and autocomplete
data = TensorDict(
{"a": torch.rand(24), "b": torch.rand(24)},
shape=(24,),
device="cpu"
)
# Single operation transforms entire structure
data = data.view(2, 3, 4).permute(1, 0, 2).to('cuda').detach()
Key Features
- ⚡ JIT Compilation: Designed for
torch.compilewithfullgraph=True, minimizing graph breaks and maximizing performance - 📐 Batch/Event Semantics: Clear distinction between batch dimensions (consistent across tensors) and event dimensions (tensor-specific)
- 🔄 Device Management: Move entire structures between CPU/GPU with single operations and flexible device compatibility
- 🔒 Type Safety: Full IDE support with static typing and autocomplete
- 🏗️ Multiple Container Types: Three specialized containers for different use cases:
TensorDictfor dynamic, dictionary-style data collectionsTensorDataClassfor type-safe, dataclass-based structuresTensorDistributionfor probabilistic modeling with 40+ probability distributions
- 🔧 Advanced Operations: Full PyTorch tensor operations support including
view(),permute(),stack(),cat(), and more - 🎯 Advanced Indexing: Complete PyTorch indexing semantics with boolean masks, tensor indices, and ellipsis support
- 📊 Shape Validation: Automatic verification of tensor compatibility with detailed error messages
- 🌳 Nested Structure Support: Create nested structure with different TensorContainers
Table of Contents
- What is TensorContainer?
- Installation
- Quick Start
- Features
- API Overview
- torch.compile Compatibility
- Examples
- Contributing
- Documentation
- License
- Authors
- Contact and Support
Installation
Using pip
pip install tensorcontainer
Requirements
- Python 3.9+
- PyTorch 2.6+
Quick Start
TensorContainer transforms how you work with structured tensor data. Instead of managing individual tensors, you can treat entire data structures as unified entities that behave like regular tensors.
# Single operation transforms entire structure
data = data.view(2, 3, 4).permute(1, 0, 2).to('cuda').detach()
1. TensorDict: Dynamic Data Collections
Perfect for reinforcement learning data and dynamic collections:
import torch
from tensorcontainer import TensorDict
# Create a container for RL training data
data = TensorDict({
'observations': torch.randn(32, 128),
'actions': torch.randn(32, 4),
'rewards': torch.randn(32, 1)
}, shape=(32,))
# Dictionary-like access with tensor operations
obs = data['observations']
data['advantages'] = torch.randn(32, 1) # Add new fields dynamically
# Batch operations work seamlessly
batch = torch.stack([data, data]) # Shape: (2, 32)
2. TensorDataClass: Type-Safe Structures
Ideal for model inputs and structured data with compile-time safety:
import torch
from tensorcontainer import TensorDataClass
class ModelInput(TensorDataClass):
features: torch.Tensor
labels: torch.Tensor
# Create with full type safety and IDE support
batch = ModelInput(
features=torch.randn(32, 64, 784),
labels=torch.randint(0, 10, (32, 64)),
shape=(32, 64)
)
# Unified operations on entire structure - reshape all tensors at once
batch = batch.view(2048)
# Type-safe access with autocomplete works on reshaped data too
loss = torch.nn.functional.cross_entropy(batch.features, batch.labels)
3. TensorDistribution: Probabilistic Modeling
Streamline probabilistic computations in reinforcement learning and generative models:
import torch
from tensorcontainer.tensor_distribution import TensorNormal
normal = TensorNormal(
loc=torch.zeros(100, 4),
scale=torch.ones(100, 4)
)
# With torch.distributions we need to extract the parameters, detach them
# and create a new Normal distribution. With TensorDistribution we just call
# .detach() on the distribution. We can also apply other tensor operations,
# such as .view()!
detached_normal = normal.detach()
Documentation
The project includes comprehensive documentation:
docs/user_guide/overview.md: Complete user guide with examples and best practicesdocs/developer_guide/compatibility.md: Python version compatibility guide and best practicesdocs/developer_guide/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.8.0.dev20250910084102
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.8.0.dev20250910084102.tar.gz | 54.1 kB | With provenance | Details for tensorcontainer-0.8.0.dev20250910084102.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tensorcontainer-0.8.0.dev20250910084102-py3-none-any.whl | Python 3 | none | any | Details for tensorcontainer-0.8.0.dev20250910084102-py3-none-any.whl |
Total release size: 132.3 kB
Release files / tensorcontainer-0.8.0.dev20250910084102.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.8.0.dev20250910084102.tar.gz |
|---|---|
| Size | 54.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
880f13512c42a2c739821820d72b2bee314f765146fdcfbda08dc4c51021247c
|
|
BLAKE2b-256 checksum How to use checksums |
52a55148d0c5940b996edf7b7800064da90addb123e2d3b96030e85660a082ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Sep 10, 2025.
Transparency logRelease files / tensorcontainer-0.8.0.dev20250910084102-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.8.0.dev20250910084102-py3-none-any.whl |
|---|---|
| Size | 78.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
679512b610cd24e1907ca0b0375f63e2f2e2db0b7e4f78d0c5907ed790293105
|
|
BLAKE2b-256 checksum How to use checksums |
90368af1294c4c5e5b4edc090c5682b3516aa00eb87b760903428933b6674e1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Sep 10, 2025.
Transparency logSecurity for tensorcontainer 0.8.0.dev20250910084102
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.8.0.dev20250910084102.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 Sep 10, 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.