-
Notifications
You must be signed in to change notification settings - Fork 182
[maintenance] lazy load dpnp.tensor/dpnp and prepare for array_api lazy importing #2509
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
Open
icfaust
wants to merge
43
commits into
uxlfoundation:main
Choose a base branch
from
icfaust:dev/lazy_load
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
7d14b79
starting point
icfaust 4a83297
Merge branch 'dev/lazy_load' of https://github.com/icfaust/scikit-lea…
icfaust 523e84b
first cut
icfaust 6f4775f
rename
icfaust 219e26f
fix various testing imports
icfaust 54af074
don't get ahead of my skis
icfaust f3c5d5b
attempt to further move things apart
icfaust bfdd3e0
remove get_unique_values_with_dpep
icfaust 436405c
remove actually
icfaust 55eab86
Update _array_api.py
icfaust a7c8fb0
try to fix
icfaust 982c7c4
Update _device_offload.py
icfaust e975a4f
Update _device_offload.py
icfaust d46d175
Update _device_offload.py
icfaust 8e8b6d9
Update _device_offload.py
icfaust 125e727
Update _sycl_usm.py
icfaust fc6fa24
Update _third_party.py
icfaust c9244b8
Update _device_offload.py
icfaust c171175
Update _device_offload.py
icfaust 18308b2
Update _device_offload.py
icfaust 603e7d3
Update _device_offload.py
icfaust bc1c0e3
Update _sycl_usm.py
icfaust 51a6b06
Update _sycl_usm.py
icfaust 1f1648c
Update _third_party.py
icfaust 0ec3ed8
Update _third_party.py
icfaust 5688076
Update _sycl_usm.py
icfaust 39d300e
Update _third_party.py
icfaust 62611c0
Update _third_party.py
icfaust 3688b1b
Update _array_api.py
icfaust 65bc9ae
Update _array_api.py
icfaust 8efea19
formatting
icfaust 4520268
Update setup.py
icfaust 474ab8f
Update _third_party.py
icfaust f744a6e
Update _third_party.py
icfaust c1ce7af
Update _third_party.py
icfaust fc41abc
Merge branch 'uxlfoundation:main' into dev/lazy_load
icfaust e697ca3
Merge branch 'main' into dev/lazy_load
icfaust 273f4a7
Update _data_conversion.py
icfaust a6013e1
Update __init__.py
icfaust c1176b4
Update __init__.py
icfaust 49ba4e6
Update _data_conversion.py
icfaust d4317b4
Update _device_offload.py
icfaust 70d7557
Update _third_party.py
icfaust 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
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
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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# ============================================================================== | ||
# Copyright Contributors to the oneDAL Project | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
|
||
from collections.abc import Iterable | ||
|
||
import numpy as np | ||
|
||
from ..utils._third_party import lazy_import | ||
|
||
|
||
@lazy_import("dpctl.memory", "dpctl.tensor") | ||
def array_to_usm(memory, tensor, queue, array): | ||
try: | ||
mem = memory.MemoryUSMDevice(array.nbytes, queue=queue) | ||
mem.copy_from_host(array.tobytes()) | ||
return tensor.usm_ndarray(array.shape, array.dtype, buffer=mem) | ||
except ValueError as e: | ||
# ValueError will raise if device does not support the dtype | ||
# retry with float32 (needed for fp16 and fp64 support issues) | ||
# try again as float32, if it is a float32 just raise the error. | ||
if array.dtype == np.float32: | ||
raise e | ||
return _array_to_usm(queue, array.astype(np.float32)) | ||
|
||
|
||
@lazy_import("dpnp", "dpctl.tensor") | ||
def to_dpnp(dpnp, tensor, array): | ||
if isinstance(array, tensor.usm_ndarray): | ||
return dpnp.array(array, copy=False) | ||
else: | ||
return array | ||
|
||
|
||
def copy_to_usm(queue, array): | ||
if hasattr(array, "__array__"): | ||
return array_to_usm(queue, array) | ||
else: | ||
if isinstance(array, Iterable): | ||
array = [copy_to_usm(queue, i) for i in array] | ||
return array | ||
|
||
|
||
def copy_to_dpnp(queue, array): | ||
if hasattr(array, "__array__"): | ||
return to_dpnp(array_to_usm(queue, array)) | ||
else: | ||
if isinstance(array, Iterable): | ||
array = [copy_to_dpnp(queue, i) for i in array] | ||
return array | ||
|
||
|
||
@lazy_import("dpctl.memory") | ||
def usm_to_numpy(memorymod, item, usm_iface): | ||
buffer = memorymod.as_usm_memory(item).copy_to_host() | ||
order = "C" | ||
if usm_iface["strides"] is not None and len(usm_iface["strides"]) > 1: | ||
if usm_iface["strides"][0] < usm_iface["strides"][1]: | ||
order = "F" | ||
item = np.ndarray( | ||
shape=usm_iface["shape"], | ||
dtype=usm_iface["typestr"], | ||
buffer=buffer, | ||
order=order, | ||
) | ||
return item |
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Wouldn't importing the module inside the function have the same effect?
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.
Trying to avoid adding an unnecessary slowdown via the dictionary search of sys.modules. I don't think it impacts the readability as it is, and follows precedent set by other codebases like sqlite3: https://stackoverflow.com/a/61647085
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.
I don't follow. Their idea is to use the module multiple times, but here it gets only used inside a single function. Why would that lazy loader decorator be more efficient than importing the module inside of the function?