Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Guidelines for modifications:
* Stefan Van de Mosselaer
* Stephan Pleines
* Sujal
* Sylvester Kaczmarek
* Tiffany Chen
* Trushant Adeshara
* Tsz Ki GAO
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fixed nested dictionary backend conversion when the requested array types are supplied as a one-shot iterable.
5 changes: 4 additions & 1 deletion source/isaaclab/isaaclab/utils/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,15 @@ def convert_dict_to_backend(
# Define the conversion functions for each backend.
tensor_type_conversions = TENSOR_TYPE_CONVERSIONS[backend]

# Collect validated selections for recursion without exhausting an invalid iterator.
selected_types = []
# Parse the array types and convert them to the corresponding types: "numpy" -> np.ndarray, etc.
parsed_types = []
for t in array_types:
# Check type is valid.
if t not in TENSOR_TYPES:
raise ValueError(f"Unknown array type: '{t}'. Supported array types are 'numpy', 'torch', and 'warp'.")
selected_types.append(t)
# Exclude types that match the backend, since we do not need to convert these.
if t == backend:
continue
Expand All @@ -272,7 +275,7 @@ def convert_dict_to_backend(
output_dict[key] = tensor_type_conversions[data_type](value)
# -- nested dictionaries
elif isinstance(value, dict):
output_dict[key] = convert_dict_to_backend(value, backend=backend, array_types=array_types)
output_dict[key] = convert_dict_to_backend(value, backend=backend, array_types=selected_types)
# -- everything else
else:
output_dict[key] = value
Expand Down
32 changes: 26 additions & 6 deletions source/isaaclab/test/utils/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,34 @@ def test_update_class_from_dict_restores_enums_from_values():
assert all(isinstance(el, _Flavors) for el in cfg.cone)


def test_nested_conversion_preserves_requested_backend():
"""Nested conversions should preserve the backend selected by the caller."""
data = {"outer": {"values": np.array([1.0, 2.0, 3.0], dtype=np.float32)}}
@pytest.mark.parametrize("type_selection", [tuple, iter], ids=["tuple", "iterator"])
def test_nested_conversion_preserves_requested_backend(type_selection):
"""Every branch must use the caller's original conversion selection."""
data = {
"values": np.array([1.0], dtype=np.float32),
"outer": {"values": np.array([2.0], dtype=np.float32)},
"sibling": {"inner": {"values": np.array([3.0], dtype=np.float32)}},
}

converted = dict_utils.convert_dict_to_backend(data, backend="torch", array_types=type_selection(("numpy",)))

for value, expected in (
(converted["values"], 1.0),
(converted["outer"]["values"], 2.0),
(converted["sibling"]["inner"]["values"], 3.0),
):
assert isinstance(value, torch.Tensor)
torch.testing.assert_close(value, torch.tensor([expected]))


converted = dict_utils.convert_dict_to_backend(data, backend="torch", array_types=("numpy",))
def test_conversion_rejects_unknown_type_before_advancing_iterator() -> None:
"""An invalid selection must raise ValueError before reading later entries."""
def selections():
yield "unknown"
raise AssertionError("Invalid selection should stop iteration")

assert isinstance(converted["outer"]["values"], torch.Tensor)
torch.testing.assert_close(converted["outer"]["values"], torch.tensor([1.0, 2.0, 3.0]))
with pytest.raises(ValueError, match="Unknown array type"):
dict_utils.convert_dict_to_backend({}, array_types=selections())


def test_slice_round_trip_preserves_tuple_containers():
Expand Down