-
Notifications
You must be signed in to change notification settings - Fork 135
Fix slow dot in numba #1426
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
Merged
+75
−29
Merged
Fix slow dot in numba #1426
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -565,26 +565,27 @@ | |
def int_to_float_fn(inputs, out_dtype): | ||
"""Create a Numba function that converts integer and boolean ``ndarray``s to floats.""" | ||
|
||
if all( | ||
input.type.numpy_dtype == np.dtype(out_dtype) for input in inputs | ||
) and isinstance(np.dtype(out_dtype), np.floating): | ||
if ( | ||
all(inp.type.dtype == out_dtype for inp in inputs) | ||
and np.dtype(out_dtype).kind == "f" | ||
): | ||
|
||
@numba_njit | ||
@numba_njit(inline="always") | ||
def inputs_cast(x): | ||
return x | ||
|
||
elif any(i.type.numpy_dtype.kind in "ib" for i in inputs): | ||
elif any(i.type.numpy_dtype.kind in "uib" for i in inputs): | ||
args_dtype = np.dtype(f"f{out_dtype.itemsize}") | ||
|
||
@numba_njit | ||
@numba_njit(inline="always") | ||
def inputs_cast(x): | ||
return x.astype(args_dtype) | ||
|
||
else: | ||
args_dtype_sz = max(_arg.type.numpy_dtype.itemsize for _arg in inputs) | ||
args_dtype = np.dtype(f"f{args_dtype_sz}") | ||
|
||
@numba_njit | ||
@numba_njit(inline="always") | ||
def inputs_cast(x): | ||
return x.astype(args_dtype) | ||
|
||
|
@@ -593,17 +594,49 @@ | |
|
||
@numba_funcify.register(Dot) | ||
def numba_funcify_Dot(op, node, **kwargs): | ||
# Numba's `np.dot` does not support integer dtypes, so we need to cast to | ||
# float. | ||
# Numba's `np.dot` does not support integer dtypes, so we need to cast to float. | ||
x, y = node.inputs | ||
[out] = node.outputs | ||
|
||
out_dtype = node.outputs[0].type.numpy_dtype | ||
inputs_cast = int_to_float_fn(node.inputs, out_dtype) | ||
x_dtype = x.type.dtype | ||
y_dtype = y.type.dtype | ||
dot_dtype = f"float{max((32, out.type.numpy_dtype.itemsize * 8))}" | ||
out_dtype = out.type.dtype | ||
|
||
@numba_njit | ||
def dot(x, y): | ||
return np.asarray(np.dot(inputs_cast(x), inputs_cast(y))).astype(out_dtype) | ||
if x_dtype == dot_dtype and y_dtype == dot_dtype: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need these branches otherwise, I get some if x.dtype != dot_dtype:
x = x.astype(dot_dtype) When it's actually needed. Can be simplified once |
||
|
||
@numba_njit | ||
def dot(x, y): | ||
return np.asarray(np.dot(x, y)) | ||
|
||
elif x_dtype == dot_dtype and y_dtype != dot_dtype: | ||
|
||
@numba_njit | ||
def dot(x, y): | ||
return np.asarray(np.dot(x, y.astype(dot_dtype))) | ||
|
||
elif x_dtype != dot_dtype and y_dtype == dot_dtype: | ||
|
||
@numba_njit | ||
def dot(x, y): | ||
return np.asarray(np.dot(x.astype(dot_dtype), y)) | ||
|
||
else: | ||
|
||
@numba_njit() | ||
def dot(x, y): | ||
return np.asarray(np.dot(x.astype(dot_dtype), y.astype(dot_dtype))) | ||
|
||
if out_dtype == dot_dtype: | ||
return dot | ||
|
||
else: | ||
|
||
@numba_njit | ||
def dot_with_cast(x, y): | ||
return dot(x, y).astype(out_dtype) | ||
|
||
return dot | ||
return dot_with_cast | ||
|
||
|
||
@numba_funcify.register(Solve) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old check doesn't work or stopped working at some point:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't use this function anymore in dot, but it's used elsewhere