main
cu 87 lines 3.35 KB
Raw
1 /*
2 * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
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
17 #include <torch/csrc/stable/tensor.h>
18
19 #include "libtorch_stable/torch_utils.h"
20
21 #include "libtorch_stable/cutlass_extensions/common.hpp"
22
23 #if defined ENABLE_NVFP4_SM100 && ENABLE_NVFP4_SM100
24 void cutlass_scaled_fp4_mm_sm100a(torch::stable::Tensor& D,
25 torch::stable::Tensor const& A,
26 torch::stable::Tensor const& B,
27 torch::stable::Tensor const& A_sf,
28 torch::stable::Tensor const& B_sf,
29 torch::stable::Tensor const& alpha);
30 #endif
31
32 #if defined ENABLE_NVFP4_SM120 && ENABLE_NVFP4_SM120
33 void cutlass_scaled_fp4_mm_sm120a(torch::stable::Tensor& D,
34 torch::stable::Tensor const& A,
35 torch::stable::Tensor const& B,
36 torch::stable::Tensor const& A_sf,
37 torch::stable::Tensor const& B_sf,
38 torch::stable::Tensor const& alpha);
39 #endif
40
41 void cutlass_scaled_fp4_mm(torch::stable::Tensor& D,
42 const torch::stable::Tensor& A,
43 const torch::stable::Tensor& B,
44 const torch::stable::Tensor& A_sf,
45 const torch::stable::Tensor& B_sf,
46 const torch::stable::Tensor& alpha) {
47 // Make sure we're on A's device.
48 const torch::stable::accelerator::DeviceGuard device_guard(
49 A.get_device_index());
50 const int32_t sm = get_sm_version_num();
51
52 #if defined(ENABLE_NVFP4_SM100) && ENABLE_NVFP4_SM100
53 if (sm >= 100 && sm < 120) {
54 cutlass_scaled_fp4_mm_sm100a(D, A, B, A_sf, B_sf, alpha);
55 return;
56 }
57 #endif
58
59 #if defined(ENABLE_NVFP4_SM120) && ENABLE_NVFP4_SM120
60 if (sm >= 120 && sm < 130) {
61 cutlass_scaled_fp4_mm_sm120a(D, A, B, A_sf, B_sf, alpha);
62 return;
63 }
64 #endif
65
66 STD_TORCH_CHECK_NOT_IMPLEMENTED(
67 false, "No compiled nvfp4 mm kernel for SM ", sm,
68 ". Recompile with CUDA >= 12.8 and CC >= 100.");
69 }
70
71 bool cutlass_scaled_mm_supports_fp4(int64_t cuda_device_capability) {
72 int runtimeVersion;
73 cudaRuntimeGetVersion(&runtimeVersion);
74 if (runtimeVersion < 12080) return false;
75 // Only report support when the SM-specific kernel was actually compiled in,
76 // so the Python-side backend selector does not choose CUTLASS and then hit
77 // TORCH_CHECK_NOT_IMPLEMENTED (or worse, fall through to Marlin).
78 #if defined(ENABLE_NVFP4_SM100) && ENABLE_NVFP4_SM100
79 if (cuda_device_capability >= 100 && cuda_device_capability < 120)
80 return true;
81 #endif
82 #if defined(ENABLE_NVFP4_SM120) && ENABLE_NVFP4_SM120
83 if (cuda_device_capability >= 120 && cuda_device_capability < 130)
84 return true;
85 #endif
86 return false;
87 }