1919
2020import  dpctl .memory  as  dpm 
2121import  dpctl .tensor  as  dpt 
22+ from  dpctl .tensor ._device  import  normalize_queue_device 
2223
2324
2425def  contract_iter2 (shape , strides1 , strides2 ):
@@ -64,7 +65,7 @@ def contract_iter2(shape, strides1, strides2):
6465    return  (sh , st1 , disp1 , st2 , disp2 )
6566
6667
67- def  has_memory_overlap (x1 , x2 ):
68+ def  _has_memory_overlap (x1 , x2 ):
6869    m1  =  dpm .as_usm_memory (x1 )
6970    m2  =  dpm .as_usm_memory (x2 )
7071    if  m1 .sycl_device  ==  m2 .sycl_device :
@@ -77,7 +78,7 @@ def has_memory_overlap(x1, x2):
7778        return  False 
7879
7980
80- def  copy_to_numpy (ary ):
81+ def  _copy_to_numpy (ary ):
8182    if  type (ary ) is  not dpt .usm_ndarray :
8283        raise  TypeError 
8384    h  =  ary .usm_data .copy_to_host ().view (ary .dtype )
@@ -93,7 +94,7 @@ def copy_to_numpy(ary):
9394    )
9495
9596
96- def  copy_from_numpy (np_ary , usm_type = "device" , sycl_queue = None ):
97+ def  _copy_from_numpy (np_ary , usm_type = "device" , sycl_queue = None ):
9798    "Copies numpy array `np_ary` into a new usm_ndarray" 
9899    # This may peform a copy to meet stated requirements 
99100    Xnp  =  np .require (np_ary , requirements = ["A" , "O" , "C" , "E" ])
@@ -111,7 +112,8 @@ def copy_from_numpy(np_ary, usm_type="device", sycl_queue=None):
111112    return  Xusm 
112113
113114
114- def  copy_from_numpy_into (dst , np_ary ):
115+ def  _copy_from_numpy_into (dst , np_ary ):
116+     "Copies `np_ary` into `dst` of type :class:`dpctl.tensor.usm_ndarray" 
115117    if  not  isinstance (np_ary , np .ndarray ):
116118        raise  TypeError ("Expected numpy.ndarray, got {}" .format (type (np_ary )))
117119    src_ary  =  np .broadcast_to (np .asarray (np_ary , dtype = dst .dtype ), dst .shape )
@@ -122,6 +124,54 @@ def copy_from_numpy_into(dst, np_ary):
122124        usm_mem .copy_from_host (host_buf )
123125
124126
127+ def  from_numpy (np_ary , device = None , usm_type = "device" , sycl_queue = None ):
128+     """ 
129+     from_numpy(arg, device=None, usm_type="device", sycl_queue=None) 
130+ 
131+     Creates :class:`dpctl.tensor.usm_ndarray` from instance of 
132+     `numpy.ndarray`. 
133+ 
134+     Args: 
135+         arg: An instance of `numpy.ndarray` 
136+         device: array API specification of device where the output array 
137+             is created. 
138+         sycl_queue: a :class:`dpctl.SyclQueue` used to create the output 
139+             array is created 
140+     """ 
141+     q  =  normalize_queue_device (sycl_queue = sycl_queue , device = device )
142+     return  _copy_from_numpy (np_ary , usm_type = usm_type , sycl_queue = q )
143+ 
144+ 
145+ def  to_numpy (usm_ary ):
146+     """ 
147+     to_numpy(usm_ary) 
148+ 
149+     Copies content of :class:`dpctl.tensor.usm_ndarray` instance `usm_ary` 
150+     into `numpy.ndarray` instance of the same shape and same data type. 
151+ 
152+     Args: 
153+         usm_ary: An instance of :class:`dpctl.tensor.usm_ndarray` 
154+     Returns: 
155+         An instance of `numpy.ndarray` populated with content of `usm_ary`. 
156+     """ 
157+     return  _copy_to_numpy (usm_ary )
158+ 
159+ 
160+ def  asnumpy (usm_ary ):
161+     """ 
162+     asnumpy(usm_ary) 
163+ 
164+     Copies content of :class:`dpctl.tensor.usm_ndarray` instance `usm_ary` 
165+     into `numpy.ndarray` instance of the same shape and same data type. 
166+ 
167+     Args: 
168+         usm_ary: An instance of :class:`dpctl.tensor.usm_ndarray` 
169+     Returns: 
170+         An instance of `numpy.ndarray` populated with content of `usm_ary`. 
171+     """ 
172+     return  _copy_to_numpy (usm_ary )
173+ 
174+ 
125175class  Dummy :
126176    def  __init__ (self , iface ):
127177        self .__sycl_usm_array_interface__  =  iface 
@@ -138,9 +188,9 @@ def copy_same_dtype(dst, src):
138188        raise  ValueError 
139189
140190    # check that memory regions do not overlap 
141-     if  has_memory_overlap (dst , src ):
142-         tmp  =  copy_to_numpy (src )
143-         copy_from_numpy_into (dst , tmp )
191+     if  _has_memory_overlap (dst , src ):
192+         tmp  =  _copy_to_numpy (src )
193+         _copy_from_numpy_into (dst , tmp )
144194        return 
145195
146196    if  (dst .flags  &  1 ) and  (src .flags  &  1 ):
@@ -184,10 +234,10 @@ def copy_same_shape(dst, src):
184234        return 
185235
186236    # check that memory regions do not overlap 
187-     if  has_memory_overlap (dst , src ):
188-         tmp  =  copy_to_numpy (src )
237+     if  _has_memory_overlap (dst , src ):
238+         tmp  =  _copy_to_numpy (src )
189239        tmp  =  tmp .astype (dst .dtype )
190-         copy_from_numpy_into (dst , tmp )
240+         _copy_from_numpy_into (dst , tmp )
191241        return 
192242
193243    # simplify strides 
@@ -218,7 +268,7 @@ def copy_same_shape(dst, src):
218268        mdst .copy_from_host (tmp .view ("u1" ))
219269
220270
221- def  copy_from_usm_ndarray_to_usm_ndarray (dst , src ):
271+ def  _copy_from_usm_ndarray_to_usm_ndarray (dst , src ):
222272    if  type (dst ) is  not dpt .usm_ndarray  or  type (src ) is  not dpt .usm_ndarray :
223273        raise  TypeError 
224274
@@ -389,7 +439,7 @@ def astype(usm_ary, newdtype, order="K", casting="unsafe", copy=True):
389439                buffer = R .usm_data ,
390440                strides = new_strides ,
391441            )
392-         copy_from_usm_ndarray_to_usm_ndarray (R , usm_ary )
442+         _copy_from_usm_ndarray_to_usm_ndarray (R , usm_ary )
393443        return  R 
394444    else :
395445        return  usm_ary 
0 commit comments