[Vulkan] SDPA buffer IO uses the wrong stride when head_dim is not divisible by 4

Ouverte
#23,001 0 commentaires 0 réactions 1 personne assignée Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@SS-JIA y travaille déjà.

Depuis le 22/9/2026.

Évaluation

Cette issue n'a pas encore été évaluée.

Description

module: vulkan
Describe the bug

Vulkan SDPA returns incorrect results with buffer IO when head_dim is not divisible by four. The AV shader treats each head as padded to a whole number of vec4s, but ordinary width-packed buffer tensors store the heads contiguously.

A small example is Hq=2, Hkv=1, head_dim=5, and one live context token. Give both query heads attention probability [1] and use a texture V cache containing [1,2,3,4,5]. Both output heads should equal that V row:

Expected: [1,2,3,4,5, 1,2,3,4,5]
Actual:   [1,2,3,4,5, 0,0,0,1,2]

Both the base and tile2 GQA kernels produce the incorrect buffer result. Switching IO to textures gives the expected output. This reproduces with the original AV kernel; no reduction optimization patch is needed.

Repro

Add the following test and includes to backends/vulkan/test/op_tests/sdpa_test.cpp, build vulkan_sdpa_test, and run it with --gtest_filter=VulkanSDPATest.av_unaligned_buffer_rows. I compiled and ran this exact test as a standalone GoogleTest executable linked against the Vulkan runtime: it reports two failures, one for each buffer variant; the texture cases pass.

The test calls the AV node directly so QK and softmax cannot affect the result. It reserves space for eight query tokens, then resizes to one-token decode. Probability padding is zero.

C++ test
#include <gtest/gtest.h>

#include <executorch/backends/vulkan/runtime/api/api.h>
#include <executorch/backends/vulkan/runtime/graph/ComputeGraph.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/SDPA.h>
#include <executorch/runtime/platform/runtime.h>

#include <vector>

TEST(VulkanSDPATest, av_unaligned_buffer_rows) {
  using namespace vkcompute;
  executorch::runtime::runtime_init();
  for (const auto io : {utils::kTexture3D, utils::kBuffer}) {
    for (const int64_t variant :
         {kShaderOverrideForceBase, kShaderOverrideForceTile2}) {
      SCOPED_TRACE(io == utils::kBuffer ? "buffer IO" : "texture IO");
      SCOPED_TRACE(variant);
      GraphConfig config;
      config.expect_dynamic_shapes = true;
      config.set_storage_type_override(io);
      config.set_memory_layout_override(utils::kWidthPacked);
      ComputeGraph graph(config);
      auto p = graph.add_input_tensor({1, 2, 8, 8}, vkapi::kFloat);
      auto v = graph.add_tensor(
          {1, 8, 1, 5}, vkapi::kFloat,
          utils::kTexture3D, utils::kWidthPacked);
      auto vstage = graph.set_input_tensor(v);
      auto q = graph.add_tensor({1, 8, 2, 5}, vkapi::kFloat);
      auto out = graph.add_tensor({1, 8, 2, 5}, vkapi::kFloat);
      auto position = graph.add_symint(0);
      add_sdpa_compute_out_node(
          graph, p.value, v, q, v, position, out,
          vkcompute::SDPAMode::LLM, graph.add_scalar<int64_t>(variant));
      auto ostage = graph.set_output_tensor(out);
      graph.prepare();
      graph.prepack();

      graph.resize_input(0, {1, 2, 4, 4});
      graph.virtual_resize(q, {1, 1, 2, 5});
      graph.propagate_resize();
      std::vector<float> probabilities(2 * 4 * 4, 0.0f);
      probabilities[0] = probabilities[16] = 1.0f;
      std::vector<float> values(8 * 5, 0.0f);
      for (int d = 0; d < 5; ++d) values[d] = float(d + 1);
      graph.maybe_cast_and_copy_into_staging(
          p.staging, probabilities.data(), probabilities.size(), vkapi::kFloat);
      graph.maybe_cast_and_copy_into_staging(
          vstage, values.data(), values.size(), vkapi::kFloat);
      graph.execute();
      std::vector<float> actual(10);
      graph.maybe_cast_and_copy_from_staging(
          ostage, actual.data(), actual.size(), vkapi::kFloat);
      const std::vector<float> expected = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
      EXPECT_EQ(actual, expected);
    }
  }
}
Where the stride mismatch comes from

calculate_packed_dim_info() gives ordinary width-packed buffers alignment 1. In contrast, store_out_d4() indexes output as (s * Q_H + q_h) * D4 + d4 in vectors, with D4 = ceil(head_dim / 4). At head_dim=5, that starts the second head at element 8 instead of element 5.

The buffer V-cache loader makes the same padded-stride assumption. In a separate two-token, last-token-one-hot case with the second V row [101,102,103,104,105], buffer-cache output starts [104,105,0,0,0] instead of that row. The issue therefore affects reads as well as output stores.

I also reproduced the problem at head dimensions 6 and 13; dimension 8 is a passing control. Separate buffer and texture upload/download roundtrips pass for all four dimensions, so this does not appear to be a staging problem. Broader AV checks reproduce the unaligned-buffer failure in FP16 and FP32, including multi-token prefill. The SDPA registration and runtime entry point do not appear to reject these dimensions.

The buffer paths need to honor the actual row stride, or explicitly enforce a padded layout. If unaligned head dimensions are unsupported, they should be rejected or routed to a supported path rather than returning incorrect values.

Versions
  • Apple M1 Pro, macOS 26.6.2 (25G83).
  • MoltenVK, Vulkan SDK 1.4.341.1.
  • Tested with the original AV kernel in a local build based on 5721f54c0513924683aae7d4cd54338440b8f7c2 (the SwiGLU worktree). This is not a full build of current main.
  • The tensor-layout implementation, AV cooperative shader, output-store helper, and V-cache-load helper are byte-identical to current main at 5cc1dc6539dd3affe28a0247d60011d49b436265; source links above are pinned there.

cc @SS-JIA @manuelcandales @digantdesai @cbilgin

Langage dominant
Python
Étoiles
5.2k
Forks
1.3k
Merge moyen
1 j 7 h
PR mergées (30 j)
644

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de pytorch/executorch

Toutes les issues de pytorch/executorch

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.