Skip to content

Commit d7d2724

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 30f16ac + 835dc17 commit d7d2724

File tree

6 files changed

+47
-199
lines changed

6 files changed

+47
-199
lines changed

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
namespace vkcompute {
1414
namespace api {
1515

16+
std::vector<int64_t> calculate_sizes(
17+
const vkapi::VulkanImage& image,
18+
const utils::GPUMemoryLayout memory_layout) {
19+
auto sizes = std::vector<int64_t>{
20+
image.extents().width, image.extents().height, image.extents().depth};
21+
const auto packed_dim = utils::to_packed_dim<int32_t>(memory_layout);
22+
sizes.at(packed_dim) *= 4;
23+
return sizes;
24+
}
25+
1626
std::vector<int64_t> calculate_dim_order(
1727
const size_t ndim,
1828
const int32_t packed_dim) {
@@ -186,6 +196,18 @@ utils::uvec3 calculate_image_extents(
186196
// vTensorStorage
187197
//
188198

199+
utils::StorageType storage_type(const vkapi::VulkanImage& image) {
200+
const auto type = image.type();
201+
switch (type) {
202+
case VK_IMAGE_TYPE_3D:
203+
return utils::kTexture3D;
204+
case VK_IMAGE_TYPE_2D:
205+
return utils::kTexture2D;
206+
default:
207+
VK_THROW("Unsupported image type", type);
208+
}
209+
}
210+
189211
vkapi::VulkanImage allocate_image(
190212
Context* const context_ptr,
191213
utils::uvec3& image_extents,
@@ -285,7 +307,7 @@ vTensorStorage::vTensorStorage(
285307
Context* const context,
286308
const vkapi::VulkanImage& image)
287309
: context_(context),
288-
storage_type_{utils::kTexture2D},
310+
storage_type_{storage_type(image)},
289311
image_extents_(
290312
{image.extents().width,
291313
image.extents().height,
@@ -461,16 +483,19 @@ vTensor::vTensor(
461483
}
462484

463485
// NOLINTNEXTLINE
464-
vTensor::vTensor(Context* const context, const vkapi::VulkanImage& image)
465-
: dtype_(),
486+
vTensor::vTensor(
487+
Context* context,
488+
const vkapi::VulkanImage& image,
489+
const utils::GPUMemoryLayout memory_layout)
490+
: dtype_(vkapi::element_scalartype(image.format())),
466491
// Calculate tensor metadata
467-
sizes_(),
468-
packed_dim_(),
492+
sizes_(calculate_sizes(image, memory_layout)),
493+
packed_dim_(utils::to_packed_dim<int32_t>(memory_layout)),
469494
dim_order_(),
470495
axis_map_(default_axis_map()),
471496
strides_(),
472-
numel_(),
473-
padded_sizes_(),
497+
numel_(utils::multiply_integers(sizes_)),
498+
padded_sizes_(calculate_padded_sizes(sizes_, packed_dim_)),
474499
unsqueezed_strides_(),
475500
padded_numel_(),
476501
logical_limits_(),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ class vTensor final {
187187

188188
vTensor(const vTensor& other) = delete;
189189

190-
explicit vTensor(Context* context, const vkapi::VulkanImage& image);
190+
explicit vTensor(
191+
Context* context,
192+
const vkapi::VulkanImage& image,
193+
const utils::GPUMemoryLayout memory_layout = utils::kChannelsPacked);
191194

192195
/*
193196
* This constructor allows for the creation of a vTensor that references the

backends/vulkan/runtime/utils/AhbUtils.h

Lines changed: 0 additions & 185 deletions
This file was deleted.

backends/vulkan/runtime/vk_api/Descriptor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ DescriptorSet& DescriptorSet::bind(
116116
DescriptorSet& DescriptorSet::bind(
117117
const uint32_t idx,
118118
const VulkanImage& image) {
119-
// Check is only accurate for images allocated with VMA
120-
if (image.vma_allocator() != VK_NULL_HANDLE) {
121-
VK_CHECK_COND(
122-
image.has_memory(),
123-
"Image must be bound to memory for it to be usable");
124-
}
119+
// If the image does not have an allocator attached, then it is externally
120+
// allocated; assume it is already bound to memory. Otherwise, it must be
121+
// bound to a VmaAllocation to be used.
122+
VK_CHECK_COND(
123+
image.vma_allocator() == VK_NULL_HANDLE || image.has_memory(),
124+
"Image must be bound to memory for it to be usable");
125125

126126
VkImageLayout binding_layout = image.layout();
127127
if (shader_layout_signature_[idx] == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {

backends/vulkan/runtime/vk_api/Types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ inline ScalarType element_scalartype(const VkFormat vkformat) {
8484
case VK_FORMAT_R8G8B8A8_SINT:
8585
return kChar;
8686
case VK_FORMAT_R8G8B8A8_UINT:
87+
case VK_FORMAT_R8G8B8A8_UNORM:
8788
return kByte;
8889
case VK_FORMAT_R32G32B32A32_SINT:
8990
return kInt;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ class VulkanImage final {
179179
return memory_.allocation;
180180
}
181181

182+
inline VkImageType type() const {
183+
return image_properties_.image_type;
184+
}
185+
182186
inline VkFormat format() const {
183187
return image_properties_.image_format;
184188
}

0 commit comments

Comments
 (0)