Skip to content
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

Enhance performance of FlopsScorer for findslices #63

Merged
merged 3 commits into from
Feb 7, 2025
Merged

Conversation

jofrevalles
Copy link
Member

Summary

This PR addresses the performance issues of the FlopsScorer, a scorer used by the findslices function. Before this PR, this function was not very efficient since it created a lot of PostOrderDFS instances, and that was slow.
In this PR, we modify the FlopsScorer so now it directly calculates the difference in score resulting from the slice, rather than creating some copies of the contraction path. This implementation is more efficient (it can reduce the runtime about ~50%) and I have checked that it yields to the same results as before.

Example

See this performance gain:

julia> using Tenet; using EinExprs; using BenchmarkTools

julia> tn = rand(TensorNetwork, 200, 3)
TensorNetwork (#tensors=200, #inds=300)

julia> path = einexpr(
           tn,
           optimizer = EinExprs.Greedy,
       )
Symbol[] 1 flops, 1 elems
├─ Symbol[] 3 flops, 1 elems
...

julia> max_dims_path = maximum(ndims, Branches(path))
55

julia> t_size= 2^27
134217728

julia> @btime setnew = findslices(EinExprs.FlopsScorer(), path, size = t_size, temperature=0.) # OLD implementation
  16.420 s (362734668 allocations: 11.88 GiB)
Set{Symbol} with 48 elements:
  
  
  
  
   

julia> @btime setold = findslices(EinExprs.FlopsScorerOld(), path, size = t_size, temperature=0.) # NEW implementation
  31.575 s (568955147 allocations: 20.08 GiB)
Set{Symbol} with 48 elements:
  
  
  
  
  

As you can see, we reduced by 50% the runtime and almost 50% the memory allocations too.

@jofrevalles jofrevalles changed the title Enhance performance of FlopsScorer for findslices` Enhance performance of FlopsScorer for findslices Feb 5, 2025
@jofrevalles
Copy link
Member Author

jofrevalles commented Feb 7, 2025

Okay! This can be merged, I have done some benchmarks everything seems to have improved:

julia> tn = rand(TensorNetwork, 150, 3)
TensorNetwork (#tensors=150, #inds=225)

julia> path = einexpr(
                  tn,
                  optimizer = EinExprs.Greedy,
              )
Symbol[] 1855774691935911936000 flops, 1 elems
├─ [, , , :N, :T, , , , ,     , , , , , , , , :e, :l] 10822878003370238410752000 flops, ...

julia> max_dims_path = maximum(ndims, Branches(path))
46

julia> tn_size = 2^41
2199023255552

julia> @btime setnew = findslices(EinExprs.FlopsScorer(), path, size = tn_size, temperature=0.) # NEW implementation
  4.375 s (97022116 allocations: 3.09 GiB)
Set{Symbol} with 20 elements:
  
  :c
  :h
  
  
  
  
  
  
  
  :v
  
  
  
  
  
  :I
  
  
  :L

julia> @btime setold = findslices(EinExprs.FlopsScorerOld(), path, size = tn_size, temperature=0.) # OLD implementation
  5.791 s (145228462 allocations: 5.21 GiB)
Set{Symbol} with 20 elements:
  
  :c
  :h
  
  
  
  
  
  
  
  :v
  
  
  
  
  
  :I
  
  
  :L

And here is the benchmark plot:
benchmark_results_joined_v3

@mofeing mofeing merged commit 01c11f0 into master Feb 7, 2025
8 checks passed
@mofeing mofeing deleted the fix/FlopsScorer branch February 7, 2025 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants