-
Notifications
You must be signed in to change notification settings - Fork 241
metal lowbit kernels: executorch ops #1322
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
facebook-github-bot
merged 1 commit into
pytorch:main
from
manuelcandales:export-D65957345
Dec 13, 2024
Merged
Changes from all commits
Commits
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
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <torchao/experimental/kernels/mps/src/common.h> | ||
|
||
class MetalShaderLibrary { | ||
public: | ||
MetalShaderLibrary(const std::string& src) : shaderSource(src) { | ||
lib = compileLibraryFromSource(shaderSource); | ||
} | ||
MetalShaderLibrary(const MetalShaderLibrary&) = delete; | ||
MetalShaderLibrary(MetalShaderLibrary&&) = delete; | ||
|
||
id<MTLComputePipelineState> getPipelineStateForFunc( | ||
const std::string& fname) { | ||
id<MTLFunction> func = loadFunc(fname); | ||
|
||
NSError* error = nil; | ||
id<MTLDevice> device = get_metal_device(); | ||
auto cpl = [device newComputePipelineStateWithFunction:func error:&error]; | ||
if (cpl == nil) { | ||
throw std::runtime_error( | ||
"Failed to construct pipeline state: " + | ||
std::string(error.description.UTF8String)); | ||
} | ||
return cpl; | ||
|
||
} | ||
|
||
private: | ||
std::string shaderSource; | ||
id<MTLLibrary> lib = nil; | ||
|
||
id<MTLFunction> loadFunc(const std::string& func_name) const { | ||
id<MTLFunction> func = [lib | ||
newFunctionWithName:[NSString stringWithUTF8String:func_name.c_str()]]; | ||
if (func == nil) { | ||
throw std::runtime_error("Can't get function:" + func_name); | ||
} | ||
return func; | ||
} | ||
|
||
id<MTLLibrary> compileLibraryFromSource( | ||
const std::string& source) { | ||
NSError* error = nil; | ||
MTLCompileOptions* options = [MTLCompileOptions new]; | ||
[options setLanguageVersion:MTLLanguageVersion3_1]; | ||
NSString* kernel_source = [NSString stringWithUTF8String:source.c_str()]; | ||
id<MTLDevice> device = get_metal_device(); | ||
id<MTLLibrary> library = [device newLibraryWithSource:kernel_source | ||
options:options | ||
error:&error]; | ||
if (library == nil) { | ||
throw std::runtime_error( | ||
"Failed to compile: " + std::string(error.description.UTF8String)); | ||
} | ||
return library; | ||
} | ||
}; |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include <Metal/Metal.h> | ||
#include <MetalPerformanceShaders/MetalPerformanceShaders.h> | ||
#include <stdexcept> | ||
|
||
id<MTLDevice> getMetalDevice() { | ||
@autoreleasepool { | ||
NSArray* devices = [MTLCopyAllDevices() autorelease]; | ||
if (devices.count == 0) { | ||
throw std::runtime_error("Metal is not supported"); | ||
} | ||
static id<MTLDevice> MTL_DEVICE = devices[0]; | ||
return MTL_DEVICE; | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#ifdef USE_ATEN | ||
#include <ATen/native/mps/OperationUtils.h> | ||
using namespace at::native::mps; | ||
#elif defined(USE_EXECUTORCH) | ||
#include <executorch/backends/apple/mps/runtime/MPSStream.h> | ||
using namespace executorch::backends::mps::delegate; | ||
#else | ||
#include <torchao/experimental/kernels/mps/src/OperationUtils.h> | ||
#endif | ||
|
||
inline void dispatch_block( | ||
MPSStream* mpsStream, | ||
void (^block)()) { | ||
#if defined(USE_ATEN) | ||
dispatch_sync_with_rethrow(mpsStream->queue(), block); | ||
#elif defined(USE_EXECUTORCH) | ||
dispatch_sync(mpsStream->queue(), block); | ||
#else | ||
(void)mpsStream; | ||
block(); | ||
#endif | ||
} | ||
|
||
inline void optionally_wait_for_command_completion(MPSStream* mpsStream) { | ||
#if defined(USE_ATEN) | ||
#elif defined(USE_EXECUTORCH) | ||
ET_CHECK(mpsStream->synchronize(SyncType::COMMIT_AND_WAIT) == executorch::runtime::Error::Ok); | ||
#else | ||
id<MTLCommandEncoder> encoder = mpsStream->commandEncoder(); | ||
id<MTLCommandBuffer> cmdBuffer = mpsStream->commandBuffer(); | ||
[encoder endEncoding]; | ||
[cmdBuffer commit]; | ||
[cmdBuffer waitUntilCompleted]; | ||
#endif | ||
} | ||
|
||
inline id<MTLDevice> get_metal_device() { | ||
#if defined(USE_ATEN) || defined(USE_EXECUTORCH) | ||
return MPSDevice::getInstance()->device(); | ||
#else | ||
return getMetalDevice(); | ||
#endif | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
all: test_lowbit | ||
|
||
test_lowbit: test_lowbit.mm | ||
clang++ -I${TORCHAO_ROOT} -O3 -std=c++17 -Wall -Wextra -o $@ $< -framework Metal -framework Foundation | ||
test_lowbit: test_lowbit.mm ../src/OperationUtils.mm | ||
clang++ -I${TORCHAO_ROOT} -O3 -std=c++17 -Wall -Wextra -o $@ $^ -framework Metal -framework Foundation | ||
|
||
run: test_lowbit | ||
./test_lowbit |
This file contains 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 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.
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.
So for PT integration we are using a separate implementation of this? And for executorch we use one from delegate?
Do we not have the same from aten?
Also what is the interaction with device and the stream? It seems that you are creating your own device when compiling for PyTorch, but then use
getCurrentMPSStream
from aten?Same question for ET. @malfet thoughts on this?
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.
For PT we are using the getCurrentMPSStream implementation from the ATen MPS code, and for ET we are using the getCurrentMPSStream implementation from the ET MPS delegate code.
I am not creating a device in any of those cases (PT or ET). In those cases I basically just retrieve the device via MPSDevice::getInstance()->device(). It is the same in that respect PT vs ET.