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: Extract Duplicate Code into Helper Function in ivy\func_wrapper.py #26572

Merged
merged 11 commits into from
Nov 30, 2023

Conversation

Sai-Suraj-27
Copy link
Contributor

@Sai-Suraj-27 Sai-Suraj-27 commented Oct 4, 2023

PR Description

I identified duplicate sections of code in the following functions:
https://github.com/unifyai/ivy/blob/2bfb19a2ca3b277e95cba987353aa349ef28015e/ivy/func_wrapper.py#L94
https://github.com/unifyai/ivy/blob/2bfb19a2ca3b277e95cba987353aa349ef28015e/ivy/func_wrapper.py#L137

So, I extracted these into their own method/function to increase the readability and decrease the amount of duplicate/repeating code in the file. This refactoring simplifies the code, and reduces redundancy.

Related Issue

Close #26573

Checklist

  • Did you add a function?
  • Did you add the tests?
  • Did you run your tests and are your tests passing?
  • Did pre-commit not fail on any check?
  • Did you follow the steps we provided?

Socials

https://twitter.com/Sai_Suraj_27

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 Passed!

@github-actions
Copy link
Contributor

github-actions bot commented Oct 4, 2023

Thank you for this PR, here is the CI results:


This pull request does not result in any additional test failures. Congratulations!

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.

The changes look good to me, but I'll request @RickSanchezStoic's review just in case this breaks the casting modes. Thanks @Sai-Suraj-27 😄

return downcast_helper("complex", dtype, "complex", intersect)


def downcast_helper(arg0, dtype, arg2, intersect):
Copy link
Contributor

Choose a reason for hiding this comment

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

let's change this to arg1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@RickSanchezStoic sir, I made the requested changes. Please check now, Thank you.



def downcast_helper(arg0, dtype, arg2, intersect):
index = casting_modes_dict[arg0]().index(dtype) - 1
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@Sai-Suraj-27
Copy link
Contributor Author

@RickSanchezStoic sir, its been a week since I made the requested changes. Please look into the PR when you are free, Thank you.

@ivy-seed ivy-seed assigned Dsantra92 and unassigned umairjavaid Oct 14, 2023
@vedpatwardhan
Copy link
Contributor

Hey @Sai-Suraj-27, can we refactor this further to have a single helper which receives a flag about whether to use the upcast or downcast logic? Seems like the >=0 and <len(casting_modes_dict[arg0]()) condition in the while loop and the index +=1 and index -= 1 are the only 2 things different in the 2 helpers. What do you think? Thanks 😄

@Sai-Suraj-27
Copy link
Contributor Author

@vedpatwardhan sir, After observing carefully I see that only in the following case of if "uint" in str(dtype) we are using "int" instead of "uint" again. In all other cases of both downcaster and upcaster functions, we are using the same dtype for the whole condition. So, my doubt is, should it be actually "uint" instead of "int" on line 143? or is it "int" only? I think it should be "uint" but I'm not sure.

https://github.com/unifyai/ivy/blob/19cb1a48f27cf4b3ed27b07fb81b7d7a10d52fba/ivy/func_wrapper.py#L139
https://github.com/unifyai/ivy/blob/19cb1a48f27cf4b3ed27b07fb81b7d7a10d52fba/ivy/func_wrapper.py#L143
Regarding the further refactoring into a single helper function, Sure. I will immediately do it after I get clarity on this, Thank you.

@vedpatwardhan
Copy link
Contributor

I think that's just a mistake, can't really think of a reason why it would be int instead of uint on
https://github.com/unifyai/ivy/blob/19cb1a48f27cf4b3ed27b07fb81b7d7a10d52fba/ivy/func_wrapper.py#L143
Good catch @Sai-Suraj-27 😄

@Sai-Suraj-27
Copy link
Contributor Author

@vedpatwardhan Sorry for the late response, I have refactored further to use only a single helper function. Please check now.

index = casting_modes_dict[arg]().index(dtype) + step
result = ""
if is_upcast:
while index < len(casting_modes_dict[arg]()):
Copy link
Contributor

Choose a reason for hiding this comment

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

ig we could replace this with a single loop instead of 2 where the condition would change based on the value of is_upcast. What do you think?

Copy link
Contributor Author

@Sai-Suraj-27 Sai-Suraj-27 Oct 19, 2023

Choose a reason for hiding this comment

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

I thought of the same but I wasn't sure if index will be always >= 0. If the index value is always >= 0, then we can remove the if-else condition and merge the 2 loops to the following:

result = ""
while 0 <= index < len(casting_modes_dict[arg]()):
   if casting_modes_dict[arg]()[index] not in intersect:
       result = casting_modes_dict[arg]()[index]
       break
   index += step
return result

Please tell me your view, Thank you.

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 it's safe to assume that index will always be >=0 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made the changes. Can you please look into it now, Thank you.

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.

lgtm! Feel free to merge the PR @Dsantra92, thanks @Sai-Suraj-27 😄

@Sai-Suraj-27
Copy link
Contributor Author

Hey, @vedpatwardhan You approved this PR almost a month back but no one merged it. Can you please merge this, thank you.

@vedpatwardhan
Copy link
Contributor

Yep merging the PR now, thanks @Sai-Suraj-27 😄

@vedpatwardhan vedpatwardhan merged commit 99f184d into ivy-llc:main Nov 30, 2023
261 of 269 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extract Duplicate Code into Helper Function in ivy\func_wrapper.py
5 participants