MCPcopy Index your code
hub / github.com/pytorch/pytorch / array

Function array

torch/_numpy/_ndarray.py:485–522  ·  view source on GitHub ↗
(obj, dtype=None, *, copy=True, order="K", subok=False, ndmin=0, like=None)

Source from the content-addressed store, hash-verified

483
484
485def array(obj, dtype=None, *, copy=True, order="K", subok=False, ndmin=0, like=None):
486 if subok is not False:
487 raise NotImplementedError("'subok' parameter is not supported.")
488 if like is not None:
489 raise NotImplementedError("'like' parameter is not supported.")
490 if order != "K":
491 raise NotImplementedError()
492
493 # a happy path
494 if (
495 isinstance(obj, ndarray)
496 and copy is False
497 and dtype is None
498 and ndmin <= obj.ndim
499 ):
500 return obj
501
502 if isinstance(obj, (list, tuple)):
503 # FIXME and they have the same dtype, device, etc
504 if obj and all(isinstance(x, torch.Tensor) for x in obj):
505 # list of arrays: *under torch.Dynamo* these are FakeTensors
506 obj = torch.stack(obj)
507 else:
508 # XXX: remove tolist
509 # lists of ndarrays: [1, [2, 3], ndarray(4)] convert to lists of lists
510 obj = _tolist(obj)
511
512 # is obj an ndarray already?
513 if isinstance(obj, ndarray):
514 obj = obj.tensor
515
516 # is a specific dtype requested?
517 torch_dtype = None
518 if dtype is not None:
519 torch_dtype = _dtypes.dtype(dtype).torch_dtype
520
521 tensor = _util._coerce_to_tensor(obj, torch_dtype, copy, ndmin)
522 return ndarray(tensor)
523
524
525def asarray(a, dtype=None, order="K", *, like=None):

Callers 15

assert_array_compareFunction · 0.90
test_0D_arrayMethod · 0.90
test_1D_arrayMethod · 0.90
test_2D_arrayMethod · 0.90
test_3D_arrayMethod · 0.90
test_0D_arrayMethod · 0.90
test_1D_arrayMethod · 0.90
test_2D_arrayMethod · 0.90
test_3D_arrayMethod · 0.90
test_0D_arrayMethod · 0.90
test_1D_arrayMethod · 0.90

Calls 5

isinstanceFunction · 0.85
_tolistFunction · 0.85
ndarrayClass · 0.85
allFunction · 0.70
dtypeMethod · 0.45

Tested by 15

test_0D_arrayMethod · 0.72
test_1D_arrayMethod · 0.72
test_2D_arrayMethod · 0.72
test_3D_arrayMethod · 0.72
test_0D_arrayMethod · 0.72
test_1D_arrayMethod · 0.72
test_2D_arrayMethod · 0.72
test_3D_arrayMethod · 0.72
test_0D_arrayMethod · 0.72
test_1D_arrayMethod · 0.72
test_2D_arrayMethod · 0.72
test_3D_arrayMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…