-
Notifications
You must be signed in to change notification settings - Fork 1
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
Create basic interface between computational graph to mindspore
framework
#158
Merged
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a513f03
add FeynCalc.jl
112cb8f
refactor FeynCalc_FermiL
168822b
solve merge conflict
peter0627ustc 3604247
Merge branch 'computgraph_pchou' into computgraph_zhiyi
peter0627ustc 19219fc
add SelfEnergygraph
peter0627ustc 66da488
Merge branch 'computgraph' into computgraph_zhiyi
peter0627ustc ab58931
merge new update
peter0627ustc f8829af
add mindspore.jl
5e5917d
add toMindspore.jl
peter0627ustc 46566ec
update toMindspore.jl
peter0627ustc 7c178c3
merge conflict
b23453c
fix bug
peter0627ustc 0bdf095
Merge branch 'computgraph_zhiyi' of https://github.com/numericalEFT/F…
4b3f75b
refactor interface to mindspore for AD
peter0627ustc 1442796
Merge branch 'computgraph_zhiyi' of https://github.com/numericalEFT/F…
8111fc9
fix bug
peter0627ustc d886b5b
Merge branch 'computgraph_zhiyi' of https://github.com/numericalEFT/F…
13c06f6
add a dict mapping the graph_id to the leaf_id
249d309
Merge branch 'computgraph' into computgraph_zhiyi
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
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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# ms = pyimport("mindspore") | ||
|
||
""" | ||
function to_pystatic(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector) | ||
|
||
Returns the static representation of a computational graph node `g` with operator `operator`, subgraphs `subgraphs`, and subgraph factors `subgraph_factors` in python. | ||
""" | ||
function to_pystatic(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector) | ||
error( | ||
"Static representation for computational graph nodes with operator $(operator) not yet implemented! " * | ||
"Please define a method `to_static(::Type{$(operator)}, subgraphs::$(typeof(subgraphs)), subgraph_factors::$(typeof(subgraph_factors)))`." | ||
) | ||
end | ||
|
||
function to_pystatic(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} | ||
if length(subgraphs) == 1 | ||
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" | ||
return "(g$(subgraphs[1].id)$factor_str)" | ||
else | ||
terms = ["g$(g.id)" * (gfactor == 1 ? "" : " * $gfactor") for (g, gfactor) in zip(subgraphs, subgraph_factors)] | ||
return "(" * join(terms, " + ") * ")" | ||
end | ||
end | ||
|
||
function to_pystatic(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} | ||
if length(subgraphs) == 1 | ||
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" | ||
return "(g$(subgraphs[1].id)$factor_str)" | ||
else | ||
terms = ["g$(g.id)" * (gfactor == 1 ? "" : " * $gfactor") for (g, gfactor) in zip(subgraphs, subgraph_factors)] | ||
return "(" * join(terms, " * ") * ")" | ||
# return "(" * join(["g$(g.id)" for g in subgraphs], " * ") * ")" | ||
end | ||
end | ||
|
||
function to_pystatic(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} | ||
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" | ||
return "((g$(subgraphs[1].id))**$N$factor_str)" | ||
end | ||
|
||
function to_pystatic(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} | ||
if length(subgraphs) == 1 | ||
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" | ||
return "(g$(subgraphs[1].id)$factor_str)" | ||
else | ||
terms = ["g$(g.id)" * (gfactor == 1 ? "" : " * $gfactor") for (g, gfactor) in zip(subgraphs, subgraph_factors)] | ||
return "(" * join(terms, " + ") * ")" | ||
end | ||
end | ||
|
||
function to_pystatic(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} | ||
if length(subgraphs) == 1 | ||
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" | ||
return "(g$(subgraphs[1].id)$factor_str)" | ||
else | ||
terms = ["g$(g.id)" * (gfactor == 1 ? "" : " * $gfactor") for (g, gfactor) in zip(subgraphs, subgraph_factors)] | ||
return "(" * join(terms, " * ") * ")" | ||
end | ||
end | ||
|
||
function to_pystatic(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} | ||
factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" | ||
return "((g$(subgraphs[1].id))**$N$factor_str)" | ||
end | ||
|
||
""" | ||
function to_julia_str(graphs::AbstractVector{<:AbstractGraph}) | ||
|
||
Compile a list of graphs into a string for a python static function and output a python script which support the static graph representation in mindspore framework. | ||
""" | ||
|
||
function to_python_str_ms(graphs::AbstractVector{<:AbstractGraph}) | ||
head = "import mindspore as ms\[email protected]\n" | ||
head *= "def graphfunc():\n" | ||
body = " graph_list = []\n" | ||
leafidx = 1 | ||
root = [id(g) for g in graphs] | ||
inds_visitedleaf = Int[] | ||
inds_visitednode = Int[] | ||
for graph in graphs | ||
for g in PostOrderDFS(graph) #leaf first search | ||
g_id = id(g) | ||
target = "g$(g_id)" | ||
isroot = false | ||
if g_id in root | ||
isroot = true | ||
end | ||
if isempty(subgraphs(g)) #leaf | ||
g_id in inds_visitedleaf && continue | ||
factor_str = factor(g) == 1 ? "" : " * $(factor(g))" | ||
body *= " $target = ms.Tensor(1.0)$factor_str\n" | ||
leafidx += 1 | ||
push!(inds_visitedleaf, g_id) | ||
else | ||
g_id in inds_visitednode && continue | ||
factor_str = factor(g) == 1 ? "" : " * $(factor(g))" | ||
body *= " $target = $(to_pystatic(operator(g), subgraphs(g), subgraph_factors(g)))$factor_str\n" | ||
push!(inds_visitednode, g_id) | ||
end | ||
if isroot | ||
body *= " graph_list.append($target)\n" | ||
end | ||
end | ||
end | ||
tail = " return graph_list\n" | ||
tail *= "output = graphfunc()" | ||
expr = head * body * tail | ||
# return head * body * tail | ||
f = open("GraphFunc.py", "w") | ||
write(f, expr) | ||
end | ||
|
||
# function to_mindspore_graph(graphs::AbstractVector{<:AbstractGraph}) | ||
# pyexpr = to_python_str_ms(graphs) | ||
# py""" | ||
# import mindspore as ms | ||
# exec($pyexpr) | ||
# ms_graph = jit(fn=graphfunc) | ||
# out = ms_graph() | ||
# """ | ||
# return py"out" | ||
# 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.
delete blank line