Skip to content

Commit 1f7a56d

Browse files
Merge remote-tracking branch 'origin/master' into elementwise-func-fixes
2 parents c363c02 + 5ebd6c3 commit 1f7a56d

30 files changed

+5266
-52
lines changed

dpctl/tensor/__init__.py

+20
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,22 @@
111111
less_equal,
112112
log,
113113
log1p,
114+
log2,
115+
log10,
116+
logical_and,
117+
logical_not,
118+
logical_or,
119+
logical_xor,
114120
multiply,
121+
negative,
115122
not_equal,
123+
positive,
124+
pow,
116125
proj,
117126
real,
118127
sin,
119128
sqrt,
129+
square,
120130
subtract,
121131
)
122132
from ._reduction import sum
@@ -211,13 +221,23 @@
211221
"less",
212222
"less_equal",
213223
"log",
224+
"logical_and",
225+
"logical_not",
226+
"logical_or",
227+
"logical_xor",
214228
"log1p",
229+
"log2",
230+
"log10",
231+
"negative",
232+
"positive",
215233
"proj",
216234
"real",
217235
"sin",
218236
"sqrt",
237+
"square",
219238
"divide",
220239
"multiply",
240+
"pow",
221241
"subtract",
222242
"equal",
223243
"not_equal",

dpctl/tensor/_copy_utils.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import dpctl.tensor as dpt
2424
import dpctl.tensor._tensor_impl as ti
2525
import dpctl.utils
26+
from dpctl.tensor._ctors import _get_dtype
2627
from dpctl.tensor._device import normalize_queue_device
2728

2829
__doc__ = (
@@ -364,7 +365,8 @@ def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
364365
array (usm_ndarray):
365366
An input array.
366367
new_dtype (dtype):
367-
The data type of the resulting array.
368+
The data type of the resulting array. If `None`, gives default
369+
floating point type supported by device where `array` is allocated.
368370
order ({"C", "F", "A", "K"}, optional):
369371
Controls memory layout of the resulting array if a copy
370372
is returned.
@@ -392,7 +394,7 @@ def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
392394
"Recognized values are 'A', 'C', 'F', or 'K'"
393395
)
394396
ary_dtype = usm_ary.dtype
395-
target_dtype = dpt.dtype(newdtype)
397+
target_dtype = _get_dtype(newdtype, usm_ary.sycl_queue)
396398
if not dpt.can_cast(ary_dtype, target_dtype, casting=casting):
397399
raise TypeError(
398400
f"Can not cast from {ary_dtype} to {newdtype} "

dpctl/tensor/_ctors.py

+2
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,8 @@ def full(
11011101
fill_value = int(fill_value.real)
11021102
elif fill_value_type is complex and np.issubdtype(dtype, np.floating):
11031103
fill_value = fill_value.real
1104+
elif fill_value_type is int and np.issubdtype(dtype, np.integer):
1105+
fill_value = _to_scalar(fill_value, dtype)
11041106

11051107
hev, _ = ti._full_usm_ndarray(fill_value, res, sycl_queue)
11061108
hev.wait()

dpctl/tensor/_elementwise_funcs.py

+236-10
Original file line numberDiff line numberDiff line change
@@ -563,25 +563,171 @@
563563
)
564564

565565
# U22: ==== LOG2 (x)
566-
# FIXME: implement U22
566+
_log2_docstring_ = """
567+
log2(x, out=None, order='K')
568+
569+
Computes the base-2 logarithm for each element `x_i` of input array `x`.
570+
571+
Args:
572+
x (usm_ndarray):
573+
Input array, expected to have numeric data type.
574+
out ({None, usm_ndarray}, optional):
575+
Output array to populate.
576+
Array have the correct shape and the expected data type.
577+
order ("C","F","A","K", optional):
578+
Memory layout of the newly output array, if parameter `out` is `None`.
579+
Default: "K".
580+
Returns:
581+
usm_narray:
582+
An array containing the base-2 logarithm of `x`.
583+
The data type of the returned array is determined by the
584+
Type Promotion Rules.
585+
"""
586+
587+
log2 = UnaryElementwiseFunc(
588+
"log2", ti._log2_result_type, ti._log2, _log2_docstring_
589+
)
567590

568591
# U23: ==== LOG10 (x)
569-
# FIXME: implement U23
592+
_log10_docstring_ = """
593+
log10(x, out=None, order='K')
594+
595+
Computes the base-10 logarithm for each element `x_i` of input array `x`.
596+
597+
Args:
598+
x (usm_ndarray):
599+
Input array, expected to have numeric data type.
600+
out ({None, usm_ndarray}, optional):
601+
Output array to populate.
602+
Array have the correct shape and the expected data type.
603+
order ("C","F","A","K", optional):
604+
Memory layout of the newly output array, if parameter `out` is `None`.
605+
Default: "K".
606+
Returns:
607+
usm_narray:
608+
An array containing the base-1- logarithm of `x`.
609+
The data type of the returned array is determined by the
610+
Type Promotion Rules.
611+
"""
612+
613+
log10 = UnaryElementwiseFunc(
614+
"log10", ti._log10_result_type, ti._log10, _log10_docstring_
615+
)
570616

571617
# B15: ==== LOGADDEXP (x1, x2)
572618
# FIXME: implement B15
573619

574620
# B16: ==== LOGICAL_AND (x1, x2)
575-
# FIXME: implement B16
621+
_logical_and_docstring_ = """
622+
logical_and(x1, x2, out=None, order='K')
623+
624+
Computes the logical AND for each element `x1_i` of the input array `x1`
625+
with the respective element `x2_i` of the input array `x2`.
626+
627+
Args:
628+
x1 (usm_ndarray):
629+
First input array.
630+
x2 (usm_ndarray):
631+
Second input array.
632+
out ({None, usm_ndarray}, optional):
633+
Output array to populate.
634+
Array have the correct shape and the expected data type.
635+
order ("C","F","A","K", optional):
636+
Memory layout of the newly output array, if parameter `out` is `None`.
637+
Default: "K".
638+
Returns:
639+
usm_narray:
640+
An array containing the element-wise logical AND results.
641+
"""
642+
logical_and = BinaryElementwiseFunc(
643+
"logical_and",
644+
ti._logical_and_result_type,
645+
ti._logical_and,
646+
_logical_and_docstring_,
647+
)
576648

577649
# U24: ==== LOGICAL_NOT (x)
578-
# FIXME: implement U24
650+
_logical_not_docstring = """
651+
logical_not(x, out=None, order='K')
652+
Computes the logical NOT for each element `x_i` of input array `x`.
653+
Args:
654+
x (usm_ndarray):
655+
Input array.
656+
out (usm_ndarray):
657+
Output array to populate. Array must have the correct
658+
shape and the expected data type.
659+
order ("C","F","A","K", optional): memory layout of the new
660+
output array, if parameter `out` is `None`.
661+
Default: "K".
662+
Return:
663+
usm_ndarray:
664+
An array containing the element-wise logical NOT results.
665+
"""
666+
667+
logical_not = UnaryElementwiseFunc(
668+
"logical_not",
669+
ti._logical_not_result_type,
670+
ti._logical_not,
671+
_logical_not_docstring,
672+
)
579673

580674
# B17: ==== LOGICAL_OR (x1, x2)
581-
# FIXME: implement B17
675+
_logical_or_docstring_ = """
676+
logical_or(x1, x2, out=None, order='K')
677+
678+
Computes the logical OR for each element `x1_i` of the input array `x1`
679+
with the respective element `x2_i` of the input array `x2`.
680+
681+
Args:
682+
x1 (usm_ndarray):
683+
First input array.
684+
x2 (usm_ndarray):
685+
Second input array.
686+
out ({None, usm_ndarray}, optional):
687+
Output array to populate.
688+
Array have the correct shape and the expected data type.
689+
order ("C","F","A","K", optional):
690+
Memory layout of the newly output array, if parameter `out` is `None`.
691+
Default: "K".
692+
Returns:
693+
usm_narray:
694+
An array containing the element-wise logical OR results.
695+
"""
696+
logical_or = BinaryElementwiseFunc(
697+
"logical_or",
698+
ti._logical_or_result_type,
699+
ti._logical_or,
700+
_logical_or_docstring_,
701+
)
582702

583703
# B18: ==== LOGICAL_XOR (x1, x2)
584-
# FIXME: implement B18
704+
_logical_xor_docstring_ = """
705+
logical_xor(x1, x2, out=None, order='K')
706+
707+
Computes the logical XOR for each element `x1_i` of the input array `x1`
708+
with the respective element `x2_i` of the input array `x2`.
709+
710+
Args:
711+
x1 (usm_ndarray):
712+
First input array.
713+
x2 (usm_ndarray):
714+
Second input array.
715+
out ({None, usm_ndarray}, optional):
716+
Output array to populate.
717+
Array have the correct shape and the expected data type.
718+
order ("C","F","A","K", optional):
719+
Memory layout of the newly output array, if parameter `out` is `None`.
720+
Default: "K".
721+
Returns:
722+
usm_narray:
723+
An array containing the element-wise logical XOR results.
724+
"""
725+
logical_xor = BinaryElementwiseFunc(
726+
"logical_xor",
727+
ti._logical_xor_result_type,
728+
ti._logical_xor,
729+
_logical_xor_docstring_,
730+
)
585731

586732
# B19: ==== MULTIPLY (x1, x2)
587733
_multiply_docstring_ = """
@@ -615,7 +761,27 @@
615761
)
616762

617763
# U25: ==== NEGATIVE (x)
618-
# FIXME: implement U25
764+
_negative_docstring_ = """
765+
negative(x, out=None, order='K')
766+
767+
Computes the numerical negative for each element `x_i` of input array `x`.
768+
Args:
769+
x (usm_ndarray):
770+
Input array, expected to have numeric data type.
771+
out (usm_ndarray):
772+
Output array to populate. Array must have the correct
773+
shape and the expected data type.
774+
order ("C","F","A","K", optional): memory layout of the new
775+
output array, if parameter `out` is `None`.
776+
Default: "K".
777+
Return:
778+
usm_ndarray:
779+
An array containing the negative of `x`.
780+
"""
781+
782+
negative = UnaryElementwiseFunc(
783+
"negative", ti._negative_result_type, ti._negative, _negative_docstring_
784+
)
619785

620786
# B20: ==== NOT_EQUAL (x1, x2)
621787
_not_equal_docstring_ = """
@@ -647,10 +813,48 @@
647813
)
648814

649815
# U26: ==== POSITIVE (x)
650-
# FIXME: implement U26
816+
_positive_docstring_ = """
817+
positive(x, out=None, order='K')
818+
819+
Computes the numerical positive for each element `x_i` of input array `x`.
820+
Args:
821+
x (usm_ndarray):
822+
Input array, expected to have numeric data type.
823+
out (usm_ndarray):
824+
Output array to populate. Array must have the correct
825+
shape and the expected data type.
826+
order ("C","F","A","K", optional): memory layout of the new
827+
output array, if parameter `out` is `None`.
828+
Default: "K".
829+
Return:
830+
usm_ndarray:
831+
An array containing the values of `x`.
832+
"""
833+
834+
positive = UnaryElementwiseFunc(
835+
"positive", ti._positive_result_type, ti._positive, _positive_docstring_
836+
)
651837

652838
# B21: ==== POW (x1, x2)
653-
# FIXME: implement B21
839+
_pow_docstring_ = """
840+
pow(x1, x2, out=None, order='K')
841+
842+
Calculates `x1_i` raised to `x2_i` for each element `x1_i` of the input array
843+
`x1` with the respective element `x2_i` of the input array `x2`.
844+
845+
Args:
846+
x1 (usm_ndarray):
847+
First input array, expected to have a numeric data type.
848+
x2 (usm_ndarray):
849+
Second input array, also expected to have a numeric data type.
850+
Returns:
851+
usm_ndarray:
852+
an array containing the element-wise result. The data type of
853+
the returned array is determined by the Type Promotion Rules.
854+
"""
855+
pow = BinaryElementwiseFunc(
856+
"pow", ti._pow_result_type, ti._pow, _pow_docstring_
857+
)
654858

655859
# U??: ==== PROJ (x)
656860
_proj_docstring = """
@@ -738,7 +942,29 @@
738942
# FIXME: implement U31
739943

740944
# U32: ==== SQUARE (x)
741-
# FIXME: implement U32
945+
_square_docstring_ = """
946+
square(x, out=None, order='K')
947+
948+
Computes `x_i**2` (or `x_i*x_i`) for each element `x_i` of input array `x`.
949+
Args:
950+
x (usm_ndarray):
951+
Input array, expected to have numeric data type.
952+
out ({None, usm_ndarray}, optional):
953+
Output array to populate.
954+
Array have the correct shape and the expected data type.
955+
order ("C","F","A","K", optional):
956+
Memory layout of the newly output array, if parameter `out` is `None`.
957+
Default: "K".
958+
Returns:
959+
usm_ndarray:
960+
An array containing the square `x`.
961+
The data type of the returned array is determined by
962+
the Type Promotion Rules.
963+
"""
964+
965+
square = UnaryElementwiseFunc(
966+
"square", ti._square_result_type, ti._square, _square_docstring_
967+
)
742968

743969
# U33: ==== SQRT (x)
744970
_sqrt_docstring_ = """

0 commit comments

Comments
 (0)