set_capture_non_tensor_stack#
- class tensordict.set_capture_non_tensor_stack(mode: bool)#
Sets whether stacking identical non-tensor data gives one value instead of a stack.
By default,
torch.stack()ofNonTensorDatareturns aNonTensorStack, even when all the values are equal. Withmode=True, a stack of equal values gives a singleNonTensorData. TheCAPTURE_NONTENSOR_STACKenvironment variable sets the mode at import. Use it as a context manager or a decorator, or callset_capture_non_tensor_stack(mode).set()to change the mode for the rest of the program.Deprecated since version 0.15: Capturing identical non-tensor data (
set_capture_non_tensor_stack(True)orCAPTURE_NONTENSOR_STACK=1) is deprecated and will be removed in TensorDict 0.17. UseNonTensorStack.datato get the single value of a stack of identical values.set_capture_non_tensor_stack(False), the default, does not warn, and will keep working, doing nothing, after the removal.- Parameters:
mode (bool) – if
True, stacking identical non-tensor data gives one value, and the call emits aDeprecationWarning.
Examples
>>> import torch >>> from tensordict import NonTensorData, set_capture_non_tensor_stack >>> with set_capture_non_tensor_stack(False): ... stack = torch.stack([NonTensorData("a"), NonTensorData("a")]) >>> type(stack).__name__, stack.data ('NonTensorStack', 'a')