-
Notifications
You must be signed in to change notification settings - Fork 133
Add CHEDDAR GPU backend/emitter #2896
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
AlexanderViand
wants to merge
2
commits into
google:main
Choose a base branch
from
AlexanderViand:cheddar-backend
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 all commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # This build file is necessary to mark this directory as a subpackage for bazel | ||
| # to have access to the files. | ||
|
|
||
| package( | ||
| default_applicable_licenses = ["@heir//:license"], | ||
| default_visibility = ["//visibility:public"], | ||
| ) |
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,56 @@ | ||
| load("@rules_cc//cc:cc_library.bzl", "cc_library") | ||
| load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake") | ||
|
|
||
| package( | ||
| default_visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| filegroup( | ||
| name = "all_srcs", | ||
| srcs = glob( | ||
| ["**"], | ||
| exclude = [ | ||
| "bazel-*/**", | ||
| "build/**", | ||
| "cmake-build*/**", | ||
| ], | ||
| ), | ||
| ) | ||
|
|
||
| cmake( | ||
| name = "cheddar_cmake", | ||
| cache_entries = { | ||
| "CMAKE_BUILD_TYPE": "Release", | ||
| "BUILD_UNITTEST": "OFF", | ||
| "ENABLE_EXTENSION": "ON", | ||
| "USE_GMP": "OFF", | ||
| # Build for the local GPU architecture to keep build times manageable. | ||
| "CMAKE_CUDA_ARCHITECTURES": "native", | ||
| # Assumes conventional CUDA toolkit location and configuration. | ||
| "CMAKE_CUDA_COMPILER": "/usr/local/cuda/bin/nvcc", | ||
| "CUDAToolkit_ROOT": "/usr/local/cuda", | ||
| "CMAKE_CUDA_HOST_COMPILER:FILEPATH": "/usr/bin/g++", | ||
| "CMAKE_CUDA_FLAGS": "-ccbin=/usr/bin/g++", | ||
| # Wipe toolchain-provided linker flags that can inject unsupported host options. | ||
| "CMAKE_EXE_LINKER_FLAGS": "", | ||
| "CMAKE_MODULE_LINKER_FLAGS": "", | ||
| "CMAKE_SHARED_LINKER_FLAGS": "", | ||
| }, | ||
| generate_crosstool_file = False, | ||
| lib_source = ":all_srcs", | ||
| out_include_dir = "include", | ||
| out_shared_libs = [ | ||
| "libcheddar.so", | ||
| ], | ||
| targets = ["cheddar"], | ||
| ) | ||
|
|
||
| # Wrap the cmake output with CUDA Thrust headers so downstream consumers | ||
| # can resolve cheddar's public #include <thrust/...> directives. | ||
| cc_library( | ||
| name = "cheddar", | ||
| deps = [ | ||
| ":cheddar_cmake", | ||
| "@cuda//:thrust", | ||
| ], | ||
| ) |
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,21 @@ | ||
| """Helper macros for CHEDDAR opt-in build configuration.""" | ||
|
|
||
| def if_cheddar_enabled(if_true, if_false = []): | ||
| """Select based on whether CHEDDAR is enabled.""" | ||
| return select({ | ||
| "@heir//:config_enable_cheddar": if_true, | ||
| "//conditions:default": if_false, | ||
| }) | ||
|
|
||
| def requires_cheddar(): | ||
| """Returns target_compatible_with for CHEDDAR-requiring targets.""" | ||
| return select({ | ||
| "@heir//:config_enable_cheddar": [], | ||
| "//conditions:default": ["@platforms//:incompatible"], | ||
| }) | ||
|
|
||
| def cheddar_deps(extra = []): | ||
| """Returns CHEDDAR library deps, empty when disabled.""" | ||
| return if_cheddar_enabled( | ||
| ["@cheddar//:cheddar"] + extra, | ||
| ) |
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,156 @@ | ||
| # Cheddar dialect implementation | ||
|
|
||
| load("@heir//lib/Dialect:dialect.bzl", "add_heir_dialect_library") | ||
| load("@llvm-project//mlir:tblgen.bzl", "td_library") | ||
| load("@rules_cc//cc:cc_library.bzl", "cc_library") | ||
|
|
||
| package( | ||
| default_applicable_licenses = ["@heir//:license"], | ||
| default_visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| cc_library( | ||
| name = "Dialect", | ||
| srcs = [ | ||
| "CheddarDialect.cpp", | ||
| ], | ||
| hdrs = [ | ||
| "CheddarAttributes.h", | ||
| "CheddarDialect.h", | ||
| "CheddarOps.h", | ||
| "CheddarTypes.h", | ||
| ], | ||
| deps = [ | ||
| ":CheddarAttributes", | ||
| ":CheddarOps", | ||
| ":CheddarTypes", | ||
| ":attributes_inc_gen", | ||
| ":dialect_inc_gen", | ||
| ":ops_inc_gen", | ||
| ":types_inc_gen", | ||
| "@heir//lib/Dialect:HEIRInterfaces", | ||
| "@llvm-project//llvm:Support", | ||
| "@llvm-project//mlir:IR", | ||
| "@llvm-project//mlir:InferTypeOpInterface", | ||
| ], | ||
| ) | ||
|
|
||
| cc_library( | ||
| name = "CheddarAttributes", | ||
| srcs = [ | ||
| "CheddarAttributes.cpp", | ||
| ], | ||
| hdrs = [ | ||
| "CheddarAttributes.h", | ||
| "CheddarDialect.h", | ||
| ], | ||
| deps = [ | ||
| ":attributes_inc_gen", | ||
| ":dialect_inc_gen", | ||
| "@llvm-project//mlir:IR", | ||
| ], | ||
| ) | ||
|
|
||
| cc_library( | ||
| name = "CheddarTypes", | ||
| srcs = [ | ||
| "CheddarTypes.cpp", | ||
| ], | ||
| hdrs = [ | ||
| "CheddarAttributes.h", | ||
| "CheddarDialect.h", | ||
| "CheddarTypes.h", | ||
| ], | ||
| deps = [ | ||
| ":CheddarAttributes", | ||
| ":attributes_inc_gen", | ||
| ":dialect_inc_gen", | ||
| ":types_inc_gen", | ||
| "@heir//lib/Dialect:HEIRInterfaces", | ||
| "@llvm-project//mlir:IR", | ||
| ], | ||
| ) | ||
|
|
||
| cc_library( | ||
| name = "CheddarOps", | ||
| srcs = [ | ||
| "CheddarOps.cpp", | ||
| ], | ||
| hdrs = [ | ||
| "CheddarDialect.h", | ||
| "CheddarOps.h", | ||
| "CheddarTypes.h", | ||
| ], | ||
| deps = [ | ||
| ":CheddarAttributes", | ||
| ":CheddarTypes", | ||
| ":dialect_inc_gen", | ||
| ":ops_inc_gen", | ||
| ":types_inc_gen", | ||
| "@heir//lib/Dialect:HEIRInterfaces", | ||
| "@heir//lib/Utils", | ||
| "@heir//lib/Utils:RotationUtils", | ||
| "@llvm-project//mlir:IR", | ||
| "@llvm-project//mlir:InferTypeOpInterface", | ||
| "@llvm-project//mlir:Support", | ||
| ], | ||
| ) | ||
|
|
||
| td_library( | ||
| name = "td_files", | ||
| srcs = [ | ||
| "CheddarAttributes.td", | ||
| "CheddarDialect.td", | ||
| "CheddarOps.td", | ||
| "CheddarTypes.td", | ||
| ], | ||
| # include from the heir-root to enable fully-qualified include-paths | ||
| includes = ["../../../.."], | ||
| deps = [ | ||
| "@heir//lib/Dialect:td_files", | ||
| "@llvm-project//mlir:BuiltinDialectTdFiles", | ||
| "@llvm-project//mlir:InferTypeOpInterfaceTdFiles", | ||
| "@llvm-project//mlir:OpBaseTdFiles", | ||
| ], | ||
| ) | ||
|
|
||
| add_heir_dialect_library( | ||
| name = "dialect_inc_gen", | ||
| dialect = "Cheddar", | ||
| kind = "dialect", | ||
| td_file = "CheddarDialect.td", | ||
| deps = [ | ||
| ":td_files", | ||
| ], | ||
| ) | ||
|
|
||
| add_heir_dialect_library( | ||
| name = "attributes_inc_gen", | ||
| dialect = "Cheddar", | ||
| kind = "attribute", | ||
| td_file = "CheddarAttributes.td", | ||
| deps = [ | ||
| ":td_files", | ||
| ], | ||
| ) | ||
|
|
||
| add_heir_dialect_library( | ||
| name = "types_inc_gen", | ||
| dialect = "Cheddar", | ||
| kind = "type", | ||
| td_file = "CheddarTypes.td", | ||
| deps = [ | ||
| ":td_files", | ||
| ], | ||
| ) | ||
|
|
||
| add_heir_dialect_library( | ||
| name = "ops_inc_gen", | ||
| dialect = "Cheddar", | ||
| kind = "op", | ||
| td_file = "CheddarOps.td", | ||
| deps = [ | ||
| ":td_files", | ||
| "@heir//lib/Dialect:td_files", | ||
| ], | ||
| ) |
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,7 @@ | ||
| #include "lib/Dialect/Cheddar/IR/CheddarAttributes.h" | ||
|
|
||
| namespace mlir { | ||
| namespace heir { | ||
| namespace cheddar {} // namespace cheddar | ||
| } // namespace heir | ||
| } // namespace mlir |
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,9 @@ | ||
| #ifndef LIB_DIALECT_CHEDDAR_IR_CHEDDARATTRIBUTES_H_ | ||
| #define LIB_DIALECT_CHEDDAR_IR_CHEDDARATTRIBUTES_H_ | ||
|
|
||
| #include "lib/Dialect/Cheddar/IR/CheddarDialect.h" | ||
|
|
||
| #define GET_ATTRDEF_CLASSES | ||
| #include "lib/Dialect/Cheddar/IR/CheddarAttributes.h.inc" | ||
|
|
||
| #endif // LIB_DIALECT_CHEDDAR_IR_CHEDDARATTRIBUTES_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,30 @@ | ||
| #ifndef LIB_DIALECT_CHEDDAR_IR_CHEDDARATTRIBUTES_TD_ | ||
| #define LIB_DIALECT_CHEDDAR_IR_CHEDDARATTRIBUTES_TD_ | ||
|
|
||
| include "CheddarDialect.td" | ||
|
|
||
| include "mlir/IR/AttrTypeBase.td" | ||
| include "mlir/IR/DialectBase.td" | ||
|
|
||
| class Cheddar_Attribute<string attrName, string attrMnemonic> | ||
| : AttrDef<Cheddar_Dialect, attrName> { | ||
| let mnemonic = attrMnemonic; | ||
| let assemblyFormat = "`<` struct(params) `>`"; | ||
| } | ||
|
|
||
| def Cheddar_ParameterFileAttr | ||
| : Cheddar_Attribute<"ParameterFile", "parameter_file"> { | ||
| let summary = "Path to a CHEDDAR parameter JSON file"; | ||
| let description = [{ | ||
| This attribute holds the path to a CHEDDAR parameter JSON file. | ||
|
|
||
| CHEDDAR parameters are loaded from pre-built JSON files that specify | ||
| ring dimension, prime chains, scale, and other CKKS parameters. | ||
| }]; | ||
| let parameters = (ins | ||
| StringRefParameter<"path to JSON parameter file">:$path, | ||
| DefaultValuedParameter<"bool", "false", "use 64-bit word type">:$use64Bit | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This field appears to be unused. (In fact, this entire attribute appears to be unused) |
||
| ); | ||
| } | ||
|
|
||
| #endif // LIB_DIALECT_CHEDDAR_IR_CHEDDARATTRIBUTES_TD_ | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So there's a lot of stuff in this PR around adding the cheddar backend (bazel overlay, opt-in config, etc.) but as far as I can tell nothing tests it because there are no end-to-end tests in this PR.
Can we either include an end-to-end test in this PR, or else have this PR only include the MLIR-only parts? (dialect, transforms, lit tests, emitter).
In particular, when I pull this branch to my local machine and I try running
bazel build @cheddar, I getThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@missing tests: indeed, that's an oopsie on my side while extracting this out of the overall cheddar WIP branch. As we discussed in the office hour yesterday, I'll split this PR into three
@ local machine: Does your local machine have an Nvidia GPU with CUDA/etc installed? Otherwise, I'm pretty sure that this is an expected failure, due to @rules_cuda being unable to find the required CUDA related stuff? Maybe there's a way to set up the BUILD file to get a nicer/more helpful error message? Since you're directly asking bezel to build the cheddar target, enable_cheddar isn't "protecting" you here, I guess?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From https://bazel-contrib.github.io/rules_cuda/0.3.0/
I would like to try this again on a machine that has an NVIDIA GPU as well as the hermetic LLVM toolchain. In theory, with the hermetic toolchain we should be able to build the targets even if the machine does not have a GPU or CUDA installed. But yes, we should split that into a standalone PR.