{"id":7777,"library":"tensordict-nightly","title":"TensorDict Nightly","description":"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.","status":"active","version":"2026.4.15","language":"en","source_language":"en","source_url":"https://github.com/pytorch/tensordict","tags":["pytorch","tensors","data-structure","machine-learning","reinforcement-learning","nightly"],"install":[{"cmd":"pip install tensordict-nightly","lang":"bash","label":"Latest nightly build"},{"cmd":"pip install tensordict-nightly --no-deps # for uv + PyTorch nightlies","lang":"bash","label":"Installing with uv and PyTorch nightlies"}],"dependencies":[{"reason":"Core dependency, TensorDict is built for PyTorch tensors.","package":"torch","optional":false},{"reason":"Required for certain operations or data handling.","package":"numpy","optional":true},{"reason":"Used for serialization, especially in multiprocessing/distributed settings.","package":"cloudpickle","optional":true},{"reason":"Used for version parsing and compatibility checks.","package":"packaging","optional":true},{"reason":"Backward compatibility for Python versions prior to 3.8.","package":"importlib_metadata","optional":true},{"reason":"Optional dependency for faster JSON serialization, primarily for Python < 3.13.","package":"orjson","optional":true}],"imports":[{"symbol":"TensorDict","correct":"from tensordict import TensorDict"},{"note":"The `_tensor` property on `MemoryMappedTensor` was removed in v0.11.0 and now raises a RuntimeError. Access the instance directly.","wrong":"my_memmap_tensor._tensor","symbol":"MemoryMappedTensor","correct":"from tensordict import MemoryMappedTensor"},{"symbol":"TensorDictModule","correct":"from tensordict.nn import TensorDictModule"},{"symbol":"tensorclass","correct":"from tensordict import tensorclass"}],"quickstart":{"code":"import torch\nfrom tensordict import TensorDict\n\n# Create a TensorDict with a specified batch_size\ntd = TensorDict(\n    {\"observations\": torch.randn(128, 84),\n     \"actions\": torch.randn(128, 4)},\n    batch_size=[128]\n)\n\nprint(\"Original TensorDict:\\n\", td)\nprint(\"Batch size:\", td.batch_size)\n\n# Accessing elements\nobs = td[\"observations\"]\nprint(\"\\nObservations shape:\", obs.shape)\n\n# Adding a new key\ntd[\"rewards\"] = torch.randn(128, 1)\nprint(\"\\nTensorDict after adding rewards:\\n\", td)\n\n# Moving to device\nif torch.cuda.is_available():\n    td_gpu = td.to(\"cuda\")\n    print(f\"\\nTensorDict moved to {td_gpu.device}:\\n\", td_gpu)\n\n# Slicing\nsub_td = td[:64]\nprint(\"\\nSliced TensorDict (first 64 elements):\\n\", sub_td)\nprint(\"Sliced batch size:\", sub_td.batch_size)","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to version 3.10 or higher.","message":"Python 3.9 support was dropped in TensorDict v0.11.0. Python 3.10 or newer is now required.","severity":"breaking","affected_versions":">=0.11.0"},{"fix":"Use `lock_`, `unlock_`, and `rename_key_` instead for in-place modifications, e.g., `td.lock_()` instead of `td.lock()`.","message":"Deprecated methods `lock`, `unlock`, and `rename_key` (without a trailing underscore) were removed in v0.11.0.","severity":"breaking","affected_versions":">=0.11.0"},{"fix":"Access the tensor content directly, e.g., `my_memmap_tensor` instead of `my_memmap_tensor._tensor`.","message":"The `MemoryMappedTensor._tensor` property now raises a `RuntimeError` since v0.11.0. Users should interact with the `MemoryMappedTensor` instance directly as it is a tensor subclass.","severity":"breaking","affected_versions":">=0.11.0"},{"fix":"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.","message":"From v0.10.0, lists assigned to a TensorDict will be automatically stacked by default, potentially raising a `FutureWarning`.","severity":"gotcha","affected_versions":">=0.10.0"},{"fix":"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=...))`.","message":"Calling `.to()` method on a `TensorDict` with a `dtype` argument will raise an error. The `to()` method is for device casting only.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"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.","cause":"Passing a dictionary with non-string keys (e.g., integers, tuples) to the `TensorDict` constructor or `make_tensordict` function.","error":"IndexError: tuple index out of range"},{"fix":"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.","cause":"Attempting to modify a `TensorDict` instance that has been locked (e.g., by calling `td.lock_()` or being created with `lock=True`).","error":"RuntimeError: Cannot modify locked TensorDict."},{"fix":"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.","cause":"When creating or moving a `TensorDict` to CPU with `non_blocking=True` (which is often implicit or default), data transfer might not be synchronized, leading to incorrect values if accessed immediately. This is more prevalent with non-CUDA devices.","error":"Wrong values in TensorDict with device='cpu' specified"},{"fix":"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.","cause":"These are general issues often related to specific versions of PyTorch or `tensordict`, or complex model architectures and distributed setups where `TensorDict` or `tensorclass` objects are used in state dictionaries or sampling processes.","error":"_dist_sample hasattr error OR load_state_dict fails if checkpoint lacks entries for TensorDictParams"}]}