|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/backends/vulkan/runtime/graph/ops/OperatorRegistry.h> |
| 10 | + |
| 11 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/Common.h> |
| 12 | + |
| 13 | +namespace vkcompute { |
| 14 | + |
| 15 | +// Test wrapper for the LLM KV-cache SDPA op (llama.custom_sdpa.default). |
| 16 | +// |
| 17 | +// The production op reads the dynamic context length from an `input_pos` |
| 18 | +// symint, but input_pos is not a free parameter: the op enforces |
| 19 | +// context_len = S + input_pos, and context_len is exactly the KV-cache's dim |
| 20 | +// -3. Since q is [B=1, S, H, D] and the caches are [B=1, context_len, H_kv, D], |
| 21 | +// input_pos is fully determined by the tensor shapes. Deriving it here from |
| 22 | +// those shapes keeps the single source of truth (the cache size) authoritative; |
| 23 | +// exposing input_pos as its own ValueSpec would add a redundant degree of |
| 24 | +// freedom that a test could set inconsistent with the cache shape. |
| 25 | +// |
| 26 | +// Decode vs prefill is selected automatically inside the op via |
| 27 | +// is_single_token() (S == 1 -> coop/GEMV shaders, S > 1 -> tiled shaders), so |
| 28 | +// impl_selector is accepted for interface uniformity but only "default" is |
| 29 | +// meaningful here. |
| 30 | +// |
| 31 | +// Args: q, k_cache, v_cache, impl_selector, out |
| 32 | +void test_sdpa(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 33 | + int arg_idx = 0; |
| 34 | + const ValueRef q = args.at(arg_idx++); |
| 35 | + const ValueRef k_cache = args.at(arg_idx++); |
| 36 | + const ValueRef v_cache = args.at(arg_idx++); |
| 37 | + const ValueRef impl_selector_str = args.at(arg_idx++); |
| 38 | + const ValueRef out = args.at(arg_idx++); |
| 39 | + |
| 40 | + const std::string impl_selector = graph.extract_string(impl_selector_str); |
| 41 | + VK_CHECK_COND( |
| 42 | + impl_selector == "default", |
| 43 | + "test_sdpa only supports the 'default' impl_selector"); |
| 44 | + |
| 45 | + const int64_t seq_len = graph.size_at<int64_t>(-3, q); |
| 46 | + const int64_t context_len = graph.size_at<int64_t>(-3, k_cache); |
| 47 | + VK_CHECK_COND(context_len >= seq_len); |
| 48 | + const int32_t input_pos_val = |
| 49 | + utils::safe_downcast<int32_t>(context_len - seq_len); |
| 50 | + |
| 51 | + const ValueRef input_pos_symint = graph.add_symint(input_pos_val); |
| 52 | + |
| 53 | + VK_GET_OP_FN("llama.custom_sdpa.default") |
| 54 | + (graph, |
| 55 | + { |
| 56 | + q, |
| 57 | + k_cache, |
| 58 | + v_cache, |
| 59 | + input_pos_symint, |
| 60 | + kDummyValueRef, // attn_mask |
| 61 | + kDummyValueRef, // dropout_p |
| 62 | + kDummyValueRef, // is_causal (implementation assumes causal) |
| 63 | + kDummyValueRef, // scale (implementation computes 1/sqrt(head_dim)) |
| 64 | + out, |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +REGISTER_OPERATORS { |
| 69 | + VK_REGISTER_OP(test_etvk.test_sdpa.default, test_sdpa); |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace vkcompute |
0 commit comments