(obj, dtype=None, *, copy=True, order="K", subok=False, ndmin=0, like=None)
| 483 | |
| 484 | |
| 485 | def 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 | |
| 525 | def asarray(a, dtype=None, order="K", *, like=None): |
searching dependent graphs…