Skip to content

Commit 85dc26b

Browse files
committed
refacto fix zero stride columns
1 parent 4ccef3a commit 85dc26b

2 files changed

Lines changed: 32 additions & 25 deletions

File tree

src/torchjd/sparse/_structured_sparse_tensor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,14 +424,14 @@ def make_sst(physical: Tensor, v_to_ps: list[list[int]]) -> StructuredSparseTens
424424
return StructuredSparseTensor(physical, v_to_ps)
425425

426426

427-
def clear_null_stride_columns(physical: Tensor, strides: Tensor) -> tuple[Tensor, Tensor]:
427+
def fix_zero_stride_columns(physical: Tensor, strides: Tensor) -> tuple[Tensor, Tensor]:
428428
"""Remove columns of strides that are all 0 and sum the corresponding elements in the physical tensor."""
429-
all_zero_columns = (strides == 0).all(dim=0)
429+
are_columns_zero = (strides == 0).all(dim=0)
430430

431-
if not (all_zero_columns).any():
431+
if not (are_columns_zero).any():
432432
return physical, strides
433433

434-
all_zero_columns_indices = all_zero_columns.nonzero().flatten().tolist()
435-
physical = physical.sum(dim=all_zero_columns_indices)
436-
strides = strides[:, ~all_zero_columns]
434+
zero_column_indices = torch.arange(len(are_columns_zero))[are_columns_zero].tolist()
435+
physical = physical.sum(dim=zero_column_indices)
436+
strides = strides[:, ~are_columns_zero]
437437
return physical, strides

tests/unit/sparse/test_structured_sparse_tensor.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import torch
22
from pytest import mark
3+
from torch import Tensor, tensor
34
from torch.ops import aten # type: ignore
45
from torch.testing import assert_close
56
from utils.tensors import randn_, tensor_, zeros_
@@ -12,9 +13,9 @@
1213
from torchjd.sparse._aten_function_overrides.shape import unsquash_pdim
1314
from torchjd.sparse._structured_sparse_tensor import (
1415
StructuredSparseTensor,
15-
clear_null_stride_columns,
1616
encode_by_order,
1717
fix_ungrouped_dims,
18+
fix_zero_stride_columns,
1819
get_groupings,
1920
)
2021

@@ -283,26 +284,32 @@ def test_concatenate(
283284
@mark.parametrize(
284285
["physical", "strides", "expected_physical", "expected_strides"],
285286
[
286-
([[1, 2, 3], [4, 5, 6]], [[1, 0], [1, 0], [2, 0]], [6, 15], [[1], [1], [2]]),
287287
(
288-
[[1, 2, 3], [4, 5, 6]],
289-
[[1, 1], [1, 0], [2, 0]],
290-
[[1, 2, 3], [4, 5, 6]],
291-
[[1, 1], [1, 0], [2, 0]],
288+
tensor_([[1, 2, 3], [4, 5, 6]]),
289+
tensor([[1, 0], [1, 0], [2, 0]]),
290+
tensor_([6, 15]),
291+
tensor([[1], [1], [2]]),
292+
),
293+
(
294+
tensor_([[1, 2, 3], [4, 5, 6]]),
295+
tensor([[1, 1], [1, 0], [2, 0]]),
296+
tensor_([[1, 2, 3], [4, 5, 6]]),
297+
tensor([[1, 1], [1, 0], [2, 0]]),
298+
),
299+
(
300+
tensor_([[3, 2, 1], [6, 5, 4]]),
301+
tensor([[0, 0], [0, 0], [0, 0]]),
302+
tensor_(21),
303+
tensor([[], [], []], dtype=torch.int64),
292304
),
293305
],
294306
)
295-
def test_clear_null_stride_columns(
296-
physical: list,
297-
strides: list,
298-
expected_physical: list,
299-
expected_strides: list,
307+
def test_fix_zero_stride_columns(
308+
physical: Tensor,
309+
strides: Tensor,
310+
expected_physical: Tensor,
311+
expected_strides: Tensor,
300312
):
301-
physical, strides = torch.tensor(physical), torch.tensor(strides)
302-
expected_physical, expected_strides = torch.tensor(expected_physical), torch.tensor(
303-
expected_strides
304-
)
305-
306-
physical, strides = clear_null_stride_columns(physical, strides)
307-
assert_close(physical, expected_physical)
308-
assert_close(strides, expected_strides)
313+
physical, strides = fix_zero_stride_columns(physical, strides)
314+
assert torch.equal(physical, expected_physical)
315+
assert torch.equal(strides, expected_strides)

0 commit comments

Comments
 (0)