Skip to content

Commit a21b89b

Browse files
author
The dataclass_array Authors
committed
Fix _ArrayField.inner_shape for scalar values in PyTorch contexts.
Corrected an edge case in `_ArrayField.inner_shape` where, when called on a scalar value (empty `full_shape`) within a PyTorch environment, it now returns an empty shape `()` to prevent shape validation errors, particularly when used inside `torch.vmap`. PiperOrigin-RevId: 855281432
1 parent 7b39334 commit a21b89b

25 files changed

Lines changed: 34 additions & 26 deletions

.github/workflows/pytest_and_autopublish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ jobs:
2222
- run: pip --version
2323
# As changes can be pushed to both etils and visu3d, we install etils from `main` branch
2424
# If modifying this, also modify `pyproject.toml`
25-
- run: pip install "etils[array_types,edc,enp,epath,epy,etree] @ git+https://github.com/google/etils"
26-
- run: pip install -e .[dev]
25+
- run: pip install --no-cache-dir "etils[array_types,edc,enp,epath,epy,etree] @ git+https://github.com/google/etils"
26+
- run: pip install --no-cache-dir -e .[dev]
2727
- run: pip freeze
2828

2929
# Run tests (in parallel)

dataclass_array/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 The dataclass_array Authors.
1+
# Copyright 2025 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/array_dataclass.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 The dataclass_array Authors.
1+
# Copyright 2025 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -1120,6 +1120,14 @@ def full_shape(self) -> DcOrArrayT:
11201120
@functools.cached_property
11211121
def inner_shape(self) -> Shape:
11221122
"""Returns the the static shape resolved for the current value."""
1123+
# torch.func.vmap calls `tree_unflatten([0] * num_leaves)` internally,
1124+
# messing up shape inference.
1125+
if (
1126+
enp.lazy.has_torch
1127+
and isinstance(self.value, int)
1128+
and self.value == 0
1129+
):
1130+
return ()
11231131
if not self.inner_shape_non_static:
11241132
return ()
11251133
static_shape = self.full_shape[-len(self.inner_shape_non_static) :]

dataclass_array/array_dataclass_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 The dataclass_array Authors.
1+
# Copyright 2025 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 The dataclass_array Authors.
1+
# Copyright 2025 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/field_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 The dataclass_array Authors.
1+
# Copyright 2025 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/import_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 The dataclass_array Authors.
1+
# Copyright 2025 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 The dataclass_array Authors.
1+
# Copyright 2025 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/shape_parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 The dataclass_array Authors.
1+
# Copyright 2025 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/shape_parsing_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 The dataclass_array Authors.
1+
# Copyright 2025 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)