|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: LicenseRef-Apache2 |
| 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 | +from functools import lru_cache |
| 17 | + |
| 18 | +import torch |
| 19 | +import torch.nn.functional as F # noqa: N812 |
| 20 | + |
| 21 | + |
| 22 | +def _raise_subquadratic_self_test_error(op_name: str, detail: str) -> None: |
| 23 | + raise RuntimeError( |
| 24 | + f"subquadratic_ops_torch.{op_name} failed a CUDA self-test ({detail}). " |
| 25 | + "This often happens with CUDA_ERROR_UNSUPPORTED_PTX_VERSION or unsupported GPU/toolchain " |
| 26 | + "combinations. Refusing to run this subquadratic kernel because it can otherwise return " |
| 27 | + "invalid outputs without raising." |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +def _assert_close_or_raise(op_name: str, actual: torch.Tensor, expected: torch.Tensor) -> None: |
| 32 | + torch.cuda.synchronize(actual.device) |
| 33 | + if not torch.isfinite(actual).all(): |
| 34 | + _raise_subquadratic_self_test_error(op_name, "non-finite output") |
| 35 | + |
| 36 | + if not torch.allclose(actual, expected, rtol=1e-4, atol=1e-4): |
| 37 | + max_diff = (actual.float() - expected.float()).abs().max().item() |
| 38 | + rel = ( |
| 39 | + (actual.float() - expected.float()).pow(2).sum().sqrt() / (expected.float().pow(2).sum().sqrt() + 1e-30) |
| 40 | + ).item() |
| 41 | + _raise_subquadratic_self_test_error(op_name, f"max_diff={max_diff:.6g}, rel={rel:.6g}") |
| 42 | + |
| 43 | + |
| 44 | +@lru_cache(maxsize=None) |
| 45 | +def ensure_subquadratic_causal_conv1d_supported(device_index: int | None = None) -> None: |
| 46 | + """Validate subquadratic_ops_torch.causal_conv1d before using it for model data.""" |
| 47 | + if not torch.cuda.is_available(): |
| 48 | + return |
| 49 | + |
| 50 | + device_index = torch.cuda.current_device() if device_index is None else device_index |
| 51 | + device = torch.device("cuda", device_index) |
| 52 | + |
| 53 | + from subquadratic_ops_torch.causal_conv1d import causal_conv1d as subq_causal_conv1d |
| 54 | + |
| 55 | + batch_size = 1 |
| 56 | + hidden_size = 4 |
| 57 | + seq_len = 8 |
| 58 | + kernel_size = 3 |
| 59 | + pad_size = kernel_size - 1 |
| 60 | + |
| 61 | + u = torch.linspace(-1.0, 1.0, steps=batch_size * hidden_size * seq_len, device=device).reshape( |
| 62 | + batch_size, hidden_size, seq_len |
| 63 | + ) |
| 64 | + weight = torch.linspace(-0.5, 0.5, steps=hidden_size * kernel_size, device=device).reshape( |
| 65 | + hidden_size, kernel_size |
| 66 | + ) |
| 67 | + |
| 68 | + expected = F.conv1d( |
| 69 | + u, |
| 70 | + weight.unsqueeze(1), |
| 71 | + bias=None, |
| 72 | + stride=1, |
| 73 | + padding=pad_size, |
| 74 | + groups=hidden_size, |
| 75 | + )[..., :seq_len] |
| 76 | + actual = subq_causal_conv1d(F.pad(u, (pad_size, 0)), weight)[..., pad_size:] |
| 77 | + _assert_close_or_raise("causal_conv1d", actual, expected) |
| 78 | + |
| 79 | + |
| 80 | +@lru_cache(maxsize=None) |
| 81 | +def ensure_subquadratic_fft_causal_conv1d_supported(device_index: int | None = None) -> None: |
| 82 | + """Validate subquadratic_ops_torch.fft_causal_conv1d before using it for model data.""" |
| 83 | + if not torch.cuda.is_available(): |
| 84 | + return |
| 85 | + |
| 86 | + device_index = torch.cuda.current_device() if device_index is None else device_index |
| 87 | + device = torch.device("cuda", device_index) |
| 88 | + |
| 89 | + from subquadratic_ops_torch.fft_causal_conv1d import fft_causal_conv1d as subq_fft_causal_conv1d |
| 90 | + |
| 91 | + batch_size = 1 |
| 92 | + hidden_size = 4 |
| 93 | + seq_len = 8 |
| 94 | + kernel_size = 5 |
| 95 | + |
| 96 | + u = torch.linspace(-1.0, 1.0, steps=batch_size * hidden_size * seq_len, device=device).reshape( |
| 97 | + batch_size, hidden_size, seq_len |
| 98 | + ) |
| 99 | + weight = torch.linspace(-0.5, 0.5, steps=hidden_size * kernel_size, device=device).reshape( |
| 100 | + hidden_size, kernel_size |
| 101 | + ) |
| 102 | + |
| 103 | + expected = F.conv1d( |
| 104 | + u, |
| 105 | + weight.flip(-1).unsqueeze(1), |
| 106 | + bias=None, |
| 107 | + stride=1, |
| 108 | + padding=kernel_size - 1, |
| 109 | + groups=hidden_size, |
| 110 | + )[..., :seq_len] |
| 111 | + actual = subq_fft_causal_conv1d(u, weight) |
| 112 | + _assert_close_or_raise("fft_causal_conv1d", actual, expected) |
| 113 | + |
| 114 | + |
| 115 | +@lru_cache(maxsize=None) |
| 116 | +def ensure_subquadratic_b2b_causal_conv1d_supported(device_index: int | None = None) -> None: |
| 117 | + """Validate subquadratic_ops_torch.b2b_causal_conv1d before using it for model data.""" |
| 118 | + if not torch.cuda.is_available(): |
| 119 | + return |
| 120 | + |
| 121 | + device_index = torch.cuda.current_device() if device_index is None else device_index |
| 122 | + device = torch.device("cuda", device_index) |
| 123 | + |
| 124 | + from subquadratic_ops_torch.b2b_causal_conv1d import b2b_causal_conv1d as subq_b2b_causal_conv1d |
| 125 | + |
| 126 | + batch_size = 1 |
| 127 | + hidden_size = 2 |
| 128 | + seq_len = 10 |
| 129 | + proj_kernel_size = 3 |
| 130 | + mixer_kernel_size = 7 |
| 131 | + |
| 132 | + x = torch.linspace(-1.0, 1.0, steps=batch_size * 3 * hidden_size * seq_len, device=device).reshape( |
| 133 | + batch_size, 3 * hidden_size, seq_len |
| 134 | + ) |
| 135 | + proj_weight = torch.linspace(-0.5, 0.5, steps=3 * hidden_size * proj_kernel_size, device=device).reshape( |
| 136 | + 3 * hidden_size, proj_kernel_size |
| 137 | + ) |
| 138 | + mixer_weight = torch.linspace(-0.25, 0.25, steps=hidden_size * mixer_kernel_size, device=device).reshape( |
| 139 | + hidden_size, mixer_kernel_size |
| 140 | + ) |
| 141 | + bias = torch.linspace(-0.1, 0.1, steps=hidden_size, device=device) |
| 142 | + |
| 143 | + actual = subq_b2b_causal_conv1d(x, proj_weight, mixer_weight, bias) |
| 144 | + |
| 145 | + projected = F.conv1d( |
| 146 | + F.pad(x, (proj_kernel_size - 1, 0)), |
| 147 | + proj_weight.flip(-1).unsqueeze(1), |
| 148 | + groups=3 * hidden_size, |
| 149 | + ) |
| 150 | + x1, x2, v = projected[:, ::3], projected[:, 1::3], projected[:, 2::3] |
| 151 | + z = x2 * v |
| 152 | + mixed = F.conv1d( |
| 153 | + F.pad(z, (mixer_kernel_size - 1, 0)), |
| 154 | + mixer_weight.flip(-1).unsqueeze(1), |
| 155 | + groups=hidden_size, |
| 156 | + ) |
| 157 | + expected = x1 * (mixed + bias[None, :, None] * z) |
| 158 | + _assert_close_or_raise("b2b_causal_conv1d", actual, expected) |
0 commit comments