|
| 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/examples/models/llama2/custom_ops/op_sdpa.h> |
| 10 | +#include <executorch/extension/aten_util/make_aten_functor_from_et_functor.h> |
| 11 | +#include <executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h> |
| 12 | + |
| 13 | +#include <torch/library.h> |
| 14 | + |
| 15 | +namespace torch { |
| 16 | +namespace executor { |
| 17 | + |
| 18 | +namespace native { |
| 19 | + |
| 20 | +Tensor& sdpa_with_kv_cache_out_no_context( |
| 21 | + const Tensor& q_projected, |
| 22 | + const Tensor& k_projected, |
| 23 | + const Tensor& v_projected, |
| 24 | + Tensor& key_cache, |
| 25 | + Tensor& value_cache, |
| 26 | + const int64_t start_pos, |
| 27 | + const int64_t seq_len, |
| 28 | + // @lint-ignore CLANGTIDY facebook-hte-ConstantArgumentPassByValue |
| 29 | + // @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy |
| 30 | + const optional<Tensor> attn_mask, |
| 31 | + const double dropout_p, |
| 32 | + const bool is_causal, |
| 33 | + // @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy |
| 34 | + const optional<double> scale, |
| 35 | + Tensor& output) { |
| 36 | + exec_aten::RuntimeContext context{}; |
| 37 | + return torch::executor::native::sdpa_with_kv_cache_out( |
| 38 | + context, |
| 39 | + q_projected, |
| 40 | + k_projected, |
| 41 | + v_projected, |
| 42 | + key_cache, |
| 43 | + value_cache, |
| 44 | + start_pos, |
| 45 | + seq_len, |
| 46 | + attn_mask, |
| 47 | + dropout_p, |
| 48 | + is_causal, |
| 49 | + scale, |
| 50 | + output); |
| 51 | +} |
| 52 | + |
| 53 | +at::Tensor sdpa_with_kv_cache_aten( |
| 54 | + const at::Tensor& q_projected, |
| 55 | + const at::Tensor& k_projected, |
| 56 | + const at::Tensor& v_projected, |
| 57 | + at::Tensor& key_cache, |
| 58 | + at::Tensor& value_cache, |
| 59 | + const int64_t start_pos, |
| 60 | + const int64_t seq_len, |
| 61 | + // @lint-ignore CLANGTIDY facebook-hte-ConstantArgumentPassByValue |
| 62 | + // @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy |
| 63 | + const c10::optional<at::Tensor> attn_mask, |
| 64 | + const double dropout_p, |
| 65 | + const bool is_causal, |
| 66 | + // @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy |
| 67 | + const c10::optional<double> scale) { |
| 68 | + auto output = at::empty_like(q_projected); |
| 69 | + WRAP_TO_ATEN(sdpa_with_kv_cache_out_no_context, 11) |
| 70 | + (q_projected, |
| 71 | + k_projected, |
| 72 | + v_projected, |
| 73 | + key_cache, |
| 74 | + value_cache, |
| 75 | + start_pos, |
| 76 | + seq_len, |
| 77 | + attn_mask, |
| 78 | + dropout_p, |
| 79 | + is_causal, |
| 80 | + scale, |
| 81 | + output); |
| 82 | + return output; |
| 83 | +} |
| 84 | + |
| 85 | +} // namespace native |
| 86 | +} // namespace executor |
| 87 | +} // namespace torch |
| 88 | + |
| 89 | +TORCH_LIBRARY(llama, m) { |
| 90 | + m.def( |
| 91 | + "sdpa_with_kv_cache(Tensor query, Tensor key, Tensor value, Tensor(a!) key_cache, " |
| 92 | + "Tensor(b!) value_cache, SymInt start_pos, SymInt seq_len, Tensor? attn_mask=None, " |
| 93 | + "float drpout_p=0.0, bool is_causal=False, float? scale=None) -> Tensor"); |
| 94 | + m.def( |
| 95 | + "sdpa_with_kv_cache.out(Tensor query, Tensor key, Tensor value, Tensor(a!) key_cache, " |
| 96 | + "Tensor(b!) value_cache, SymInt start_pos, SymInt seq_len, Tensor? attn_mask=None, " |
| 97 | + "float drpout_p=0.0, bool is_causal=False, float? scale=None, *, Tensor(c!) out) -> Tensor(c!)"); |
| 98 | +} |
| 99 | + |
| 100 | +TORCH_LIBRARY_IMPL(llama, CompositeExplicitAutograd, m) { |
| 101 | + m.impl( |
| 102 | + "sdpa_with_kv_cache", torch::executor::native::sdpa_with_kv_cache_aten); |
| 103 | + m.impl( |
| 104 | + "sdpa_with_kv_cache.out", |
| 105 | + WRAP_TO_ATEN( |
| 106 | + torch::executor::native::sdpa_with_kv_cache_out_no_context, 11)); |
| 107 | +} |
0 commit comments