Skip to content

Commit ed97214

Browse files
authored
[ET-VK][ez] Add padded_numel_ member to vTensor for shader bounds checking (pytorch#16856)
Introduce a new padded_numel_ member to the vTensor class that stores the total number of elements based on padded sizes (before packing). This value is useful for bounds checking in shader code, as it represents the logical size of the padded tensor before any int8 packing is applied. The padded_numel_ is computed as utils::multiply_integers(padded_sizes_) and is maintained alongside numel_ (based on original sizes) and physical_numel_ (after packing). Also adds corresponding accessor functions padded_sizes_of() and padded_numel_of() to ComputeGraph for convenient access to these values from ValueRefs. Differential Revision: [D91281383](https://our.internmc.facebook.com/intern/diff/D91281383/)
1 parent 46afcfa commit ed97214

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ vTensor::vTensor(
709709
axis_map_(calculate_axis_map(sizes_, axis_map_layout)),
710710
strides_(calculate_strides(sizes.size(), padded_sizes_, dim_order_)),
711711
numel_(utils::multiply_integers(sizes_)),
712+
padded_numel_(utils::multiply_integers(padded_sizes_)),
712713
physical_numel_(calculate_gpu_buffer_numel(dtype_, padded_sizes_)),
713714
hashed_layout_(create_hashed_layout(
714715
dim_order_,
@@ -761,6 +762,7 @@ vTensor::vTensor(
761762
axis_map_(calculate_axis_map(sizes_, axis_map_layout)),
762763
strides_(),
763764
numel_(utils::multiply_integers(sizes_)),
765+
padded_numel_(utils::multiply_integers(padded_sizes_)),
764766
physical_numel_(calculate_gpu_buffer_numel(dtype_, padded_sizes_)),
765767
hashed_layout_(create_hashed_layout(
766768
dim_order_,
@@ -793,6 +795,7 @@ vTensor::vTensor(vTensor& other)
793795
axis_map_(other.axis_map_.begin(), other.axis_map_.end()),
794796
strides_(other.strides_.begin(), other.strides_.end()),
795797
numel_(other.numel_),
798+
padded_numel_(other.padded_numel_),
796799
physical_numel_(other.physical_numel_),
797800
hashed_layout_(other.hashed_layout_),
798801
min_nbytes_per_ubo_{other.min_nbytes_per_ubo_},
@@ -817,6 +820,7 @@ vTensor::vTensor(
817820
axis_map_(calculate_axis_map(sizes_, utils::kDefaultAxisMap)),
818821
strides_(calculate_strides(sizes_.size(), padded_sizes_, dim_order_)),
819822
numel_(utils::multiply_integers(sizes_)),
823+
padded_numel_(utils::multiply_integers(padded_sizes_)),
820824
physical_numel_(calculate_gpu_buffer_numel(dtype_, padded_sizes_)),
821825
hashed_layout_(create_hashed_layout(
822826
dim_order_,
@@ -1076,7 +1080,7 @@ const vkapi::BufferBindInfo vTensor::numel_ubo() {
10761080
const vkapi::BufferBindInfo vTensor::buffer_meta_ubo() {
10771081
size_t ubo_nbytes = sizeof(BufferMetadata);
10781082
if (!buffer_meta_.buffer()) {
1079-
BufferMetadata data(sizes_, dim_order_, strides_, numel_);
1083+
BufferMetadata data(sizes_, dim_order_, strides_, padded_numel_);
10801084
buffer_meta_ = ParamsBuffer(storage_->context_, data);
10811085
}
10821086
return vkapi::BufferBindInfo(buffer_meta_.buffer(), 0, ubo_nbytes);
@@ -1139,6 +1143,7 @@ void vTensor::acquire_allocation(vkapi::Allocation&& allocation) {
11391143

11401144
void vTensor::update_metadata() {
11411145
numel_ = utils::multiply_integers(sizes_);
1146+
padded_numel_ = utils::multiply_integers(padded_sizes_);
11421147
physical_numel_ = calculate_gpu_buffer_numel(dtype_, padded_sizes_);
11431148
strides_ = calculate_strides(sizes_.size(), padded_sizes_, dim_order_);
11441149

@@ -1150,7 +1155,7 @@ void vTensor::update_metadata() {
11501155
uniform_data_->dim_order_v =
11511156
flip_and_unsqueeze_ivec4(dim_order_, kTensorDimOrder, numel_);
11521157
uniform_data_->strides_v =
1153-
flip_and_unsqueeze_ivec4(strides_, kTensorStrides, numel_);
1158+
flip_and_unsqueeze_ivec4(strides_, kTensorStrides, padded_numel_);
11541159
uniform_data_->logical_limits.limits = calculate_logical_limits(
11551160
dtype_, packed_dim_info_, padded_sizes_, axis_map_);
11561161

@@ -1173,7 +1178,7 @@ void vTensor::update_metadata() {
11731178
}
11741179

11751180
if (buffer_meta_.buffer()) {
1176-
BufferMetadata data(sizes_, dim_order_, strides_, numel_);
1181+
BufferMetadata data(sizes_, dim_order_, strides_, padded_numel_);
11771182
buffer_meta_.update(data);
11781183
}
11791184

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ class vTensor final {
331331
// number of elements based on the canonical sizes
332332
size_t numel_;
333333

334+
// number of elements based on the padded sizes (before packing)
335+
size_t padded_numel_;
336+
334337
// number of elements required for GPU buffer storage (with padding/packing)
335338
// This is pre-computed to avoid recomputing calculate_gpu_buffer_numel
336339
int64_t physical_numel_;
@@ -508,6 +511,10 @@ class vTensor final {
508511
return numel_;
509512
}
510513

514+
inline size_t padded_numel() const {
515+
return padded_numel_;
516+
}
517+
511518
inline int64_t physical_numel() const {
512519
return physical_numel_;
513520
}

‎backends/vulkan/runtime/graph/ComputeGraph.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ std::vector<int64_t> ComputeGraph::sizes_of(const ValueRef idx) const {
296296
VK_THROW("Could not get sizes of value with type ", val.type());
297297
}
298298

299+
std::vector<int64_t> ComputeGraph::padded_sizes_of(const ValueRef idx) const {
300+
const Value& val = values_.at(idx);
301+
if (val.isTensor()) {
302+
return val.toConstTensor().padded_sizes();
303+
}
304+
VK_THROW("Could not get padded sizes of value with type ", val.type());
305+
}
306+
299307
int64_t ComputeGraph::dim_of(const ValueRef idx) const {
300308
const Value& val = values_.at(idx);
301309
if (val.isTensor()) {

‎backends/vulkan/runtime/graph/ComputeGraph.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ class ComputeGraph final {
327327

328328
std::vector<int64_t> sizes_of(const ValueRef idx) const;
329329

330+
std::vector<int64_t> padded_sizes_of(const ValueRef idx) const;
331+
330332
/*
331333
* Returns the size of the tensor at `idx` along the specified dimension.
332334
* Negative indexing is allowed.
@@ -355,7 +357,13 @@ class ComputeGraph final {
355357
}
356358

357359
inline int32_t numel_of(const ValueRef idx) const {
358-
return values_.at(idx).toConstTensor().numel();
360+
return utils::safe_downcast<int32_t>(
361+
values_.at(idx).toConstTensor().numel());
362+
}
363+
364+
inline int32_t padded_numel_of(const ValueRef idx) const {
365+
return utils::safe_downcast<int32_t>(
366+
values_.at(idx).toConstTensor().padded_numel());
359367
}
360368

361369
inline size_t staging_buffer_numel_of(const ValueRef idx) const {

0 commit comments

Comments
 (0)