Skip to content

Allow af_matmul to use an existing af_array for output #2481

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

Merged
merged 1 commit into from
Jun 20, 2019

Conversation

mark-poscablo
Copy link
Contributor

Similar to #2324, but for matmul. Still WIP as the OpenCL BLAS tests are failing. Made a PR for now so that my changes will be visible to you guys while I'm working on this.

@WilliamTambellini
Copy link
Contributor

Cool. Would that work also for batched matmul (per slice matmul) ?

@mark-poscablo
Copy link
Contributor Author

@WilliamTambellini I think that must be ensured, though I haven't added this specific test yet in the published commits. The goal is that all of the existing behavior of af_matmul will not change (which includes its current batching behavior), but we will have the added ability to reuse an existing af_array. Thanks for bringing it up!

@9prady9
Copy link
Member

9prady9 commented Apr 3, 2019

@WilliamTambellini This change is to the C-API, we will be adding a new gemm API that we have been discussing in the issue #2446 . It will look different from regular matmul. Something like the following

void af::gemm(af::array& C, const array &lhs, const array &rhs,
const matProp optLhs = AF_MAT_NONE, const matProp optRhs = AF_MAT_NONE,
const float alpha = 1, const float beta = 1);

C would be your pre-allocated output array. After each call to this new API, C will have aplha*(lhs x rhs) + beta*C. What happens when lhs and rhs are batched inputs is a design choice we haven't made yet. Once this PR is merged, we will raise the new function PR and we can continue this discussion on that.

9prady9
9prady9 previously requested changes Apr 3, 2019
@WilliamTambellini
Copy link
Contributor

WilliamTambellini commented Apr 3, 2019

ok tks @9prady9
@mark-poscablo Could you consequently add a unitest for accumulated batched gemm :
Example:
A= 3 x 2 x 2
0 1
1 2
2 3
0 1
1 2
2 3

B= 2 x 3 x 2
0 1 2
1 2 3
0 1 2
1 2 3

After looping over the slices with 1 gemm per slice on C shaped 3 x 3 (single slice) with beta = 1, C should be :
C=3 x 3
2 4 6
4 10 16
6 16 26

@mark-poscablo mark-poscablo changed the title [WIP] Allow af_matmul to use an existing af_array for output Allow af_matmul to use an existing af_array for output Apr 8, 2019
@pavanky
Copy link
Member

pavanky commented Apr 10, 2019

Why just matmul ? None of the other functions support using an existing af_array.

@mark-poscablo
Copy link
Contributor Author

Why just matmul ? None of the other functions support using an existing af_array.

matmul specifically because this is a part of the approach we're taking regarding #2446 (see @9prady9's comment above). We have done this also with a few other functions such as af_approx1 (#2324)

@pavanky pavanky dismissed their stale review April 10, 2019 21:42

letting umar figure it out

@umar456 umar456 added this to the v3.7.0 milestone May 31, 2019
@mark-poscablo
Copy link
Contributor Author

Coming back to this after some time. Working on leaving the old behavior intact and separating the new behavior into new functions.

Copy link
Member

@9prady9 9prady9 left a comment

Choose a reason for hiding this comment

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

Looks good.

I understand why af_ calls are present in src/api/c/blas.cpp - don't like it however though.

Why are tests commented out ? Do you have more changes coming in ?

@mark-poscablo
Copy link
Contributor Author

@9prady9 Yes I have more coming in. Pushed some changes for now to see how they fare in different systems though.

@9prady9 9prady9 dismissed their stale review June 19, 2019 06:02

Newer changes have been pushed.

Copy link
Member

@9prady9 9prady9 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 great overall.

  • I think sticking to C style arrays for alpha & beta is a good decision.
  • I have some nit picky comments on variables names in our internal implementation, but we can address them in another PR.

9prady9
9prady9 previously approved these changes Jun 19, 2019
* Make af_gemm accept preallocated arrays
* Update the docs and add test for doc snippet
@umar456 umar456 merged commit 4159b1a into arrayfire:master Jun 20, 2019
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.

5 participants