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

refactor: remove infer_device and rename handle_device_shifting decorator #23373

Merged
merged 19 commits into from
Sep 26, 2023

Conversation

ShreyanshBardia
Copy link
Contributor

@ShreyanshBardia ShreyanshBardia commented Sep 10, 2023

PR Description

Related Issue

Close task

Checklist

  • Did you add a function?
  • Did you add the tests?
  • Did you follow the steps we provided?

Socials:

@github-actions
Copy link
Contributor

Thanks for contributing to Ivy! 😊👏
Here are some of the important points from our Contributing Guidelines 📝:
1. Feel free to ignore the run_tests (1), run_tests (2), … jobs, and only look at the display_test_results job. 👀 It contains the following two sections:
- Combined Test Results: This shows the results of all the ivy tests that ran on the PR. ✔️
- New Failures Introduced: This lists the tests that are passing on main, but fail on the PR Fork. Please try to make sure that there are no such tests. 💪
2. The lint / Check formatting / check-formatting tests check for the formatting of your code. 📜 If it fails, please check the exact error message in the logs and fix the same. ⚠️🔧
3. Finally, the test-docstrings / run-docstring-tests check for the changes made in docstrings of the functions. This may be skipped, as well. 📚
Happy coding! 🎉👨‍💻

@ivy-leaves ivy-leaves added Ivy Functional API Ivy API Experimental Run CI for testing API experimental/New feature or request labels Sep 10, 2023
@ivy-leaves
Copy link

If you are working on an open task, please edit the PR description to link to the issue you've created.

For more information, please check ToDo List Issues Guide.

Thank you 🤗

@ShreyanshBardia ShreyanshBardia changed the title Refactor decorator remove infer_device and rename handle_device_shifting decorator Sep 10, 2023
@ShreyanshBardia ShreyanshBardia changed the title remove infer_device and rename handle_device_shifting decorator remove infer_device and rename handle_device_shifting decorator Sep 10, 2023
Copy link
Contributor

@vedpatwardhan vedpatwardhan left a comment

Choose a reason for hiding this comment

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

Looks good to me on a high level, but I'll leave this with @sherry30 to review as he's the author of the task. Thanks @ShreyanshBardia @sherry30 😄

Copy link
Contributor

@sherry30 sherry30 left a comment

Choose a reason for hiding this comment

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

Hi Shreyansh,
can you run the following tests and make sure its working fine on your PR.

# testing device argument ##full_like ##arange
import ivy
# ivy.set_backend("paddle")
# ivy.set_backend("tensorflow")
# ivy.set_backend("jax")
ivy.set_backend("torch")
# ivy.set_backend("numpy")

ivy.set_soft_device_mode(False)
ivy.set_default_device("cpu")
x = ivy.array([1, 2, 3], device="cpu")
gpu_dev = "gpu:0"

# device is passed with arrays ## device
y = ivy.full_like(x, 3, device=gpu_dev)
print(y.device)

# device is passed without arrays ##device
y = ivy.arange(0, 10, device=gpu_dev)
print(y.device)

# device is not passed with arrays ##array dev
y = ivy.full_like(x, 3)
print(y.device)

# device is not passed without arryas ##default
y = ivy.arange(0, 10)
print(y.device)


# testing device argument ##full_like ##arange # soft_device mode True

import ivy
# ivy.set_backend("paddle")
# ivy.set_backend("tensorflow")
# ivy.set_backend("jax")
ivy.set_backend("torch")
# ivy.set_backend("numpy")

ivy.set_soft_device_mode(True)
ivy.set_default_device("gpu:0")
x = ivy.array([1, 2, 3], device="cpu")
cpu_dev = "cpu"

# device is passed with arrays ## device
y = ivy.full_like(x, 3, device=cpu_dev)
print(y.device)

# device is passed without arrays ##device
y = ivy.arange(0, 10, device=cpu_dev)
print(y.device)

# device is not passed with arrays ##default
y = ivy.full_like(x, 3)
print(y.device)

# device is not passed without arryas ##default
y = ivy.arange(0, 10)
print(y.device)

If it works fine on these then its good to merge
Another thing we have to make sure.
In all the backend functions, the device argument is not being used anywhere (for functions that accept a device argument).
torch is an exception of this.
Let me know if you have any questions

@ShreyanshBardia

This comment was marked as resolved.

@ShreyanshBardia

This comment was marked as resolved.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

PR Compliance Checks

Thank you for your Pull Request! We have run several checks on this pull request in order to make sure it's suitable for merging into this project. The results are listed in the following section.

Issue Reference

In order to be considered for merging, the pull request description must refer to a specific issue number. This is described in our contributing guide and our PR template.
This check is looking for a phrase similar to: "Fixes #XYZ" or "Resolves #XYZ" where XYZ is the issue number that this PR is meant to address.

@ShreyanshBardia

This comment was marked as resolved.

@ShreyanshBardia ShreyanshBardia changed the title remove infer_device and rename handle_device_shifting decorator refactor: remove infer_device and rename handle_device_shifting decorator Sep 13, 2023
@ShreyanshBardia
Copy link
Contributor Author

ShreyanshBardia commented Sep 13, 2023

I don't understand this statement of yours
Another thing we have to make sure. In all the backend functions, the device argument is not being used anywhere (for functions that accept a device argument).

Do you mean we shouldn't be passing device through kwargs also? Because this is what is happening currently which is generating error because in creation functions device is a required keyword argument. Either we need to assign a default value to device if it is of no use or we need to pass device in kwargs similar to pytorch

@sherry30
Copy link
Contributor

No what I meant was, the functions will still have the device argument in the backend, but it will be unused in all the backends.
Since we removed manual device shifting, it shouldn't be used anywhere in the backend functions with torch backend as an exception.

About the other problem, yea I guess we can give it a default value of None in all the backend functions. because there's no need to pass a device argument to them everytime since infer_device is removed.
What do you think @vedpatwardhan

@vedpatwardhan
Copy link
Contributor

No what I meant was, the functions will still have the device argument in the backend, but it will be unused in all the backends. Since we removed manual device shifting, it shouldn't be used anywhere in the backend functions with torch backend as an exception.

About the other problem, yea I guess we can give it a default value of None in all the backend functions. because there's no need to pass a device argument to them everytime since infer_device is removed. What do you think @vedpatwardhan

I'm not completely sure if I understand this, but do you mean given that we've got the context manager working with the device shifting decorator we wouldn't need to pass the device argument to creation functions in the backends for e.g.? Certainly if those functions also operate on the correct device through the context manager, we should consider not using the device argument in the implementation of creation functions either, but there were a few corner cases right? Do they play a role here?
Thanks @sherry30 😄

@sherry30
Copy link
Contributor

sherry30 commented Sep 19, 2023

@vedpatwardhan Yep that's what I meant, the corner cases are in torch, but we can still pass in the default value of device as None just for consistency. The value of device will be passed by the handle_soft_device_variable in the torch backend.

@vedpatwardhan
Copy link
Contributor

sounds good! As long as the op for functions using infer_device currently is executed on the expected device, this approach works for me. Thanks for clarifying @sherry30 😄

@ShreyanshBardia
Copy link
Contributor Author

@sherry30 should I update default values to None then?

@sherry30
Copy link
Contributor

Yes, you can set them as None by default @ShreyanshBardia

@ShreyanshBardia
Copy link
Contributor Author

@sherry30 can you take a look now

Copy link
Contributor

@sherry30 sherry30 left a comment

Choose a reason for hiding this comment

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

lgtm!
Lets get one final review from @vedpatwardhan before merging

Thanks

Copy link
Contributor

@vedpatwardhan vedpatwardhan left a comment

Choose a reason for hiding this comment

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

I haven't one through the code line by line but seems good to merge, thanks @ShreyanshBardia @sherry30 😄

@ShreyanshBardia ShreyanshBardia merged commit f019232 into ivy-llc:main Sep 26, 2023
114 of 137 checks passed
@ShreyanshBardia ShreyanshBardia deleted the refactor-decorator branch September 26, 2023 11:46
iababio pushed a commit to iababio/ivy that referenced this pull request Sep 27, 2023
druvdub pushed a commit to druvdub/ivy that referenced this pull request Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ivy API Experimental Run CI for testing API experimental/New feature or request Ivy Functional API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants