-
Notifications
You must be signed in to change notification settings - Fork 13
feat(autogram): Add StructuredSparseTensor.
#466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 183 commits
Commits
Show all changes
188 commits
Select commit
Hold shift + click to select a range
32ef75e
Add `DiagonalSparseTensor` with the default fallback to dense mechanism.
PierreQuinton 5c76d69
Ignore mypy
PierreQuinton 34f4dce
Remove useless comment.
PierreQuinton a0b7ffc
Change repr
PierreQuinton f476b29
revert removing `__init__`
PierreQuinton 447d714
Give implementation for pointwise
PierreQuinton 85556a8
Add decorator to handle other functions. Add two examples of such fun…
PierreQuinton c5f868c
Improve naming.
PierreQuinton 7f5c097
Merge branch 'main' into block-diagonal-tensor
ValerianRey efa8019
improve
PierreQuinton e36b3c5
Remove inplace functions from the list of pointwise functions (they s…
PierreQuinton 018a994
Fix `to_dense`
PierreQuinton e91323c
Verify input.
PierreQuinton f728153
Make a builder for DSPs and move checks in it. This should always be …
PierreQuinton b2b5d7a
Implement pointwise and inplace pointwise in `_HANDLED_FUNCTIONS`.
PierreQuinton 2b80788
Move Pointwise functions definitions.
PierreQuinton 85c8e41
Clean filed of DST, remove virtual shape, it is just the shape, and m…
PierreQuinton 123d2ce
Merge branch 'dev-new-engine' into block-diagonal-tensor
ValerianRey 7c0bc45
Use DST for initial jac_output
ValerianRey 8509a33
Add test for to_dense and inplace_pointwise
PierreQuinton 0eed3a3
Revert "Clean filed of DST, remove virtual shape, it is just the shap…
PierreQuinton 55a7cbc
Make `contiguous_data` and `v_to_p` public.
PierreQuinton 5796236
Merge remote-tracking branch 'origin/block-diagonal-tensor' into bloc…
PierreQuinton 0ede4ce
Add linting comment when importing aten
ValerianRey bb165b0
Specify indexing="ij" in meshgrid call
ValerianRey 6e57a3f
Use tuple for v_indices_grid
ValerianRey 8351af5
Use lists for _POINTWISE_FUNCTIONS and _IN_PLACE_POINTWISE_FUNCTIONS
ValerianRey 278bf24
Sort _POINTWISE_FUNCTIONS and _IN_PLACE_POINTWISE_FUNCTIONS
ValerianRey d14a1a5
Fix pointwise function override
ValerianRey 17abe7f
Make `contiguous_data` and `v_to_p` public.
PierreQuinton 6bbd702
Use wraps decorator instead of update_wrapper
ValerianRey fac5f7d
Make densification explicit in tests before assert_close
ValerianRey 68bb2a3
Revert "Make `contiguous_data` and `v_to_p` public."
ValerianRey 3db55c3
Add pow implementation for DST
ValerianRey 90b205c
Remove type hint of __torch_dispatch__ so that mypy stops complaining
ValerianRey 2b36084
Make signature of to_dense match that of Tensor.to_dense.
ValerianRey 6236baa
Change type: ignore so that mypy doesn't complain
ValerianRey 6d28918
Add unsqueeze_default implementation for DST
ValerianRey f4a436c
Add more info in the print when falling back to dense
ValerianRey ad4d843
Add implementation for trivial views
ValerianRey b353ff1
Add expand_default, div_Scalar, and slice_Tensor
ValerianRey d640266
Add mul_Tensor and transpose_int
ValerianRey 6145e4b
Use diagonal_sparse_tensor in view_default
ValerianRey 444e7cd
Use DiagonalSparseTensor type hint when applicable
ValerianRey 5c859b5
Add debug_info
ValerianRey 960de60
Improve error message in view_default
ValerianRey e12080e
Add multi-dimensional v_to_p
ValerianRey a38f907
Rename mean and sum to mean_default and sum_default
ValerianRey b7a3f53
Fix condition and comment in diagonal_sparse_tensor
ValerianRey aa3d8a0
Improve implementation of view
ValerianRey c49411f
Implement `einsum` for `v_to_p: list[int]`
PierreQuinton 6c0e9ec
Factor access to physical shape in view.
PierreQuinton 2f774db
rename `physical_to_virtual` to `p_to_vs`.
PierreQuinton 7344118
Refactor p_to_vs.
PierreQuinton 0884495
Adapt `einsum` to `v_to_p: list[list[int]]`
PierreQuinton a5fda15
Rename `v_to_p` to `v_to_ps`
PierreQuinton d03d3a4
Remove diagonal_sparse_tensor, remove to_diagonal_sparse_tensor
ValerianRey 3bf75fb
Remove aten.div.Scalar from _POINTWISE_FUNCTIONS
ValerianRey 35e841a
Fix set union to be able to handle empty list of sets
ValerianRey 2d37031
fix type hint of bmm_default
ValerianRey 7a84940
Add mm_default
ValerianRey 30adf30
Use to_dense when comparing result of einsum in test_einsum
ValerianRey e6ce46e
Make einsum work with DST only, re-add to_diagonal_sparse_tensor
ValerianRey 5253363
Remove list comprehension
PierreQuinton 75d138c
Add sortin function
PierreQuinton f33107b
Add test_view2 and new parametrization for test_view
ValerianRey 17047f6
Add test_sort_dst
ValerianRey 6d1cfa5
Revert "Remove list comprehension"
ValerianRey a5390b7
Rename map to mapping in sort_dst
ValerianRey 2cc95ea
Use empty ps to indicate virtual dim of size one.
ValerianRey ef5a2b1
Remove outdated todo in einsum
ValerianRey ea7d4dd
Improve implementation of sort_dst
ValerianRey 8403b11
Restructure packages
ValerianRey f2b2ef6
Make _HANDLED_FUNCTIONS and implements class attributes of DST
ValerianRey 97213eb
Move pointwise function implementations to the end of the file and av…
ValerianRey 866b4b1
Uniformize name of physical
ValerianRey b73de79
Improve print when falling back to dense
ValerianRey e393096
Rename first_sort to encode_by_order and improve its docstring
ValerianRey 7a8b8a4
Improve repr of DST
ValerianRey 28c72ed
Minor formatting fix
ValerianRey 1c1885c
Move _strides to where it is used:
ValerianRey 09f3efc
Minor reformating
ValerianRey 5703fd2
Add check that v_to_ps are correctly encoded
ValerianRey 8a13d49
Fix to_diagonal_sparse_tensor to not create physical with dim of size 1.
ValerianRey 82d4b33
Add function to create a DST without having to care about dims of siz…
ValerianRey 9d21a6c
Remove test_view and rename test_view2 to test_view
ValerianRey 42e8228
Minor reformatting
ValerianRey 33194c0
Move p_to_vs outside of DiagonalSparseTensor and rename it p_to_vs_fr…
ValerianRey 21ca8e3
Add get_groupings, fix_ungrouped_dims, and use it in constructor
ValerianRey 15a615f
Add check of maximal grouping in DST.__init__
ValerianRey ee8e181
Use make_dst in einsum
ValerianRey 8992d3f
Add possibility to slice dimension of size 1.
ValerianRey cf4f950
Fix to_dense when a pdims is []
ValerianRey ebffc3c
Stop creating index tensors on data device. I think it's ok / faster …
ValerianRey fe84b80
Rename current_product to current_size
ValerianRey 92bcaa6
Add unsquash_pdim
ValerianRey ceeeea6
Revamp unsquash_dim:
ValerianRey 6ab6b4c
Add possibility to unsquash physical dimensions in view.
ValerianRey b746af8
Fix get_groupings and add test that failed before this fix
ValerianRey 59d1f2c
Fix dim encoding in transpose_int
ValerianRey 4c48523
Name more variables in __torch_dispatch__
ValerianRey 123ac3c
Remove todos in view_default
ValerianRey 469ddb3
Add support for shape inference in view_default
ValerianRey bd3e569
Add _unsafe_view_default
ValerianRey 42462bc
Add threshold_backward_default
ValerianRey 19e049d
Add sum_dim_IntList
ValerianRey 519d11b
Add broadcast_tensors_default
ValerianRey 64d0ae8
Fix mul_Tensor
ValerianRey 47f32f8
Add missing default value for keepdim in sum_dim_IntList
ValerianRey b4eb021
Improve print when falling back to dense
ValerianRey 909bcb7
Add mul_Scalar
ValerianRey c72f99e
Fix mul_Tensor to be able to handle non-tensor input
ValerianRey 4b3593b
Make test_einsum parametrizable
ValerianRey 2052d81
Add failing example to test_einsum
ValerianRey a1ccad5
Add even simpler failing example in test_einsum
ValerianRey 2675d8e
Add assertion about result being DST in test_einsum
ValerianRey 3121b6a
Add a test to get_groupings. May want to add the same to `test_fix_un…
PierreQuinton 048a1e2
Merge remote-tracking branch 'origin/block-diagonal-tensor' into bloc…
PierreQuinton 5a2a7a0
Add `strides_from_p_dims_and_p_shape`
PierreQuinton 4b83644
Add `merge_strides`
PierreQuinton 7e8439a
Add `stride_to_shape`
PierreQuinton c7047e1
Add `to_target_physical_strides`
PierreQuinton c6e3fd9
Add new_implementation idea in einsum.
PierreQuinton cecd69b
Merge branch 'dev-new-engine' into block-diagonal-tensor
ValerianRey d2e53a3
Always use randn_ in test
ValerianRey 16e7c1c
Fix order of sorting in merg_strides
PierreQuinton 2c23dbe
Merge remote-tracking branch 'origin/block-diagonal-tensor' into bloc…
PierreQuinton 16e6165
Add strides_v2
ValerianRey 59cf10b
Add more failing parametrizations to test_get_groupings and test_fix_…
ValerianRey e0dc1a7
Add test_concatenate
ValerianRey 3dffd1e
Add strides_to_pdims
ValerianRey 48387ab
Add (passing) test_to_dense2 to test to_dense when the tensor has a v…
ValerianRey f87ecbb
Use new strides in to_dense
ValerianRey b883f20
Simplify to_dense
ValerianRey 7044e37
Remove unused variable dims in the loop of to_dense
ValerianRey 384d550
Pre-compute strides
ValerianRey 18044dd
Replace for-loop with for comprehension to create v_indices_grid, and…
ValerianRey b0a0e7a
Remove for-loop in computation of v_indices_grid in to_dense
ValerianRey 456adf1
Add internal strides to debug_info
ValerianRey dc37d29
Simplify creation of strides in to_dense
ValerianRey a9e298a
Remove torch. prefix when possible
ValerianRey ac2a2c5
Create strides tensor in constructor
ValerianRey cfba7e0
Remove comments about the device of the indices tensors. We actually …
ValerianRey 2c94488
Replace self.physical.device and self.physical.dtype by self.device a…
ValerianRey 9c1ad5b
Move unwrap_to_dense out of DiagonalSparseTensor
ValerianRey 7252757
Extract print_fallback
ValerianRey 7b4c784
Add placeholder cat_default
ValerianRey 767281c
Add to_most_efficient_tensor, use it in view and einsum
ValerianRey 6a3145f
Add permute_default
ValerianRey 9f1860a
Fix some return type hints
ValerianRey 137f64e
Add div_Tensor, factorize prepare_for_elementwise_op
ValerianRey dc4c1a5
Add add_Tensor for same v_to_ps
ValerianRey f057d7e
Add squeeze_dims
ValerianRey dc35696
Handle cases where pdims is empty list in the creation of normal tens…
ValerianRey c97612f
Add hardtanh_backward_default
ValerianRey 56b66ba
Add hardswish_backward_default
ValerianRey 10f250c
Add more concatenate tests
ValerianRey 48de187
Add comment about strides
ValerianRey b60a29d
unsquash_pdim_from_strides
ValerianRey 9ede7ec
Simplify test_concatenate params
ValerianRey 5a25233
Add basic implementation of cat_default for when all strides match an…
ValerianRey 5062b06
Add concat implementation for when the physical dimension on which to…
ValerianRey 3b9a9a8
Revamp grouping detection
ValerianRey ab15e1a
Remove get_groupings_generalized
ValerianRey 26de009
Remove longest_common_prefix
ValerianRey 2419c7e
Restructure sparse package
ValerianRey 59bcf06
Rename DiagonalSparseTensor to StructuredSparseTensor
ValerianRey f693e99
Improve error message for cat_default
ValerianRey 7991ac1
Add alias impl for StructuedSparseDensity.implements
ValerianRey 93e3a61
Improve error message in cat_default
ValerianRey 3c3b4a4
Rename variables in einsum
ValerianRey b303501
Improve error message in einsum
ValerianRey 81dd29e
Add failing parametrization of test_einsum
ValerianRey 8593a74
Move `einsum` to the top of the file `einsum.py`
PierreQuinton 4ccef3a
clear_null_stride_columns
mattbuot 85dc26b
refacto fix zero stride columns
mattbuot 1c73416
Add jupyter notebooks to .gitignore
ValerianRey 5547ff6
Use strides-based representation instead of v_to_ps-based
ValerianRey 7f2c974
Fix StructuredSparseTensor.__new__
ValerianRey 51a579a
Fix tests
ValerianRey 6e94a89
Add initial implementation of view_default
ValerianRey fc3339c
Fix order of rows of new_strides in view_default
ValerianRey fac9c72
Fix creation of SST in autogram
ValerianRey 66c2210
One-line view_default.
ValerianRey 5cbab08
update interface of unsquash_pdim
mattbuot a5c8cbf
Add get_full_source
ValerianRey c1fc115
Remove alternative implementation
ValerianRey bb8faaf
Remove useless clone
ValerianRey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| # Jupyter notebooks | ||
| *.ipynb | ||
|
|
||
| # uv | ||
| uv.lock | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Need to import this to execute the code inside and thus to override the functions | ||
| from . import _aten_function_overrides | ||
| from ._structured_sparse_tensor import StructuredSparseTensor, make_sst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import backward, einsum, pointwise, shape |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from torch import Tensor | ||
| from torch.ops import aten # type: ignore | ||
|
|
||
| from torchjd.sparse._structured_sparse_tensor import StructuredSparseTensor, impl | ||
|
|
||
|
|
||
| @impl(aten.threshold_backward.default) | ||
| def threshold_backward_default( | ||
| grad_output: StructuredSparseTensor, self: Tensor, threshold | ||
| ) -> StructuredSparseTensor: | ||
| new_physical = aten.threshold_backward.default(grad_output.physical, self, threshold) | ||
|
|
||
| return StructuredSparseTensor(new_physical, grad_output.strides) | ||
|
|
||
|
|
||
| @impl(aten.hardtanh_backward.default) | ||
| def hardtanh_backward_default( | ||
| grad_output: StructuredSparseTensor, | ||
| self: Tensor, | ||
| min_val: Tensor | int | float, | ||
| max_val: Tensor | int | float, | ||
| ) -> StructuredSparseTensor: | ||
| if isinstance(self, StructuredSparseTensor): | ||
| raise NotImplementedError() | ||
|
|
||
| new_physical = aten.hardtanh_backward.default(grad_output.physical, self, min_val, max_val) | ||
| return StructuredSparseTensor(new_physical, grad_output.strides) | ||
|
|
||
|
|
||
| @impl(aten.hardswish_backward.default) | ||
| def hardswish_backward_default(grad_output: StructuredSparseTensor, self: Tensor): | ||
| if isinstance(self, StructuredSparseTensor): | ||
| raise NotImplementedError() | ||
|
|
||
| new_physical = aten.hardswish_backward.default(grad_output.physical, self) | ||
| return StructuredSparseTensor(new_physical, grad_output.strides) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| import torch | ||
| from torch import Tensor, tensor | ||
| from torch.ops import aten # type: ignore | ||
|
|
||
| from torchjd.sparse._structured_sparse_tensor import ( | ||
| StructuredSparseTensor, | ||
| impl, | ||
| to_most_efficient_tensor, | ||
| to_structured_sparse_tensor, | ||
| ) | ||
|
|
||
|
|
||
| def einsum(*args: tuple[StructuredSparseTensor, list[int]], output: list[int]) -> Tensor: | ||
|
|
||
| # First part of the algorithm, determine how to cluster physical indices as well as the common | ||
| # p_shapes corresponding to matching v_dims. Second part translates to physical einsum. | ||
|
|
||
| # get a map from einsum index to (tensor_idx, v_dims) | ||
| # get a map from einsum index to merge of strides corresponding to v_dims with that index | ||
| # use to_target_physical_strides on each physical and v_to_ps | ||
| # cluster pairs of (einsum_index, new_stride) using new_v_to_ps and possibly its corresponding | ||
| # p_to_vs | ||
| # get unique indices | ||
| # map output indices (there can be splits) | ||
| # call physical einsum | ||
| # build resulting sst | ||
|
|
||
| # OVER | ||
|
|
||
| # an index in the physical einsum is uniquely characterized by a virtual einsum index and a | ||
| # stride corresponding to the physical stride in the virtual one (note that as the virtual shape | ||
| # for two virtual index that match should match, then we want to match the strides and reshape | ||
| # accordingly). | ||
| # We want to cluster such indices whenever several appear in the same p_to_vs | ||
|
|
||
| # TODO: Handle ellipsis | ||
| # If we have an index v for some virtual dim whose corresponding v_to_ps is a non-trivial list | ||
| # [p_1, ..., p_k], then we have to create fresh sub-indices for each dimension. | ||
| # For this reason, an index is decomposed into sub-indices that are then independently | ||
| # clustered. | ||
| # So if an index i in args for some StructuredSparseTensor corresponds to a v_to_ps [j, k, l], | ||
| # We will consider three indices (i, 0), (i, 1) and (i, 2). | ||
| # If furthermore [k] correspond to the v_to_ps of some other tensor with index j, then | ||
| # (i, 1) and (j, 0) will be clustered together (and end up being mapped to the same indice in | ||
| # the resulting einsum). | ||
| # Note that this is a problem if two virtual dimensions (from possibly different | ||
| # StructuredSparseTensors) have the same size but not the same decomposition into physical | ||
| # dimension sizes. For now lets leave the responsibility to care about that in the calling | ||
| # functions, if we can factor code later on we will. | ||
|
|
||
| index_parents = dict[tuple[int, int], tuple[int, int]]() | ||
|
|
||
| def get_representative(index: tuple[int, int]) -> tuple[int, int]: | ||
| if index not in index_parents: | ||
| # If an index is not yet in a cluster, put it in its own. | ||
| index_parents[index] = index | ||
| current = index_parents[index] | ||
| if current != index: | ||
| # Compress path to representative | ||
| index_parents[index] = get_representative(current) | ||
| return index_parents[index] | ||
|
|
||
| def group_indices(indices: list[tuple[int, int]]) -> None: | ||
| first_representative = get_representative(indices[0]) | ||
| for i in indices[1:]: | ||
| curr_representative = get_representative(i) | ||
| index_parents[curr_representative] = first_representative | ||
|
|
||
| new_indices_pair = list[list[tuple[int, int]]]() | ||
| physicals = list[Tensor]() | ||
| indices_to_n_pdims = dict[int, int]() | ||
| for t, indices in args: | ||
| assert isinstance(t, StructuredSparseTensor) | ||
| physicals.append(t.physical) | ||
| for pdims, index in zip(t.v_to_ps, indices): | ||
| if index in indices_to_n_pdims: | ||
| if indices_to_n_pdims[index] != len(pdims): | ||
| raise NotImplementedError( | ||
| "einsum currently does not support having a different number of physical " | ||
| "dimensions corresponding to matching virtual dimensions of different " | ||
| f"tensors. Found {[(t.debug_info(), indices) for t, indices in args]}, " | ||
| f"output_indices={output}." | ||
| ) | ||
| else: | ||
| indices_to_n_pdims[index] = len(pdims) | ||
| p_to_vs = ... # p_to_vs_from_v_to_ps(t.v_to_ps) | ||
| for indices_ in p_to_vs: | ||
| # elements in indices[indices_] map to the same dimension, they should be clustered | ||
| # together | ||
| group_indices([(indices[i], sub_i) for i, sub_i in indices_]) | ||
| # record the physical dimensions, index[v] for v in vs will end-up mapping to the same | ||
| # final dimension as they were just clustered, so we can take the first, which exists as | ||
| # t is a valid SST. | ||
| new_indices_pair.append([(indices[vs[0][0]], vs[0][1]) for vs in p_to_vs]) | ||
|
|
||
| current = 0 | ||
| pair_to_int = dict[tuple[int, int], int]() | ||
|
|
||
| def unique_int(pair: tuple[int, int]) -> int: | ||
| nonlocal current | ||
| if pair in pair_to_int: | ||
| return pair_to_int[pair] | ||
| pair_to_int[pair] = current | ||
| current += 1 | ||
| return pair_to_int[pair] | ||
|
|
||
| new_indices = [ | ||
| [unique_int(get_representative(i)) for i in indices] for indices in new_indices_pair | ||
| ] | ||
| new_output = list[int]() | ||
| v_to_ps = list[list[int]]() | ||
| for i in output: | ||
| current_v_to_ps = [] | ||
| for j in range(indices_to_n_pdims[i]): | ||
| k = unique_int(get_representative((i, j))) | ||
| if k in new_output: | ||
| current_v_to_ps.append(new_output.index(k)) | ||
| else: | ||
| current_v_to_ps.append(len(new_output)) | ||
| new_output.append(k) | ||
| v_to_ps.append(current_v_to_ps) | ||
|
|
||
| physical = torch.einsum(*[x for y in zip(physicals, new_indices) for x in y], new_output) | ||
| # Need to use the safe constructor, otherwise the dimensions may not be maximally grouped. | ||
| # Maybe there is a way to fix that though. | ||
| return to_most_efficient_tensor(physical, v_to_ps) | ||
|
|
||
|
|
||
| def prepare_for_elementwise_op( | ||
| t1: Tensor | int | float, t2: Tensor | int | float | ||
| ) -> tuple[StructuredSparseTensor, StructuredSparseTensor]: | ||
| """ | ||
| Prepares two SSTs of the same shape from two args, one of those being a SST, and the other being | ||
| a SST, Tensor, int or float. | ||
| """ | ||
|
|
||
| assert isinstance(t1, StructuredSparseTensor) or isinstance(t2, StructuredSparseTensor) | ||
|
|
||
| if isinstance(t1, int) or isinstance(t1, float): | ||
| t1_ = tensor(t1, device=t2.device) | ||
| else: | ||
| t1_ = t1 | ||
|
|
||
| if isinstance(t2, int) or isinstance(t2, float): | ||
| t2_ = tensor(t2, device=t1.device) | ||
| else: | ||
| t2_ = t2 | ||
|
|
||
| t1_, t2_ = aten.broadcast_tensors.default([t1_, t2_]) | ||
| t1_ = to_structured_sparse_tensor(t1_) | ||
| t2_ = to_structured_sparse_tensor(t2_) | ||
|
|
||
| return t1_, t2_ | ||
|
|
||
|
|
||
| @impl(aten.mul.Tensor) | ||
| def mul_Tensor(t1: Tensor | int | float, t2: Tensor | int | float) -> Tensor: | ||
| # Element-wise multiplication with broadcasting | ||
| t1_, t2_ = prepare_for_elementwise_op(t1, t2) | ||
| all_dims = list(range(t1_.ndim)) | ||
| return einsum((t1_, all_dims), (t2_, all_dims), output=all_dims) | ||
|
|
||
|
|
||
| @impl(aten.div.Tensor) | ||
| def div_Tensor(t1: Tensor | int | float, t2: Tensor | int | float) -> Tensor: | ||
| t1_, t2_ = prepare_for_elementwise_op(t1, t2) | ||
| t2_ = StructuredSparseTensor(1.0 / t2_.physical, t2_.strides) | ||
| all_dims = list(range(t1_.ndim)) | ||
| return einsum((t1_, all_dims), (t2_, all_dims), output=all_dims) | ||
|
|
||
|
|
||
| @impl(aten.mul.Scalar) | ||
| def mul_Scalar(t: StructuredSparseTensor, scalar) -> StructuredSparseTensor: | ||
| # TODO: maybe it could be that scalar is a scalar SST and t is a normal tensor. Need to check | ||
| # that | ||
|
|
||
| assert isinstance(t, StructuredSparseTensor) | ||
| new_physical = aten.mul.Scalar(t.physical, scalar) | ||
| return StructuredSparseTensor(new_physical, t.strides) | ||
|
|
||
|
|
||
| @impl(aten.add.Tensor) | ||
| def add_Tensor( | ||
| t1: Tensor | int | float, t2: Tensor | int | float, alpha: Tensor | float = 1.0 | ||
| ) -> StructuredSparseTensor: | ||
| t1_, t2_ = prepare_for_elementwise_op(t1, t2) | ||
|
|
||
| if torch.equal(t1_.strides, t2_.strides): | ||
| new_physical = t1_.physical + t2_.physical * alpha | ||
| return StructuredSparseTensor(new_physical, t1_.strides) | ||
| else: | ||
| raise NotImplementedError() | ||
|
|
||
|
|
||
| @impl(aten.bmm.default) | ||
| def bmm_default(mat1: Tensor, mat2: Tensor) -> Tensor: | ||
| assert isinstance(mat1, StructuredSparseTensor) or isinstance(mat2, StructuredSparseTensor) | ||
| assert ( | ||
| mat1.ndim == 3 | ||
| and mat2.ndim == 3 | ||
| and mat1.shape[0] == mat2.shape[0] | ||
| and mat1.shape[2] == mat2.shape[1] | ||
| ) | ||
|
|
||
| mat1_ = to_structured_sparse_tensor(mat1) | ||
| mat2_ = to_structured_sparse_tensor(mat2) | ||
|
|
||
| # TODO: Verify that the dimension `0` of mat1_ and mat2_ have the same physical dimension sizes | ||
| # decompositions. If not, can reshape to common decomposition? | ||
| return einsum((mat1_, [0, 1, 2]), (mat2_, [0, 2, 3]), output=[0, 1, 3]) | ||
|
|
||
|
|
||
| @impl(aten.mm.default) | ||
| def mm_default(mat1: Tensor, mat2: Tensor) -> Tensor: | ||
| assert isinstance(mat1, StructuredSparseTensor) or isinstance(mat2, StructuredSparseTensor) | ||
| assert mat1.ndim == 2 and mat2.ndim == 2 and mat1.shape[1] == mat2.shape[0] | ||
|
|
||
| mat1_ = to_structured_sparse_tensor(mat1) | ||
| mat2_ = to_structured_sparse_tensor(mat2) | ||
|
|
||
| return einsum((mat1_, [0, 1]), (mat2_, [1, 2]), output=[0, 2]) | ||
|
|
||
|
|
||
| @impl(aten.mean.default) | ||
| def mean_default(t: StructuredSparseTensor) -> Tensor: | ||
| assert isinstance(t, StructuredSparseTensor) | ||
| return aten.sum.default(t.physical) / t.numel() | ||
|
|
||
|
|
||
| @impl(aten.sum.default) | ||
| def sum_default(t: StructuredSparseTensor) -> Tensor: | ||
| assert isinstance(t, StructuredSparseTensor) | ||
| return aten.sum.default(t.physical) | ||
|
|
||
|
|
||
| @impl(aten.sum.dim_IntList) | ||
| def sum_dim_IntList( | ||
| t: StructuredSparseTensor, dim: list[int], keepdim: bool = False, dtype=None | ||
| ) -> Tensor: | ||
| assert isinstance(t, StructuredSparseTensor) | ||
|
|
||
| if dtype: | ||
| raise NotImplementedError() | ||
|
|
||
| all_dims = list(range(t.ndim)) | ||
| result = einsum((t, all_dims), output=[d for d in all_dims if d not in dim]) | ||
|
|
||
| if keepdim: | ||
| for d in dim: | ||
| result = result.unsqueeze(d) | ||
|
|
||
| return result |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.