tensor_ops
tensor_ops
¶
Tensor container utilities used across VersatIL.
dict_apply
¶
Apply a transform to every leaf in a nested dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
NestedDictionary[LeafInput]
|
Nested dictionary whose leaves should be transformed. |
required |
transform
|
Callable[[LeafInput], LeafOutput]
|
Function applied to each leaf value. |
required |
Returns:
| Type | Description |
|---|---|
NestedDictionary[LeafOutput]
|
Nested dictionary with the same keys and transformed leaf values. |
Source code in src/versatil/common/tensor_ops.py
tensor_to_str
¶
Format a tensor as a compact numeric list for logs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Tensor to format. Scalars are treated as one-element tensors. |
required |
Returns:
| Type | Description |
|---|---|
str
|
String with each value formatted to three significant digits. |
Source code in src/versatil/common/tensor_ops.py
recursive_dict_list_tuple_apply
¶
Apply type-specific handlers to leaves inside nested containers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
TensorTree
|
Tensor tree containing dictionaries, sequences, and supported leaves. |
required |
type_handler_map
|
TensorTreeHandlerMap
|
Mapping from leaf type to its transform function. |
required |
Returns:
| Type | Description |
|---|---|
TensorTree
|
Tensor tree with handlers applied to matching leaves. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a container type is passed as a leaf handler. |
NotImplementedError
|
If a leaf value has no matching handler. |
Source code in src/versatil/common/tensor_ops.py
to_device
¶
Move every tensor in a nested container to a device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
TensorTree
|
Tensor tree containing tensors and pass-through string or None leaves. |
required |
device
|
device | str
|
Destination device. |
required |
Returns:
| Type | Description |
|---|---|
TensorTree
|
Tensor tree with all tensor leaves moved to the destination device. |