-
Notifications
You must be signed in to change notification settings - Fork 111
[Feat] Add an experimental cuda nvvm backend #976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sjfeng1999
wants to merge
2
commits into
main
Choose a base branch
from
pr/nvvm-target
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # Copyright (c) 2026 FlyDSL Project Contributors | ||
| # | ||
| # NVVM backend descriptor. | ||
| # Self-registers into global properties consumed by downstream CMakeLists.txt. | ||
| # | ||
| # Stage one ships FlyNVVM SM80 atom types, FlyToNVVM conversion, Python | ||
| # bindings, and CUDA runtime support. The Python-side properties below keep the | ||
| # generated dialect bindings and stubs in sync with enabled backends. | ||
|
|
||
| # TableGen / header subdirectories under include/flydsl/ | ||
| set_property(GLOBAL APPEND PROPERTY FLYDSL_BACKEND_INCLUDE_DIALECT_SUBDIRS "FlyNVVM") | ||
| set_property(GLOBAL APPEND PROPERTY FLYDSL_BACKEND_INCLUDE_CONVERSION_SUBDIRS "FlyToNVVM") | ||
|
|
||
| # C++ library subdirectories under lib/ | ||
| set_property(GLOBAL APPEND PROPERTY FLYDSL_BACKEND_LIB_DIALECT_SUBDIRS "FlyNVVM") | ||
| set_property(GLOBAL APPEND PROPERTY FLYDSL_BACKEND_LIB_CONVERSION_SUBDIRS "FlyToNVVM") | ||
|
|
||
| # CAPI wrapper subdirectory under lib/CAPI/Dialect/ | ||
| set_property(GLOBAL APPEND PROPERTY FLYDSL_BACKEND_CAPI_SUBDIRS "FlyNVVM") | ||
|
|
||
| # CAPI link targets for _mlirRegisterEverything (EMBED_CAPI_LINK_LIBS) | ||
| set_property(GLOBAL APPEND PROPERTY FLYDSL_BACKEND_EMBED_CAPI_LIBS "MLIRCPIFlyNVVM") | ||
|
|
||
| # Link targets for fly-opt | ||
| set_property(GLOBAL APPEND PROPERTY FLYDSL_BACKEND_FLYOPT_LINK_LIBS "MLIRCPIFlyNVVM") | ||
|
|
||
| # Upstream MLIR dialect sources needed by this backend's Python bindings | ||
| set_property(GLOBAL APPEND PROPERTY FLYDSL_BACKEND_UPSTREAM_DIALECT_SOURCES | ||
| "MLIRPythonSources.Dialects.nvvm") | ||
|
|
||
| # Stubgen modules for this backend | ||
| set_property(GLOBAL APPEND PROPERTY FLYDSL_BACKEND_STUBGEN_MODULES | ||
| "flydsl._mlir._mlir_libs._mlirDialectsFlyNVVM") | ||
|
|
||
| # Convenience boolean for Python CMakeLists gating of NVVM-specific bindings. | ||
| set(FLYDSL_HAS_NVVM ON) |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # Copyright (c) 2026 FlyDSL Project Contributors | ||
| # | ||
| # Run: | ||
| # FLYDSL_COMPILE_BACKEND=cuda FLYDSL_RUNTIME_KIND=cuda \ | ||
| # python3 examples/cuda/01-MmaSync.py | ||
|
|
||
| import torch | ||
|
|
||
| import flydsl.compiler as flyc | ||
| import flydsl.expr as fx | ||
|
|
||
| # One mma.sync.aligned instruction tile: M=16, N=8, K=16. | ||
| INST_M = 16 | ||
| INST_N = 8 | ||
| INST_K = 16 | ||
|
|
||
|
|
||
| @flyc.kernel | ||
| def gemm_kernel( | ||
| A: fx.Tensor, # (M, K) row-major | ||
| B: fx.Tensor, # (N, K) row-major (so C = A @ B^T) | ||
| C: fx.Tensor, # (M, N) row-major | ||
| ): | ||
| tid = fx.thread_idx.x | ||
| bid = fx.block_idx.x | ||
|
|
||
| bA = fx.zipped_divide(A, (INST_M, INST_K)) | ||
| bB = fx.zipped_divide(B, (INST_N, INST_K)) | ||
| bC = fx.zipped_divide(C, (INST_M, INST_N)) | ||
|
|
||
| bA = fx.slice(bA, (None, bid)) | ||
| bB = fx.slice(bB, (None, bid)) | ||
| bC = fx.slice(bC, (None, bid)) | ||
|
|
||
| mma_atom = fx.make_mma_atom(fx.nvvm.MmaSync(16, 8, 16, fx.Float16)) | ||
| tiled_mma = fx.make_tiled_mma(mma_atom, fx.make_layout((1, 1, 1), (0, 0, 0))) | ||
| thr_mma = tiled_mma.thr_slice(tid) | ||
|
|
||
| copy_atom_f16 = fx.make_copy_atom(fx.UniversalCopy16b(), fx.Float16) | ||
| copy_atom_f32 = fx.make_copy_atom(fx.UniversalCopy32b(), fx.Float32) | ||
| tiled_copy_A = fx.make_tiled_copy_A(copy_atom_f16, tiled_mma) | ||
| tiled_copy_B = fx.make_tiled_copy_B(copy_atom_f16, tiled_mma) | ||
| tiled_copy_C = fx.make_tiled_copy_C(copy_atom_f32, tiled_mma) | ||
|
|
||
| thr_copy_A = tiled_copy_A.get_slice(tid) | ||
| thr_copy_B = tiled_copy_B.get_slice(tid) | ||
| thr_copy_C = tiled_copy_C.get_slice(tid) | ||
|
|
||
| copy_src_A = thr_copy_A.partition_S(bA) | ||
| copy_src_B = thr_copy_B.partition_S(bB) | ||
| copy_dst_C = thr_copy_C.partition_S(bC) | ||
|
|
||
| frag_A = thr_mma.make_fragment_A(bA) | ||
| frag_B = thr_mma.make_fragment_B(bB) | ||
| frag_C = thr_mma.make_fragment_C(bC) | ||
|
|
||
| copy_frag_A = thr_copy_A.retile(frag_A) | ||
| copy_frag_B = thr_copy_B.retile(frag_B) | ||
| copy_frag_C = thr_copy_C.retile(frag_C) | ||
|
|
||
| fx.copy(copy_atom_f16, copy_src_A, copy_frag_A, pred=None) | ||
| fx.copy(copy_atom_f16, copy_src_B, copy_frag_B, pred=None) | ||
|
|
||
| frag_C.fill(0) | ||
| fx.gemm(mma_atom, frag_C, frag_A, frag_B, frag_C) | ||
|
|
||
| fx.copy(copy_atom_f32, copy_frag_C, copy_dst_C, pred=None) | ||
|
|
||
|
|
||
| @flyc.jit | ||
| def nvvm_gemm( | ||
| A: fx.Tensor, | ||
| B: fx.Tensor, | ||
| C: fx.Tensor, | ||
| stream: fx.Stream = fx.Stream(None), | ||
| ): | ||
| gemm_kernel(A, B, C).launch(grid=(1, 1, 1), block=(32, 1, 1), stream=stream) | ||
|
|
||
|
|
||
| M, N, K = INST_M, INST_N, INST_K | ||
| A = torch.randn(M, K, dtype=torch.float16).cuda() | ||
| B = torch.randn(N, K, dtype=torch.float16).cuda() | ||
| C = torch.zeros(M, N, dtype=torch.float32).cuda() | ||
|
|
||
| nvvm_gemm(A, B, C, stream=torch.cuda.Stream()) | ||
| torch.cuda.synchronize() | ||
|
|
||
| expected = A.float() @ B.float().T | ||
| is_correct = torch.allclose(C, expected, atol=1e-2, rtol=1e-2) | ||
| print("Result correct:", is_correct) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) 2026 FlyDSL Project Contributors | ||
|
|
||
| #ifndef FLYDSL_C_FLYNVVMDIALECT_H | ||
| #define FLYDSL_C_FLYNVVMDIALECT_H | ||
|
|
||
| #include "mlir-c/IR.h" | ||
| #include "mlir-c/Support.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(FlyNVVM, fly_nvvm); | ||
|
|
||
| MLIR_CAPI_EXPORTED void mlirRegisterFlyToNVVMConversionPass(void); | ||
|
|
||
| /// Backend plugin registration: insert all NVVM dialects into \p registry. | ||
| MLIR_CAPI_EXPORTED void flydsl_register_nvvm_dialects(MlirDialectRegistry registry); | ||
| /// Backend plugin registration: register all NVVM passes. | ||
| MLIR_CAPI_EXPORTED void flydsl_register_nvvm_passes(void); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif // FLYDSL_C_FLYNVVMDIALECT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| set(LLVM_TARGET_DEFINITIONS Passes.td) | ||
| mlir_tablegen(Passes.h.inc -gen-pass-decls -name FlyToNVVM) | ||
| mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix FlyToNVVM) | ||
| mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix FlyToNVVM) | ||
|
|
||
| add_mlir_generic_tablegen_target(FlyToNVVMPassIncGen) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) 2026 FlyDSL Project Contributors | ||
|
|
||
| #ifndef CONVERSION_FLYTONVVM_FLYTONVVM_H | ||
| #define CONVERSION_FLYTONVVM_FLYTONVVM_H | ||
|
|
||
| #include "mlir/Pass/Pass.h" | ||
|
|
||
| namespace mlir { | ||
| #define GEN_PASS_DECL_FLYTONVVMCONVERSIONPASS | ||
| #include "flydsl/Conversion/FlyToNVVM/Passes.h.inc" | ||
| } // namespace mlir | ||
|
|
||
| #endif // CONVERSION_FLYTONVVM_FLYTONVVM_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) 2026 FlyDSL Project Contributors | ||
|
|
||
| include "mlir/Pass/PassBase.td" | ||
|
|
||
| def FlyToNVVMConversionPass : Pass<"convert-fly-to-nvvm"> { | ||
| let summary = "Lower Fly to MLIR upstream and nvvm dialects "; | ||
| let dependentDialects = [ | ||
| "arith::ArithDialect", | ||
| "scf::SCFDialect", | ||
| "vector::VectorDialect", | ||
| "LLVM::LLVMDialect", | ||
| "NVVM::NVVMDialect" | ||
| ]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| add_subdirectory(IR) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) 2026 FlyDSL Project Contributors | ||
|
|
||
| #ifndef FLYNVVM_ATOM | ||
| #define FLYNVVM_ATOM | ||
|
|
||
| include "flydsl/Dialect/FlyNVVM/IR/Dialect.td" | ||
| include "flydsl/Dialect/FlyNVVM/IR/MmaAtom.td" | ||
| include "flydsl/Dialect/FlyNVVM/IR/CopyAtom.td" | ||
|
|
||
| #endif // FLYNVVM_ATOM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| set(LLVM_TARGET_DEFINITIONS Dialect.td) | ||
|
|
||
| mlir_tablegen(Dialect.h.inc -gen-dialect-decls) | ||
| mlir_tablegen(Dialect.cpp.inc -gen-dialect-defs) | ||
|
|
||
| set(LLVM_TARGET_DEFINITIONS Atom.td) | ||
| mlir_tablegen(Atom.h.inc -gen-typedef-decls -typedefs-dialect=fly_nvvm) | ||
| mlir_tablegen(Atom.cpp.inc -gen-typedef-defs -typedefs-dialect=fly_nvvm) | ||
|
|
||
| add_public_tablegen_target(MLIRFlyNVVMIncGen) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright (c) 2026 FlyDSL Project Contributors | ||
|
|
||
| #ifndef FLYNVVM_COPYATOM | ||
| #define FLYNVVM_COPYATOM | ||
|
|
||
| include "flydsl/Dialect/FlyNVVM/IR/Dialect.td" | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // CopyOp SM75 — PTX Warp-level Matrix Load Instruction: ldmatrix | ||
| // ldmatrix.sync.aligned.m8n8.x{1,2,4}[.trans].shared.b16 | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| def FlyNVVM_CopyOpSM75_LdMatrix : FlyNVVM_CopyOp<"CopyOpSM75_LdMatrix", "sm75.ldmatrix", []> { | ||
| let parameters = (ins "int32_t":$num, "bool":$trans); | ||
| let assemblyFormat = "`<` `num` `=` $num `,` `trans` `=` $trans `>`"; | ||
| let genVerifyDecl = 1; | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // CopyOp SM80 — PTX Data Movement and Conversion Instruction: cp.async | ||
| // cp.async.{ca,cg}.shared.global (global -> shared, asynchronous) | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| def FlyNVVM_CopyOpSM80_CpAsync : FlyNVVM_CopyOp<"CopyOpSM80_CpAsync", "sm80.cp.async", []> { | ||
| let parameters = (ins "int32_t":$bitSize); | ||
| let assemblyFormat = "`<` $bitSize `>`"; // TODO: cache modifiers | ||
| let genVerifyDecl = 1; | ||
| } | ||
|
|
||
|
|
||
| #endif // FLYNVVM_COPYATOM |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.