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: added unflatten frontend and backend support #27416

Merged
merged 81 commits into from
Jan 22, 2024

Conversation

Kacper-W-Kozdon
Copy link
Contributor

@Kacper-W-Kozdon Kacper-W-Kozdon commented Nov 30, 2023

feat: added unflatten Pytorch frontend and backend support

Currently added ivy.unflatten, unflatten for torch backend and unflatten in ivy/frontends/torch. Still requires unflatten (or the corresponding functions) in other backends.

Closes #22252

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

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!

@Kacper-W-Kozdon
Copy link
Contributor Author

Kacper-W-Kozdon commented Nov 30, 2023

Reformatting Task Checklist

IMPORTANT NOTICE 🚨:

The Ivy Docs represent the ground truth for the task descriptions and this checklist should only be used as a supplementary item to aid with the review process.

LEGEND 🗺:

  • ❌ : Check item is not completed.
  • ✅ : Check item is ready for review.
  • 🆘 : Stuck/Doubting implementation (PR author should add comments explaining why).
  • ⏩ : Check is not applicable to function (skip).
  • 🆗 : Check item is already implemented and does not require any edits.

CHECKS 📑:

    • ❌: Make sure that the aforementioned methods are added into the correct category-specific parent class, such as ivy.ArrayWithElementwise, ivy.ContainerWithManipulation etc.
    • ✅: Add the correct Docstrings to every function and its relevant methods, including those you did not implement yourself. The following should be added:
        • ❌: The function's Array API standard description in ivy/functional/miscellaneous_operations.py. If the function is not part of the Array API standard then a description of similar style should be added to the same file.
          The following modifications should be made to the description:
          • ✅: Remove type definitions in the Parameters and Returns sections.
          • ❌: Add out to the Parameters section if function accepts an out argument.
          • ✅: Replace out with ret in the Returns section.
    • ✅: Add thorough Docstring Examples for every function and its relevant methods and ensure they pass the docstring tests.

      Functional Examples in ivy/functional/miscellaneous_operations.py.

        • ❌: Cover all possible variants for each of the arguments independently (not combinatorily).
        • ✅: Vary the values and input shapes considerably between examples.
        • ❌: Start out simple and get more complex with each example.
        • ❌: Show an example with:
          • ❌: out unused.
          • ❌: out used to update a new array y.
          • ❌: out used to inplace update the input array x (if x has the same dtype and shape as the return).
        • ❌: If broadcasting is relevant for the function, then show examples which highlight this.

      Nestable Function Examples in ivy/functional/miscellaneous_operations.py.
      Only if the function supports nestable operations.

        • ❌: Add an example that passes in an ivy.Container instance in place of one of the arguments.
        • ❌: Add an example passes in ivy.Container instances for multiple arguments.

      Container Static Method Examples in ivy/container/miscellaneous_operations.py.

        • ❌: The example from point (6.f) should be replicated, but added to the ivy.Container static method docstring in with ivy.<func_name> replaced with ivy.Container.static_<func_name> in the example.
        • ❌: The example from point (6.g) should be replicated, but added to the ivy.Container static method docstring, with ivy.<func_name> replaced with ivy.Container.static_<func_name> in the example.

      Array Instance Method Example in ivy/array/miscellaneous_operations.py.

        • ❌: Call this instance method of the ivy.Array class.

      Container Instance Method Example in ivy/container/miscellaneous_operations.py.

        • ❌: Call this instance method of the ivy.Container class.

      Array Operator Examples in ivy/array/array.py.

        • ❌: Call the operator on two ivy.Array instances.
        • ❌: Call the operator with an ivy.Array instance on the left and ivy.Container on the right.

      Array Reverse Operator Example in ivy/array/array.py.

        • ❌: Call the operator with a Number on the left and an ivy.Array instance on the right.

      Container Operator Examples in ivy/container/container.py.

        • ❌: Call the operator on two ivy.Container instances containing Number instances at the leaves.
        • ❌: Call the operator on two ivy.Container instances containing ivy.Array instances at the leaves.
        • ❌: Call the operator with an ivy.Container instance on the left and ivy.Array on the right.

      Container Reverse Operator Example in ivy/container/container.py.

        • ❌: Following example in the ivy.Container.__radd__ docstring, with the operator called with a Number on the left and an ivy.Container instance on the right.

      Tests

        • ❌: Docstring examples tests passing.
        • ✅: Lint checks passing.

@Kacper-W-Kozdon Kacper-W-Kozdon changed the title feature: ivy.unflatten frontend (draft) feat: ivy.unflatten frontend (draft) Nov 30, 2023
@Kacper-W-Kozdon Kacper-W-Kozdon changed the title feat: ivy.unflatten frontend (draft) feat: ivy.unflatten frontend (not finished) Nov 30, 2023
@Kacper-W-Kozdon
Copy link
Contributor Author

The frontend for unflatten is still missing most of the backends. Some backends use aliases. I'll implement that next.

@ivy-leaves ivy-leaves added PyTorch Frontend Developing the PyTorch Frontend, checklist triggered by commenting add_frontend_checklist Ivy Functional API labels Nov 30, 2023
@joaozenobio
Copy link
Contributor

joaozenobio commented Dec 1, 2023

Hi @Kacper-W-Kozdon, thanks for starting a contribution! Ping me when you are ready to get a review or if you have any questions. 😁

@joaozenobio joaozenobio changed the title feat: ivy.unflatten frontend (not finished) feat: added ivy.unflatten Pytorch frontend and backend support Dec 1, 2023
@joaozenobio joaozenobio changed the title feat: added ivy.unflatten Pytorch frontend and backend support feat: added unflatten Pytorch frontend and backend support Dec 1, 2023
AbdullahSabry and others added 18 commits December 4, 2023 19:04
Co-authored-by: NripeshN <nripesh14@gmail.com>
Co-authored-by: NripeshN <nripesh14@gmail.com>
Co-authored-by: Bhushan Srivastava <59949692+he11owthere@users.noreply.github.com>
Co-authored-by: NripeshN <nripesh14@gmail.com>
@Kacper-W-Kozdon
Copy link
Contributor Author

Kacper-W-Kozdon commented Jan 2, 2024

Hi @Kacper-W-Kozdon ! Thanks for the modifications. From the pytest log, the error about the "dim" parameter is probably due to the generation of the values and as you observed a problem with the dtype_values_axis(), I'll ask you to try creating the these values separately using simpler helper functions, such as dtype_and_values(), get_axis(), etc.., and then after success we can prove there is a problem with the dtype_values_axis(). Feel free to pin me when you're done and Merry Christmas! 🎄🎁

There are still some problems with tests but splitting dtypes_values_axis() into dtype_and_values() + get_axis() seemingly helped with the dim issues.

UPDATE:
Running pytest on just the torch backend got a 100% pass- but it might be good to double-check it. If all's good, I can look into adding the rest of the backends in case they are still not implemented.
pytestdebug.log

@Kacper-W-Kozdon
Copy link
Contributor Author

@joaozenobio the PR is ready for review.

Here dim and shape can be both positional and keyword.

Ex: 

In: torch.unflatten(input=torch.zeros((2, 4, 2)), dim=1, sizes=(2, 2)).shape
Out: torch.Size([2, 2, 2, 2])
Copy link
Contributor

@joaozenobio joaozenobio left a comment

Choose a reason for hiding this comment

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

Hi @Kacper-W-Kozdon! Thanks for the updates 😄 I have tested your pytorch test and, if I did not got the wrong version of your code, the frontend test for torch did not pass. The error shown was:

x = tensor([[False, False]]), dim = 0, shape = (1, 2)
@handle_out_argument
def unflatten(
    x: torch.Tensor,
    /,
    dim: int = 0,
    shape: Tuple[int] = None,
    *,
    out: Optional[torch.Tensor] = None,
) -> torch.Tensor:
>       res = torch.unflatten(x, dim, shape)
E       RuntimeError: unflatten: Provided sizes [1, 2] don't multiply up to the size of dim 0 (1) in the input tensor

Could you please verify if the generation of this numbers are possible to be generated in your test?

Also, if you wish you can continue working with the other backends and also build a test function for testing the backend in this same PR 🚀

@Kacper-W-Kozdon
Copy link
Contributor Author

Hi @Kacper-W-Kozdon! Thanks for the updates 😄 I have tested your pytorch test and, if I did not got the wrong version of your code, the frontend test for torch did not pass. The error shown was:

x = tensor([[False, False]]), dim = 0, shape = (1, 2)
@handle_out_argument
def unflatten(
    x: torch.Tensor,
    /,
    dim: int = 0,
    shape: Tuple[int] = None,
    *,
    out: Optional[torch.Tensor] = None,
) -> torch.Tensor:
>       res = torch.unflatten(x, dim, shape)
E       RuntimeError: unflatten: Provided sizes [1, 2] don't multiply up to the size of dim 0 (1) in the input tensor

Could you please verify if the generation of this numbers are possible to be generated in your test?

Also, if you wish you can continue working with the other backends and also build a test function for testing the backend in this same PR 🚀

No, you have the right one and I already know where to look for the cause. If the dimension to unflatten has size 1, there's nothing to factorise and the original shape was passed as the argument instead. This means that I need to add a condition for the dimension value equal to 1 and that should solve this problem. I will also look into other backends this week, it shouldn't take too much work. At the very least numpy and tensorflow have some functions similar to unflatten.

@joaozenobio
Copy link
Contributor

Awesome! When you are done making the necessary modifications just pin me 😄

And thanks for the initiative! I am going to tag your PR to reference the other backends too 🚀

@joaozenobio joaozenobio changed the title feat: added unflatten Pytorch frontend and backend support feat: added unflatten frontend and all backends support Jan 16, 2024
@joaozenobio joaozenobio changed the title feat: added unflatten frontend and all backends support feat: added unflatten frontend and backend support Jan 16, 2024
@Kacper-W-Kozdon
Copy link
Contributor Author

Awesome! When you are done making the necessary modifications just pin me 😄

And thanks for the initiative! I am going to tag your PR to reference the other backends too 🚀

Test fixed (technically it should cover all needs as is, but after adding the missing backends I might move factorization function to some more fitting category if I find one and just for completeness- instead of full factorization into primes, get a list with just partial factors), all that was required was changing factors = [] to factors = [1], so now shape never gets an empty list.

Tensorflow backend for unflatten added- tensorflow doesn't have unflatten, so its behaviour was recreated with reshape.

Numpy seems to have unflatten natively, so I only have yet to check whichever backend was left (jax, I think?) from the ones considered in pytest. I'll have the PR ready by the end of the day.

@Kacper-W-Kozdon
Copy link
Contributor Author

Awesome! When you are done making the necessary modifications just pin me 😄

And thanks for the initiative! I am going to tag your PR to reference the other backends too 🚀

Ready for review- all backends, pytorch frontend.
pytest log:
pytestdebug.log

@joaozenobio
Copy link
Contributor

Nice work @Kacper-W-Kozdon ! All tests are passing and the superset behavior was well managed. Your PR is good to go 😄

@joaozenobio
Copy link
Contributor

Hi @Kacper-W-Kozdon ! Your work with the frontend is great! With all the backends functions implemented, would you like to continue adding the features for this function implementing the Ivy Experimental API function and test and the data classes functions and tests, or would you like to finish our work here?

@Kacper-W-Kozdon
Copy link
Contributor Author

Hi @Kacper-W-Kozdon ! Your work with the frontend is great! With all the backends functions implemented, would you like to continue adding the features for this function implementing the Ivy Experimental API function and test and the data classes functions and tests, or would you like to finish our work here?

Sure, I would happily look into that- but I'll have to get more familiar with the containers used by Ivy and how their methods are written. It shouldn't take too long but it'll be a touch slower than just the frontend implementation. Would it be possible to merge the current progress as is and open a new issue for the experimental API and classes?

@joaozenobio
Copy link
Contributor

Sure thing! I'll merge your PR now and tag you in the new issue 😄

@joaozenobio joaozenobio merged commit 12c965a into Transpile-AI:main Jan 22, 2024
357 of 413 checks passed
@joaozenobio joaozenobio mentioned this pull request Jan 25, 2024
@joaozenobio
Copy link
Contributor

Hi @Kacper-W-Kozdon ! Sorry for the delay, here is the new issue you should bond to your PR related to the next steps on the unflatten implementation. Thanks for the hard work!! 🚀

#28054

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 Paddle Paddle Backend 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.

unflatten
10 participants