Skip to content

Commit 6e9ca6e

Browse files
committed
Update on "[11/n][ET-VK] Introduce vTensor creation from external image"
Nearly all metadata is initialized to null/dummy values, except those absolutely needed in the pipeline: (1) image extents, (2) logical limits. Differential Revision: [D63843819](https://our.internmc.facebook.com/intern/diff/D63843819/) [ghstack-poisoned]
2 parents 5bf6b54 + 2414bbc commit 6e9ca6e

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

backends/vulkan/runtime/api/containers/Tensor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ vTensor::vTensor(
497497
numel_(utils::multiply_integers(sizes_)),
498498
padded_sizes_(calculate_padded_sizes(sizes_, packed_dim_)),
499499
unsqueezed_strides_(),
500-
padded_numel_(),
500+
padded_numel_(utils::multiply_integers(padded_sizes_)),
501501
logical_limits_(),
502502
// Utility Uniform Buffers that can be passed to shaders as arguments
503503
sizes_uniform_(),

backends/vulkan/runtime/vk_api/memory/Allocator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ VulkanImage Allocator::create_image(
134134
image_props,
135135
view_props,
136136
sampler_props,
137-
initial_layout,
138137
sampler,
138+
initial_layout,
139139
allocate_memory);
140140
}
141141

backends/vulkan/runtime/vk_api/memory/Image.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ VulkanImage::VulkanImage(
115115
const ImageProperties& image_props,
116116
const ViewProperties& view_props,
117117
const SamplerProperties& sampler_props,
118-
const VkImageLayout layout,
119118
VkSampler sampler,
119+
const VkImageLayout layout,
120120
const bool allocate_memory)
121121
: device_{device},
122122
image_properties_(image_props),
@@ -186,7 +186,8 @@ VulkanImage::VulkanImage(
186186
const ImageProperties& image_props,
187187
VkImage image,
188188
VkImageView image_view,
189-
VkSampler sampler)
189+
VkSampler sampler,
190+
const VkImageLayout layout)
190191
: device_{device},
191192
image_properties_{image_props},
192193
view_properties_{},
@@ -200,7 +201,7 @@ VulkanImage::VulkanImage(
200201
image_view,
201202
sampler,
202203
},
203-
layout_{VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL} {}
204+
layout_{layout} {}
204205

205206
VulkanImage::VulkanImage(const VulkanImage& other) noexcept
206207
: device_(other.device_),

backends/vulkan/runtime/vk_api/memory/Image.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,17 @@ class VulkanImage final {
9999
const ImageProperties&,
100100
const ViewProperties&,
101101
const SamplerProperties&,
102-
const VkImageLayout layout,
103102
VkSampler,
103+
const VkImageLayout,
104104
const bool allocate_memory = true);
105105

106106
explicit VulkanImage(
107107
VkDevice,
108108
const ImageProperties&,
109109
VkImage,
110110
VkImageView,
111-
VkSampler);
111+
VkSampler,
112+
const VkImageLayout);
112113

113114
protected:
114115
/*

backends/vulkan/test/vulkan_compute_api_test.cpp

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

2424
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/QPackUtils.h>
2525

26+
#include <executorch/backends/vulkan/runtime/vk_api/VkUtils.h>
27+
2628
#include <executorch/backends/vulkan/test/utils/test_utils.h>
2729

2830
using namespace vkcompute;
@@ -1094,6 +1096,49 @@ TEST_F(VulkanComputeAPITest, print_object_sizes) {
10941096
EXPECT_TRUE(sizeof(ExecuteNode) < 500);
10951097
}
10961098

1099+
TEST_F(VulkanComputeAPITest, test_tensor_creation_from_vulkan_image) {
1100+
const auto w = 16;
1101+
const auto h = 12;
1102+
const auto d = 1;
1103+
const utils::uvec3 image_extents = {w, h, d};
1104+
1105+
vkapi::Adapter* adapter_ptr = context()->adapter_ptr();
1106+
1107+
vkapi::ImageSampler::Properties sampler_props{
1108+
VK_FILTER_NEAREST,
1109+
VK_SAMPLER_MIPMAP_MODE_NEAREST,
1110+
VK_SAMPLER_ADDRESS_MODE_REPEAT,
1111+
VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK,
1112+
};
1113+
1114+
VkFormat image_format = VK_FORMAT_R32G32B32A32_SFLOAT;
1115+
VkImageType image_type = VK_IMAGE_TYPE_3D;
1116+
VkImageViewType image_view_type = VK_IMAGE_VIEW_TYPE_3D;
1117+
1118+
VkSampler sampler = adapter_ptr->sampler_cache().retrieve(sampler_props);
1119+
1120+
auto image = adapter_ptr->vma().create_image(
1121+
context()->device(),
1122+
vkapi::create_extent3d(image_extents),
1123+
image_format,
1124+
image_type,
1125+
image_view_type,
1126+
sampler_props,
1127+
sampler,
1128+
/*allow_transfer = */ true,
1129+
/*allocate_memory = */ true);
1130+
1131+
auto tensor = vTensor(context(), image);
1132+
1133+
const auto exp_sizes = std::vector<int64_t>{w, h, d * 4};
1134+
EXPECT_TRUE(tensor.sizes() == exp_sizes);
1135+
EXPECT_TRUE(tensor.packed_dim() == 2);
1136+
1137+
const auto exp_numel = w * h * d * 4;
1138+
EXPECT_TRUE(tensor.numel() == exp_numel);
1139+
EXPECT_TRUE(tensor.padded_numel() == exp_numel);
1140+
}
1141+
10971142
TEST(VulkanComputeGraphTest, test_values_scalars) {
10981143
GraphConfig config;
10991144
ComputeGraph graph(config);

0 commit comments

Comments
 (0)