Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy kwarg to asarray #991

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change default value for copy to None
  • Loading branch information
neosunhan committed Aug 29, 2022
commit 3503889aa062880b1324dc041acdb960b9893295
10 changes: 9 additions & 1 deletion heat/core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def array(
def asarray(
obj: Iterable,
dtype: Optional[Type[datatype]] = None,
copy: bool = False,
copy: bool = None,
neosunhan marked this conversation as resolved.
Show resolved Hide resolved
order: str = "C",
is_split: Optional[bool] = None,
device: Optional[Union[str, Device]] = None,
Expand Down Expand Up @@ -486,6 +486,14 @@ def asarray(
>>> ht.asarray(a, dtype=ht.float64) is a
False
"""
if isinstance(copy, bool) and not copy:
if not (
isinstance(obj, DNDarray)
and (dtype is None or dtype == obj.dtype)
and (is_split is None or is_split == obj.split)
and (device is None or device == obj.device)
):
raise ValueError("copy is necessary")
Comment on lines +497 to +504
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is too broad. In some cases, numpy arrays and torch tensors can also be used without copying.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the check fits better in the array function where the real logic is.

return array(obj, dtype=dtype, copy=copy, order=order, is_split=is_split, device=device)


Expand Down
3 changes: 3 additions & 0 deletions heat/core/tests/test_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ def test_asarray(self):
asarr[0] = 0
self.assertEqual(asarr.larray[0].item(), arr[0].item())

with self.assertRaises(ValueError):
ht.asarray([1, 2, 3, 4], copy=False)

def test_empty(self):
# scalar input
simple_empty_float = ht.empty(3)
Expand Down