-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metal lowbit kernels: executorch ops
Differential Revision: D65957345 Pull Request resolved: #1322
- Loading branch information
1 parent
31234db
commit ebc4303
Showing
14 changed files
with
364 additions
and
147 deletions.
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.