Repository navigation
ENH: Einsum calls BLAS if it advantageous to do so - #9425
Conversation
|
|
||
| # Standard GEMM operation | ||
| >>> _can_blas(['ij', 'jk'], 'ik', set('j')) | ||
| 'GEMM' |
| elif inputs[0] == inputs[1]: | ||
| return True | ||
|
|
||
| # DDOT doesnt make sense if you have to tranpose |
|
|
||
| # Checks have already been handled | ||
| input_str, results_index = einsum_str.split('->') | ||
| input_indices = input_str.split(',') |
There was a problem hiding this comment.
Rather than using integer indexing below, use tuple unpacking, e.g.,
indices_left, indices_right = input_str.split(',')
| return False | ||
|
|
||
| # DDOT | ||
| elif inputs[0] == inputs[1]: |
There was a problem hiding this comment.
Given that each of these clauses contains a return, consider making them all if instead of elif
| sets = [set(x) for x in inputs] | ||
| keep_left = sets[0] - idx_removed | ||
| keep_right = sets[1] - idx_removed | ||
| input_left = inputs[0] |
There was a problem hiding this comment.
I would prefer tuple unpacking here: input_left, input_right = inputs
| # Can only do two | ||
| if len(inputs) != 2: | ||
| return False | ||
|
|
There was a problem hiding this comment.
At this point, I would probably unpack input_left, input_right = inputs and do everything with the separated inputs.
| return False | ||
|
|
||
| # Make sure there is overlap | ||
| if len(set(inputs[0]) & set(inputs[1])) == 0: |
There was a problem hiding this comment.
prefer implicit boolean evaluation: if not (left_inputs & right_inputs)
| # Cannot handle partial inner | ||
| if len(keep_left & keep_right): | ||
| return False | ||
|
|
There was a problem hiding this comment.
This would be a good place to summarize the invariants we've established and the logic in the big if/else below. E.g., if I'm following this correctly:
We've shown the expression can be expressed as a BLAS call with tensordot. Now check if it can be done without a copy.
| return False | ||
|
|
||
| # Conventional matrix tensordot | ||
| else: |
There was a problem hiding this comment.
It doesn't immediately follow to me that the conditions above check all other possibilities. Maybe there's a more tree like way to express them that would make this clearer?
Also, what exactly does "conventional matrix tensordot" mean?
There was a problem hiding this comment.
Thanks for the comments, changes have been made based off them. I would be very open to cleaning up the logic if you have any explicit ideas.
There was a problem hiding this comment.
if not keep_left or not keep_right:
|
Thanks @dgasmith . |
|
Nice - thanks! |
* Einsum now optionally uses BLAS * The einsum call is now optimized by default * cleans up the dot logic * MAINT: Correct spelling, tranpose <- transpose.
Einsum will now call
tensordotif it is advantageous to do so:This is also quite advantageous even if a copy is required:
For level 3 BLAS the array is always copied while for level 1 and 2 the data must be aligned for BLAS to be called as
einsumis faster than BLAS in these cases.There was also a small bug in
tensordotwhere it would fail for inner product cases.This PR also switches
einsumto optimize the contraction by default. I was originally concerned that there may be remaining bugs in the path code, but I have not seen any issues since this feature was originally released. I hope the algorithm is battle tested enough that the optimized path can be the default due to the speedups that can occur.The downside is the path logic takes ~20-50us:
A few arbitrary cutoffs can be placed to prevent the logic overhead (switching back to c_einsum immediately if there are less then say 1000 values in total). However, some discussion is desired if this is need or wanted as I feel a little uncomfortable taking different paths based on size of a contraction.