@@ -343,8 +343,7 @@ vTensorStorage::vTensorStorage(
343
343
storage_type_,
344
344
dtype,
345
345
allocate_memory)),
346
- last_access_{},
347
- has_copies_{false } {}
346
+ last_access_{} {}
348
347
349
348
vTensorStorage::vTensorStorage (
350
349
Context* const context,
@@ -361,21 +360,6 @@ vTensorStorage::vTensorStorage(
361
360
buffer_(vkapi::VulkanBuffer()),
362
361
last_access_{} {}
363
362
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
-
379
363
vTensorStorage::~vTensorStorage () {
380
364
flush ();
381
365
}
@@ -397,21 +381,6 @@ void vTensorStorage::transition(
397
381
vkapi::PipelineStageFlags prev_stage = last_access_.stage ;
398
382
vkapi::MemoryAccessFlags prev_access = last_access_.access ;
399
383
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
-
415
384
const bool prev_written = (prev_access & vkapi::MemoryAccessType::WRITE) != 0 ;
416
385
417
386
VkImageLayout cur_layout = VK_IMAGE_LAYOUT_UNDEFINED;
@@ -458,20 +427,6 @@ void vTensorStorage::transition(
458
427
last_access_.access = cur_access;
459
428
}
460
429
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
-
475
430
//
476
431
// vTensor
477
432
//
@@ -503,14 +458,14 @@ vTensor::vTensor(
503
458
numel_uniform_offset_(kUniformOffsetUnset ),
504
459
logical_limits_uniform_offset_(kUniformOffsetUnset ),
505
460
// Construct Tensor storage
506
- storage_(
461
+ storage_(std::make_shared<vTensorStorage>(
507
462
context,
508
463
storage_type,
509
464
axis_map_,
510
465
packed_dim_,
511
466
padded_sizes_,
512
467
dtype_,
513
- allocate_memory) {
468
+ allocate_memory)) {
514
469
uniform_data_ = std::make_shared<UniformData>(UniformData{
515
470
sizes_,
516
471
unsqueezed_strides_,
@@ -519,7 +474,7 @@ vTensor::vTensor(
519
474
VK_CHECK_COND (
520
475
dim_order_is_valid (dim_order_), " computed dim order is invalid" );
521
476
522
- set_logical_limits (storage_. image_extents_ );
477
+ set_logical_limits (storage_-> image_extents_ );
523
478
}
524
479
525
480
// NOLINTNEXTLINE
@@ -546,13 +501,13 @@ vTensor::vTensor(
546
501
numel_uniform_offset_(kUniformOffsetUnset ),
547
502
logical_limits_uniform_offset_(kUniformOffsetUnset ),
548
503
// Construct Tensor storage
549
- storage_(context, image) {
504
+ storage_(std::make_shared<vTensorStorage>( context, image) ) {
550
505
uniform_data_ = std::make_shared<UniformData>(UniformData{
551
506
sizes_,
552
507
{0 , 0 , 0 , 0 },
553
508
{{0 , 0 , 0 }},
554
509
static_cast <size_t >(utils::multiply_integers (sizes_))});
555
- set_logical_limits (storage_. image_extents_ );
510
+ set_logical_limits (storage_-> image_extents_ );
556
511
}
557
512
558
513
vTensor::vTensor (vTensor& other)
@@ -583,8 +538,7 @@ vTensor::vTensor(vTensor& other)
583
538
vTensor::vTensor (
584
539
vTensor& other,
585
540
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)
588
542
: dtype_(other.dtype_),
589
543
// Copy tensor size metadata
590
544
sizes_(sizes.begin(), sizes.end()),
@@ -604,7 +558,7 @@ vTensor::vTensor(
604
558
numel_uniform_offset_(kUniformOffsetUnset ),
605
559
logical_limits_uniform_offset_(kUniformOffsetUnset ),
606
560
// Copy Tensor storage
607
- storage_(other.storage_, vkapi::element_size(dtype_) * offset_numel ) {
561
+ storage_(other.storage_) {
608
562
uniform_data_ = std::make_shared<UniformData>(UniformData{
609
563
sizes_,
610
564
unsqueezed_strides_,
@@ -613,10 +567,6 @@ vTensor::vTensor(
613
567
614
568
VK_CHECK_COND (
615
569
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" );
620
570
}
621
571
622
572
uint32_t vTensor::UniformData::write_attribute (
@@ -647,31 +597,31 @@ uint32_t vTensor::UniformData::write_attribute(
647
597
vkapi::VulkanImage& vTensor::image (
648
598
vkapi::PipelineBarrier& pipeline_barrier,
649
599
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_ ;
652
602
}
653
603
654
604
vkapi::VulkanImage& vTensor::image (
655
605
vkapi::PipelineBarrier& pipeline_barrier,
656
606
const vkapi::PipelineStageFlags stage,
657
607
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_ ;
660
610
}
661
611
662
612
vkapi::VulkanBuffer& vTensor::buffer (
663
613
vkapi::PipelineBarrier& pipeline_barrier,
664
614
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_ ;
667
617
}
668
618
669
619
vkapi::VulkanBuffer& vTensor::buffer (
670
620
vkapi::PipelineBarrier& pipeline_barrier,
671
621
const vkapi::PipelineStageFlags stage,
672
622
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_ ;
675
625
}
676
626
677
627
void vTensor::set_logical_limits (const utils::uvec3& image_extents) {
@@ -695,10 +645,10 @@ utils::GPUMemoryLayout vTensor::estimate_memory_layout() const {
695
645
696
646
const vkapi::BufferBindInfo vTensor::sizes_ubo () {
697
647
const size_t size_per_ubo =
698
- storage_. context_ ->adapter_ptr ()->min_ubo_alignment ();
648
+ storage_-> context_ ->adapter_ptr ()->min_ubo_alignment ();
699
649
const size_t max_ubo_size = kMaxMetadataFieldCount * size_per_ubo;
700
650
if (!uniforms_.buffer ()) {
701
- uniforms_ = ParamsBuffer (storage_. context_ , max_ubo_size, true );
651
+ uniforms_ = ParamsBuffer (storage_-> context_ , max_ubo_size, true );
702
652
}
703
653
if (sizes_uniform_offset_ == kUniformOffsetUnset ) {
704
654
VK_CHECK_COND (
@@ -714,10 +664,10 @@ const vkapi::BufferBindInfo vTensor::sizes_ubo() {
714
664
715
665
const vkapi::BufferBindInfo vTensor::strides_ubo () {
716
666
const size_t size_per_ubo =
717
- storage_. context_ ->adapter_ptr ()->min_ubo_alignment ();
667
+ storage_-> context_ ->adapter_ptr ()->min_ubo_alignment ();
718
668
const size_t max_ubo_size = kMaxMetadataFieldCount * size_per_ubo;
719
669
if (!uniforms_.buffer ()) {
720
- uniforms_ = ParamsBuffer (storage_. context_ , max_ubo_size, true );
670
+ uniforms_ = ParamsBuffer (storage_-> context_ , max_ubo_size, true );
721
671
}
722
672
if (unsqueezed_strides_offset_ == kUniformOffsetUnset ) {
723
673
VK_CHECK_COND (
@@ -735,10 +685,10 @@ const vkapi::BufferBindInfo vTensor::strides_ubo() {
735
685
736
686
const vkapi::BufferBindInfo vTensor::logical_limits_ubo () {
737
687
const size_t size_per_ubo =
738
- storage_. context_ ->adapter_ptr ()->min_ubo_alignment ();
688
+ storage_-> context_ ->adapter_ptr ()->min_ubo_alignment ();
739
689
const size_t max_ubo_size = kMaxMetadataFieldCount * size_per_ubo;
740
690
if (!uniforms_.buffer ()) {
741
- uniforms_ = ParamsBuffer (storage_. context_ , max_ubo_size, true );
691
+ uniforms_ = ParamsBuffer (storage_-> context_ , max_ubo_size, true );
742
692
}
743
693
if (logical_limits_uniform_offset_ == kUniformOffsetUnset ) {
744
694
VK_CHECK_COND (
@@ -754,10 +704,10 @@ const vkapi::BufferBindInfo vTensor::logical_limits_ubo() {
754
704
755
705
const vkapi::BufferBindInfo vTensor::numel_ubo () {
756
706
const size_t size_per_ubo =
757
- storage_. context_ ->adapter_ptr ()->min_ubo_alignment ();
707
+ storage_-> context_ ->adapter_ptr ()->min_ubo_alignment ();
758
708
const size_t max_ubo_size = kMaxMetadataFieldCount * size_per_ubo;
759
709
if (!uniforms_.buffer ()) {
760
- uniforms_ = ParamsBuffer (storage_. context_ , max_ubo_size, true );
710
+ uniforms_ = ParamsBuffer (storage_-> context_ , max_ubo_size, true );
761
711
}
762
712
if (numel_uniform_offset_ == kUniformOffsetUnset ) {
763
713
VK_CHECK_COND (
@@ -774,7 +724,7 @@ const vkapi::BufferBindInfo vTensor::numel_ubo() {
774
724
size_t vTensor::staging_buffer_numel () const {
775
725
const bool is_int8 = dtype_ == vkapi::kChar ;
776
726
const bool int8_supported =
777
- storage_. context_ ->adapter_ptr ()->has_full_int8_buffers_support ();
727
+ storage_-> context_ ->adapter_ptr ()->has_full_int8_buffers_support ();
778
728
if (is_int8 && !int8_supported) {
779
729
return utils::align_up_4 (numel ());
780
730
}
@@ -787,22 +737,22 @@ size_t vTensor::staging_buffer_numel() const {
787
737
VkMemoryRequirements vTensor::get_memory_requirements () const {
788
738
switch (storage_type ()) {
789
739
case utils::kBuffer :
790
- return storage_. buffer_ .get_memory_requirements ();
740
+ return storage_-> buffer_ .get_memory_requirements ();
791
741
case utils::kTexture2D :
792
742
case utils::kTexture3D :
793
- return storage_. image_ .get_memory_requirements ();
743
+ return storage_-> image_ .get_memory_requirements ();
794
744
}
795
745
return {};
796
746
}
797
747
798
748
void vTensor::bind_allocation (const vkapi::Allocation& allocation) {
799
749
switch (storage_type ()) {
800
750
case utils::kBuffer :
801
- storage_. buffer_ .bind_allocation (allocation);
751
+ storage_-> buffer_ .bind_allocation (allocation);
802
752
break ;
803
753
case utils::kTexture2D :
804
754
case utils::kTexture3D :
805
- storage_. image_ .bind_allocation (allocation);
755
+ storage_-> image_ .bind_allocation (allocation);
806
756
break ;
807
757
}
808
758
}
@@ -845,11 +795,11 @@ void vTensor::check_sizes(const std::vector<int64_t>& sizes) const {
845
795
utils::uvec3 virtual_extents =
846
796
calculate_image_extents (padded_sizes_, axis_map_, packed_dim_);
847
797
848
- bool valid_resize = virtual_extents[0 ] <= storage_. image_extents_ [0 ];
798
+ bool valid_resize = virtual_extents[0 ] <= storage_-> image_extents_ [0 ];
849
799
valid_resize =
850
- valid_resize && virtual_extents[1 ] <= storage_. image_extents_ [1 ];
800
+ valid_resize && virtual_extents[1 ] <= storage_-> image_extents_ [1 ];
851
801
valid_resize =
852
- valid_resize && virtual_extents[2 ] <= storage_. image_extents_ [2 ];
802
+ valid_resize && virtual_extents[2 ] <= storage_-> image_extents_ [2 ];
853
803
854
804
VK_CHECK_COND (
855
805
valid_resize,
@@ -859,7 +809,7 @@ void vTensor::check_sizes(const std::vector<int64_t>& sizes) const {
859
809
// new sizes of the tensor.
860
810
int64_t numel = utils::multiply_integers (sizes);
861
811
bool valid_resize =
862
- numel + storage_. buffer_offset_ <= storage_. buffer_length_ ;
812
+ numel + storage_-> buffer_offset_ <= storage_-> buffer_length_ ;
863
813
VK_CHECK_COND (
864
814
valid_resize,
865
815
" tensor sizes requires a larger buffer than the current one." );
0 commit comments