Skip to content

Add vmap for SVD and inverse and fix some other ops - #849

Merged
angeloskath merged 1 commit into
ml-explore:mainfrom
nicolov:nicolov-vmap
Mar 21, 2024
Merged

angeloskath merged 1 commit into
ml-explore:mainfrom
nicolov:nicolov-vmap

Conversation

@nicolov

@nicolov nicolov commented Mar 19, 2024 •

Copy link
Copy Markdown
Contributor

Proposed changes

Add vmap for SVD and inverse, following #836

Also fix a bug in the vmap implementation of some binary ops, that was causing an issue on Linux (but not on Mac, probably because of different initial sizing and/or initialization of std::vector).

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Comment thread mlx/backend/common/inverse.cpp Resolved
Comment thread mlx/backend/common/svd.cpp Resolved

@awni awni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks great! Minor nits inline.

@awni

awni commented Mar 20, 2024

Copy link
Copy Markdown
Member

@nicolov the tests are failing.. could you take a look?

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks great!

I left a comment regarding handling non vectorized axes. The linux test seems to fail on the vmap test and the Mac test seems unrelated and possibly a fluke. I launched both tests again but it is unlikely that the linux one will clear.

Comment thread mlx/backend/common/inverse.cpp Resolved
Comment thread mlx/backend/common/svd.cpp Resolved
@nicolov

nicolov commented Mar 20, 2024

Copy link
Copy Markdown
Contributor Author

I'm looking into it. Linux (not Mac) calls moveaxis with a big axis number in mlx::core::detail::vmap_replace. @angeloskath's suggestion does not fix the issue sadly.

@angeloskath

Copy link
Copy Markdown
Member

Yeah indeed I also saw that (that's weird and I may look into it later if you haven't fixed it by then). My suggestion has to do with the general correctness for cases where the input is not vmapped. So it wouldn't fix that...

@nicolov

nicolov commented Mar 20, 2024

Copy link
Copy Markdown
Contributor Author

My suggestion has to do with the general correctness for cases where the input is not vmapped.

But in those cases vmap wouldn't be called at all, no?

@nicolov nicolov changed the title Add vmap for SVD and inverse Add vmap for SVD and inverse and fix some other ops Mar 20, 2024
Comment thread mlx/primitives.cpp
const std::vector<array>& inputs,
const std::vector<int>& axes) {
auto [a, b, to_ax] = vmap_binary_op(inputs, axes, stream());
return {{equal(a, b, stream())}, axes};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for debugging that!

Comment thread mlx/transforms.cpp Outdated
Comment on lines +660 to +662
assert(
"Expected the number of outputs and vmapped axes to match. Please fix "
"the vmap implementation for this primitive.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure what is the purpose of this loc, maybe remove?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this was assert(v_outputs.size() == v_out_axes.size(), "...") but I kind of agree that we should remove it.

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's merge after removing the assert.

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great!

@angeloskath
angeloskath merged commit 105d236 into ml-explore:main Mar 21, 2024
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.

3 participants