Skip to content

Commit de9200c

Browse files
authored
[ET-VK] Use shared pointer for vTensorStorage
Differential Revision: D76047204 Pull Request resolved: #11400
1 parent f96ca77 commit de9200c

File tree

5 files changed

+44
-238
lines changed

5 files changed

+44
-238
lines changed

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

Lines changed: 33 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ vTensorStorage::vTensorStorage(
343343
storage_type_,
344344
dtype,
345345
allocate_memory)),
346-
last_access_{},
347-
has_copies_{false} {}
346+
last_access_{} {}
348347

349348
vTensorStorage::vTensorStorage(
350349
Context* const context,
@@ -361,21 +360,6 @@ vTensorStorage::vTensorStorage(
361360
buffer_(vkapi::VulkanBuffer()),
362361
last_access_{} {}
363362

364-
vTensorStorage::vTensorStorage(
365-
vTensorStorage& other,
366-
const int64_t buffer_offset)
367-
: context_(other.context_),
368-
storage_type_{other.storage_type_},
369-
image_extents_(other.image_extents_),
370-
buffer_length_{other.buffer_length_},
371-
buffer_offset_{buffer_offset},
372-
image_(other.image_),
373-
buffer_(other.buffer_, buffer_offset),
374-
last_access_{other.last_access_},
375-
has_copies_{false} {
376-
other.has_copies_ = true;
377-
}
378-
379363
vTensorStorage::~vTensorStorage() {
380364
flush();
381365
}
@@ -397,21 +381,6 @@ void vTensorStorage::transition(
397381
vkapi::PipelineStageFlags prev_stage = last_access_.stage;
398382
vkapi::MemoryAccessFlags prev_access = last_access_.access;
399383

400-
// If the underlying resource is a copy of another tensor's resource the
401-
// last_access may not be accurate, since the original storage may have been
402-
// written to as part of the original tensor. Likewise, if the underlying
403-
// resource has copies, then the resource may have been updated as part of the
404-
// view tensors.
405-
//
406-
// If the resource is a copy, or has copies of it, then cowardly assume that
407-
// it has previously been written to as part of a compute shader before the
408-
// current access event so that the appropriate memory barriers may be
409-
// inserted.
410-
if (is_copy() || has_copies_) {
411-
prev_stage = vkapi::PipelineStage::COMPUTE;
412-
prev_access = vkapi::kWrite;
413-
}
414-
415384
const bool prev_written = (prev_access & vkapi::MemoryAccessType::WRITE) != 0;
416385

417386
VkImageLayout cur_layout = VK_IMAGE_LAYOUT_UNDEFINED;
@@ -458,20 +427,6 @@ void vTensorStorage::transition(
458427
last_access_.access = cur_access;
459428
}
460429

461-
bool vTensorStorage::is_copy() const {
462-
if (storage_type_ == utils::kBuffer) {
463-
return buffer_.is_copy();
464-
}
465-
return image_.is_copy();
466-
}
467-
468-
bool vTensorStorage::is_copy_of(const vTensorStorage& other) const {
469-
if (storage_type_ == utils::kBuffer) {
470-
return buffer_.is_copy_of(other.buffer_);
471-
}
472-
return image_.is_copy_of(other.image_);
473-
}
474-
475430
//
476431
// vTensor
477432
//
@@ -503,14 +458,14 @@ vTensor::vTensor(
503458
numel_uniform_offset_(kUniformOffsetUnset),
504459
logical_limits_uniform_offset_(kUniformOffsetUnset),
505460
// Construct Tensor storage
506-
storage_(
461+
storage_(std::make_shared<vTensorStorage>(
507462
context,
508463
storage_type,
509464
axis_map_,
510465
packed_dim_,
511466
padded_sizes_,
512467
dtype_,
513-
allocate_memory) {
468+
allocate_memory)) {
514469
uniform_data_ = std::make_shared<UniformData>(UniformData{
515470
sizes_,
516471
unsqueezed_strides_,
@@ -519,7 +474,7 @@ vTensor::vTensor(
519474
VK_CHECK_COND(
520475
dim_order_is_valid(dim_order_), "computed dim order is invalid");
521476

522-
set_logical_limits(storage_.image_extents_);
477+
set_logical_limits(storage_->image_extents_);
523478
}
524479

525480
// NOLINTNEXTLINE
@@ -546,13 +501,13 @@ vTensor::vTensor(
546501
numel_uniform_offset_(kUniformOffsetUnset),
547502
logical_limits_uniform_offset_(kUniformOffsetUnset),
548503
// Construct Tensor storage
549-
storage_(context, image) {
504+
storage_(std::make_shared<vTensorStorage>(context, image)) {
550505
uniform_data_ = std::make_shared<UniformData>(UniformData{
551506
sizes_,
552507
{0, 0, 0, 0},
553508
{{0, 0, 0}},
554509
static_cast<size_t>(utils::multiply_integers(sizes_))});
555-
set_logical_limits(storage_.image_extents_);
510+
set_logical_limits(storage_->image_extents_);
556511
}
557512

558513
vTensor::vTensor(vTensor& other)
@@ -583,8 +538,7 @@ vTensor::vTensor(vTensor& other)
583538
vTensor::vTensor(
584539
vTensor& other,
585540
const std::vector<int64_t>& sizes,
586-
const std::vector<int64_t>& dim_order,
587-
const int64_t offset_numel)
541+
const std::vector<int64_t>& dim_order)
588542
: dtype_(other.dtype_),
589543
// Copy tensor size metadata
590544
sizes_(sizes.begin(), sizes.end()),
@@ -604,7 +558,7 @@ vTensor::vTensor(
604558
numel_uniform_offset_(kUniformOffsetUnset),
605559
logical_limits_uniform_offset_(kUniformOffsetUnset),
606560
// Copy Tensor storage
607-
storage_(other.storage_, vkapi::element_size(dtype_) * offset_numel) {
561+
storage_(other.storage_) {
608562
uniform_data_ = std::make_shared<UniformData>(UniformData{
609563
sizes_,
610564
unsqueezed_strides_,
@@ -613,10 +567,6 @@ vTensor::vTensor(
613567

614568
VK_CHECK_COND(
615569
dim_order_is_valid(dim_order_), "new dim order provided is invalid");
616-
VK_CHECK_COND(
617-
offset_numel + numel() <= other.numel(),
618-
"Tensor alias cannot access more elements than available in the original"
619-
"tensor");
620570
}
621571

622572
uint32_t vTensor::UniformData::write_attribute(
@@ -647,31 +597,31 @@ uint32_t vTensor::UniformData::write_attribute(
647597
vkapi::VulkanImage& vTensor::image(
648598
vkapi::PipelineBarrier& pipeline_barrier,
649599
const vkapi::PipelineStageFlags stage) & {
650-
storage_.transition(pipeline_barrier, stage, vkapi::MemoryAccessType::READ);
651-
return storage_.image_;
600+
storage_->transition(pipeline_barrier, stage, vkapi::MemoryAccessType::READ);
601+
return storage_->image_;
652602
}
653603

654604
vkapi::VulkanImage& vTensor::image(
655605
vkapi::PipelineBarrier& pipeline_barrier,
656606
const vkapi::PipelineStageFlags stage,
657607
const vkapi::MemoryAccessFlags access) & {
658-
storage_.transition(pipeline_barrier, stage, access);
659-
return storage_.image_;
608+
storage_->transition(pipeline_barrier, stage, access);
609+
return storage_->image_;
660610
}
661611

662612
vkapi::VulkanBuffer& vTensor::buffer(
663613
vkapi::PipelineBarrier& pipeline_barrier,
664614
const vkapi::PipelineStageFlags stage) & {
665-
storage_.transition(pipeline_barrier, stage, vkapi::MemoryAccessType::READ);
666-
return storage_.buffer_;
615+
storage_->transition(pipeline_barrier, stage, vkapi::MemoryAccessType::READ);
616+
return storage_->buffer_;
667617
}
668618

669619
vkapi::VulkanBuffer& vTensor::buffer(
670620
vkapi::PipelineBarrier& pipeline_barrier,
671621
const vkapi::PipelineStageFlags stage,
672622
const vkapi::MemoryAccessFlags access) & {
673-
storage_.transition(pipeline_barrier, stage, access);
674-
return storage_.buffer_;
623+
storage_->transition(pipeline_barrier, stage, access);
624+
return storage_->buffer_;
675625
}
676626

677627
void vTensor::set_logical_limits(const utils::uvec3& image_extents) {
@@ -695,10 +645,10 @@ utils::GPUMemoryLayout vTensor::estimate_memory_layout() const {
695645

696646
const vkapi::BufferBindInfo vTensor::sizes_ubo() {
697647
const size_t size_per_ubo =
698-
storage_.context_->adapter_ptr()->min_ubo_alignment();
648+
storage_->context_->adapter_ptr()->min_ubo_alignment();
699649
const size_t max_ubo_size = kMaxMetadataFieldCount * size_per_ubo;
700650
if (!uniforms_.buffer()) {
701-
uniforms_ = ParamsBuffer(storage_.context_, max_ubo_size, true);
651+
uniforms_ = ParamsBuffer(storage_->context_, max_ubo_size, true);
702652
}
703653
if (sizes_uniform_offset_ == kUniformOffsetUnset) {
704654
VK_CHECK_COND(
@@ -714,10 +664,10 @@ const vkapi::BufferBindInfo vTensor::sizes_ubo() {
714664

715665
const vkapi::BufferBindInfo vTensor::strides_ubo() {
716666
const size_t size_per_ubo =
717-
storage_.context_->adapter_ptr()->min_ubo_alignment();
667+
storage_->context_->adapter_ptr()->min_ubo_alignment();
718668
const size_t max_ubo_size = kMaxMetadataFieldCount * size_per_ubo;
719669
if (!uniforms_.buffer()) {
720-
uniforms_ = ParamsBuffer(storage_.context_, max_ubo_size, true);
670+
uniforms_ = ParamsBuffer(storage_->context_, max_ubo_size, true);
721671
}
722672
if (unsqueezed_strides_offset_ == kUniformOffsetUnset) {
723673
VK_CHECK_COND(
@@ -735,10 +685,10 @@ const vkapi::BufferBindInfo vTensor::strides_ubo() {
735685

736686
const vkapi::BufferBindInfo vTensor::logical_limits_ubo() {
737687
const size_t size_per_ubo =
738-
storage_.context_->adapter_ptr()->min_ubo_alignment();
688+
storage_->context_->adapter_ptr()->min_ubo_alignment();
739689
const size_t max_ubo_size = kMaxMetadataFieldCount * size_per_ubo;
740690
if (!uniforms_.buffer()) {
741-
uniforms_ = ParamsBuffer(storage_.context_, max_ubo_size, true);
691+
uniforms_ = ParamsBuffer(storage_->context_, max_ubo_size, true);
742692
}
743693
if (logical_limits_uniform_offset_ == kUniformOffsetUnset) {
744694
VK_CHECK_COND(
@@ -754,10 +704,10 @@ const vkapi::BufferBindInfo vTensor::logical_limits_ubo() {
754704

755705
const vkapi::BufferBindInfo vTensor::numel_ubo() {
756706
const size_t size_per_ubo =
757-
storage_.context_->adapter_ptr()->min_ubo_alignment();
707+
storage_->context_->adapter_ptr()->min_ubo_alignment();
758708
const size_t max_ubo_size = kMaxMetadataFieldCount * size_per_ubo;
759709
if (!uniforms_.buffer()) {
760-
uniforms_ = ParamsBuffer(storage_.context_, max_ubo_size, true);
710+
uniforms_ = ParamsBuffer(storage_->context_, max_ubo_size, true);
761711
}
762712
if (numel_uniform_offset_ == kUniformOffsetUnset) {
763713
VK_CHECK_COND(
@@ -774,7 +724,7 @@ const vkapi::BufferBindInfo vTensor::numel_ubo() {
774724
size_t vTensor::staging_buffer_numel() const {
775725
const bool is_int8 = dtype_ == vkapi::kChar;
776726
const bool int8_supported =
777-
storage_.context_->adapter_ptr()->has_full_int8_buffers_support();
727+
storage_->context_->adapter_ptr()->has_full_int8_buffers_support();
778728
if (is_int8 && !int8_supported) {
779729
return utils::align_up_4(numel());
780730
}
@@ -787,22 +737,22 @@ size_t vTensor::staging_buffer_numel() const {
787737
VkMemoryRequirements vTensor::get_memory_requirements() const {
788738
switch (storage_type()) {
789739
case utils::kBuffer:
790-
return storage_.buffer_.get_memory_requirements();
740+
return storage_->buffer_.get_memory_requirements();
791741
case utils::kTexture2D:
792742
case utils::kTexture3D:
793-
return storage_.image_.get_memory_requirements();
743+
return storage_->image_.get_memory_requirements();
794744
}
795745
return {};
796746
}
797747

798748
void vTensor::bind_allocation(const vkapi::Allocation& allocation) {
799749
switch (storage_type()) {
800750
case utils::kBuffer:
801-
storage_.buffer_.bind_allocation(allocation);
751+
storage_->buffer_.bind_allocation(allocation);
802752
break;
803753
case utils::kTexture2D:
804754
case utils::kTexture3D:
805-
storage_.image_.bind_allocation(allocation);
755+
storage_->image_.bind_allocation(allocation);
806756
break;
807757
}
808758
}
@@ -845,11 +795,11 @@ void vTensor::check_sizes(const std::vector<int64_t>& sizes) const {
845795
utils::uvec3 virtual_extents =
846796
calculate_image_extents(padded_sizes_, axis_map_, packed_dim_);
847797

848-
bool valid_resize = virtual_extents[0] <= storage_.image_extents_[0];
798+
bool valid_resize = virtual_extents[0] <= storage_->image_extents_[0];
849799
valid_resize =
850-
valid_resize && virtual_extents[1] <= storage_.image_extents_[1];
800+
valid_resize && virtual_extents[1] <= storage_->image_extents_[1];
851801
valid_resize =
852-
valid_resize && virtual_extents[2] <= storage_.image_extents_[2];
802+
valid_resize && virtual_extents[2] <= storage_->image_extents_[2];
853803

854804
VK_CHECK_COND(
855805
valid_resize,
@@ -859,7 +809,7 @@ void vTensor::check_sizes(const std::vector<int64_t>& sizes) const {
859809
// new sizes of the tensor.
860810
int64_t numel = utils::multiply_integers(sizes);
861811
bool valid_resize =
862-
numel + storage_.buffer_offset_ <= storage_.buffer_length_;
812+
numel + storage_->buffer_offset_ <= storage_->buffer_length_;
863813
VK_CHECK_COND(
864814
valid_resize,
865815
"tensor sizes requires a larger buffer than the current one.");

0 commit comments

Comments
 (0)