Skip to content

use map instead of broadcast for Tensor operators#380

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
cvsvensson:master
May 12, 2026
Merged

use map instead of broadcast for Tensor operators#380
ChrisRackauckas merged 1 commit into
SciML:masterfrom
cvsvensson:master

Conversation

@cvsvensson
Copy link
Copy Markdown
Contributor

For deeply nested tensor products (4 or more), cheap operations like adjoint and size get slowdowns and start to allocate. The reason seems to be the combination of broadcasting and nested tuples. This can be fixed by replacing e.g. adjoint.(L.ops) with map(adjoint, L.ops). This PR replaces all such uses of broadcast with map for TensorProductOperator and TensorSumOperator.

Here are some benchmarks:

using SciMLOperators, BenchmarkTools
mo = MatrixOperator(rand(1, 1))
deep_prod = TensorProductOperator([mo for _ in 1:10]...)

@btime adjoint($deep_prod)
# before: 1.380 μs (29 allocations: 1.42 KiB)
# after : 12.713 ns (0 allocations: 0 bytes)

@btime $deep_prod + $deep_prod
# before: 7.850 μs (73 allocations: 3.08 KiB)
# after: 14.028 ns (0 allocations: 0 bytes)

@ChrisRackauckas ChrisRackauckas merged commit 5dc33c1 into SciML:master May 12, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants