From 34104f0ca067df21b743b56ceaeb8c1424f6fc89 Mon Sep 17 00:00:00 2001 From: houpc Date: Sat, 17 Feb 2024 21:48:58 +0800 Subject: [PATCH 1/4] bugfix in to_static --- src/backend/compiler_python.jl | 67 +--------------------------------- src/backend/static.jl | 36 ++++++++++++------ 2 files changed, 26 insertions(+), 77 deletions(-) diff --git a/src/backend/compiler_python.jl b/src/backend/compiler_python.jl index 9bbc6c0a..3fd231e1 100644 --- a/src/backend/compiler_python.jl +++ b/src/backend/compiler_python.jl @@ -1,68 +1,3 @@ -# 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_python_str(graphs::AbstractVector{<:AbstractGraph}) @@ -103,7 +38,7 @@ function to_python_str(graphs::AbstractVector{<:AbstractGraph}, framework::Symbo push!(inds_visitedleaf, g_id) else g_id in inds_visitednode && continue - body *= " $target = $(to_pystatic(operator(g), subgraphs(g), subgraph_factors(g)))\n" + body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g), lang=:python))\n" push!(inds_visitednode, g_id) end if isroot diff --git a/src/backend/static.jl b/src/backend/static.jl index 40df4cc5..1ad99ca8 100644 --- a/src/backend/static.jl +++ b/src/backend/static.jl @@ -1,16 +1,16 @@ """ function to_static(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`. + Returns the static representation of a computational graph node `g` with operator `operator`, subgraphs `subgraphs`, and subgraph factors `subgraph_factors`. """ -function to_static(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector) +function to_static(operator::Type, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector; lang::Symbol=:julia) 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_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {F,W} if length(subgraphs) == 1 factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" return "(g$(subgraphs[1].id)$factor_str)" @@ -20,7 +20,7 @@ function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{Graph{F,W} end end -function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {F,W} if length(subgraphs) == 1 factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" return "(g$(subgraphs[1].id)$factor_str)" @@ -31,12 +31,19 @@ function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{Graph{F,W end end -function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} +function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {N,F,W} factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" - return "((g$(subgraphs[1].id))^$N$factor_str)" + if lang == :julia + op_str = "^" + elseif lang == :c || lang == :python + op_str = "**" + else + error("Unsupported language") + end + return "((g$(subgraphs[1].id))$(op_str)$N$factor_str)" end -function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {F,W} if length(subgraphs) == 1 factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" return "(g$(subgraphs[1].id)$factor_str)" @@ -46,7 +53,7 @@ function to_static(::Type{ComputationalGraphs.Sum}, subgraphs::Vector{FeynmanGra end end -function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W} +function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {F,W} if length(subgraphs) == 1 factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" return "(g$(subgraphs[1].id)$factor_str)" @@ -56,9 +63,16 @@ function to_static(::Type{ComputationalGraphs.Prod}, subgraphs::Vector{FeynmanGr end end -function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W} +function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}; lang::Symbol=:julia) where {N,F,W} factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" - return "((g$(subgraphs[1].id))^$N$factor_str)" + if lang == :julia + op_str = "^" + elseif lang == :c || lang == :python + op_str = "**" + else + error("Unsupported language") + end + return "((g$(subgraphs[1].id))$(op_str)$N$factor_str)" end """ @@ -165,7 +179,7 @@ function to_Cstr(graphs::AbstractVector{<:AbstractGraph}; root::AbstractVector{I else g_id in inds_visitednode && continue declare *= " g$g_id," - body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g)));\n" + body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g), lang=:c));\n" push!(inds_visitednode, g_id) end if isroot From 14e87a38745fb2b405bf2da316f5671c3d8a8497 Mon Sep 17 00:00:00 2001 From: houpc Date: Tue, 20 Feb 2024 13:40:53 +0800 Subject: [PATCH 2/4] use pow in compiler_C --- src/backend/static.jl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/backend/static.jl b/src/backend/static.jl index 1ad99ca8..60d0845f 100644 --- a/src/backend/static.jl +++ b/src/backend/static.jl @@ -35,7 +35,9 @@ function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{Graph factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" if lang == :julia op_str = "^" - elseif lang == :c || lang == :python + elseif lang == :c + return "pow(g$(subgraphs[1].id), $N)$factor_str" + elseif lang == :python op_str = "**" else error("Unsupported language") @@ -67,7 +69,9 @@ function to_static(::Type{ComputationalGraphs.Power{N}}, subgraphs::Vector{Feynm factor_str = subgraph_factors[1] == 1 ? "" : " * $(subgraph_factors[1])" if lang == :julia op_str = "^" - elseif lang == :c || lang == :python + elseif lang == :c + return "pow(g$(subgraphs[1].id), $N)$factor_str" + elseif lang == :python op_str = "**" else error("Unsupported language") @@ -266,6 +270,9 @@ function compile_C(graphs::AbstractVector{<:AbstractGraph}, filename::String; datatype::DataType=_dtype.weight, root::AbstractVector{Int}=[id(g) for g in graphs], func_name="eval_graph") func_string, leafmap = to_Cstr(graphs; datatype=datatype, root=root, name=func_name) open(filename, "a") do f + if position(f) == 0 + write(f, "#include \n") + end write(f, func_string) end return leafmap From 559bb9c79c132841653e4803c26a031a7539db0b Mon Sep 17 00:00:00 2001 From: houpc Date: Tue, 20 Feb 2024 13:57:01 +0800 Subject: [PATCH 3/4] bugfix --- src/frontend/frontends.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frontend/frontends.jl b/src/frontend/frontends.jl index 637e08e1..bb9a92d8 100644 --- a/src/frontend/frontends.jl +++ b/src/frontend/frontends.jl @@ -1,6 +1,6 @@ module FrontEnds -import ..ComputationalGraphs +import ..ComputationalGraphs as IR import ..ComputationalGraphs: Graph, FeynmanGraph, _dtype import ..QuantumOperators import ..Taylor From fae5efa6fbb2f47968e9555ffef210e41f8d55f4 Mon Sep 17 00:00:00 2001 From: houpc Date: Fri, 23 Feb 2024 22:48:14 +0800 Subject: [PATCH 4/4] update v1.0.1 --- Project.toml | 2 +- src/frontend/frontends.jl | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 99dca66f..de06d941 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "FeynmanDiagram" uuid = "e424a512-dbd9-41ff-9883-094748823e72" authors = ["Kun Chen", "Pengcheng Hou", "Daniel Cerkoney"] -version = "1.0.0" +version = "1.0.1" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" diff --git a/src/frontend/frontends.jl b/src/frontend/frontends.jl index bb9a92d8..72ece730 100644 --- a/src/frontend/frontends.jl +++ b/src/frontend/frontends.jl @@ -1,6 +1,6 @@ module FrontEnds -import ..ComputationalGraphs as IR +import ..ComputationalGraphs import ..ComputationalGraphs: Graph, FeynmanGraph, _dtype import ..QuantumOperators import ..Taylor @@ -134,11 +134,11 @@ function leafstates(leaf_maps::Vector{Dict{Int,G}}, labelProd::LabelProduct) whe for idx in 1:len_leaves g = leafmap[idx] vertices = g.properties.vertices - if IR.diagram_type(g) == IR.Interaction + if ComputationalGraphs.diagram_type(g) == ComputationalGraphs.Interaction In = Out = vertices[1][1].label push!(leafType[ikey], 0) push!(leafLoopIndex[ikey], 1) - elseif IR.diagram_type(g) == IR.Propagator + elseif ComputationalGraphs.diagram_type(g) == ComputationalGraphs.Propagator if (Op.isfermionic(vertices[1])) In, Out = vertices[2][1].label, vertices[1][1].label # push!(leafType[ikey], g.orders[1] * 2 + 1) @@ -197,7 +197,7 @@ function leafstates(leaf_maps::Vector{Dict{Int,G}}, maxloopNum::Int) where {G<:G for idx in 1:len_leaves leaf = leafmap[idx] - @assert IR.isleaf(leaf) + @assert ComputationalGraphs.isleaf(leaf) diagId, leaf_orders = leaf.properties, leaf.orders loopmom = copy(diagId.extK) len = length(loopmom)