2323 zeros_like ,
2424)
2525from .._dtypes import float32 , float64
26- from .._array_object import Array , CPU_DEVICE , Device
26+ from .._array_object import Array
27+ from .._devices import CPU_DEVICE , ALL_DEVICES , Device
28+ from .._info import __array_namespace_info__
2729from .._flags import set_array_api_strict_flags
2830
2931def test_asarray_errors ():
@@ -212,6 +214,7 @@ def test_zeros_like_errors():
212214 assert_raises (ValueError , lambda : zeros_like (asarray (1 ), dtype = int ))
213215 assert_raises (ValueError , lambda : zeros_like (asarray (1 ), dtype = "i" ))
214216
217+
215218def test_meshgrid_dtype_errors ():
216219 # Doesn't raise
217220 meshgrid ()
@@ -221,6 +224,57 @@ def test_meshgrid_dtype_errors():
221224 assert_raises (ValueError , lambda : meshgrid (asarray ([1. ], dtype = float32 ), asarray ([1. ], dtype = float64 )))
222225
223226
227+
228+ def _full (a , * args , ** kwds ):
229+ return full (a , fill_value = 42 , * args , ** kwds )
230+
231+
232+ def _full_like (a , * args , ** kwds ):
233+ return full_like (a , fill_value = 42 , * args , ** kwds )
234+
235+
236+ class TestDefaultDType :
237+
238+ info = __array_namespace_info__ ()
239+
240+ @pytest .mark .parametrize ("device" , ALL_DEVICES )
241+ @pytest .mark .parametrize ("func" , [empty , zeros , ones , _full ])
242+ def test_ones_etc (self , func , device ):
243+ a = func (1 , device = device )
244+ assert a .dtype == self .info .default_dtypes (device = device )["real floating" ]
245+
246+ @pytest .mark .parametrize ("func" , [empty_like , zeros_like , ones_like , _full_like ])
247+ def test_ones_like_etc_correct (self , func ):
248+ # float32 is preserved
249+ a = ones (2 , dtype = float32 )
250+ device = Device ('F32_device' )
251+ b = func (a , device = device )
252+ assert b .dtype == self .info .default_dtypes (device = device )["real floating" ]
253+
254+ @pytest .mark .parametrize ("func" , [empty_like , zeros_like , ones_like , _full_like ])
255+ def test_ones_like_etc_incorrect (self , func ):
256+ a = ones (2 )
257+ assert a .dtype == float64
258+ assert a .device == Device ()
259+
260+ # XXX: a.dtype not supported by the device: ValueError or TypeError?
261+
262+ # >>> a = torch.ones(3, dtype=torch.float64, device='cpu')
263+ # >>> torch.ones_like(a, device='mps')
264+ # TypeError: Cannot convert a MPS Tensor to float64 dtype as the MPS framework
265+ # doesn't support float64.
266+ with pytest .raises (TypeError ):
267+ func (a , device = Device ('F32_device' ))
268+ # TODO:
269+ # def asarray(
270+ # def arange(
271+ # def eye(
272+ # def linspace(
273+ # def meshgrid(*arrays: Array, indexing: Literal["xy", "ij"] = "xy") -> tuple[Array, ...]:
274+ # def tril(x: Array, /, *, k: int = 0) -> Array:
275+ # def triu(x: Array, /, *, k: int = 0) -> Array:
276+
277+
224278@pytest .mark .parametrize ("api_version" , ['2021.12' , '2022.12' , '2023.12' ])
225279def from_dlpack_2023_12 (api_version ):
226280 if api_version != '2022.12' :
0 commit comments