Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Speeding up adding factors + computing wiring #129

Merged

Conversation

antoine-dedieu
Copy link
Contributor

@antoine-dedieu antoine-dedieu commented Mar 29, 2022

Main goal of this PR is to speed up the process of adding factors to a FactorGraph and to compile wiring. In particular

  • we do not instantiate factors in a FactorGraph by default. Instead all the computations, including wiring, happen at the factor groups level.
  • all the factors-related operations are computed on demand, when the user requires them
  • we create a subfolder pgmax/groups/ to clarify the structure

Before:

  • adding factors for the RBM exp takes 25s, building run_bp takes 45s
  • adding factors for the convor exp takes 20s, building run_bp takes 25s

After:

  • adding factors for the RBM exp takes 4s, building run_bp takes 4s
  • adding factors for the convor exp takes 2s, building run_bp takes 12s

Our next step will accelerate the wiring compilation further by using numba

@antoine-dedieu antoine-dedieu changed the title Speedup factor graph Speedup factor graph creation + wiring Mar 29, 2022
@antoine-dedieu antoine-dedieu changed the title Speedup factor graph creation + wiring Speedup adding factors + computing wiring Mar 29, 2022
@antoine-dedieu antoine-dedieu changed the title Speedup adding factors + computing wiring Speeding up adding factors + computing wiring Mar 29, 2022
@StannisZhou StannisZhou self-requested a review March 30, 2022 05:45
@codecov-commenter
Copy link

codecov-commenter commented Mar 30, 2022

Codecov Report

Merging #129 (3d80958) into master (08137ce) will not change coverage.
The diff coverage is 100.00%.

❗ Current head 3d80958 differs from pull request most recent head 46f2ab0. Consider uploading reports for the commit 46f2ab0 to get more accurate results

@@            Coverage Diff            @@
##            master      #129   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        13    +3     
  Lines          854       912   +58     
=========================================
+ Hits           854       912   +58     
Impacted Files Coverage Δ
pgmax/factors/enumeration.py 100.00% <100.00%> (ø)
pgmax/factors/logical.py 100.00% <100.00%> (ø)
pgmax/fg/graph.py 100.00% <100.00%> (ø)
pgmax/fg/groups.py 100.00% <100.00%> (ø)
pgmax/fg/nodes.py 100.00% <100.00%> (ø)
pgmax/groups/enumeration.py 100.00% <100.00%> (ø)
pgmax/groups/logical.py 100.00% <100.00%> (ø)
pgmax/groups/variables.py 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 08137ce...46f2ab0. Read the comment docs.

Copy link
Contributor

@StannisZhou StannisZhou left a comment

Choose a reason for hiding this comment

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

Made some detailed comments, although nothing major.

It looks like there should be a way to use numba parallel and significantly speed up wiring compilation while unifying the compile wiring interface for all factor groups. So although the current factor group wiring compilation is still a bit annoying, we can go with what we have now after some additional cleanups (since we are likely going to rewrite many of these in the next PR).

def add_factor(
self,
variable_names: List,
factor_configs: np.ndarray,
log_potentials: Optional[np.ndarray] = None,
log_potentials: Optional[np.ndarray] = np.empty((0,)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change None to empty?

Copy link
Contributor Author

@antoine-dedieu antoine-dedieu Apr 1, 2022

Choose a reason for hiding this comment

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

As I mentioned, for more consistency with the default FactorGroup parent

@StannisZhou StannisZhou marked this pull request as ready for review April 1, 2022 06:56
@antoine-dedieu
Copy link
Contributor Author

Thanks for your thorough review @StannisZhou
There are a few points we should discuss related to mypy behaviour
I totally agree that the next PR should be focused on speeding the factor group wiring using numba!

@StannisZhou StannisZhou force-pushed the speedup_factor_graph branch from b0c05ef to e604c49 Compare April 4, 2022 06:18
@StannisZhou StannisZhou force-pushed the speedup_factor_graph branch from 0998a56 to 46f2ab0 Compare April 4, 2022 06:58
Copy link
Contributor

@StannisZhou StannisZhou left a comment

Choose a reason for hiding this comment

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

Left a few more minor comments. LGTM after addressing the comments and making all tests pass!

@@ -32,7 +35,7 @@ def test_factor_graph():
with pytest.raises(
ValueError,
match=re.escape(
f"A factor involving variables {frozenset([0])} already exists."
f"A {enumeration_factor.EnumerationFactor} involving variables {tuple([0])} already exists."
Copy link
Contributor

Choose a reason for hiding this comment

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

I updated fg.graph for this part. Please update this test accordingly!

The fix right now is not entirely correct for LogicalFactors, but a proper fix seems unnecessarily complicated. I will create a issue to keep track of this but for now we can go with the not entirely correct solution using frozenset

@antoine-dedieu antoine-dedieu merged commit d12f993 into vicariousinc:master Apr 4, 2022
@antoine-dedieu antoine-dedieu deleted the speedup_factor_graph branch April 4, 2022 18:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants