|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 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 | + |
| 16 | +"""Shared fixtures and helpers for Triton flash attention tests.""" |
| 17 | + |
| 18 | +import pytest |
| 19 | +import torch |
| 20 | +import torch.nn.functional as F |
| 21 | + |
| 22 | + |
| 23 | +def make_qkv(total, num_heads, num_kv_heads, head_dim, device="cuda", dtype=torch.float16): |
| 24 | + """Create packed Q, K, V tensors.""" |
| 25 | + q = torch.randn(total, num_heads, head_dim, device=device, dtype=dtype) |
| 26 | + k = torch.randn(total, num_kv_heads, head_dim, device=device, dtype=dtype) |
| 27 | + v = torch.randn(total, num_kv_heads, head_dim, device=device, dtype=dtype) |
| 28 | + return q, k, v |
| 29 | + |
| 30 | + |
| 31 | +def make_varlen_meta(seq_lens, device="cuda"): |
| 32 | + """Create b_start_loc and b_seq_len from a list of sequence lengths.""" |
| 33 | + b_seq_len = torch.tensor(seq_lens, device=device, dtype=torch.int32) |
| 34 | + b_start_loc = torch.zeros(len(seq_lens), device=device, dtype=torch.int32) |
| 35 | + b_start_loc[1:] = torch.cumsum(b_seq_len[:-1], dim=0) |
| 36 | + return b_start_loc, b_seq_len |
| 37 | + |
| 38 | + |
| 39 | +def sdpa_reference(q, k, v, b_start_loc, b_seq_len, is_causal=True): |
| 40 | + """SDPA reference. Supports GQA. Returns [total_tokens, num_heads, dim].""" |
| 41 | + batch = b_seq_len.shape[0] |
| 42 | + num_q, num_kv = q.shape[1], k.shape[1] |
| 43 | + parts = [] |
| 44 | + for b in range(batch): |
| 45 | + s, n = int(b_start_loc[b].item()), int(b_seq_len[b].item()) |
| 46 | + qb = q[s : s + n].unsqueeze(0).permute(0, 2, 1, 3) |
| 47 | + kb = k[s : s + n].unsqueeze(0).permute(0, 2, 1, 3) |
| 48 | + vb = v[s : s + n].unsqueeze(0).permute(0, 2, 1, 3) |
| 49 | + if num_q != num_kv: |
| 50 | + r = num_q // num_kv |
| 51 | + kb = kb.repeat_interleave(r, dim=1) |
| 52 | + vb = vb.repeat_interleave(r, dim=1) |
| 53 | + ob = F.scaled_dot_product_attention(qb, kb, vb, is_causal=is_causal) |
| 54 | + parts.append(ob.permute(0, 2, 1, 3).squeeze(0)) |
| 55 | + return torch.cat(parts, dim=0) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope="module") |
| 59 | +def tiny_llama_dir(tmp_path_factory): |
| 60 | + """Tiny Llama: 2 layers, 64 hidden, 4 q-heads, 2 kv-heads, head_dim=16.""" |
| 61 | + from _test_utils.torch.transformers_models import create_tiny_llama_dir |
| 62 | + |
| 63 | + return create_tiny_llama_dir( |
| 64 | + tmp_path_factory.mktemp("tiny_llama"), |
| 65 | + with_tokenizer=True, |
| 66 | + num_hidden_layers=2, |
| 67 | + hidden_size=64, |
| 68 | + num_attention_heads=4, |
| 69 | + num_key_value_heads=2, |
| 70 | + intermediate_size=64, |
| 71 | + max_position_embeddings=64, |
| 72 | + ) |
0 commit comments