-
Notifications
You must be signed in to change notification settings - Fork 54
integrate with OMEinsum #2222
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
Open
mofeing
wants to merge
18
commits into
main
Choose a base branch
from
ss/omeinsum-ext
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
integrate with OMEinsum #2222
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
04ce83b
test integration with OMEinsum
mofeing d9f3e8b
skip rewrite of `analyze_binary`
mofeing 2c4aaa2
change eltype of test arrays to ComplexF32
mofeing 282740b
remove `@skip_rewrite_func`
mofeing 30e46fa
overlay `get_output_array`, `einsum!` methods
mofeing 24388ea
fixes to binary einsum
mofeing 243f7d7
fix scalar multiplication
mofeing 701fb9a
fix tensor contraction test
mofeing f7c63ae
fix multiply-and-add behavior
mofeing e86a4b4
test different eltypes
mofeing 76403af
format
mofeing 6ef625a
fix transposition
mofeing 2cc6f9f
refactor mlir data set
mofeing cf185d2
fix indexing for diagonal
mofeing 54d69b2
test diagonal extraction
mofeing 7d04778
comment out broken tests
mofeing f731e1d
Merge branch 'main' into ss/omeinsum-ext
mofeing 4bdd403
promote before madd on `tensorpermute!`
mofeing 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
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,72 @@ | ||
| module ReactantOMEinsumExt | ||
|
|
||
| using Reactant | ||
| using Reactant: @reactant_overlay, looped_any, use_overlayed_version, @opcall | ||
| using OMEinsum | ||
| using OMEinsum: _analyze_binary_input | ||
|
|
||
| @reactant_overlay @noinline function OMEinsum.get_output_array(xs, size, fillzero) | ||
| # we ignore fillzero here, as it's easier for us to zero-initialize arrays | ||
| if looped_any(use_overlayed_version, xs) | ||
| T = promote_type(map(eltype, xs)...) | ||
| return @opcall fill(zero(T), size) | ||
| else | ||
| return Reactant.call_with_native(OMEinsum.get_output_array, xs, size, fillzero) | ||
| end | ||
| end | ||
|
|
||
| @reactant_overlay @noinline function OMEinsum.einsum!(ixs, iy, @nospecialize(xs::NTuple{1,Any}), @nospecialize(y), sx, sy, size_dict) | ||
| if looped_any(use_overlayed_version, xs) | ||
| @assert use_overlayed_version(y) | ||
| # TODO | ||
| error("unary einsum support not implemented yet") | ||
| else | ||
| return Reactant.call_with_native(OMEinsum.einsum!, ixs, iy, xs, y, sx, sy, size_dict) | ||
mofeing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
|
|
||
| @reactant_overlay @noinline function OMEinsum.einsum!(ixs, iy, @nospecialize(xs::NTuple{2,Any}), @nospecialize(y), sx, sy, size_dict) | ||
mofeing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if looped_any(use_overlayed_version, xs) | ||
| @assert use_overlayed_version(y) | ||
|
|
||
| # shortcut for scalar multiplication | ||
| if looped_any(x -> x isa Number, xs) | ||
| c = sy * y + sx * xs[1] * xs[2] | ||
| y.mlir_data = c.mlir_data | ||
| return y | ||
| end | ||
|
|
||
| LT = keytype(size_dict) | ||
| a, b = xs | ||
| ia, ib = collect.(LT, ixs) | ||
| iyv = collect(LT, iy) | ||
| inner, a_outer, b_outer, batch = _analyze_binary_input(ia, ib, iyv) | ||
|
|
||
| contracting_dimensions = ( | ||
| Int[findfirst(==(i), ia) for i in inner], | ||
| Int[findfirst(==(i), ib) for i in inner], | ||
| ) | ||
| batching_dimensions = ( | ||
| Int[findfirst(==(i), ia) for i in batch], | ||
| Int[findfirst(==(i), ib) for i in batch], | ||
| ) | ||
|
|
||
| c = @opcall dot_general(a, b; contracting_dimensions, batching_dimensions) | ||
|
|
||
| # permute dims to match iy | ||
| ic = vcat(batch, a_outer, b_outer) | ||
| perm = Int[findfirst(==(i), ic) for i in iyv] | ||
| c = @opcall transpose(c, perm) | ||
| @assert size(c) == size(y) | ||
| @assert eltype(c) == eltype(y) | ||
|
|
||
| # just like GEMM, we do: y = sy * y + sx * c | ||
| c = sy * y + sx * c | ||
| y.mlir_data = c.mlir_data | ||
| return y | ||
| else | ||
| return Reactant.call_with_native(OMEinsum.einsum!, ixs, iy, xs, y, sx, sy, size_dict) | ||
mofeing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
|
|
||
| end | ||
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,173 @@ | ||
| using Test | ||
| using Reactant | ||
| using OMEinsum | ||
|
|
||
| @testset "sum" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 3) | ||
| a_re = Reactant.to_rarray(a) | ||
|
|
||
| f = ein"ij->" | ||
| c = f(a) | ||
| c_re = @jit f(a_re) | ||
| @test c ≈ c_re | ||
|
|
||
| f = ein"ij->i" | ||
| c = f(a) | ||
| c_re = @jit f(a_re) | ||
| @test c ≈ c_re | ||
|
|
||
| f = ein"ij->j" | ||
| c = f(a) | ||
| c_re = @jit f(a_re) | ||
| @test c ≈ c_re | ||
| end | ||
|
|
||
| @testset "trace" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 3, 3) | ||
| a_re = Reactant.to_rarray(a) | ||
|
|
||
| f = ein"ii->" | ||
| c = f(a) | ||
| c_re = @jit f(a_re) | ||
| @test c ≈ c_re | ||
| end | ||
|
|
||
| @testset "transpose" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 3) | ||
| a_re = Reactant.to_rarray(a) | ||
|
|
||
| f = ein"ij->ji" | ||
| c = f(a) | ||
| c_re = @jit f(a_re) | ||
| @test c ≈ c_re | ||
|
|
||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 3, 4, 5) | ||
| a_re = Reactant.to_rarray(a) | ||
|
|
||
| f = ein"ijkl->jilk" | ||
| c = f(a) | ||
| c_re = @jit f(a_re) | ||
| @test c ≈ c_re | ||
| end | ||
|
|
||
| @testset "matmul" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 3) | ||
| b = Reactant.TestUtils.construct_test_array(ComplexF32, 3, 4) | ||
| a_re = Reactant.to_rarray(a) | ||
| b_re = Reactant.to_rarray(b) | ||
|
|
||
| f = ein"ij,jk->ik" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
|
|
||
| @testset "with different eltype" begin | ||
| b = Reactant.TestUtils.construct_test_array(Float32, 3, 4) | ||
| b_re = Reactant.to_rarray(b) | ||
|
|
||
| f = ein"ij,jk->ik" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
| end | ||
| end | ||
|
|
||
| @testset "hadamard product" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 3) | ||
| b = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 3) | ||
| a_re = Reactant.to_rarray(a) | ||
| b_re = Reactant.to_rarray(b) | ||
|
|
||
| f = ein"ij,ij->ij" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
|
|
||
| b = Reactant.TestUtils.construct_test_array(ComplexF32, 3, 2) | ||
| b_re = Reactant.to_rarray(b) | ||
| f = ein"ij,ji->ij" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
| end | ||
|
|
||
| @testset "inner product" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 4, 3) | ||
| b = Reactant.TestUtils.construct_test_array(ComplexF32, 3, 4) | ||
| a_re = Reactant.to_rarray(a) | ||
| b_re = Reactant.to_rarray(b) | ||
|
|
||
| f = ein"ij,ji->" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
| end | ||
|
|
||
| @testset "outer product" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 3) | ||
| b = Reactant.TestUtils.construct_test_array(ComplexF32, 4, 5) | ||
| a_re = Reactant.to_rarray(a) | ||
| b_re = Reactant.to_rarray(b) | ||
|
|
||
| f = ein"ij,kl->ijkl" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
|
|
||
| f = ein"ij,kl->klij" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
|
|
||
| f = ein"ij,kl->ikjl" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
| end | ||
|
|
||
| @testset "scale" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 3) | ||
| b = fill(ComplexF32(2.0)) | ||
| a_re = Reactant.to_rarray(a) | ||
| b_re = Reactant.to_rarray(b) | ||
|
|
||
| f = ein"ij,->ij" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
| end | ||
|
|
||
| @testset "batch matmul" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 3, 6) | ||
| b = Reactant.TestUtils.construct_test_array(ComplexF32, 3, 4, 6) | ||
| a_re = Reactant.to_rarray(a) | ||
| b_re = Reactant.to_rarray(b) | ||
|
|
||
| f = ein"ijb,jkb->ikb" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
|
|
||
| f = ein"ijb,jkb->bik" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
| end | ||
|
|
||
| @testset "tensor contraction" begin | ||
| a = Reactant.TestUtils.construct_test_array(ComplexF32, 2, 4, 3) | ||
| b = Reactant.TestUtils.construct_test_array(ComplexF32, 3, 5, 4) | ||
| a_re = Reactant.to_rarray(a) | ||
| b_re = Reactant.to_rarray(b) | ||
|
|
||
| f = ein"ijk,klj->il" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
|
|
||
| # contraction of NOT all common indices | ||
| f = ein"ijk,klj->ikl" | ||
| c = f(a, b) | ||
| c_re = @jit f(a_re, b_re) | ||
| @test c ≈ c_re | ||
| end |
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.