-
Notifications
You must be signed in to change notification settings - Fork 30
Implement dpctl.tensor.isdtype #1133
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
oleksandr-pavlyk
merged 5 commits into
IntelPython:master
from
vlad-perevezentsev:add_dpctl.tensor.isdtype
Mar 22, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a33dd2
Add an implementation of dpt.isdtype()
vlad-perevezentsev b8e8ff3
Merge master into add_dpctl.tensor.isdtype
vlad-perevezentsev 2c7c097
Fix doсstrings
vlad-perevezentsev a23b6bf
Merge master into add_dpctl.tensor.isdtype
vlad-perevezentsev 36233fd
Fix remarks
vlad-perevezentsev 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# Data Parallel Control (dpctl) | ||
# | ||
# Copyright 2020-2023 Intel Corporation | ||
# | ||
# 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. | ||
|
||
|
||
import pytest | ||
|
||
import dpctl.tensor as dpt | ||
|
||
list_dtypes = [ | ||
"bool", | ||
"int8", | ||
"int16", | ||
"int32", | ||
"int64", | ||
"uint8", | ||
"uint16", | ||
"uint32", | ||
"uint64", | ||
"float16", | ||
"float32", | ||
"float64", | ||
"complex64", | ||
"complex128", | ||
] | ||
|
||
|
||
dtype_categories = { | ||
"bool": ["bool"], | ||
"signed integer": ["int8", "int16", "int32", "int64"], | ||
"unsigned integer": ["uint8", "uint16", "uint32", "uint64"], | ||
"integral": [ | ||
"int8", | ||
"int16", | ||
"int32", | ||
"int64", | ||
"uint8", | ||
"uint16", | ||
"uint32", | ||
"uint64", | ||
], | ||
"real floating": ["float16", "float32", "float64"], | ||
"complex floating": ["complex64", "complex128"], | ||
"numeric": [d for d in list_dtypes if d != "bool"], | ||
} | ||
|
||
|
||
@pytest.mark.parametrize("kind_str", dtype_categories.keys()) | ||
@pytest.mark.parametrize("dtype_str", list_dtypes) | ||
def test_isdtype_kind_str(dtype_str, kind_str): | ||
dt = dpt.dtype(dtype_str) | ||
is_in_kind = dpt.isdtype(dt, kind_str) | ||
expected = dtype_str in dtype_categories[kind_str] | ||
assert is_in_kind == expected | ||
|
||
|
||
@pytest.mark.parametrize("dtype_str", list_dtypes) | ||
def test_isdtype_kind_tuple(dtype_str): | ||
dt = dpt.dtype(dtype_str) | ||
if dtype_str.startswith("bool"): | ||
assert dpt.isdtype(dt, ("real floating", "bool")) | ||
assert not dpt.isdtype( | ||
dt, ("integral", "real floating", "complex floating") | ||
) | ||
elif dtype_str.startswith("int"): | ||
assert dpt.isdtype(dt, ("real floating", "signed integer")) | ||
assert not dpt.isdtype( | ||
dt, ("bool", "unsigned integer", "real floating") | ||
) | ||
elif dtype_str.startswith("uint"): | ||
assert dpt.isdtype(dt, ("bool", "unsigned integer")) | ||
assert not dpt.isdtype(dt, ("real floating", "complex floating")) | ||
elif dtype_str.startswith("float"): | ||
assert dpt.isdtype(dt, ("complex floating", "real floating")) | ||
assert not dpt.isdtype(dt, ("integral", "complex floating", "bool")) | ||
else: | ||
assert dpt.isdtype(dt, ("integral", "complex floating")) | ||
assert not dpt.isdtype(dt, ("bool", "integral", "real floating")) | ||
|
||
|
||
@pytest.mark.parametrize("dtype_str", list_dtypes) | ||
def test_isdtype_kind_tuple_dtypes(dtype_str): | ||
dt = dpt.dtype(dtype_str) | ||
if dtype_str.startswith("bool"): | ||
assert dpt.isdtype(dt, (dpt.int32, dpt.bool)) | ||
assert not dpt.isdtype(dt, (dpt.int16, dpt.uint32, dpt.float64)) | ||
|
||
elif dtype_str.startswith("int"): | ||
assert dpt.isdtype(dt, (dpt.int8, dpt.int16, dpt.int32, dpt.int64)) | ||
assert not dpt.isdtype(dt, (dpt.bool, dpt.float32, dpt.complex64)) | ||
|
||
elif dtype_str.startswith("uint"): | ||
assert dpt.isdtype(dt, (dpt.uint8, dpt.uint16, dpt.uint32, dpt.uint64)) | ||
assert not dpt.isdtype(dt, (dpt.bool, dpt.int32, dpt.float32)) | ||
|
||
elif dtype_str.startswith("float"): | ||
assert dpt.isdtype(dt, (dpt.float16, dpt.float32, dpt.float64)) | ||
assert not dpt.isdtype(dt, (dpt.bool, dpt.complex64, dpt.int8)) | ||
|
||
else: | ||
assert dpt.isdtype(dt, (dpt.complex64, dpt.complex128)) | ||
assert not dpt.isdtype(dt, (dpt.bool, dpt.uint64, dpt.int8)) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"kind", | ||
[ | ||
[dpt.int32, dpt.bool], | ||
"f4", | ||
float, | ||
123, | ||
"complex", | ||
], | ||
) | ||
def test_isdtype_invalid_kind(kind): | ||
with pytest.raises((TypeError, ValueError)): | ||
dpt.isdtype(dpt.int32, kind) |
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.
Similar here, save
dpt.dtype(dtype_str)
and reuse it.