Skip to content

TensorDict API Additions and Indexing Semantic Fixes - #29

Merged
mctigger merged 7 commits into
mainfrom
fix-setitem
Feb 25, 2026
Merged

TensorDict API Additions and Indexing Semantic Fixes#29
mctigger merged 7 commits into
mainfrom
fix-setitem

Conversation

@mctigger

@mctigger mctigger commented Feb 25, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds new TensorDict convenience APIs for structural and key-level transformations: like, select, exclude, apply, and rename.
  • Fixes TensorContainer indexing and assignment behavior to better align with torch.Tensor semantics, including non-tuple indexing, ellipsis handling, and zero-dim indexing behavior.
  • Strengthens type safety and compatibility around indexing (IndexType, EllipsisType) and expands test coverage for indexing and key operations.

What Changed

TensorContainer indexing/setitem fixes

  • Refined __getitem__/__setitem__ indexing flow to consistently support torch-style index patterns.
  • Removed scalar-promotion behavior in slice assignment paths that caused incorrect __setitem__ semantics.
  • Improved error context for assignment failures by reporting key/index/shape details.
  • Added compatibility type definitions for indexing, including explicit EllipsisType support for Python 3.9.

TensorDict API additions

  • Added:
    • like(data, device=None) to build a new instance preserving shape/device defaults.
    • select(*keys) to keep only selected top-level keys.
    • exclude(*keys) to drop selected top-level keys.
    • apply(fn) to transform all leaf tensors recursively.
    • rename(mapping) to rename top-level keys with duplicate/missing-key validation.

Tests and docs

  • Added dedicated tests for new APIs:
    • tests/tensor_dict/test_like.py
    • tests/tensor_dict/test_select.py
    • tests/tensor_dict/test_exclude.py
    • tests/tensor_dict/test_apply.py
    • tests/tensor_dict/test_rename.py
  • Expanded/refactored indexing tests:
    • tests/tensor_dict/test_getitem.py
    • tests/tensor_dict/test_setitem.py
  • Added/expanded docstrings and examples for indexing behavior in TensorContainer.
  • Updated package version metadata in pyproject.toml for the fix release.

Impact

  • Improves correctness and predictability of indexing/assignment operations.
  • Expands ergonomic TensorDict manipulation APIs while preserving shape/device invariants.
  • Reduces risk of regressions via substantially broader test coverage.

Tim Joseph added 4 commits February 12, 2026 17:25
Handle scalar, None, and boolean indexing like PyTorch tensors and add
parity tests to prevent regressions.
Consolidate and reorganize `TensorDict.__getitem__` tests into logical classes.
This improves readability, maintainability, and ensures comprehensive coverage of indexing behaviors.

Key changes include:
- Grouping tests by functionality: `TestGetitemBasic`, `TestGetitemIsolation`, `TestGetitemZeroDim`, `TestGetitemSlicing`, `TestGetitemErrors`.
- Introducing `zero_dim_td` fixture for consistent 0-dim TensorDict testing.
- Parameterizing boolean and boolean tensor indexing tests for 0-dim TensorDicts.
- Parameterizing slicing tests with a comprehensive list of `ALL_GETITEM_INDICES`.
- Adding new tests for nested batch shape preservation and single-dimension container indexing.
- Ensuring `assert_tensor_indexing_parity` includes detailed error messages.
Introduce five new TensorDict convenience methods for common container
transformations: creating a shape/device-compatible instance (`like`),
including keys (`select`), excluding keys (`exclude`), applying tensor
functions recursively (`apply`), and top-level key remapping (`rename`).

Preserve shape, device, and subclass behavior across these operations and
add validation for missing keys and duplicate rename targets to keep
errors explicit and predictable.

Add dedicated test suites for each new method, including compiled-path
checks where applicable, and expand getitem coverage with additional
multidimensional/edge-case indices plus a tracked xfail for ellipsis
event-dimension indexing parity.
@mctigger mctigger changed the title Fix setitem TensorDict API Additions and Indexing Semantic Fixes Feb 25, 2026
Tim Joseph added 3 commits February 25, 2026 15:14
@mctigger
mctigger merged commit 77ec694 into main Feb 25, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant