refactored _log_probability function to avoid gil in multithreading #1008
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.
I noticed that when using training
HiddenMarkovModel
with emissions composed byExponentialDistribution
wrapped inIndependentComponentsDistribution
, the training was orders of magnitude slower than when usingExponentialDistribution
directly. I was able to replicate the same behaviour also withNormalDistribution
and so I narrowed it down toIndependentComponentsDistribution
.The slower speed is noticable only when using multithreading in fit (n_jobs > 1).
I was able to narrow down the cause to the function
_log_probability
of IndependentComponentsDistribution.The following version of the function leads to slow behavior:
But the following is fast
This happens even though
self.cython
is always 1, and so theelse
block is never executed.Moreover, this is also slow:
I think that branch in the for loop somehow makes it harder for cython to compile efficient code.
Here a complete reproducible example that elicits the slow behaviour:
The first fit call is slow with the original pomegranate code:
The second fit call without
IndependentComponentsDistribution
is fast:With the modifications in this commit, the slow fit call becomes fast: