From 2a96ea8ac2baf928e0ebf3fcb18cab11dd51c3d9 Mon Sep 17 00:00:00 2001 From: Jorge Pineda Date: Fri, 8 Mar 2024 12:10:38 -0800 Subject: [PATCH] [ET-VK] Organize utils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor our `OpUtils.*`, `StagingUtils.*`, and `Utils.*` which have accumulated helper functions during the redesign. We split them and move them into two new folders: 1. `ops/utils`: Used by files in `ops/` and `ops/impl/`. 2. `ops/impl/utils`: Used only by files in `ops/impl/`. In general, this helps to unflatten the `ops/` directory: ``` [jorgep31415@devvm15882.vll0 /data/users/jorgep31415/fbsource/xplat/executorch/backends/vulkan/runtime/graph/ops (98b0e789c)]$ tree . ├── ExecuteNode.cpp ├── ExecuteNode.h ├── impl │ ├── Arithmetic.cpp │ ├── Arithmetic.h │ ├── Staging.cpp │ ├── Staging.h │ └── utils │ ├── DimUtils.h │ ├── ScalarUtils.h │ ├── TensorUtils.cpp │ └── TensorUtils.h ├── OperatorRegistry.cpp ├── OperatorRegistry.h ├── PrepackNode.cpp ├── PrepackNode.h └── utils ├── BindingUtils.cpp ├── BindingUtils.h ├── StagingUtils.cpp └── StagingUtils.h 3 directories, 18 files ``` Since we're heavily updating include declarations, we also introduce the convention to only include the VK-API via ``` #include ``` instead of including more specific files, e.g., ``` #include ``` Differential Revision: [D54690346](https://our.internmc.facebook.com/intern/diff/D54690346/) [ghstack-poisoned] --- .../vulkan/runtime/graph/ComputeGraph.cpp | 7 +++- backends/vulkan/runtime/graph/ComputeGraph.h | 4 +- backends/vulkan/runtime/graph/GraphConfig.h | 2 +- .../vulkan/runtime/graph/ops/ExecuteNode.cpp | 2 +- .../vulkan/runtime/graph/ops/ExecuteNode.h | 4 +- .../runtime/graph/ops/OperatorRegistry.cpp | 2 - .../vulkan/runtime/graph/ops/PrepackNode.cpp | 4 +- .../vulkan/runtime/graph/ops/PrepackNode.h | 4 +- .../runtime/graph/ops/impl/Arithmetic.cpp | 4 +- .../runtime/graph/ops/impl/Arithmetic.h | 2 - .../vulkan/runtime/graph/ops/impl/Staging.cpp | 7 ++-- .../ops/{OpUtils.h => impl/utils/DimUtils.h} | 23 ----------- .../graph/ops/impl/utils/ScalarUtils.h | 39 +++++++++++++++++++ .../utils/TensorUtils.cpp} | 12 +++++- .../graph/ops/impl/utils/TensorUtils.h | 28 +++++++++++++ .../ops/{Utils.cpp => utils/BindingUtils.cpp} | 12 +----- .../ops/{Utils.h => utils/BindingUtils.h} | 2 - .../graph/ops/{ => utils}/StagingUtils.cpp | 27 +++++++++++-- .../graph/ops/{ => utils}/StagingUtils.h | 39 ------------------- .../vulkan/test/vulkan_compute_api_test.cpp | 7 ++-- 20 files changed, 126 insertions(+), 105 deletions(-) rename backends/vulkan/runtime/graph/ops/{OpUtils.h => impl/utils/DimUtils.h} (75%) create mode 100644 backends/vulkan/runtime/graph/ops/impl/utils/ScalarUtils.h rename backends/vulkan/runtime/graph/ops/{OpUtils.cpp => impl/utils/TensorUtils.cpp} (68%) create mode 100644 backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h rename backends/vulkan/runtime/graph/ops/{Utils.cpp => utils/BindingUtils.cpp} (83%) rename backends/vulkan/runtime/graph/ops/{Utils.h => utils/BindingUtils.h} (94%) rename backends/vulkan/runtime/graph/ops/{ => utils}/StagingUtils.cpp (87%) rename backends/vulkan/runtime/graph/ops/{ => utils}/StagingUtils.h (51%) diff --git a/backends/vulkan/runtime/graph/ComputeGraph.cpp b/backends/vulkan/runtime/graph/ComputeGraph.cpp index 6aa9171d9f4..6583d4a5a3e 100644 --- a/backends/vulkan/runtime/graph/ComputeGraph.cpp +++ b/backends/vulkan/runtime/graph/ComputeGraph.cpp @@ -6,12 +6,15 @@ * LICENSE file in the root directory of this source tree. */ -#include +// @lint-ignore-every CLANGTIDY +// facebook-security-vulnerable-integer-sign-conversion -#include +#include #include +#include + namespace at { namespace native { namespace vulkan { diff --git a/backends/vulkan/runtime/graph/ComputeGraph.h b/backends/vulkan/runtime/graph/ComputeGraph.h index 7917304f0c9..776f5e12ee5 100644 --- a/backends/vulkan/runtime/graph/ComputeGraph.h +++ b/backends/vulkan/runtime/graph/ComputeGraph.h @@ -12,9 +12,7 @@ #ifdef USE_VULKAN_API -#include -#include -#include +#include #include diff --git a/backends/vulkan/runtime/graph/GraphConfig.h b/backends/vulkan/runtime/graph/GraphConfig.h index e2c8d6bed03..ce0f0839a9f 100644 --- a/backends/vulkan/runtime/graph/GraphConfig.h +++ b/backends/vulkan/runtime/graph/GraphConfig.h @@ -10,7 +10,7 @@ #ifdef USE_VULKAN_API -#include +#include namespace at { namespace native { diff --git a/backends/vulkan/runtime/graph/ops/ExecuteNode.cpp b/backends/vulkan/runtime/graph/ops/ExecuteNode.cpp index c9c338bc179..f6649fb19c3 100644 --- a/backends/vulkan/runtime/graph/ops/ExecuteNode.cpp +++ b/backends/vulkan/runtime/graph/ops/ExecuteNode.cpp @@ -10,7 +10,7 @@ #include -#include +#include namespace at { namespace native { diff --git a/backends/vulkan/runtime/graph/ops/ExecuteNode.h b/backends/vulkan/runtime/graph/ops/ExecuteNode.h index f3c2bba9c0c..5bcad8fb804 100644 --- a/backends/vulkan/runtime/graph/ops/ExecuteNode.h +++ b/backends/vulkan/runtime/graph/ops/ExecuteNode.h @@ -10,9 +10,7 @@ #ifdef USE_VULKAN_API -#include -#include -#include +#include #include diff --git a/backends/vulkan/runtime/graph/ops/OperatorRegistry.cpp b/backends/vulkan/runtime/graph/ops/OperatorRegistry.cpp index ba559d870c5..9f489e1c3f7 100644 --- a/backends/vulkan/runtime/graph/ops/OperatorRegistry.cpp +++ b/backends/vulkan/runtime/graph/ops/OperatorRegistry.cpp @@ -8,8 +8,6 @@ #include -#include - namespace at { namespace native { namespace vulkan { diff --git a/backends/vulkan/runtime/graph/ops/PrepackNode.cpp b/backends/vulkan/runtime/graph/ops/PrepackNode.cpp index 69e6ffabd68..43ad64c9427 100644 --- a/backends/vulkan/runtime/graph/ops/PrepackNode.cpp +++ b/backends/vulkan/runtime/graph/ops/PrepackNode.cpp @@ -10,8 +10,8 @@ #include -#include -#include +#include +#include namespace at { namespace native { diff --git a/backends/vulkan/runtime/graph/ops/PrepackNode.h b/backends/vulkan/runtime/graph/ops/PrepackNode.h index 59071e93714..80465703da2 100644 --- a/backends/vulkan/runtime/graph/ops/PrepackNode.h +++ b/backends/vulkan/runtime/graph/ops/PrepackNode.h @@ -10,9 +10,7 @@ #ifdef USE_VULKAN_API -#include -#include -#include +#include #include diff --git a/backends/vulkan/runtime/graph/ops/impl/Arithmetic.cpp b/backends/vulkan/runtime/graph/ops/impl/Arithmetic.cpp index ff9b4ff2b2d..52cd04c4920 100644 --- a/backends/vulkan/runtime/graph/ops/impl/Arithmetic.cpp +++ b/backends/vulkan/runtime/graph/ops/impl/Arithmetic.cpp @@ -8,11 +8,13 @@ #include -#include #include #include +#include +#include + namespace at { namespace native { namespace vulkan { diff --git a/backends/vulkan/runtime/graph/ops/impl/Arithmetic.h b/backends/vulkan/runtime/graph/ops/impl/Arithmetic.h index 8e5c345a92c..b81ee21e648 100644 --- a/backends/vulkan/runtime/graph/ops/impl/Arithmetic.h +++ b/backends/vulkan/runtime/graph/ops/impl/Arithmetic.h @@ -12,8 +12,6 @@ #include -#include - namespace at { namespace native { namespace vulkan { diff --git a/backends/vulkan/runtime/graph/ops/impl/Staging.cpp b/backends/vulkan/runtime/graph/ops/impl/Staging.cpp index 953a06426ab..349e0000865 100644 --- a/backends/vulkan/runtime/graph/ops/impl/Staging.cpp +++ b/backends/vulkan/runtime/graph/ops/impl/Staging.cpp @@ -8,9 +8,10 @@ #include -#include -#include -#include +#include + +#include +#include namespace at { namespace native { diff --git a/backends/vulkan/runtime/graph/ops/OpUtils.h b/backends/vulkan/runtime/graph/ops/impl/utils/DimUtils.h similarity index 75% rename from backends/vulkan/runtime/graph/ops/OpUtils.h rename to backends/vulkan/runtime/graph/ops/impl/utils/DimUtils.h index b5acb3945ac..299c3bb99fc 100644 --- a/backends/vulkan/runtime/graph/ops/OpUtils.h +++ b/backends/vulkan/runtime/graph/ops/impl/utils/DimUtils.h @@ -12,8 +12,6 @@ #include -#include - namespace at { namespace native { namespace vulkan { @@ -75,27 +73,6 @@ uint32_t dim_at(const vTensor& v_in) { return dim_at(v_in.sizes()); } -/* - * For most global work group sizes, returns {4, 4, 4}, but adjusts the size for - * 2D global work group sizes. Always maintains a total of 64 invocations - */ -api::utils::uvec3 adaptive_work_group_size( - const api::utils::uvec3& global_work_group); - -template -T extract_scalar(const Value& value) { - if (value.isInt()) { - return static_cast(value.toInt()); - } - if (value.isDouble()) { - return static_cast(value.toDouble()); - } - if (value.isBool()) { - return static_cast(value.toBool()); - } - VK_THROW("Cannot extract scalar from Value with type ", value.type()); -} - } // namespace vulkan } // namespace native } // namespace at diff --git a/backends/vulkan/runtime/graph/ops/impl/utils/ScalarUtils.h b/backends/vulkan/runtime/graph/ops/impl/utils/ScalarUtils.h new file mode 100644 index 00000000000..38cb8eed3ba --- /dev/null +++ b/backends/vulkan/runtime/graph/ops/impl/utils/ScalarUtils.h @@ -0,0 +1,39 @@ +/* + * 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 + +#ifdef USE_VULKAN_API + +#include + +#include + +namespace at { +namespace native { +namespace vulkan { + +template +T extract_scalar(const Value& value) { + if (value.isInt()) { + return static_cast(value.toInt()); + } + if (value.isDouble()) { + return static_cast(value.toDouble()); + } + if (value.isBool()) { + return static_cast(value.toBool()); + } + VK_THROW("Cannot extract scalar from Value with type ", value.type()); +} + +} // namespace vulkan +} // namespace native +} // namespace at + +#endif /* USE_VULKAN_API */ diff --git a/backends/vulkan/runtime/graph/ops/OpUtils.cpp b/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.cpp similarity index 68% rename from backends/vulkan/runtime/graph/ops/OpUtils.cpp rename to backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.cpp index ce82aef0925..72e1bc5a0d8 100644 --- a/backends/vulkan/runtime/graph/ops/OpUtils.cpp +++ b/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.cpp @@ -6,7 +6,9 @@ * LICENSE file in the root directory of this source tree. */ -#include +#include + +#include namespace at { namespace native { @@ -29,6 +31,14 @@ api::utils::uvec3 adaptive_work_group_size( return local_group_size; } +api::utils::ivec4 get_size_as_ivec4(const vTensor& t) { + return api::utils::make_ivec4( + {dim_at(t), + dim_at(t), + dim_at(t), + dim_at(t)}); +} + } // namespace vulkan } // namespace native } // namespace at diff --git a/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h b/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h new file mode 100644 index 00000000000..a01e8c0a4df --- /dev/null +++ b/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h @@ -0,0 +1,28 @@ +/* + * 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 + +#ifdef USE_VULKAN_API + +#include + +namespace at { +namespace native { +namespace vulkan { + +api::utils::uvec3 adaptive_work_group_size( + const api::utils::uvec3& global_work_group); + +api::utils::ivec4 get_size_as_ivec4(const vTensor& t); + +} // namespace vulkan +} // namespace native +} // namespace at + +#endif /* USE_VULKAN_API */ diff --git a/backends/vulkan/runtime/graph/ops/Utils.cpp b/backends/vulkan/runtime/graph/ops/utils/BindingUtils.cpp similarity index 83% rename from backends/vulkan/runtime/graph/ops/Utils.cpp rename to backends/vulkan/runtime/graph/ops/utils/BindingUtils.cpp index dda813c669a..6e471167ec6 100644 --- a/backends/vulkan/runtime/graph/ops/Utils.cpp +++ b/backends/vulkan/runtime/graph/ops/utils/BindingUtils.cpp @@ -6,22 +6,12 @@ * LICENSE file in the root directory of this source tree. */ -#include - -#include +#include namespace at { namespace native { namespace vulkan { -api::utils::ivec4 get_size_as_ivec4(const vTensor& t) { - return api::utils::make_ivec4( - {dim_at(t), - dim_at(t), - dim_at(t), - dim_at(t)}); -} - void bind_tensor_to_descriptor_set( vTensor& tensor, api::PipelineBarrier& pipeline_barrier, diff --git a/backends/vulkan/runtime/graph/ops/Utils.h b/backends/vulkan/runtime/graph/ops/utils/BindingUtils.h similarity index 94% rename from backends/vulkan/runtime/graph/ops/Utils.h rename to backends/vulkan/runtime/graph/ops/utils/BindingUtils.h index b79c95eb934..28649a1194d 100644 --- a/backends/vulkan/runtime/graph/ops/Utils.h +++ b/backends/vulkan/runtime/graph/ops/utils/BindingUtils.h @@ -16,8 +16,6 @@ namespace at { namespace native { namespace vulkan { -api::utils::ivec4 get_size_as_ivec4(const vTensor& t); - void bind_tensor_to_descriptor_set( vTensor& tensor, api::PipelineBarrier& pipeline_barrier, diff --git a/backends/vulkan/runtime/graph/ops/StagingUtils.cpp b/backends/vulkan/runtime/graph/ops/utils/StagingUtils.cpp similarity index 87% rename from backends/vulkan/runtime/graph/ops/StagingUtils.cpp rename to backends/vulkan/runtime/graph/ops/utils/StagingUtils.cpp index 1637cfb2e1a..50f812df841 100644 --- a/backends/vulkan/runtime/graph/ops/StagingUtils.cpp +++ b/backends/vulkan/runtime/graph/ops/utils/StagingUtils.cpp @@ -6,15 +6,36 @@ * LICENSE file in the root directory of this source tree. */ -#include +// @lint-ignore-every CLANGTIDY facebook-security-vulnerable-memcpy -#include -#include +#include + +#include + +#include namespace at { namespace native { namespace vulkan { +template +void memcpy_to_mapping_impl( + const void* src, + api::MemoryMap& dst_mapping, + const size_t nbytes) { + T* data_ptr = dst_mapping.template data(); + memcpy(data_ptr, reinterpret_cast(src), nbytes); +} + +template +void memcpy_from_mapping_impl( + api::MemoryMap& src_mapping, + void* dst, + const size_t nbytes) { + T* data_ptr = src_mapping.template data(); + memcpy(reinterpret_cast(dst), data_ptr, nbytes); +} + void memcpy_to_mapping( const void* src, api::MemoryMap& dst_mapping, diff --git a/backends/vulkan/runtime/graph/ops/StagingUtils.h b/backends/vulkan/runtime/graph/ops/utils/StagingUtils.h similarity index 51% rename from backends/vulkan/runtime/graph/ops/StagingUtils.h rename to backends/vulkan/runtime/graph/ops/utils/StagingUtils.h index c101581a77e..2e5de6efb06 100644 --- a/backends/vulkan/runtime/graph/ops/StagingUtils.h +++ b/backends/vulkan/runtime/graph/ops/utils/StagingUtils.h @@ -12,49 +12,10 @@ #include -#include - namespace at { namespace native { namespace vulkan { -// -// Functions to memcpy data into staging buffer -// - -void memcpy_to_mapping( - const void* src, - api::MemoryMap& dst_mapping, - const size_t nbytes, - const api::ScalarType dtype); -void memcpy_from_mapping( - const api::MemoryMap& src_mapping, - void* dst, - const size_t nbytes, - const api::ScalarType dtype); - -// -// Utility functions for memcpy -// - -template -void memcpy_to_mapping_impl( - const void* src, - api::MemoryMap& dst_mapping, - const size_t nbytes) { - T* data_ptr = dst_mapping.template data(); - memcpy(data_ptr, reinterpret_cast(src), nbytes); -} - -template -void memcpy_from_mapping_impl( - api::MemoryMap& src_mapping, - void* dst, - const size_t nbytes) { - T* data_ptr = src_mapping.template data(); - memcpy(reinterpret_cast(dst), data_ptr, nbytes); -} - // // Functions to copy data into and out of a staging buffer // diff --git a/backends/vulkan/test/vulkan_compute_api_test.cpp b/backends/vulkan/test/vulkan_compute_api_test.cpp index 334a2937105..6b041ab826a 100644 --- a/backends/vulkan/test/vulkan_compute_api_test.cpp +++ b/backends/vulkan/test/vulkan_compute_api_test.cpp @@ -10,14 +10,15 @@ #include -#include - #include -#include #include #include +#include + +#include + using namespace at::native::vulkan; #define CREATE_FLOAT_TEXTURE(sizes, allocate_memory) \