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

API: Implement tensordot and matmul #22

Merged
merged 1 commit into from
Mar 27, 2024

Conversation

mtsokol
Copy link
Member

@mtsokol mtsokol commented Mar 19, 2024

Tracking issue #21

Hi @willow-ahrens @hameerabbasi,

This WIP PR introduces finch.tensordot(x1, x2, axes) and finch.matmul(x1, x2)/x1 @ x2.
I still need to complete exhaustive testing.


@willow-ahrens I've got one question regarding matmul. I just noticed that there's a slight difference between tensordot and matmul in Array API for >2D input, namely tensordot aggregates non-contracted dims from one input and the other, where matmul takes two innermost dims for multiplication, and the rest of dims is treated as a stack/batch dimensions.

Here's a NumPy code showing it (described in notes in docs):

  >>> a = np.ones([9, 5, 7, 4])
  >>> c = np.ones([9, 5, 4, 3])
  >>> np.tensordot(a, c, axes=((-1,), (-2,))).shape
  (9, 5, 7, 9, 5, 3)
  >>> np.matmul(a, c).shape
  (9, 5, 7, 3)
  >>> # n is 7, k is 4, m is 3

So I'm not sure I can implement matmul as self.tensordot(other, axes=((-1,), (-2,))). WDYT?

@mtsokol mtsokol self-assigned this Mar 19, 2024
@mtsokol mtsokol marked this pull request as draft March 19, 2024 18:32
@willow-ahrens
Copy link
Collaborator

Yeah, perhaps we need to define einsum better in Finch and express both matmul and tensordot in terms of that.

Side note:

I think we should implement mul! in Julia for Finch Tensor and call that here.

@mtsokol mtsokol force-pushed the tensordot-matmul branch 3 times, most recently from d9f991f to ef02c9a Compare March 26, 2024 13:10
@mtsokol mtsokol marked this pull request as ready for review March 26, 2024 13:10
@mtsokol
Copy link
Member Author

mtsokol commented Mar 26, 2024

@willow-ahrens The PR is ready from my side! (I also want to benchmark these functions)

For now I constrained matmul input to 2D tensors, once we have mul that can perform matmul on stack of matrices I will come back to this.

Copy link
Collaborator

@hameerabbasi hameerabbasi left a comment

Choose a reason for hiding this comment

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

Couple of API changes, thanks for the excellent work here, @mtsokol.

src/finch/__init__.py Show resolved Hide resolved
src/finch/tensor.py Outdated Show resolved Hide resolved
src/finch/tensor.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@hameerabbasi hameerabbasi left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @mtsokol!

@mtsokol mtsokol merged commit c1fd746 into finch-tensor:main Mar 27, 2024
5 checks passed
@mtsokol mtsokol deleted the tensordot-matmul branch March 27, 2024 11:52
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