Skip to content

Commit

Permalink
Merge pull request #187 from numericalEFT/computgraph_pchou
Browse files Browse the repository at this point in the history
Update to v1.0.2
  • Loading branch information
houpc authored Nov 2, 2024
2 parents 87071a2 + e1ebd33 commit 1890a06
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FeynmanDiagram"
uuid = "e424a512-dbd9-41ff-9883-094748823e72"
authors = ["Kun Chen", "Pengcheng Hou", "Daniel Cerkoney"]
version = "1.0.1"
version = "1.0.2"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
38 changes: 20 additions & 18 deletions src/backend/compiler_python.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,54 @@
# Arguments:
- `graphs` vector of computational graphs
"""
function to_python_str(graphs::AbstractVector{<:AbstractGraph})
function to_python_str(graphs::AbstractVector{<:AbstractGraph};
root::AbstractVector{Int}=[id(g) for g in graphs], name::String="eval_graph", in_place::Bool=false)
head = ""
body = ""
leafidx = 0
root = [id(g) for g in graphs]
inds_visitedleaf = Int[]
inds_visitednode = Int[]
gid_to_leafid = Dict{String,Int64}()
rootidx = 0
map_validx_leaf = Dict{Int,eltype(graphs)}() # mapping from the index of the leafVal to the leaf graph
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
target_root = "root[:, $(findfirst(x -> x == g_id, root)-1)]"
isroot = true
end
if isempty(subgraphs(g)) #leaf
g_id in inds_visitedleaf && continue
body *= " $target = leaf[$(leafidx)]\n"
gid_to_leafid[target] = leafidx
body *= " $target = leafVal[:, $(leafidx)]\n"
leafidx += 1
map_validx_leaf[leafidx] = g
push!(inds_visitedleaf, g_id)
else
g_id in inds_visitednode && continue
body *= " $target = $(to_static(operator(g), subgraphs(g), subgraph_factors(g), lang=:python))\n"
push!(inds_visitednode, g_id)
end
if isroot
body *= " root$(rootidx) = $target\n"
rootidx += 1
body *= " $target_root = $target\n"
end
end
end
head *= "def graphfunc(leaf):\n"
output = ["root$(i)" for i in 0:rootidx-1]
output = join(output, ",")
tail = " return $output\n\n"

expr = head * body * tail
if in_place
head *= "def $name(root, leafVal):\n"
else
head *= "import torch\n"
head *= "def $name(leafVal):\n"
head *= " root = torch.empty(leafVal.shape[0], $(length(graphs)), dtype=leafVal.dtype, device=leafVal.device)\n"
end
tail = " return root\n\n"

return expr, gid_to_leafid
return head * body * tail, map_validx_leaf
end
function compile_Python(graphs::AbstractVector{<:AbstractGraph}, filename::String="GraphFunc.py")
py_string, leafmap = to_python_str(graphs)
open(filename, "w") do f
function compile_Python(graphs::AbstractVector{<:AbstractGraph}, filename::String;
root::AbstractVector{Int}=[id(g) for g in graphs], func_name="eval_graph")
py_string, leafmap = to_python_str(graphs, root=root, name=func_name)
open(filename, "a") do f
write(f, py_string)
end
return leafmap
Expand Down

2 comments on commit 1890a06

@houpc
Copy link
Member Author

@houpc houpc commented on 1890a06 Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/118556

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.2 -m "<description of version>" 1890a064f2e9a3cc72f8baa4189b871b80ed34dd
git push origin v1.0.2

Please # to comment.