Add Metal backend type definitions and utilities#15019
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/15019
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit f46adc5 with merge base 926312e ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
| // Let's use 2 for MPS | ||
| return 2; |
There was a problem hiding this comment.
Don't you wanna be consistent with c10/core/DeviceType.h?
It is either 11 or 13
| using AOTIRuntimeError = Error; | ||
| using AOTITorchError = Error; |
There was a problem hiding this comment.
Do you need to distinguish both errors? Seems like AOTIRuntimeError is not used
There was a problem hiding this comment.
Yes, AOTInductorModelContainerRun and family return AOTIRuntimeError while aoti_torch_empty_strided and family return AOTITorchError
| // Utility function to convert sizes pointer to vector | ||
| std::vector<executorch::aten::SizesType> convert_sizes_to_vector( | ||
| int64_t ndim, | ||
| const int64_t* sizes_ptr) { | ||
| std::vector<executorch::aten::SizesType> sizes(ndim); | ||
| for (int i = 0; i < ndim; i++) { | ||
| sizes[i] = static_cast<executorch::aten::SizesType>(sizes_ptr[i]); | ||
| } | ||
| return sizes; | ||
| } | ||
|
|
||
| // Utility function to convert strides pointer to vector or calculate from sizes | ||
| std::vector<executorch::aten::StridesType> convert_strides_to_vector( | ||
| int64_t ndim, | ||
| const int64_t* sizes_ptr, | ||
| const int64_t* strides_ptr) { | ||
| std::vector<executorch::aten::StridesType> strides(ndim); | ||
|
|
||
| if (strides_ptr != nullptr) { | ||
| // Use provided strides. | ||
| for (int64_t i = 0; i < ndim; i++) { | ||
| strides[i] = static_cast<executorch::aten::StridesType>(strides_ptr[i]); | ||
| } | ||
| } else { | ||
| // Calculate strides from sizes. | ||
| if (ndim > 0) { | ||
| strides[ndim - 1] = static_cast<executorch::aten::StridesType>( | ||
| 1); // Last dimension has stride 1 | ||
| for (int64_t i = ndim - 2; i >= 0; i--) { | ||
| if (sizes_ptr[i + 1] == 0) { | ||
| strides[i] = strides[i + 1]; // Copy stride when size is 0 | ||
| } else { | ||
| strides[i] = static_cast<executorch::aten::StridesType>( | ||
| static_cast<int64_t>(strides[i + 1]) * sizes_ptr[i + 1]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return strides; | ||
| } |
There was a problem hiding this comment.
Let's move these to a different common location. There's no metal specific logic here. Maybe backends/aoti/utils.h
| std::vector<executorch::aten::SizesType> sizes, | ||
| std::vector<executorch::aten::StridesType> strides) { |
There was a problem hiding this comment.
this implementation will copy. maybe do this
const std::vector<executorch::aten::SizesType>& sizes,
const std::vector<executorch::aten::StridesType>& strides
Implement foundational types and utilities for Metal backend including: - AOTI type aliases (AOTITensorHandle, AOTIRuntimeError, AOTITorchError) - Device type handling functions - Tensor storage size queries - Tensor attribute utilities ghstack-source-id: 7bfa3ae ghstack-comment-id: 3392299883 Pull-Request: pytorch#15019
Implement foundational types and utilities for Metal backend including: - AOTI type aliases (AOTITensorHandle, AOTIRuntimeError, AOTITorchError) - Device type handling functions - Tensor storage size queries - Tensor attribute utilities
Implement foundational types and utilities for Metal backend including: