Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/models/llama2/custom_ops/op_sdpa.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -732,7 +732,7 @@ Tensor& sdpa_with_kv_cache_out(
const Tensor& v_projected,
Tensor& key_cache,
Tensor& value_cache,
const int64_t start_pos,
const Tensor& start_pos_tensor,
const int64_t seq_len,
const optional<Tensor>& attn_mask,
const double dropout_p,
Expand All@@ -741,6 +741,7 @@ Tensor& sdpa_with_kv_cache_out(
const optional<double> scale,
Tensor& output) {
(void)ctx;
const int64_t start_pos = *start_pos_tensor.const_data_ptr<int64_t>();
ET_KERNEL_CHECK(
ctx,
validate_cache_params(key_cache, value_cache, start_pos, seq_len),
Expand Down
2 changes: 1 addition & 1 deletion examples/models/llama2/custom_ops/op_sdpa.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,7 +22,7 @@ Tensor& sdpa_with_kv_cache_out(
const Tensor& v_projected,
Tensor& key_cache,
Tensor& value_cache,
const int64_t start_pos,
const Tensor& start_pos_tensor,
const int64_t seq_len,
const optional<Tensor>& attn_mask,
const double dropout_p,
Expand Down
12 changes: 6 additions & 6 deletions examples/models/llama2/custom_ops/op_sdpa_aot.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,7 +23,7 @@ Tensor& sdpa_with_kv_cache_out_no_context(
const Tensor& v_projected,
Tensor& key_cache,
Tensor& value_cache,
const int64_t start_pos,
const Tensor& start_pos_tensor,
const int64_t seq_len,
// @lint-ignore CLANGTIDY facebook-hte-ConstantArgumentPassByValue
// @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy
Expand All@@ -41,7 +41,7 @@ Tensor& sdpa_with_kv_cache_out_no_context(
v_projected,
key_cache,
value_cache,
start_pos,
start_pos_tensor,
seq_len,
attn_mask,
dropout_p,
Expand All@@ -56,7 +56,7 @@ at::Tensor sdpa_with_kv_cache_aten(
const at::Tensor& v_projected,
at::Tensor& key_cache,
at::Tensor& value_cache,
const int64_t start_pos,
const at::Tensor& start_pos_tensor,
const int64_t seq_len,
// @lint-ignore CLANGTIDY facebook-hte-ConstantArgumentPassByValue
// @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy
Expand All@@ -72,7 +72,7 @@ at::Tensor sdpa_with_kv_cache_aten(
v_projected,
key_cache,
value_cache,
start_pos,
start_pos_tensor,
seq_len,
attn_mask,
dropout_p,
Expand All@@ -89,11 +89,11 @@ at::Tensor sdpa_with_kv_cache_aten(
TORCH_LIBRARY(llama, m) {
m.def(
"sdpa_with_kv_cache(Tensor query, Tensor key, Tensor value, Tensor(a!) key_cache, "
"Tensor(b!) value_cache, SymInt start_pos, SymInt seq_len, Tensor? attn_mask=None, "
"Tensor(b!) value_cache, Tensor start_pos, SymInt seq_len, Tensor? attn_mask=None, "
"float drpout_p=0.0, bool is_causal=False, float? scale=None) -> Tensor");
m.def(
"sdpa_with_kv_cache.out(Tensor query, Tensor key, Tensor value, Tensor(a!) key_cache, "
"Tensor(b!) value_cache, SymInt start_pos, SymInt seq_len, Tensor? attn_mask=None, "
"Tensor(b!) value_cache, Tensor start_pos, SymInt seq_len, Tensor? attn_mask=None, "
"float drpout_p=0.0, bool is_causal=False, float? scale=None, *, Tensor(c!) out) -> Tensor(c!)");
}

Expand Down
50 changes: 26 additions & 24 deletions examples/models/llama2/custom_ops/op_sdpa_with_kv_cache_test.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,15 @@

using namespace ::testing;

using torch::executor::testing::TensorFactory;

exec_aten::Tensor op_sdpa_with_kv_cache(
const exec_aten::Tensor& query,
const exec_aten::Tensor& key,
const exec_aten::Tensor& value,
exec_aten::Tensor& key_cache,
exec_aten::Tensor& value_cache,
const int64_t start_pos,
const exec_aten::Tensor& start_pos_tensor,
const int64_t seq_len,
const exec_aten::optional<exec_aten::Tensor>& attn_mask,
double dropout_p,
Expand All@@ -39,7 +41,7 @@ exec_aten::Tensor op_sdpa_with_kv_cache(
value,
key_cache,
value_cache,
start_pos,
start_pos_tensor,
seq_len,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -178,7 +180,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTest) {
value,
key_cache_0,
value_cache_0,
0,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {0}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -213,7 +215,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTest) {
value,
key_cache_2,
value_cache_2,
0,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {0}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -248,7 +250,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTest) {
value,
key_cache_0,
value_cache_0,
1,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {1}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -283,7 +285,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTest) {
value,
key_cache_1,
value_cache_1,
1,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {1}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -318,7 +320,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTest) {
value,
key_cache_1,
value_cache_1,
2,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {2}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -353,7 +355,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTest) {
value,
key_cache_2,
value_cache_2,
2,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {2}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -407,7 +409,7 @@ TEST(OpScaledDotProductAttentionTest, LargerTest) {
value,
key_cache_0,
value_cache_0,
0,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {0}),
1,
attn_mask,
dropout_p,
Expand All@@ -429,7 +431,7 @@ TEST(OpScaledDotProductAttentionTest, LargerTest) {
value,
key_cache_2,
value_cache_2,
0,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {0}),
1,
attn_mask,
dropout_p,
Expand All@@ -451,7 +453,7 @@ TEST(OpScaledDotProductAttentionTest, LargerTest) {
value,
key_cache_0,
value_cache_0,
1,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {1}),
1,
attn_mask,
dropout_p,
Expand All@@ -473,7 +475,7 @@ TEST(OpScaledDotProductAttentionTest, LargerTest) {
value,
key_cache_1,
value_cache_1,
1,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {1}),
1,
attn_mask,
dropout_p,
Expand All@@ -495,7 +497,7 @@ TEST(OpScaledDotProductAttentionTest, LargerTest) {
value,
key_cache_1,
value_cache_1,
2,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {2}),
1,
attn_mask,
dropout_p,
Expand All@@ -517,7 +519,7 @@ TEST(OpScaledDotProductAttentionTest, LargerTest) {
value,
key_cache_2,
value_cache_2,
2,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {2}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -623,7 +625,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTestWithAttnMask) {
value,
key_cache_0,
value_cache_0,
0,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {0}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -658,7 +660,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTestWithAttnMask) {
value,
key_cache_2,
value_cache_2,
0,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {0}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -694,7 +696,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTestWithAttnMask) {
value,
key_cache_0,
value_cache_0,
1,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {1}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -729,7 +731,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTestWithAttnMask) {
value,
key_cache_1,
value_cache_1,
1,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {1}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -765,7 +767,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTestWithAttnMask) {
value,
key_cache_1,
value_cache_1,
2,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {2}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -800,7 +802,7 @@ TEST(OpScaledDotProductAttentionTest, BasicTestWithAttnMask) {
value,
key_cache_2,
value_cache_2,
2,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {2}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -856,7 +858,7 @@ TEST(OpScaledDotProductAttentionTest, SequenceTest) {
value,
key_cache_0,
value_cache_0,
0,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {0}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -897,7 +899,7 @@ TEST(OpScaledDotProductAttentionTest, SequenceTest) {
value,
key_cache_0,
value_cache_0,
1,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {1}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -938,7 +940,7 @@ TEST(OpScaledDotProductAttentionTest, SequenceTest) {
value,
key_cache_0,
value_cache_0,
2,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {2}),
1,
attn_mask,
dropout_p,
Expand DownExpand Up@@ -979,7 +981,7 @@ TEST(OpScaledDotProductAttentionTest, SequenceTest) {
value,
key_cache_0,
value_cache_0,
3,
TensorFactory<exec_aten::ScalarType::Long>().make({1}, {3}),
1,
attn_mask,
dropout_p,
Expand Down
2 changes: 1 addition & 1 deletion examples/models/llama2/source_transformation/sdpa.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,7 +39,7 @@ def forward(
v,
self.kv_cache.k_cache,
self.kv_cache.v_cache,
input_pos[-1].item(),
input_pos[-1],
seqlen,
)
return output.view(bsz, seqlen, self.dim)
Expand Down