|
| 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 Triton kernels for modelopt (attention, quantization, etc.).""" |
| 17 | + |
| 18 | +import torch |
| 19 | + |
| 20 | +from modelopt.torch.utils import import_plugin |
| 21 | + |
| 22 | +IS_AVAILABLE = False |
| 23 | +attention = None |
| 24 | +attention_calibrate = None |
| 25 | +register_triton_attention = None |
| 26 | + |
| 27 | +if torch.cuda.is_available(): |
| 28 | + with import_plugin( |
| 29 | + "triton", |
| 30 | + msg_if_missing=( |
| 31 | + "Your device is potentially capable of using the triton attention " |
| 32 | + "kernel. Try to install triton with `pip install triton`." |
| 33 | + ), |
| 34 | + ): |
| 35 | + from .triton_fa import attention as _attention |
| 36 | + from .triton_fa import attention_calibrate as _attention_calibrate |
| 37 | + |
| 38 | + attention = _attention |
| 39 | + attention_calibrate = _attention_calibrate |
| 40 | + IS_AVAILABLE = True |
| 41 | + from .hf_triton_attention import register_triton_attention as _register_triton_attention |
| 42 | + |
| 43 | + register_triton_attention = _register_triton_attention |
| 44 | + |
| 45 | +__all__ = [ |
| 46 | + "IS_AVAILABLE", |
| 47 | + "attention", |
| 48 | + "attention_calibrate", |
| 49 | + "register_triton_attention", |
| 50 | +] |
0 commit comments