Skip to content

Commit 15b6966

Browse files
committed
[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]
1 parent e67284a commit 15b6966

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ vTensorStorage::vTensorStorage(
281281
last_access_{},
282282
has_copies_{false} {}
283283

284+
vTensorStorage::vTensorStorage(
285+
Context* const context,
286+
const vkapi::VulkanImage& image)
287+
: context_(context),
288+
storage_type_{utils::kTexture2D},
289+
image_extents_(
290+
{image.extents().width,
291+
image.extents().height,
292+
image.extents().depth}),
293+
buffer_length_{0},
294+
buffer_offset_{0},
295+
image_(image),
296+
buffer_(vkapi::VulkanBuffer()),
297+
last_access_{} {}
298+
284299
vTensorStorage::vTensorStorage(
285300
vTensorStorage& other,
286301
const int64_t buffer_offset)
@@ -446,6 +461,30 @@ vTensor::vTensor(
446461
}
447462

448463
// NOLINTNEXTLINE
464+
vTensor::vTensor(Context* const context, const vkapi::VulkanImage& image)
465+
: dtype_(),
466+
// Calculate tensor metadata
467+
sizes_(),
468+
packed_dim_(),
469+
dim_order_(),
470+
axis_map_(default_axis_map()),
471+
strides_(),
472+
numel_(),
473+
padded_sizes_(),
474+
unsqueezed_strides_(),
475+
padded_numel_(),
476+
logical_limits_(),
477+
// Utility Uniform Buffers that can be passed to shaders as arguments
478+
sizes_uniform_(),
479+
strides_uniform_(),
480+
numel_uniform_(),
481+
axis_map_uniform_(),
482+
logical_limits_uniform_(),
483+
// Construct Tensor storage
484+
storage_(context, image) {
485+
set_logical_limits(storage_.image_extents_);
486+
}
487+
449488
vTensor::vTensor(vTensor& other)
450489
: dtype_(other.dtype_),
451490
// Copy tensor size metadata

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class vTensorStorage final {
9595
const vkapi::ScalarType dtype,
9696
const bool allocate_memory = true);
9797

98+
vTensorStorage(Context* const context, const vkapi::VulkanImage& image);
99+
98100
protected:
99101
/*
100102
* This allows for creation of tensors that use the same underlying storage
@@ -185,6 +187,8 @@ class vTensor final {
185187

186188
vTensor(const vTensor& other) = delete;
187189

190+
explicit vTensor(Context* context, const vkapi::VulkanImage& image);
191+
188192
/*
189193
* This constructor allows for the creation of a vTensor that references the
190194
* same buffer resource of another vTensor, with the same sizes and strides

backends/vulkan/runtime/graph/ComputeGraph.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@ ValueRef ComputeGraph::add_tensor(
296296
sizes, dtype, suggested_memory_layout(sizes), shared_object_idx);
297297
}
298298

299+
ValueRef ComputeGraph::add_tensor(const vkapi::VulkanImage& image) {
300+
ValueRef idx(static_cast<int>(values_.size()));
301+
check_no_active_value_ptrs();
302+
values_.emplace_back(api::vTensor(context(), image));
303+
return idx;
304+
}
305+
299306
ValueRef ComputeGraph::add_tensor_view(const ValueRef vref) {
300307
const vTensorPtr t = get_tensor(vref);
301308
ValueRef idx(static_cast<int>(values_.size()));

backends/vulkan/runtime/graph/ComputeGraph.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ class ComputeGraph final {
466466
const vkapi::ScalarType dtype,
467467
const int64_t shared_object_idx = -1);
468468

469+
/*
470+
* Add a `api::vTensor` value to the graph with the specified image.
471+
*/
472+
ValueRef add_tensor(const vkapi::VulkanImage& image);
473+
469474
/*
470475
* Add a `api::vTensor` value to the graph with the properties of `vref`.
471476
*/

0 commit comments

Comments
 (0)