Skip to content

Commit ac8267b

Browse files
jorgep31415facebook-github-bot
authored andcommitted
Organize utils (#2323)
Summary: bypass-github-export-checks Pull Request resolved: #2323 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 <ATen/native/vulkan/api/api.h> ``` instead of including more specific files, e.g., ``` #include <ATen/native/vulkan/api/Tensor.h> ``` ghstack-source-id: 217990425 exported-using-ghexport Reviewed By: SS-JIA Differential Revision: D54690346 fbshipit-source-id: ac5c0713fa73ab51ef6fbfc40e48198ab3fe627c
1 parent e5c2853 commit ac8267b

20 files changed

+126
-105
lines changed

backends/vulkan/runtime/graph/ComputeGraph.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include <executorch/backends/vulkan/runtime/graph/ComputeGraph.h>
9+
// @lint-ignore-every CLANGTIDY
10+
// facebook-security-vulnerable-integer-sign-conversion
1011

11-
#include <executorch/backends/vulkan/runtime/graph/ops/StagingUtils.h>
12+
#include <executorch/backends/vulkan/runtime/graph/ComputeGraph.h>
1213

1314
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Staging.h>
1415

16+
#include <executorch/backends/vulkan/runtime/graph/ops/utils/StagingUtils.h>
17+
1518
namespace at {
1619
namespace native {
1720
namespace vulkan {

backends/vulkan/runtime/graph/ComputeGraph.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
#ifdef USE_VULKAN_API
1414

15-
#include <ATen/native/vulkan/api/Context.h>
16-
#include <ATen/native/vulkan/api/Tensor.h>
17-
#include <ATen/native/vulkan/api/Types.h>
15+
#include <ATen/native/vulkan/api/api.h>
1816

1917
#include <executorch/backends/vulkan/runtime/graph/GraphConfig.h>
2018

backends/vulkan/runtime/graph/GraphConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#ifdef USE_VULKAN_API
1212

13-
#include <ATen/native/vulkan/api/Context.h>
13+
#include <ATen/native/vulkan/api/api.h>
1414

1515
namespace at {
1616
namespace native {

backends/vulkan/runtime/graph/ops/ExecuteNode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include <executorch/backends/vulkan/runtime/graph/ComputeGraph.h>
1212

13-
#include <executorch/backends/vulkan/runtime/graph/ops/Utils.h>
13+
#include <executorch/backends/vulkan/runtime/graph/ops/utils/BindingUtils.h>
1414

1515
namespace at {
1616
namespace native {

backends/vulkan/runtime/graph/ops/ExecuteNode.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
#ifdef USE_VULKAN_API
1212

13-
#include <ATen/native/vulkan/api/Context.h>
14-
#include <ATen/native/vulkan/api/Tensor.h>
15-
#include <ATen/native/vulkan/api/Types.h>
13+
#include <ATen/native/vulkan/api/api.h>
1614

1715
#include <executorch/backends/vulkan/runtime/graph/containers/Value.h>
1816

backends/vulkan/runtime/graph/ops/OperatorRegistry.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
#include <executorch/backends/vulkan/runtime/graph/ops/OperatorRegistry.h>
1010

11-
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Arithmetic.h>
12-
1311
namespace at {
1412
namespace native {
1513
namespace vulkan {

backends/vulkan/runtime/graph/ops/PrepackNode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
#include <executorch/backends/vulkan/runtime/graph/ComputeGraph.h>
1212

13-
#include <executorch/backends/vulkan/runtime/graph/ops/StagingUtils.h>
14-
#include <executorch/backends/vulkan/runtime/graph/ops/Utils.h>
13+
#include <executorch/backends/vulkan/runtime/graph/ops/utils/BindingUtils.h>
14+
#include <executorch/backends/vulkan/runtime/graph/ops/utils/StagingUtils.h>
1515

1616
namespace at {
1717
namespace native {

backends/vulkan/runtime/graph/ops/PrepackNode.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
#ifdef USE_VULKAN_API
1212

13-
#include <ATen/native/vulkan/api/Context.h>
14-
#include <ATen/native/vulkan/api/Tensor.h>
15-
#include <ATen/native/vulkan/api/Types.h>
13+
#include <ATen/native/vulkan/api/api.h>
1614

1715
#include <executorch/backends/vulkan/runtime/graph/containers/Value.h>
1816

backends/vulkan/runtime/graph/ops/impl/Arithmetic.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Arithmetic.h>
1010

11-
#include <executorch/backends/vulkan/runtime/graph/ops/OpUtils.h>
1211
#include <executorch/backends/vulkan/runtime/graph/ops/OperatorRegistry.h>
1312

1413
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Staging.h>
1514

15+
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/ScalarUtils.h>
16+
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h>
17+
1618
namespace at {
1719
namespace native {
1820
namespace vulkan {

backends/vulkan/runtime/graph/ops/impl/Arithmetic.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#include <executorch/backends/vulkan/runtime/graph/ComputeGraph.h>
1414

15-
#include <executorch/backends/vulkan/runtime/graph/ops/Utils.h>
16-
1715
namespace at {
1816
namespace native {
1917
namespace vulkan {

backends/vulkan/runtime/graph/ops/impl/Staging.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Staging.h>
1010

11-
#include <executorch/backends/vulkan/runtime/graph/ops/OpUtils.h>
12-
#include <executorch/backends/vulkan/runtime/graph/ops/StagingUtils.h>
13-
#include <executorch/backends/vulkan/runtime/graph/ops/Utils.h>
11+
#include <executorch/backends/vulkan/runtime/graph/ops/utils/StagingUtils.h>
12+
13+
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/DimUtils.h>
14+
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h>
1415

1516
namespace at {
1617
namespace native {

backends/vulkan/runtime/graph/ops/OpUtils.h renamed to backends/vulkan/runtime/graph/ops/impl/utils/DimUtils.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#include <ATen/native/vulkan/api/api.h>
1414

15-
#include <executorch/backends/vulkan/runtime/graph/containers/Value.h>
16-
1715
namespace at {
1816
namespace native {
1917
namespace vulkan {
@@ -75,27 +73,6 @@ uint32_t dim_at(const vTensor& v_in) {
7573
return dim_at<N>(v_in.sizes());
7674
}
7775

78-
/*
79-
* For most global work group sizes, returns {4, 4, 4}, but adjusts the size for
80-
* 2D global work group sizes. Always maintains a total of 64 invocations
81-
*/
82-
api::utils::uvec3 adaptive_work_group_size(
83-
const api::utils::uvec3& global_work_group);
84-
85-
template <typename T>
86-
T extract_scalar(const Value& value) {
87-
if (value.isInt()) {
88-
return static_cast<T>(value.toInt());
89-
}
90-
if (value.isDouble()) {
91-
return static_cast<T>(value.toDouble());
92-
}
93-
if (value.isBool()) {
94-
return static_cast<T>(value.toBool());
95-
}
96-
VK_THROW("Cannot extract scalar from Value with type ", value.type());
97-
}
98-
9976
} // namespace vulkan
10077
} // namespace native
10178
} // namespace at
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#ifdef USE_VULKAN_API
12+
13+
#include <ATen/native/vulkan/api/api.h>
14+
15+
#include <executorch/backends/vulkan/runtime/graph/containers/Value.h>
16+
17+
namespace at {
18+
namespace native {
19+
namespace vulkan {
20+
21+
template <typename T>
22+
T extract_scalar(const Value& value) {
23+
if (value.isInt()) {
24+
return static_cast<T>(value.toInt());
25+
}
26+
if (value.isDouble()) {
27+
return static_cast<T>(value.toDouble());
28+
}
29+
if (value.isBool()) {
30+
return static_cast<T>(value.toBool());
31+
}
32+
VK_THROW("Cannot extract scalar from Value with type ", value.type());
33+
}
34+
35+
} // namespace vulkan
36+
} // namespace native
37+
} // namespace at
38+
39+
#endif /* USE_VULKAN_API */

backends/vulkan/runtime/graph/ops/OpUtils.cpp renamed to backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include <executorch/backends/vulkan/runtime/graph/ops/OpUtils.h>
9+
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h>
10+
11+
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/DimUtils.h>
1012

1113
namespace at {
1214
namespace native {
@@ -29,6 +31,14 @@ api::utils::uvec3 adaptive_work_group_size(
2931
return local_group_size;
3032
}
3133

34+
api::utils::ivec4 get_size_as_ivec4(const vTensor& t) {
35+
return api::utils::make_ivec4(
36+
{dim_at<Dim4D::Width>(t),
37+
dim_at<Dim4D::Height>(t),
38+
dim_at<Dim4D::Channel>(t),
39+
dim_at<Dim4D::Batch>(t)});
40+
}
41+
3242
} // namespace vulkan
3343
} // namespace native
3444
} // namespace at
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#ifdef USE_VULKAN_API
12+
13+
#include <ATen/native/vulkan/api/api.h>
14+
15+
namespace at {
16+
namespace native {
17+
namespace vulkan {
18+
19+
api::utils::uvec3 adaptive_work_group_size(
20+
const api::utils::uvec3& global_work_group);
21+
22+
api::utils::ivec4 get_size_as_ivec4(const vTensor& t);
23+
24+
} // namespace vulkan
25+
} // namespace native
26+
} // namespace at
27+
28+
#endif /* USE_VULKAN_API */

backends/vulkan/runtime/graph/ops/Utils.cpp renamed to backends/vulkan/runtime/graph/ops/utils/BindingUtils.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include <executorch/backends/vulkan/runtime/graph/ops/Utils.h>
10-
11-
#include <executorch/backends/vulkan/runtime/graph/ops/OpUtils.h>
9+
#include <executorch/backends/vulkan/runtime/graph/ops/utils/BindingUtils.h>
1210

1311
namespace at {
1412
namespace native {
1513
namespace vulkan {
1614

17-
api::utils::ivec4 get_size_as_ivec4(const vTensor& t) {
18-
return api::utils::make_ivec4(
19-
{dim_at<Dim4D::Width>(t),
20-
dim_at<Dim4D::Height>(t),
21-
dim_at<Dim4D::Channel>(t),
22-
dim_at<Dim4D::Batch>(t)});
23-
}
24-
2515
void bind_tensor_to_descriptor_set(
2616
vTensor& tensor,
2717
api::PipelineBarrier& pipeline_barrier,

backends/vulkan/runtime/graph/ops/Utils.h renamed to backends/vulkan/runtime/graph/ops/utils/BindingUtils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ namespace at {
1616
namespace native {
1717
namespace vulkan {
1818

19-
api::utils::ivec4 get_size_as_ivec4(const vTensor& t);
20-
2119
void bind_tensor_to_descriptor_set(
2220
vTensor& tensor,
2321
api::PipelineBarrier& pipeline_barrier,

backends/vulkan/runtime/graph/ops/StagingUtils.cpp renamed to backends/vulkan/runtime/graph/ops/utils/StagingUtils.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,36 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include <executorch/backends/vulkan/runtime/graph/ops/StagingUtils.h>
9+
// @lint-ignore-every CLANGTIDY facebook-security-vulnerable-memcpy
1010

11-
#include <executorch/backends/vulkan/runtime/graph/ops/OpUtils.h>
12-
#include <executorch/backends/vulkan/runtime/graph/ops/Utils.h>
11+
#include <executorch/backends/vulkan/runtime/graph/ops/utils/StagingUtils.h>
12+
13+
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/DimUtils.h>
14+
15+
#include <cstring>
1316

1417
namespace at {
1518
namespace native {
1619
namespace vulkan {
1720

21+
template <typename T>
22+
void memcpy_to_mapping_impl(
23+
const void* src,
24+
api::MemoryMap& dst_mapping,
25+
const size_t nbytes) {
26+
T* data_ptr = dst_mapping.template data<T>();
27+
memcpy(data_ptr, reinterpret_cast<const T*>(src), nbytes);
28+
}
29+
30+
template <typename T>
31+
void memcpy_from_mapping_impl(
32+
api::MemoryMap& src_mapping,
33+
void* dst,
34+
const size_t nbytes) {
35+
T* data_ptr = src_mapping.template data<T>();
36+
memcpy(reinterpret_cast<T*>(dst), data_ptr, nbytes);
37+
}
38+
1839
void memcpy_to_mapping(
1940
const void* src,
2041
api::MemoryMap& dst_mapping,

backends/vulkan/runtime/graph/ops/StagingUtils.h renamed to backends/vulkan/runtime/graph/ops/utils/StagingUtils.h

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,10 @@
1212

1313
#include <executorch/backends/vulkan/runtime/graph/ComputeGraph.h>
1414

15-
#include <cstring>
16-
1715
namespace at {
1816
namespace native {
1917
namespace vulkan {
2018

21-
//
22-
// Functions to memcpy data into staging buffer
23-
//
24-
25-
void memcpy_to_mapping(
26-
const void* src,
27-
api::MemoryMap& dst_mapping,
28-
const size_t nbytes,
29-
const api::ScalarType dtype);
30-
void memcpy_from_mapping(
31-
const api::MemoryMap& src_mapping,
32-
void* dst,
33-
const size_t nbytes,
34-
const api::ScalarType dtype);
35-
36-
//
37-
// Utility functions for memcpy
38-
//
39-
40-
template <typename T>
41-
void memcpy_to_mapping_impl(
42-
const void* src,
43-
api::MemoryMap& dst_mapping,
44-
const size_t nbytes) {
45-
T* data_ptr = dst_mapping.template data<T>();
46-
memcpy(data_ptr, reinterpret_cast<const T*>(src), nbytes);
47-
}
48-
49-
template <typename T>
50-
void memcpy_from_mapping_impl(
51-
api::MemoryMap& src_mapping,
52-
void* dst,
53-
const size_t nbytes) {
54-
T* data_ptr = src_mapping.template data<T>();
55-
memcpy(reinterpret_cast<T*>(dst), data_ptr, nbytes);
56-
}
57-
5819
//
5920
// Functions to copy data into and out of a staging buffer
6021
//

0 commit comments

Comments
 (0)