Skip to content

Commit a0f860b

Browse files
ssjiaSS-JIA
authored andcommitted
[ET-VK][index][ez] Fix multidimensional index buffer addressing
Compute index and source buffer offsets from their independent tensor ranks. This preserves higher-rank source tensor support while restoring multidimensional index tensor handling. Authored with Codex. Differential Revision: [D116841489](https://our.internmc.facebook.com/intern/diff/D116841489/) ghstack-source-id: 417637892 Pull-Request: pytorch#21993
1 parent 02ff60d commit a0f860b

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

‎backends/vulkan/runtime/graph/ops/glsl/index_tensor_buffer.glsl‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,22 @@ void main() {
4343
// Convert output buffer index to tensor index
4444
TensorIndex out_tidx = linear_idx_to_tensor_idx(outp, out_bufi);
4545

46-
// aten.index.Tensor with a single index tensor gathers along dim 0. This
47-
// index space is WHCN-ordered -- axis 0 is the LAST pytorch dim -- so
48-
// pytorch dim 0 is the highest axis, and self's trailing dims map 1:1 onto
49-
// out's. With a 1-D index, rank(out) == rank(self).
50-
const uint gather_axis = ndim(outp) - 1;
51-
const uint gather_pos = idx_at(out_tidx, gather_axis);
52-
53-
// The index tensor is 1-D, so only its axis 0 (W) is populated. Indexing it
54-
// with out's full coordinate is equivalent only when out is itself 1-D.
55-
TensorIndex index_tidx;
56-
initialize(index_tidx);
57-
index_tidx.data[0][0] = gather_pos;
58-
const uint index_bufi = tensor_idx_to_linear_idx(index, index_tidx);
46+
const uint self_rank = ndim(inp);
47+
const uint index_rank = ndim(index);
48+
// WHCN order places self's trailing axes before the index axes.
49+
const uint index_axis_offset = self_rank - 1;
50+
51+
uint index_bufi = 0;
52+
for (uint d = 0; d < index_rank; ++d) {
53+
index_bufi +=
54+
stride_at(index, d) * idx_at(out_tidx, index_axis_offset + d);
55+
}
5956
const int idx = t_index[index_bufi];
6057

61-
// self shares out's trailing coordinates; only the gathered axis differs.
62-
TensorIndex self_tidx = out_tidx;
63-
self_tidx.data[div_4(gather_axis)][mod_4(gather_axis)] = uint(idx);
64-
const uint self_bufi = tensor_idx_to_linear_idx(inp, self_tidx);
58+
uint self_bufi = stride_at(inp, self_rank - 1) * uint(idx);
59+
for (uint d = 0; d + 1 < self_rank; ++d) {
60+
self_bufi += stride_at(inp, d) * idx_at(out_tidx, d);
61+
}
6562

6663
t_out[out_bufi] = t_self[self_bufi];
6764
}

0 commit comments

Comments
 (0)