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

feat: add cpu function #21255

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8178af7
cpu pytorch frontend
Prajyot70 Aug 2, 2023
6257d55
Update cpu pytorch frontend
Prajyot70 Aug 9, 2023
628b7da
Merge branch 'unifyai:master' into patch-2
Prajyot70 Aug 9, 2023
d8ce0bc
Create test_tensor
Prajyot70 Aug 10, 2023
265956b
Rename cpu pytorch frontend to functional/frontend/torch/torch.py
Prajyot70 Aug 10, 2023
577e193
Delete torch.py
Prajyot70 Aug 13, 2023
b4b7165
Create cpu
Prajyot70 Aug 18, 2023
1a298a4
Update tensor.py
Prajyot70 Sep 1, 2023
7b8ae32
Update test_tensor.py
Prajyot70 Sep 1, 2023
4d72bc0
Update torch.py
Prajyot70 Sep 2, 2023
db0c288
Delete ivy_tests/test_ivy/test_frontends/test_torch/test_tensor
Prajyot70 Sep 2, 2023
2f2ad38
Delete ivy/functional/frontends/torch/torch.py
Prajyot70 Sep 2, 2023
44688cc
Update tensor.py
Prajyot70 Sep 9, 2023
b86109b
Update test_tensor.py
Prajyot70 Sep 9, 2023
32fc93f
lint
NripeshN Dec 6, 2023
44812e5
🤖 Lint code
ivy-branch Dec 6, 2023
f568a40
Merge branch 'main' into patch-2
Prajyot70 Dec 21, 2023
d8e747e
Update tensor.py
Prajyot70 Dec 21, 2023
1dcb9ed
Update test_tensor.py
Prajyot70 Dec 21, 2023
2747f29
🤖 Lint code
ivy-branch Dec 21, 2023
61d3859
Update test_tensor.py
Prajyot70 Dec 22, 2023
bc18489
🤖 Lint code
ivy-branch Dec 22, 2023
1e765d8
Update test_tensor.py
Prajyot70 Dec 22, 2023
602cc21
🤖 Lint code
ivy-branch Dec 22, 2023
6193216
Update test_tensor.py
Prajyot70 Dec 23, 2023
311532b
Update test_tensor.py
Prajyot70 Dec 27, 2023
bfa1299
🤖 Lint code
ivy-branch Dec 27, 2023
aecd4f9
Update test_tensor.py
Prajyot70 Dec 27, 2023
4befcf7
🤖 Lint code
ivy-branch Dec 27, 2023
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
10 changes: 10 additions & 0 deletions ivy/functional/frontends/torch/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,16 @@ def acos_(self):
self.ivy_array = self.acos().ivy_array
return self

@to_ivy_arrays_and_back
def to_cpu(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Use correct name as mentioned in open issues list here: #3612 or here in original functions here https://pytorch.org/docs/stable/generated/torch.Tensor.cpu.html#torch.Tensor.cpu.

if (
ivy_framework.current_framework_str() == "torch"
and ivy_framework.current_device_str() != "cpu"
):
return ivy.to_device(self, "cpu")
else:
return self

def new_tensor(
self,
data,
Expand Down
57 changes: 56 additions & 1 deletion ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@
_quantile_helper,
)

import unittest
import torch
from unittest.mock import patch
from ivy_test import helpers
from ivy_test.helpers import CLASS_TREE, handle_frontend_methodtry:

try:
import torch
import torch
except ImportError:
torch = SimpleNamespace()

Expand Down Expand Up @@ -7831,6 +7837,55 @@ def test_torch_index_select(
)


#cpu
@handle_frontend_method
class TestTorchInstanceToCPU(unittest.TestCase):(
Copy link
Contributor

Choose a reason for hiding this comment

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

why have you implemented the test in this fashion, any reason?

Copy link
Contributor

Choose a reason for hiding this comment

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

If not, stick to the way other test functions are implemented.
Thanks

class_tree=CLASS_TREE,
init_tree="torch.tensor",
method_name="ivy.to_device",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
num_arrays=1,
min_value=-1e04,
max_value=1e04,
allow_inf=False,
),
)
def test_torch_instance_to_cpu(
self,
dtype_and_x,
frontend,
backend_fw,
frontend_method_data,
init_flags,
method_flags,
):
input_dtype, x = dtype_and_x
with patch("ivy_framework.current_framework_str", return_value="torch"), \
patch("ivy_framework.current_device_str", return_value="cpu"):
instance = frontend.init_all_as_kwargs_np(
input_dtypes=input_dtype, data=x[0]
)

result = frontend.frontend_method_data(
instance, method_name="to_cpu", input_dtypes=input_dtype
)

self.assertTrue(torch.all(result.data.cpu() == instance.data.cpu()))

with patch("ivy_framework.current_framework_str", return_value="numpy"):
result = frontend.frontend_method_data(
instance, method_name="to_cpu", input_dtypes=input_dtype
)


self.assertEqual(result, instance)


if __name__ == "__main__":
unittest.main()

Comment on lines +7885 to +7887
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these lines are unnecessary.


# int
@handle_frontend_method(
class_tree=CLASS_TREE,
Expand Down
Loading