TensorDict is a PyTorch-dedicated tensor container that provides a dictionary-like class inheriting properties from `torch.Tensor`. It streamlines the organization and manipulation of collections of tensors, enabling efficient batch operations, shape transformations, and seamless device management. As a nightly build, `tensordict-nightly` offers the latest features and bug fixes, with frequent updates that may introduce breaking changes.
pip install tensordict-nightlyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a `TensorDict`, access and add elements, move it to a different device (if CUDA is available), and perform basic slicing operations.
Upgrade your Python environment to version 3.10 or higher.
Use `lock_`, `unlock_`, and `rename_key_` instead for in-place modifications, e.g., `td.lock_()` instead of `td.lock()`.
Access the tensor content directly, e.g., `my_memmap_tensor` instead of `my_memmap_tensor._tensor`.
Review code that assigns lists to TensorDicts. If list stacking is not the desired default behavior or to suppress the warning, explicitly use context managers like `td.set_` or specify the desired behavior.
To change the dtype of tensors within a TensorDict, iterate through its items and apply `item.to(dtype=...)` or use `td.apply(lambda x: x.to(dtype=...))`.
Ensure all keys in the input dictionary are strings. If you need nested keys, use string keys or consider using `flatten_keys` later with a separator.
Unlock the TensorDict using `td.unlock_()` before modification, or use in-place methods with a trailing underscore (e.g., `td.set_(key, value)`) if the key already exists.
Explicitly set `non_blocking=False` when moving to CPU if immediate and synchronized access is critical, or ensure a synchronization call (`torch.cuda.synchronize()` if applicable) is made before accessing the data.
Check the `pytorch/tensordict` GitHub issues for similar reports and potential workarounds or targeted bug fixes for your specific `tensordict` and PyTorch versions. Consider updating to the latest nightly builds for potential fixes.