-
Notifications
You must be signed in to change notification settings - Fork 24.9k
[MPS] Extend addmm to integral types #160270
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[MPS] Extend addmm to integral types
Fixes #154901 [ghstack-poisoned]
- Loading branch information
commit abe992a0ca9b6c9119d04514f5d5cd9865f66fcf
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,50 @@ | |
return output; | ||
} | ||
|
||
Tensor& do_metal_addmm(const Tensor& self, | ||
const Tensor& other, | ||
Tensor& output, | ||
const Scalar& alpha, | ||
const Scalar& beta, | ||
const Tensor& bias) { | ||
if (beta.toDouble() == 0 && alpha.toDouble() == 1) { | ||
return do_metal_mm(self, other, output); | ||
} | ||
auto stream = getCurrentMPSStream(); | ||
auto device = MPSDevice::getInstance()->device(); | ||
auto matmulPSO = lib.getPipelineStateForFunc("addmm_" + mps::scalarToMetalTypeString(output)); | ||
dispatch_sync_with_rethrow(stream->queue(), ^() { | ||
@autoreleasepool { | ||
getMPSProfiler().beginProfileKernel(matmulPSO, "addmm", {self, other}); | ||
auto computeEncoder = stream->commandEncoder(); | ||
[computeEncoder setComputePipelineState:matmulPSO]; | ||
std::array<uint32_t, 3> sizes = {static_cast<uint32_t>(self.size(0)), | ||
static_cast<uint32_t>(self.size(1)), | ||
static_cast<uint32_t>(output.size(1))}; | ||
std::array<int64_t, 8> strides = {self.stride(0), | ||
self.stride(1), | ||
other.stride(0), | ||
other.stride(1), | ||
output.stride(0), | ||
output.stride(1), | ||
bias.stride(0), | ||
bias.stride(1)}; | ||
std::array<int64_t, 2> alpha_beta = {alpha.toInt(), beta.toInt()}; | ||
constexpr uint32_t TILE_DIM = 16; // fastest performance from tests on multiple macs | ||
uint32_t gridSizeX = (output.size(1) + TILE_DIM - 1) / TILE_DIM; | ||
uint32_t gridSizeY = (self.size(0) + TILE_DIM - 1) / TILE_DIM; | ||
|
||
MTLSize threadsPerThreadgroup = MTLSizeMake(TILE_DIM, TILE_DIM, 1); | ||
MTLSize threadgroupsPerGrid = MTLSizeMake(gridSizeX, gridSizeY, 1); | ||
mtl_setArgs(computeEncoder, self, other, output, bias, alpha_beta, strides, sizes); | ||
[computeEncoder dispatchThreadgroups:threadgroupsPerGrid threadsPerThreadgroup:threadsPerThreadgroup]; | ||
getMPSProfiler().endProfileKernel(matmulPSO); | ||
} | ||
}); | ||
return output; | ||
return output; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double return? Surprised this didn't error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I hoped some of the linters will be triggered by it, but feels like this is fine... |
||
} | ||
|
||
std::tuple<MPSGraphTensor*, MPSGraphTensor*, MPSGraphTensor*> do_mm(MPSGraph* graph, | ||
const Tensor& self, | ||
const Tensor& other) { | ||
|
@@ -644,7 +688,6 @@ static void linalg_inv_ex_out_mps_impl(const Tensor& A, bool check_errors, const | |
|
||
TORCH_CHECK(output.is_mps()); | ||
TORCH_CHECK(self.dim() == 2 && other.dim() == 2, "tensors must be 2-D"); | ||
TORCH_CHECK(supportedFloatingOrComplexType(self), "MPS device does not support addmm for non-float input"); | ||
|
||
TensorArg args[]{{output, "out", 0}, {bias, "self", 1}, {self, "mat1", 2}, {other, "mat2", 3}}; | ||
checkAllSameGPU(__func__, args); | ||
|
@@ -671,6 +714,10 @@ static void linalg_inv_ex_out_mps_impl(const Tensor& A, bool check_errors, const | |
return output; | ||
} | ||
|
||
if (use_metal_mm(self, other, output)) { | ||
return do_metal_addmm(self, other, output, alpha, beta, *bias_); | ||
} | ||
|
||
bool is_beta_non_zero = beta.toDouble() != 0.0; | ||
|
||
struct CachedGraph : public mps::MPSCachedGraph { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ugly, but can this be rewritten as an std array too?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Threadgroups are a bit weird(i.e. this statement affects GPU occupancy), let me give it a try in a separate PR, but make sure it would not regress the perf...