14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
+ import operator
18
+
17
19
import numpy as np
18
20
19
21
import dpctl
@@ -196,9 +198,12 @@ def _asarray_from_numpy_ndarray(
196
198
raise TypeError (f"Expected numpy.ndarray, got { type (ary )} " )
197
199
if usm_type is None :
198
200
usm_type = "device"
199
- if dtype is None :
200
- dtype = _get_dtype (dtype , sycl_queue , ref_type = ary .dtype )
201
201
copy_q = normalize_queue_device (sycl_queue = None , device = sycl_queue )
202
+ if dtype is None :
203
+ ary_dtype = ary .dtype
204
+ dtype = _get_dtype (dtype , copy_q , ref_type = ary_dtype )
205
+ if dtype .itemsize > ary_dtype .itemsize :
206
+ dtype = ary_dtype
202
207
f_contig = ary .flags ["F" ]
203
208
c_contig = ary .flags ["C" ]
204
209
fc_contig = f_contig or c_contig
@@ -292,7 +297,7 @@ def asarray(
292
297
for output array allocation and copying. `sycl_queue` and `device`
293
298
are exclusive keywords, i.e. use one or another. If both are
294
299
specified, a `TypeError` is raised unless both imply the same
295
- underlying SYCL queue to be used. If both a `None`, the
300
+ underlying SYCL queue to be used. If both are `None`, the
296
301
`dpctl.SyclQueue()` is used for allocation and copying.
297
302
Default: `None`.
298
303
"""
@@ -430,7 +435,7 @@ def empty(
430
435
for output array allocation and copying. `sycl_queue` and `device`
431
436
are exclusive keywords, i.e. use one or another. If both are
432
437
specified, a `TypeError` is raised unless both imply the same
433
- underlying SYCL queue to be used. If both a `None`, the
438
+ underlying SYCL queue to be used. If both are `None`, the
434
439
`dpctl.SyclQueue()` is used for allocation and copying.
435
440
Default: `None`.
436
441
"""
@@ -453,18 +458,20 @@ def empty(
453
458
return res
454
459
455
460
456
- def _coerce_and_infer_dt (* args , dt , sycl_queue ):
461
+ def _coerce_and_infer_dt (* args , dt , sycl_queue , err_msg , allow_bool = False ):
457
462
"Deduce arange type from sequence spec"
458
463
nd , seq_dt , d = _array_info_sequence (args )
459
464
if d != _host_set or nd != (len (args ),):
460
- raise ValueError ("start, stop and step must be Python scalars" )
465
+ raise ValueError (err_msg )
461
466
dt = _get_dtype (dt , sycl_queue , ref_type = seq_dt )
462
467
if np .issubdtype (dt , np .integer ):
463
468
return tuple (int (v ) for v in args ), dt
464
469
elif np .issubdtype (dt , np .floating ):
465
470
return tuple (float (v ) for v in args ), dt
466
471
elif np .issubdtype (dt , np .complexfloating ):
467
472
return tuple (complex (v ) for v in args ), dt
473
+ elif allow_bool and dt .char == "?" :
474
+ return tuple (bool (v ) for v in args ), dt
468
475
else :
469
476
raise ValueError (f"Data type { dt } is not supported" )
470
477
@@ -517,7 +524,7 @@ def arange(
517
524
for output array allocation and copying. `sycl_queue` and `device`
518
525
are exclusive keywords, i.e. use one or another. If both are
519
526
specified, a `TypeError` is raised unless both imply the same
520
- underlying SYCL queue to be used. If both a `None`, the
527
+ underlying SYCL queue to be used. If both are `None`, the
521
528
`dpctl.SyclQueue()` is used for allocation and copying.
522
529
Default: `None`.
523
530
"""
@@ -526,12 +533,14 @@ def arange(
526
533
start = 0
527
534
dpctl .utils .validate_usm_type (usm_type , allow_none = False )
528
535
sycl_queue = normalize_queue_device (sycl_queue = sycl_queue , device = device )
529
- (
536
+ (start , stop , step ,), dt = _coerce_and_infer_dt (
530
537
start ,
531
538
stop ,
532
539
step ,
533
- ), dt = _coerce_and_infer_dt (
534
- start , stop , step , dt = dtype , sycl_queue = sycl_queue
540
+ dt = dtype ,
541
+ sycl_queue = sycl_queue ,
542
+ err_msg = "start, stop, and step must be Python scalars" ,
543
+ allow_bool = False ,
535
544
)
536
545
try :
537
546
tmp = _get_arange_length (start , stop , step )
@@ -579,7 +588,7 @@ def zeros(
579
588
for output array allocation and copying. `sycl_queue` and `device`
580
589
are exclusive keywords, i.e. use one or another. If both are
581
590
specified, a `TypeError` is raised unless both imply the same
582
- underlying SYCL queue to be used. If both a `None`, the
591
+ underlying SYCL queue to be used. If both are `None`, the
583
592
`dpctl.SyclQueue()` is used for allocation and copying.
584
593
Default: `None`.
585
594
"""
@@ -627,7 +636,7 @@ def ones(
627
636
for output array allocation and copying. `sycl_queue` and `device`
628
637
are exclusive keywords, i.e. use one or another. If both are
629
638
specified, a `TypeError` is raised unless both imply the same
630
- underlying SYCL queue to be used. If both a `None`, the
639
+ underlying SYCL queue to be used. If both are `None`, the
631
640
`dpctl.SyclQueue()` is used for allocation and copying.
632
641
Default: `None`.
633
642
"""
@@ -683,7 +692,7 @@ def full(
683
692
for output array allocation and copying. `sycl_queue` and `device`
684
693
are exclusive keywords, i.e. use one or another. If both are
685
694
specified, a `TypeError` is raised unless both imply the same
686
- underlying SYCL queue to be used. If both a `None`, the
695
+ underlying SYCL queue to be used. If both are `None`, the
687
696
`dpctl.SyclQueue()` is used for allocation and copying.
688
697
Default: `None`.
689
698
"""
@@ -733,7 +742,7 @@ def empty_like(
733
742
for output array allocation and copying. `sycl_queue` and `device`
734
743
are exclusive keywords, i.e. use one or another. If both are
735
744
specified, a `TypeError` is raised unless both imply the same
736
- underlying SYCL queue to be used. If both a `None`, the
745
+ underlying SYCL queue to be used. If both are `None`, the
737
746
`dpctl.SyclQueue()` is used for allocation and copying.
738
747
Default: `None`.
739
748
"""
@@ -790,7 +799,7 @@ def zeros_like(
790
799
for output array allocation and copying. `sycl_queue` and `device`
791
800
are exclusive keywords, i.e. use one or another. If both are
792
801
specified, a `TypeError` is raised unless both imply the same
793
- underlying SYCL queue to be used. If both a `None`, the
802
+ underlying SYCL queue to be used. If both are `None`, the
794
803
`dpctl.SyclQueue()` is used for allocation and copying.
795
804
Default: `None`.
796
805
"""
@@ -847,7 +856,7 @@ def ones_like(
847
856
for output array allocation and copying. `sycl_queue` and `device`
848
857
are exclusive keywords, i.e. use one or another. If both are
849
858
specified, a `TypeError` is raised unless both imply the same
850
- underlying SYCL queue to be used. If both a `None`, the
859
+ underlying SYCL queue to be used. If both are `None`, the
851
860
`dpctl.SyclQueue()` is used for allocation and copying.
852
861
Default: `None`.
853
862
"""
@@ -911,7 +920,7 @@ def full_like(
911
920
for output array allocation and copying. `sycl_queue` and `device`
912
921
are exclusive keywords, i.e. use one or another. If both are
913
922
specified, a `TypeError` is raised unless both imply the same
914
- underlying SYCL queue to be used. If both a `None`, the
923
+ underlying SYCL queue to be used. If both are `None`, the
915
924
`dpctl.SyclQueue()` is used for allocation and copying.
916
925
Default: `None`.
917
926
"""
@@ -942,3 +951,82 @@ def full_like(
942
951
usm_type = usm_type ,
943
952
sycl_queue = sycl_queue ,
944
953
)
954
+
955
+
956
+ def linspace (
957
+ start ,
958
+ stop ,
959
+ / ,
960
+ num ,
961
+ * ,
962
+ dtype = None ,
963
+ device = None ,
964
+ endpoint = True ,
965
+ sycl_queue = None ,
966
+ usm_type = "device" ,
967
+ ):
968
+ """
969
+ linspace(start, stop, num, dtype=None, device=None, endpoint=True,
970
+ sycl_queue=None, usm_type=None): usm_ndarray
971
+
972
+ Returns evenly spaced numbers of specified interval.
973
+
974
+ Args:
975
+ start: the start of the interval.
976
+ stop: the end of the interval. If the `endpoint` is `False`, the
977
+ function must generate `num+1` evenly spaced points starting
978
+ with `start` and ending with `stop` and exclude the `stop`
979
+ from the returned array such that the returned array consists
980
+ of evenly spaced numbers over the half-open interval
981
+ `[start, stop)`. If `endpoint` is `True`, the output
982
+ array must consist of evenly spaced numbers over the closed
983
+ interval `[start, stop]`. Default: `True`.
984
+ num: number of samples. Must be a non-negative integer; otherwise,
985
+ the function must raise an exception.
986
+ dtype: output array data type. Should be a floating data type.
987
+ If `dtype` is `None`, the output array must be the default
988
+ floating point data type. Default: `None`.
989
+ device (optional): array API concept of device where the output array
990
+ is created. `device` can be `None`, a oneAPI filter selector string,
991
+ an instance of :class:`dpctl.SyclDevice` corresponding to a
992
+ non-partitioned SYCL device, an instance of
993
+ :class:`dpctl.SyclQueue`, or a `Device` object returnedby
994
+ `dpctl.tensor.usm_array.device`. Default: `None`.
995
+ usm_type ("device"|"shared"|"host", optional): The type of SYCL USM
996
+ allocation for the output array. Default: `"device"`.
997
+ sycl_queue (:class:`dpctl.SyclQueue`, optional): The SYCL queue to use
998
+ for output array allocation and copying. `sycl_queue` and `device`
999
+ are exclusive keywords, i.e. use one or another. If both are
1000
+ specified, a `TypeError` is raised unless both imply the same
1001
+ underlying SYCL queue to be used. If both are `None`, the
1002
+ `dpctl.SyclQueue()` is used for allocation and copying.
1003
+ Default: `None`.
1004
+ endpoint: boolean indicating whether to include `stop` in the
1005
+ interval. Default: `True`.
1006
+ """
1007
+ sycl_queue = normalize_queue_device (sycl_queue = sycl_queue , device = device )
1008
+ dpctl .utils .validate_usm_type (usm_type , allow_none = False )
1009
+ if endpoint not in [True , False ]:
1010
+ raise TypeError ("endpoint keyword argument must be of boolean type" )
1011
+ num = operator .index (num )
1012
+ if num < 0 :
1013
+ raise ValueError ("Number of points must be non-negative" )
1014
+ ((start , stop ,), dt ) = _coerce_and_infer_dt (
1015
+ start ,
1016
+ stop ,
1017
+ dt = dtype ,
1018
+ sycl_queue = sycl_queue ,
1019
+ err_msg = "start and stop must be Python scalars." ,
1020
+ allow_bool = True ,
1021
+ )
1022
+ if dtype is None and np .issubdtype (dt , np .integer ):
1023
+ dt = ti .default_device_fp_type (sycl_queue )
1024
+ dt = np .dtype (dt )
1025
+ start = float (start )
1026
+ stop = float (stop )
1027
+ res = dpt .empty (num , dtype = dt , sycl_queue = sycl_queue )
1028
+ hev , _ = ti ._linspace_affine (
1029
+ start , stop , dst = res , include_endpoint = endpoint , sycl_queue = sycl_queue
1030
+ )
1031
+ hev .wait ()
1032
+ return res
0 commit comments