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(frontends): Add nanmedian to PyTorch reduction ops #23394

Closed
wants to merge 0 commits into from

Conversation

spacefarers
Copy link
Contributor

@spacefarers spacefarers commented Sep 11, 2023

PR Description

Add nanmedian to frontend torch by basically copying nanmean since it is implemented in ivy.nanmedian. First time contributor please tell me if I did something wrong.

Related Issue

Close #23397

Checklist

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

@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 the PyTorch Frontend Developing the PyTorch Frontend, checklist triggered by commenting add_frontend_checklist label Sep 11, 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 🤗

Copy link
Contributor

@hmahmood24 hmahmood24 left a comment

Choose a reason for hiding this comment

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

Hey @spacefarers, thanks a lot for the PR. 😄 Before we can proceed, we need to fix the failing tests for the function you've added. We also need to test the function for a wider range of input values as mentioned in the comment I left. Can you address these points and request a re-review once done? Thanks! :)

min_value=-1e04,
max_value=1e04,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we testing for such a small range of the input values?

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.

Protected Branch

In order to be considered for merging, the pull request changes must not be implemented on the "main" branch. This is described in our Contributing Guide. We are closing this pull request and we would suggest that you implement your changes as described in our Contributing Guide and open a new pull request.

@hmahmood24 hmahmood24 changed the title Add nanmedian to reduction ops feat(frontends): Add nanmedian to PyTorch reduction ops Sep 20, 2023
@hmahmood24
Copy link
Contributor

@spacefarers, can you also fix the failing tests on this PR? Thanks

@spacefarers
Copy link
Contributor Author

Thank you for your comments. I'm referencing nanmean as well as median in the frontend functions to try to create this one. I'm confused as to why median was not calling ivy.median but instead is doing its own logic. Could you explain this to me? Does the datatypes not work? Would I need to do the same for nanmedian?

@hmahmood24
Copy link
Contributor

@spacefarers Not sure what you mean by

I'm confused as to why median was not calling ivy.median but instead is doing its own logic.

How do you arrive at this conclusion? Also, I think you have overwritten the existing test of nansum on the PR by mistake

@spacefarers
Copy link
Contributor Author

Here is the current implementation of median for pytorch frontend

@numpy_to_torch_style_args
@to_ivy_arrays_and_back
def median(input, dim=None, keepdim=False, *, out=None):
    if dim is None:
        input = ivy.reshape(input, (-1,))
        sorted_input = ivy.sort(input)
        return sorted_input[(sorted_input.shape[0] - 1) // 2]

    median_tuple = namedtuple("median", ["values", "indices"])

    if input.ndim == 0:
        result = median_tuple(input, ivy.array(0))
    else:
        sorted_indices = ivy.argsort(input, axis=dim)
        median_indices = ivy.gather(
            sorted_indices, (sorted_indices.shape[dim] - 1) // 2, axis=dim
        )
        median_values = ivy.take_along_axis(
            input, ivy.expand_dims(median_indices, axis=dim), dim
        ).squeeze(axis=dim)

        if keepdim:
            median_values = ivy.expand_dims(median_values, axis=dim)
            median_indices = ivy.expand_dims(median_indices, axis=dim)

        result = median_tuple(median_values, median_indices)
    if out is not None:
        ivy.inplace_update(out[0], result.values)
        ivy.inplace_update(out[1], result.indices)
        return out
    return result

Why is it not simply calling ivy.median?

Thanks for the notice about nansum, I'll add that in and be more careful next time!

@spacefarers
Copy link
Contributor Author

If you have a minute to answer a few questions via discord that'd be awesome. I've been stuck on this for a while now.

@hmahmood24
Copy link
Contributor

If you have a minute to answer a few questions via discord that'd be awesome. I've been stuck on this for a while now.

Sure feel free to reach out on discord

@spacefarers
Copy link
Contributor Author

Cool what's your username? Mine is the same as github it should be @spacefarers

@hmahmood24
Copy link
Contributor

Cool what's your username? Mine is the same as github it should be @spacefarers

Same as my name :)

@spacefarers
Copy link
Contributor Author

Awesome sent you a friend request

)
def test_torch_nansum(
def test_torch_nanmedian(
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: is there a reason to remove the nansum? or was it done by mistake?

@@ -2,7 +2,7 @@ name: test-ivy-cron
on:
workflow_dispatch:
schedule:
- cron: '30 * * * *'
- cron: '0 8 * * *'
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: Was this cron change on purpose? I think if we are going to change it we might need to talk with the CI team, otherwise we can just revert this changes.

@spacefarers
Copy link
Contributor Author

I apologize those are all mistakes. I'm working on the nanmedian frontend function and mistakendly affected some other things. I'll change those back along with my updated nanmedian function. Sorry again for the inconvenience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PyTorch Frontend Developing the PyTorch Frontend, checklist triggered by commenting add_frontend_checklist
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nanmedian
4 participants