Skip to content

Commit 1e937f2

Browse files
msluszniakSS-JIA
authored andcommitted
[ET-VK] Fix out-of-range broadcast size calculation for 0-dim tensors
calculate_broadcasted_output_size() guards its loop with `i >= -out_sizes.size()`. That is size_t arithmetic: when both operands are 0-dimensional the bound evaluates to 0, `i` promotes to a huge unsigned value, the guard stays true, and the body runs `out_sizes.at(size() - 1)` on an empty vector, throwing std::out_of_range. Any binary op whose output is 0-dimensional therefore aborts at graph build. Whisper hits it in the log-mel normalisation, which makes its whole encode method unloadable on Vulkan. Hold the bound in a signed local so the loop is skipped when the output is 0-dimensional. Non-empty cases are unchanged: the previous unsigned wraparound happened to compare correctly for them.
1 parent 74f553e commit 1e937f2

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ std::vector<int64_t> calculate_broadcasted_output_size(
1818
const std::vector<int64_t>& sizes1,
1919
const std::vector<int64_t>& sizes2) {
2020
std::vector<int64_t> out_sizes(std::max(sizes1.size(), sizes2.size()));
21+
// ndim must be signed. `-out_sizes.size()` is size_t arithmetic, so when both
22+
// inputs are 0-dimensional it evaluates to 0 while `i` promotes to a huge
23+
// unsigned value, the guard stays true, and `out_sizes.at(size() - 1)` throws
24+
// std::out_of_range instead of the loop being skipped.
25+
const int64_t ndim = static_cast<int64_t>(out_sizes.size());
2126

2227
// Match the sizes in reverse because sizes are in NCHW order
23-
for (int i = -1; i >= -out_sizes.size(); --i) {
24-
out_sizes.at(out_sizes.size() + i) =
28+
for (int64_t i = -1; i >= -ndim; --i) {
29+
out_sizes.at(static_cast<size_t>(ndim + i)) =
2530
std::max(utils::val_at(i, sizes1), utils::val_at(i, sizes2));
2631
}
2732

‎backends/vulkan/test/test_vulkan_delegate.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,20 @@ def forward(self, x):
10801080
sample_inputs,
10811081
)
10821082

1083+
def test_vulkan_backend_binary_op_zero_dim(self):
1084+
# Both operands, and therefore the output, are 0-dimensional. This is
1085+
# what a reduction to a scalar followed by arithmetic produces, e.g. the
1086+
# log-mel normalisation in Whisper's preprocessor.
1087+
class ZeroDimModule(torch.nn.Module):
1088+
def forward(self, x):
1089+
m = x.max()
1090+
return (m - (m - 1.0)).reshape(1)
1091+
1092+
self.lower_module_and_test_output(
1093+
ZeroDimModule(),
1094+
(torch.randn(size=(64,), dtype=torch.float32),),
1095+
)
1096+
10831097
@disable_test("layer norm compute shader not working with swiftshader")
10841098
def test_vulkan_backend_native_layer_norm(self):
10851099
class NativeLayerNormModule(torch.nn.Module):

0 commit comments

Comments
 (0)