-
Notifications
You must be signed in to change notification settings - Fork 696
Add Metal backend type definitions and utilities #15019
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6420712
Update
manuelcandales d036c07
Update
manuelcandales 1a22c5e
Update
manuelcandales d6f0bc9
Update
manuelcandales ca5f1e5
Update
manuelcandales c4c16aa
Update
manuelcandales ce0f085
Update
manuelcandales e391e17
Update
manuelcandales 89d3f14
Update
manuelcandales 7590e37
Update
manuelcandales 971a762
Update
manuelcandales f46adc5
Update
manuelcandales 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 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #include <executorch/backends/apple/metal/runtime/shims/tensor_attribute.h> | ||
| #include <executorch/backends/apple/metal/runtime/shims/utils.h> | ||
| #include <iostream> | ||
|
|
||
| namespace executorch { | ||
| namespace backends { | ||
| namespace metal { | ||
|
|
||
| extern "C" { | ||
|
|
||
| // Metal-specific device type constant | ||
| __attribute__((__visibility__("default"))) int32_t | ||
| aoti_torch_device_type_mps() { | ||
| return 13; // Consistent with c10/core/DeviceType.h | ||
| } | ||
|
|
||
| // Override aoti_torch_get_device_type to return MPS device type | ||
| AOTITorchError aoti_torch_get_device_type( | ||
| AOTITensorHandle tensor, | ||
| int32_t* ret_device_type) { | ||
| *ret_device_type = aoti_torch_device_type_mps(); | ||
| return Error::Ok; | ||
| } | ||
|
|
||
| } // extern "C" | ||
|
|
||
| } // namespace metal | ||
| } // namespace backends | ||
| } // namespace executorch |
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <executorch/backends/aoti/common_shims.h> | ||
| #include <executorch/backends/apple/metal/runtime/shims/types.h> | ||
|
|
||
| namespace executorch { | ||
| namespace backends { | ||
| namespace metal { | ||
|
|
||
| extern "C" { | ||
|
|
||
| // Metal-specific device type function | ||
| int32_t aoti_torch_device_type_mps(); | ||
|
|
||
| // Override aoti_torch_get_device_type to return MPS device type | ||
| AOTITorchError aoti_torch_get_device_type( | ||
| AOTITensorHandle tensor, | ||
| int32_t* ret_device_type); | ||
|
|
||
| } // extern "C" | ||
|
|
||
| } // namespace metal | ||
| } // namespace backends | ||
| } // namespace executorch |
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <executorch/extension/tensor/tensor.h> | ||
| #include <executorch/runtime/core/error.h> | ||
| #include <cstdint> | ||
|
|
||
| namespace executorch { | ||
| namespace backends { | ||
| namespace metal { | ||
|
|
||
| // Common using declarations for ExecutorTorch types | ||
| using executorch::runtime::Error; | ||
| using executorch::runtime::etensor::Tensor; | ||
|
|
||
| extern "C" { | ||
|
|
||
| // Common AOTI type aliases | ||
| // Note: AOTITensorHandle is aliased to Tensor* for ExecutorTorch compatibility | ||
| using AOTITensorHandle = Tensor*; | ||
| using AOTIRuntimeError = Error; | ||
| using AOTITorchError = Error; | ||
|
|
||
| } // extern "C" | ||
|
|
||
| } // namespace metal | ||
| } // namespace backends | ||
| } // namespace executorch | ||
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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #include <executorch/backends/apple/metal/runtime/shims/utils.h> | ||
| #include <executorch/runtime/platform/log.h> | ||
| #include <cstdint> | ||
|
|
||
| namespace executorch { | ||
| namespace backends { | ||
| namespace metal { | ||
|
|
||
| extern "C" { | ||
|
|
||
| // Helper function to check if a dtype is supported in Metal backend | ||
| bool is_dtype_supported_in_et_metal(int32_t dtype) { | ||
| switch (dtype) { | ||
| case static_cast<int32_t>(SupportedDTypes::INT64): | ||
| case static_cast<int32_t>(SupportedDTypes::FLOAT32): | ||
| case static_cast<int32_t>(SupportedDTypes::BFLOAT16): | ||
| return true; | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Metal-specific dtype validation utility function | ||
| AOTITorchError validate_dtype(int32_t dtype) { | ||
| if (is_dtype_supported_in_et_metal(dtype)) { | ||
| return Error::Ok; | ||
| } | ||
|
|
||
| ET_LOG( | ||
| Error, | ||
| "Unsupported dtype: %d. Supported dtypes: %d (int64), %d (float32), %d (bfloat16)", | ||
| dtype, | ||
| static_cast<int32_t>(SupportedDTypes::INT64), | ||
| static_cast<int32_t>(SupportedDTypes::FLOAT32), | ||
| static_cast<int32_t>(SupportedDTypes::BFLOAT16)); | ||
| return Error::InvalidArgument; | ||
| } | ||
|
|
||
| } // extern "C" | ||
|
|
||
| } // namespace metal | ||
| } // namespace backends | ||
| } // namespace executorch |
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <executorch/backends/aoti/utils.h> | ||
| #include <executorch/backends/apple/metal/runtime/shims/types.h> | ||
| #include <executorch/runtime/core/exec_aten/exec_aten.h> | ||
| #include <cstdint> | ||
|
|
||
| namespace executorch { | ||
| namespace backends { | ||
| namespace metal { | ||
|
|
||
| // Enum for supported data types in et-metal backend | ||
| enum class SupportedDTypes : int32_t { | ||
| // UINT8 = 0, // PyTorch's uint8 dtype code | ||
| // INT8 = 1, // PyTorch's int8 dtype code | ||
| // INT16 = 2, // PyTorch's int16 dtype code | ||
| // INT32 = 3, // PyTorch's int32 dtype code | ||
| INT64 = 4, // PyTorch's int64 dtype code | ||
| // FLOAT16 = 5, // PyTorch's float16 dtype code | ||
| FLOAT32 = 6, // PyTorch's float32 dtype code | ||
| // FLOAT64 = 7, // PyTorch's float64 dtype code | ||
| // BOOL = 11, // PyTorch's bool dtype code | ||
| BFLOAT16 = 15 // PyTorch's bfloat16 dtype code | ||
| }; | ||
|
|
||
| extern "C" { | ||
|
|
||
| // Helper function to check if a dtype is supported in Metal backend | ||
| bool is_dtype_supported_in_et_metal(int32_t dtype); | ||
|
|
||
| // Metal-specific dtype validation utility function | ||
| AOTITorchError validate_dtype(int32_t dtype); | ||
|
|
||
| } // extern "C" | ||
|
|
||
| } // namespace metal | ||
| } // namespace backends | ||
| } // namespace executorch |
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
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.
Do you need to distinguish both errors? Seems like AOTIRuntimeError is not used
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.
Yes, AOTInductorModelContainerRun and family return AOTIRuntimeError while aoti_torch_empty_strided and family return AOTITorchError