Skip to content

Commit 830bcd1

Browse files
committed
[ET-VK] Move GPUMemoryLayout and StorageType to utils/
``` find . -type f -exec sed -i 's/vkapi::kBuffer/utils::kBuffer/g' {} + find . -type f -exec sed -i 's/vkapi::kTexture/utils::kTexture/g' {} + find . -type f -exec sed -i 's/vkapi::kWidthPacked/utils::kWidthPacked/g' {} + find . -type f -exec sed -i 's/vkapi::kHeightPacked/utils::kHeightPacked/g' {} + find . -type f -exec sed -i 's/vkapi::kChannelsPacked/utils::kChannelsPacked/g' {} + find . -type f -exec sed -i 's/vkapi::to_packed_dim_nchw_offset/utils::to_packed_dim_nchw_offset/g' {} + find . -type f -exec sed -i 's/vkapi::GPUMemoryLayout/utils::GPUMemoryLayout/g' {} + find . -type f -exec sed -i 's/vkapi::StorageType/utils::StorageType/g' {} + ``` Differential Revision: [D59527913](https://our.internmc.facebook.com/intern/diff/D59527913/) [ghstack-poisoned]
1 parent ef79aac commit 830bcd1

38 files changed

+345
-330
lines changed

backends/vulkan/runtime/VulkanBackend.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,30 @@ vkapi::ScalarType get_scalar_type(const vkgraph::VkDataType& vk_datatype) {
7676
}
7777
}
7878

79-
vkapi::StorageType get_storage_type(
79+
utils::StorageType get_storage_type(
8080
const vkgraph::VkStorageType& vk_storage_type) {
8181
switch (vk_storage_type) {
8282
case vkgraph::VkStorageType::BUFFER:
83-
return vkapi::kBuffer;
83+
return utils::kBuffer;
8484
case vkgraph::VkStorageType::TEXTURE_3D:
85-
return vkapi::kTexture3D;
85+
return utils::kTexture3D;
8686
case vkgraph::VkStorageType::TEXTURE_2D:
87-
return vkapi::kTexture2D;
87+
return utils::kTexture2D;
8888
default:
8989
break;
9090
}
9191
VK_THROW("Invalid storage type encountered!");
9292
}
9393

94-
vkapi::GPUMemoryLayout get_memory_layout(
94+
utils::GPUMemoryLayout get_memory_layout(
9595
const vkgraph::VkMemoryLayout& vk_memory_layout) {
9696
switch (vk_memory_layout) {
9797
case vkgraph::VkMemoryLayout::TENSOR_WIDTH_PACKED:
98-
return vkapi::kWidthPacked;
98+
return utils::kWidthPacked;
9999
case vkgraph::VkMemoryLayout::TENSOR_HEIGHT_PACKED:
100-
return vkapi::kHeightPacked;
100+
return utils::kHeightPacked;
101101
case vkgraph::VkMemoryLayout::TENSOR_CHANNELS_PACKED:
102-
return vkapi::kChannelsPacked;
102+
return utils::kChannelsPacked;
103103
default:
104104
break;
105105
}
@@ -115,16 +115,16 @@ GraphConfig get_graph_config(ArrayRef<CompileSpec>& compile_specs) {
115115
if (strcmp(spec.key, "storage_type_override") == 0) {
116116
ET_CHECK_MSG(value_size == sizeof(int32_t), "Unexpected value size!");
117117
int value_as_int = static_cast<int>(getUInt32LE(value_data));
118-
vkapi::StorageType storage_type =
119-
static_cast<vkapi::StorageType>(value_as_int);
118+
utils::StorageType storage_type =
119+
static_cast<utils::StorageType>(value_as_int);
120120

121121
config.set_storage_type_override(storage_type);
122122
}
123123
if (strcmp(spec.key, "memory_layout_override") == 0) {
124124
ET_CHECK_MSG(value_size == sizeof(uint32_t), "Unexpected value size!");
125125
uint32_t value_as_int = getUInt32LE(value_data);
126-
vkapi::GPUMemoryLayout memory_layout =
127-
static_cast<vkapi::GPUMemoryLayout>(value_as_int);
126+
utils::GPUMemoryLayout memory_layout =
127+
static_cast<utils::GPUMemoryLayout>(value_as_int);
128128

129129
config.set_memory_layout_override(memory_layout);
130130
}
@@ -172,15 +172,15 @@ class GraphBuilder {
172172

173173
void add_tensor_to_graph(const uint32_t fb_id, VkTensorPtr tensor_fb) {
174174
const vkapi::ScalarType& dtype = get_scalar_type(tensor_fb->datatype());
175-
vkapi::StorageType storage_type =
175+
utils::StorageType storage_type =
176176
tensor_fb->storage_type() == vkgraph::VkStorageType::DEFAULT_STORAGE
177177
? compute_graph_->suggested_storage_type()
178178
: get_storage_type(tensor_fb->storage_type());
179179

180180
UIntVector dims_fb = tensor_fb->dims();
181181
const std::vector<int64_t> dims_vector(dims_fb->cbegin(), dims_fb->cend());
182182

183-
vkapi::GPUMemoryLayout memory_layout =
183+
utils::GPUMemoryLayout memory_layout =
184184
tensor_fb->memory_layout() == vkgraph::VkMemoryLayout::DEFAULT_LAYOUT
185185
? compute_graph_->suggested_memory_layout(dims_vector)
186186
: get_memory_layout(tensor_fb->memory_layout());

backends/vulkan/runtime/api/Tensor.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ namespace api {
1515

1616
std::vector<int64_t> calculate_strides(
1717
const std::vector<int64_t>& sizes,
18-
const vkapi::GPUMemoryLayout memory_layout,
18+
const utils::GPUMemoryLayout memory_layout,
1919
const bool texel_strides) {
2020
const int64_t dim_offset =
21-
vkapi::to_packed_dim_nchw_offset<int64_t>(memory_layout);
21+
utils::to_packed_dim_nchw_offset<int64_t>(memory_layout);
2222
const int64_t last_dim = sizes.size() - dim_offset;
2323
VK_CHECK_COND(last_dim >= 0);
2424

@@ -45,7 +45,7 @@ std::vector<int64_t> calculate_strides(
4545

4646
std::vector<int64_t> calculate_padded_sizes(
4747
const std::vector<int64_t>& sizes,
48-
const vkapi::GPUMemoryLayout memory_layout) {
48+
const utils::GPUMemoryLayout memory_layout) {
4949
int64_t ndim = sizes.size();
5050
if (ndim == 0) {
5151
ndim = 1;
@@ -60,7 +60,7 @@ std::vector<int64_t> calculate_padded_sizes(
6060

6161
// Pad the packed dim to the next multiple of 4.
6262
const int64_t dim_offset =
63-
vkapi::to_packed_dim_nchw_offset<int64_t>(memory_layout);
63+
utils::to_packed_dim_nchw_offset<int64_t>(memory_layout);
6464
const int64_t padded_dim_size = utils::val_at(-dim_offset, sizes);
6565
padded_sizes.at(ndim_up4 - dim_offset) = utils::align_up_4(padded_dim_size);
6666

@@ -69,7 +69,7 @@ std::vector<int64_t> calculate_padded_sizes(
6969

7070
utils::uvec3 calculate_image_extents(
7171
const std::vector<int64_t>& padded_sizes,
72-
const vkapi::GPUMemoryLayout memory_layout) {
72+
const utils::GPUMemoryLayout memory_layout) {
7373
VK_CHECK_COND(padded_sizes.size() == 4);
7474

7575
uint32_t N = utils::safe_downcast<uint32_t>(padded_sizes.at(0));
@@ -78,15 +78,15 @@ utils::uvec3 calculate_image_extents(
7878
uint32_t W = utils::safe_downcast<uint32_t>(padded_sizes.at(3));
7979

8080
switch (memory_layout) {
81-
case vkapi::kWidthPacked:
81+
case utils::kWidthPacked:
8282
VK_CHECK_COND(W % 4 == 0);
8383
W /= 4;
8484
break;
85-
case vkapi::kHeightPacked:
85+
case utils::kHeightPacked:
8686
VK_CHECK_COND(H % 4 == 0);
8787
H /= 4;
8888
break;
89-
case vkapi::kChannelsPacked:
89+
case utils::kChannelsPacked:
9090
VK_CHECK_COND(C % 4 == 0);
9191
C /= 4;
9292
break;
@@ -103,8 +103,8 @@ vTensor::vTensor(
103103
Context* const context,
104104
const std::vector<int64_t>& sizes,
105105
const vkapi::ScalarType dtype,
106-
const vkapi::StorageType storage_type,
107-
const vkapi::GPUMemoryLayout memory_layout,
106+
const utils::StorageType storage_type,
107+
const utils::GPUMemoryLayout memory_layout,
108108
const bool allocate_memory)
109109
: dtype_(dtype),
110110
memory_layout_(memory_layout),
@@ -125,7 +125,7 @@ vTensor::vTensor(
125125
padded_sizes_,
126126
dtype_,
127127
allocate_memory) {
128-
if (storage_type != vkapi::kBuffer) {
128+
if (storage_type != utils::kBuffer) {
129129
texture_limits_.limits = utils::ivec3{
130130
utils::safe_downcast<int32_t>(storage_.image_extents_.data[0]),
131131
utils::safe_downcast<int32_t>(storage_.image_extents_.data[1]),
@@ -204,33 +204,33 @@ const vkapi::BufferBindInfo vTensor::ntexels_ubo() {
204204

205205
VmaAllocationCreateInfo vTensor::get_allocation_create_info() const {
206206
switch (storage_type()) {
207-
case vkapi::kBuffer:
207+
case utils::kBuffer:
208208
return storage_.buffer_.allocation_create_info();
209-
case vkapi::kTexture2D:
210-
case vkapi::kTexture3D:
209+
case utils::kTexture2D:
210+
case utils::kTexture3D:
211211
return storage_.image_.allocation_create_info();
212212
}
213213
return {};
214214
}
215215

216216
VkMemoryRequirements vTensor::get_memory_requirements() const {
217217
switch (storage_type()) {
218-
case vkapi::kBuffer:
218+
case utils::kBuffer:
219219
return storage_.buffer_.get_memory_requirements();
220-
case vkapi::kTexture2D:
221-
case vkapi::kTexture3D:
220+
case utils::kTexture2D:
221+
case utils::kTexture3D:
222222
return storage_.image_.get_memory_requirements();
223223
}
224224
return {};
225225
}
226226

227227
void vTensor::bind_allocation(const vkapi::Allocation& allocation) {
228228
switch (storage_type()) {
229-
case vkapi::kBuffer:
229+
case utils::kBuffer:
230230
storage_.buffer_.bind_allocation(allocation);
231231
break;
232-
case vkapi::kTexture2D:
233-
case vkapi::kTexture3D:
232+
case utils::kTexture2D:
233+
case utils::kTexture3D:
234234
storage_.image_.bind_allocation(allocation);
235235
break;
236236
}
@@ -275,7 +275,7 @@ void vTensor::reallocate(const std::vector<int64_t>& new_sizes) {
275275
}
276276

277277
void vTensor::virtual_resize(const std::vector<int64_t>& new_sizes) {
278-
if (storage_type() != vkapi::kBuffer) {
278+
if (storage_type() != utils::kBuffer) {
279279
// For texture storage check that the current texture is large enough for
280280
// the new sizes of the tensor.
281281
utils::uvec3 virtual_extents =
@@ -302,7 +302,7 @@ void vTensor::virtual_resize(const std::vector<int64_t>& new_sizes) {
302302
vkapi::VulkanImage allocate_image(
303303
Context* const context_ptr,
304304
utils::uvec3& image_extents,
305-
const vkapi::StorageType storage_type,
305+
const utils::StorageType storage_type,
306306
const VkFormat image_format,
307307
const bool allocate_memory) {
308308
vkapi::Adapter* adapter_ptr = context_ptr->adapter_ptr();
@@ -318,11 +318,11 @@ vkapi::VulkanImage allocate_image(
318318
VkImageViewType image_view_type;
319319

320320
switch (storage_type) {
321-
case vkapi::kTexture3D:
321+
case utils::kTexture3D:
322322
image_type = VK_IMAGE_TYPE_3D;
323323
image_view_type = VK_IMAGE_VIEW_TYPE_3D;
324324
break;
325-
case vkapi::kTexture2D:
325+
case utils::kTexture2D:
326326
image_type = VK_IMAGE_TYPE_2D;
327327
image_view_type = VK_IMAGE_VIEW_TYPE_2D;
328328
break;
@@ -347,13 +347,13 @@ vkapi::VulkanImage allocate_image(
347347
vkapi::VulkanBuffer allocate_buffer(
348348
Context* const context_ptr,
349349
const int64_t numel,
350-
const vkapi::StorageType storage_type,
350+
const utils::StorageType storage_type,
351351
const vkapi::ScalarType dtype,
352352
const bool allocate_memory) {
353353
vkapi::Adapter* adapter_ptr = context_ptr->adapter_ptr();
354354

355355
switch (storage_type) {
356-
case vkapi::kBuffer:
356+
case utils::kBuffer:
357357
break;
358358
default:
359359
// Return an empty VulkanBuffer if Buffer storage is not used
@@ -366,8 +366,8 @@ vkapi::VulkanBuffer allocate_buffer(
366366

367367
vTensorStorage::vTensorStorage(
368368
Context* const context,
369-
const vkapi::StorageType storage_type,
370-
const vkapi::GPUMemoryLayout gpu_memory_layout,
369+
const utils::StorageType storage_type,
370+
const utils::GPUMemoryLayout gpu_memory_layout,
371371
const std::vector<int64_t>& padded_sizes,
372372
const vkapi::ScalarType dtype,
373373
const bool allocate_memory)
@@ -458,7 +458,7 @@ void vTensorStorage::transition(
458458

459459
void vTensorStorage::discard_and_reallocate(
460460
const std::vector<int64_t>& padded_sizes,
461-
const vkapi::GPUMemoryLayout gpu_memory_layout,
461+
const utils::GPUMemoryLayout gpu_memory_layout,
462462
const vkapi::ScalarType dtype) {
463463
const bool image_owns_memory = image_.owns_memory();
464464
const bool buffer_owns_memory = buffer_.owns_memory();

backends/vulkan/runtime/api/Tensor.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <executorch/backends/vulkan/runtime/api/Context.h>
1414
#include <executorch/backends/vulkan/runtime/api/ParamsBuffer.h>
1515

16-
#include <executorch/backends/vulkan/runtime/vk_api/Types.h>
16+
#include <executorch/backends/vulkan/runtime/utils/StorageUtils.h>
1717

1818
namespace vkcompute {
1919
namespace api {
@@ -31,7 +31,7 @@ namespace api {
3131
*/
3232
std::vector<int64_t> calculate_strides(
3333
const std::vector<int64_t>& sizes,
34-
const vkapi::GPUMemoryLayout memory_layout,
34+
const utils::GPUMemoryLayout memory_layout,
3535
const bool texel_strides = true);
3636

3737
/*
@@ -46,20 +46,20 @@ std::vector<int64_t> calculate_strides(
4646
* 1. The dimensionality of the tensor will be padded to a multiple of 4.
4747
* 2. The size of the packed dimension will be padded to a multiple of 4.
4848
*
49-
* The "packed dimension" is determined based on the vkapi::GPUMemoryLayout
49+
* The "packed dimension" is determined based on the utils::GPUMemoryLayout
5050
* argument.
5151
*/
5252
std::vector<int64_t> calculate_padded_sizes(
5353
const std::vector<int64_t>& sizes,
54-
const vkapi::GPUMemoryLayout memory_layout);
54+
const utils::GPUMemoryLayout memory_layout);
5555

5656
/*
5757
* Given the padded sizes of a tensor and the GPU memory layout, calculate the
5858
* 3D image extents required to store the tensor data as an image texture.
5959
*/
6060
utils::uvec3 calculate_image_extents(
6161
const std::vector<int64_t>& padded_sizes,
62-
const vkapi::GPUMemoryLayout memory_layout);
62+
const utils::GPUMemoryLayout memory_layout);
6363

6464
struct LastAccess {
6565
vkapi::PipelineStageFlags stage;
@@ -82,8 +82,8 @@ class vTensorStorage final {
8282

8383
vTensorStorage(
8484
Context* context,
85-
const vkapi::StorageType storage_type,
86-
const vkapi::GPUMemoryLayout gpu_memory_layout,
85+
const utils::StorageType storage_type,
86+
const utils::GPUMemoryLayout gpu_memory_layout,
8787
const std::vector<int64_t>& sizes,
8888
const vkapi::ScalarType dtype,
8989
const bool allocate_memory = true);
@@ -102,7 +102,7 @@ class vTensorStorage final {
102102
// Context
103103
Context* context_{};
104104

105-
vkapi::StorageType storage_type_;
105+
utils::StorageType storage_type_;
106106

107107
// Resource sizings
108108
utils::uvec3 image_extents_{};
@@ -135,7 +135,7 @@ class vTensorStorage final {
135135

136136
void discard_and_reallocate(
137137
const std::vector<int64_t>& padded_sizes,
138-
const vkapi::GPUMemoryLayout gpu_memory_layout,
138+
const utils::GPUMemoryLayout gpu_memory_layout,
139139
const vkapi::ScalarType dtype);
140140
};
141141

@@ -152,8 +152,8 @@ class vTensor final {
152152
Context* context,
153153
const std::vector<int64_t>& sizes,
154154
const vkapi::ScalarType dtype,
155-
const vkapi::StorageType storage_type = vkapi::kTexture3D,
156-
const vkapi::GPUMemoryLayout memory_layout = vkapi::kChannelsPacked,
155+
const utils::StorageType storage_type = utils::kTexture3D,
156+
const utils::GPUMemoryLayout memory_layout = utils::kChannelsPacked,
157157
const bool allocate_memory = true);
158158

159159
vTensor(const vTensor& other) = delete;
@@ -164,7 +164,7 @@ class vTensor final {
164164

165165
private:
166166
vkapi::ScalarType dtype_;
167-
vkapi::GPUMemoryLayout memory_layout_;
167+
utils::GPUMemoryLayout memory_layout_;
168168

169169
// sizes of the tensor in NCHW dimension order
170170
std::vector<int64_t> sizes_;
@@ -226,12 +226,12 @@ class vTensor final {
226226
Metadata
227227
*/
228228

229-
inline vkapi::StorageType storage_type() const {
229+
inline utils::StorageType storage_type() const {
230230
return storage_.storage_type_;
231231
}
232232

233233
inline bool has_buffer_storage() const {
234-
return storage_.storage_type_ == vkapi::kBuffer;
234+
return storage_.storage_type_ == utils::kBuffer;
235235
}
236236

237237
inline const utils::uvec3& image_extents() const {
@@ -245,7 +245,7 @@ class vTensor final {
245245
return dtype_;
246246
}
247247

248-
inline vkapi::GPUMemoryLayout gpu_memory_layout() const {
248+
inline utils::GPUMemoryLayout gpu_memory_layout() const {
249249
return memory_layout_;
250250
}
251251

0 commit comments

Comments
 (0)