-
Notifications
You must be signed in to change notification settings - Fork 9
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
avoid excessive inlining by moving YaoBlocks.mat
to func.func
defs
#344
Draft
mofeing
wants to merge
7
commits into
main
Choose a base branch
from
ss/reduce-codegen-yao
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.
+162
−35
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a4129c
avoid excessive inlining by moving `mat` to `func.func` defs
mofeing e7328e7
fix pass `Attribute` to `func.call` callee attr
mofeing 7235bd1
fix `callee` attribute type
mofeing 1862dd0
fix typo
mofeing 03907ee
fix active block on codegen of `Ry`
mofeing aa8f88c
refactor `Rx`
mofeing a863078
refactor `Rz`
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 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,42 +1,169 @@ | ||
module ReactantYaoBlocksExt | ||
|
||
using Reactant | ||
using Reactant: TracedRArray, TracedRNumber | ||
using Reactant.MLIR: IR | ||
using Reactant.MLIR.Dialects: func | ||
using YaoBlocks | ||
|
||
function YaoBlocks.mat( | ||
::Type{T}, R::RotationGate{D,Reactant.TracedRNumber{S},<:XGate} | ||
) where {D,T,S} | ||
M = Reactant.broadcast_to_size(zero(T), (2, 2)) | ||
c = cos(R.theta / 2) | ||
s = -im * sin(R.theta / 2) | ||
M[1, 1] = c | ||
M[2, 2] = c | ||
M[1, 2] = s | ||
M[2, 1] = s | ||
return M | ||
end | ||
|
||
function YaoBlocks.mat( | ||
::Type{T}, R::RotationGate{D,Reactant.TracedRNumber{S},<:YGate} | ||
) where {D,T,S} | ||
M = Reactant.broadcast_to_size(zero(T), (2, 2)) | ||
c = cos(R.theta / 2) | ||
s = sin(R.theta / 2) | ||
M[1, 1] = c | ||
M[2, 2] = c | ||
M[1, 2] = -s | ||
M[2, 1] = s | ||
return M | ||
end | ||
|
||
function YaoBlocks.mat( | ||
::Type{T}, R::RotationGate{D,Reactant.TracedRNumber{S},<:ZGate} | ||
) where {D,T,S} | ||
M = Reactant.broadcast_to_size(zero(T), (2, 2)) | ||
x = exp(im * R.theta / 2) | ||
M[1, 1] = conj(x) | ||
M[2, 2] = x | ||
return M | ||
function module_top() | ||
if !haskey(task_local_storage(), :mlir_module) | ||
error("No MLIR module is active") | ||
end | ||
return first(task_local_storage(:mlir_module)) | ||
end | ||
|
||
function symname(name, ::Type{ParamType}, ::Type{OutElType}) where {ParamType,OutElType} | ||
return name * "_" * string(ParamType) * "_" * string(OutElType) | ||
end | ||
|
||
function codegen!( | ||
::Val{:rx}, ::Type{ParamType}, ::Type{OutElType} | ||
) where {ParamType,OutElType} | ||
in_tys = [IR.TensorType((), IR.Type(ParamType))] | ||
out_tys = [IR.TensorType((2, 2), IR.Type(OutElType))] | ||
|
||
mod = module_top() | ||
IR.block!(IR.body(mod)) do | ||
f = func.func_(; | ||
sym_name=symname("rx", ParamType, OutElType), | ||
function_type=IR.FunctionType(in_tys, out_tys), | ||
body=IR.Region(), | ||
) | ||
|
||
fbody = IR.Block(in_tys, [IR.Location()]) | ||
push!(IR.region(f, 1), fbody) | ||
|
||
IR.block!(fbody) do | ||
θ = TracedRNumber{ParamType}((), IR.argument(fbody, 1)) | ||
M = Reactant.broadcast_to_size(zero(OutElType), (2, 2)) | ||
c = cos(θ / 2) | ||
s = -im * sin(θ / 2) | ||
M[1, 1] = c | ||
M[2, 2] = c | ||
M[1, 2] = s | ||
M[2, 1] = s | ||
func.return_([M.mlir_data]) | ||
end | ||
|
||
return f | ||
end | ||
end | ||
|
||
function codegen!( | ||
::Val{:ry}, ::Type{ParamType}, ::Type{OutElType} | ||
) where {ParamType,OutElType} | ||
in_tys = [IR.TensorType((), IR.Type(ParamType))] | ||
out_tys = [IR.TensorType((2, 2), IR.Type(OutElType))] | ||
|
||
mod = module_top() | ||
IR.block!(IR.body(mod)) do | ||
f = func.func_(; | ||
sym_name=symname("ry", ParamType, OutElType), | ||
function_type=IR.FunctionType(in_tys, out_tys), | ||
body=IR.Region(), | ||
) | ||
|
||
fbody = IR.Block(in_tys, [IR.Location()]) | ||
push!(IR.region(f, 1), fbody) | ||
|
||
IR.block!(fbody) do | ||
θ = TracedRNumber{ParamType}((), IR.argument(fbody, 1)) | ||
M = Reactant.broadcast_to_size(zero(OutElType), (2, 2)) | ||
c = cos(θ / 2) | ||
s = sin(θ / 2) | ||
M[1, 1] = c | ||
M[2, 2] = c | ||
M[1, 2] = -s | ||
M[2, 1] = s | ||
func.return_([M.mlir_data]) | ||
end | ||
|
||
return f | ||
end | ||
end | ||
|
||
function codegen!( | ||
::Val{:rz}, ::Type{ParamType}, ::Type{OutElType} | ||
) where {ParamType,OutElType} | ||
in_tys = [IR.TensorType((), IR.Type(ParamType))] | ||
out_tys = [IR.TensorType((2, 2), IR.Type(OutElType))] | ||
|
||
mod = module_top() | ||
IR.block!(IR.body(mod)) do | ||
f = func.func_(; | ||
sym_name=symname("rz", ParamType, OutElType), | ||
function_type=IR.FunctionType(in_tys, out_tys), | ||
body=IR.Region(), | ||
) | ||
|
||
fbody = IR.Block(in_tys, [IR.Location()]) | ||
push!(IR.region(f, 1), fbody) | ||
|
||
IR.block!(fbody) do | ||
θ = TracedRNumber{ParamType}((), IR.argument(fbody, 1)) | ||
M = Reactant.broadcast_to_size(zero(OutElType), (2, 2)) | ||
x = exp(im * θ / 2) | ||
M[1, 1] = conj(x) | ||
M[2, 2] = x | ||
func.return_([M.mlir_data]) | ||
end | ||
|
||
return f | ||
end | ||
end | ||
|
||
function hasfunc(name, ::Type{ParamType}, ::Type{OutElType}) where {ParamType,OutElType} | ||
it = IR.OperationIterator(IR.body(module_top())) | ||
return any(it) do op | ||
IR.name(op) == "func.func" || return false | ||
|
||
String(IR.attr(op, 2).named_attribute.name) == "sym_name" || | ||
error("expected sym_name attribute") | ||
|
||
_symname = String(IR.Attribute(IR.attr(op, 2).named_attribute.attribute)) | ||
_symname == symname(name, ParamType, OutElType) || return false | ||
return true | ||
end | ||
end | ||
|
||
function YaoBlocks.mat(::Type{T}, R::RotationGate{D,TracedRNumber{S},<:XGate}) where {D,T,S} | ||
hasfunc("rx", S, T) || codegen!(Val(:rx), S, T) | ||
|
||
res = IR.result( | ||
func.call( | ||
[R.theta.mlir_data]; | ||
result_0=[IR.TensorType((2, 2), IR.Type(T))], | ||
callee=IR.FlatSymbolRefAttribute(symname("rx", S, T)), | ||
), | ||
) | ||
return TracedRArray{T,2}((), res, (2, 2)) | ||
end | ||
|
||
function YaoBlocks.mat(::Type{T}, R::RotationGate{D,TracedRNumber{S},<:YGate}) where {D,T,S} | ||
hasfunc("ry", S, T) || codegen!(Val(:ry), S, T) | ||
|
||
res = IR.result( | ||
func.call( | ||
[R.theta.mlir_data]; | ||
result_0=[IR.TensorType((2, 2), IR.Type(T))], | ||
callee=IR.FlatSymbolRefAttribute(symname("ry", S, T)), | ||
), | ||
) | ||
return TracedRArray{T,2}((), res, (2, 2)) | ||
end | ||
|
||
function YaoBlocks.mat(::Type{T}, R::RotationGate{D,TracedRNumber{S},<:ZGate}) where {D,T,S} | ||
hasfunc("rz", S, T) || codegen!(Val(:rz), S, T) | ||
|
||
res = IR.result( | ||
func.call( | ||
[R.theta.mlir_data]; | ||
result_0=[IR.TensorType((2, 2), IR.Type(T))], | ||
callee=IR.FlatSymbolRefAttribute(symname("rz", S, T)), | ||
), | ||
) | ||
return TracedRArray{T,2}((), res, (2, 2)) | ||
end | ||
|
||
end |
This file contains 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 |
---|---|---|
|
@@ -211,7 +211,7 @@ rmattr!(operation::Operation, name) = | |
API.mlirOperationRemoveAttributeByName(operation, name) | ||
|
||
function lose_ownership!(operation::Operation) | ||
@assert operation.owned | ||
# @assert operation.owned | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is causing this ? This seems suspicious |
||
@atomic operation.owned = false | ||
return operation | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to use a
SymbolTable
for this: https://github.com/EnzymeAD/Reactant.jl/blob/main/src/mlir/IR/SymbolTable.jl