Skip to content

Commit 2f75c09

Browse files
Adds perf tests for SDPA
1 parent 835359b commit 2f75c09

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

tripy/tests/performance/cases/linear_block.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
@perf_fixture(
2424
params=[
25-
PerfParam(tp.float32, 1.25),
26-
PerfParam(tp.float16),
25+
PerfParam(tp.float32, 2.0),
26+
PerfParam(tp.float16, 2.0),
2727
]
2828
)
2929
def linear_block(tripy_dtype, torch_dtype):
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
import math
16+
17+
import nvtripy as tp
18+
import torch
19+
from tests.performance.conftest import PerfParam, perf_fixture
20+
21+
22+
@perf_fixture(
23+
params=[
24+
PerfParam(tp.float32, perf_threshold=1.25),
25+
PerfParam(tp.float16, perf_threshold=1.35),
26+
]
27+
)
28+
def sdpa(tripy_dtype, torch_dtype):
29+
class SDPA(tp.Module):
30+
def forward(self, query, key, value):
31+
embedding_dim = query.shape[-1]
32+
qk = query @ (tp.transpose(key, -2, -1) / tp.sqrt(tp.cast(embedding_dim, query.dtype)))
33+
return tp.cast(tp.softmax(qk, -1), query.dtype) @ value
34+
35+
class TorchSDPA(torch.nn.Module):
36+
def forward(self, query, key, value):
37+
embedding_dim = query.shape[-1]
38+
qk = query @ (key.transpose(-2, -1) / math.sqrt(embedding_dim))
39+
return torch.softmax(qk, dim=-1) @ value
40+
41+
tripy_block = SDPA()
42+
torch_block = TorchSDPA()
43+
44+
batch = tp.NamedDimension("batch", 1, 2, 2)
45+
input_infos = {
46+
"query": tp.InputInfo(shape=(batch, 1, 4096, 256), dtype=tripy_dtype),
47+
"key": tp.InputInfo(shape=(batch, 1, 4096, 256), dtype=tripy_dtype),
48+
"value": tp.InputInfo(shape=(batch, 1, 4096, 256), dtype=tripy_dtype),
49+
}
50+
51+
return tripy_block, torch_block, input_infos

0 commit comments

Comments
 (0)