-
Notifications
You must be signed in to change notification settings - Fork 871
Add chunk_gated_delta_rule triton kernel for CUDA backend #18138
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
mergennachin
wants to merge
2
commits into
main
Choose a base branch
from
mergennachin/fla_linear_attention
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.
+835
−1
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
54 changes: 54 additions & 0 deletions
54
backends/cuda/tests/chunk_gated_delta_runner/CMakeLists.txt
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,54 @@ | ||
| cmake_minimum_required(VERSION 3.24) | ||
| project(chunk_gated_delta_runner) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 17) | ||
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
|
||
| set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../..) | ||
|
|
||
| include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake) | ||
|
|
||
| set(_common_include_directories ${EXECUTORCH_ROOT}/..) | ||
|
|
||
| set(gflags_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../../third-party/gflags) | ||
| find_package(gflags REQUIRED) | ||
|
|
||
| list(APPEND CMAKE_FIND_ROOT_PATH ${CMAKE_CURRENT_BINARY_DIR}/../../../..) | ||
| find_package(executorch CONFIG REQUIRED FIND_ROOT_PATH_BOTH) | ||
| executorch_target_link_options_shared_lib(executorch) | ||
|
|
||
| set(link_libraries executorch gflags) | ||
|
|
||
| list(APPEND link_libraries optimized_native_cpu_ops_lib cpublas eigen_blas) | ||
| executorch_target_link_options_shared_lib(optimized_native_cpu_ops_lib) | ||
|
|
||
| list( | ||
| APPEND | ||
| link_libraries | ||
| extension_module | ||
| extension_data_loader | ||
| extension_tensor | ||
| extension_flat_tensor | ||
| extension_named_data_map | ||
| ) | ||
|
|
||
| if(EXECUTORCH_BUILD_CUDA) | ||
| find_package(CUDAToolkit REQUIRED) | ||
| list(APPEND link_libraries aoti_cuda_backend) | ||
| if(NOT MSVC) | ||
| executorch_target_link_options_shared_lib(aoti_cuda_backend) | ||
| endif() | ||
| endif() | ||
|
|
||
| add_executable(chunk_gated_delta_runner main.cpp) | ||
| target_include_directories( | ||
| chunk_gated_delta_runner PUBLIC ${_common_include_directories} | ||
| ) | ||
| target_link_libraries(chunk_gated_delta_runner PUBLIC ${link_libraries}) | ||
|
|
||
| if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
| target_link_options_gc_sections(chunk_gated_delta_runner) | ||
| if(NOT APPLE AND NOT MSVC) | ||
| target_link_options(chunk_gated_delta_runner PRIVATE "LINKER:-s") | ||
| endif() | ||
| endif() | ||
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,167 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <fstream> | ||
| #include <vector> | ||
|
|
||
| #include <gflags/gflags.h> | ||
|
|
||
| #include <executorch/extension/module/module.h> | ||
| #include <executorch/extension/tensor/tensor.h> | ||
mergennachin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include <executorch/runtime/core/exec_aten/exec_aten.h> | ||
|
|
||
| DEFINE_string(model_path, "", "Path to .pte file"); | ||
| DEFINE_string(data_path, "", "Path to .ptd directory (for CUDA delegate)"); | ||
| DEFINE_string(input_dir, "", "Directory with input .bin files"); | ||
| DEFINE_string(output_dir, "", "Directory to write output .bin files"); | ||
|
|
||
| using ::executorch::extension::from_blob; | ||
| using ::executorch::extension::Module; | ||
| using ::executorch::runtime::Error; | ||
| using ::executorch::runtime::EValue; | ||
|
|
||
| static std::vector<char> read_file(const std::string& path) { | ||
| std::ifstream f(path, std::ios::binary | std::ios::ate); | ||
| if (!f) { | ||
| fprintf(stderr, "Cannot open %s\n", path.c_str()); | ||
| exit(1); | ||
| } | ||
mergennachin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| std::size_t size = static_cast<std::size_t>(f.tellg()); | ||
| f.seekg(0); | ||
| std::vector<char> buf(size); | ||
| f.read(buf.data(), static_cast<std::streamsize>(size)); | ||
| return buf; | ||
| } | ||
|
|
||
| static void write_file(const std::string& path, const void* data, size_t len) { | ||
| std::ofstream f(path, std::ios::binary); | ||
| f.write(static_cast<const char*>(data), len); | ||
| } | ||
|
|
||
| int main(int argc, char** argv) { | ||
| gflags::ParseCommandLineFlags(&argc, &argv, true); | ||
| if (FLAGS_model_path.empty()) { | ||
| fprintf(stderr, "Error: --model_path required\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| std::unique_ptr<Module> module; | ||
| if (!FLAGS_data_path.empty()) { | ||
| module = std::make_unique<Module>( | ||
| FLAGS_model_path, | ||
| FLAGS_data_path, | ||
| Module::LoadMode::MmapUseMlockIgnoreErrors); | ||
| } else { | ||
| module = std::make_unique<Module>( | ||
| FLAGS_model_path, Module::LoadMode::MmapUseMlockIgnoreErrors); | ||
| } | ||
|
|
||
| auto load_err = module->load(); | ||
| if (load_err != Error::Ok) { | ||
| fprintf(stderr, "Failed to load model: 0x%x\n", static_cast<int>(load_err)); | ||
| return 1; | ||
| } | ||
|
|
||
| constexpr int B = 1, T = 128, H = 4, K = 64, V = 64; | ||
|
|
||
| std::vector<EValue> inputs; | ||
|
|
||
| if (!FLAGS_input_dir.empty()) { | ||
| // Load inputs from binary files | ||
| struct TensorSpec { | ||
| const char* name; | ||
| std::vector<exec_aten::SizesType> shape; | ||
| exec_aten::ScalarType dtype; | ||
| }; | ||
| TensorSpec specs[] = { | ||
| {"q", {B, T, H, K}, exec_aten::ScalarType::BFloat16}, | ||
| {"k", {B, T, H, K}, exec_aten::ScalarType::BFloat16}, | ||
| {"v", {B, T, H, V}, exec_aten::ScalarType::BFloat16}, | ||
| {"g", {B, T, H}, exec_aten::ScalarType::BFloat16}, | ||
| {"beta", {B, T, H}, exec_aten::ScalarType::BFloat16}, | ||
| {"initial_state", {B, H, K, V}, exec_aten::ScalarType::BFloat16}, | ||
| }; | ||
|
|
||
| // Keep data and TensorPtrs alive for the duration of execution | ||
| static std::vector<std::vector<char>> input_bufs; | ||
| static std::vector<executorch::extension::TensorPtr> input_tensors; | ||
| input_bufs.resize(6); | ||
| input_tensors.clear(); | ||
|
|
||
| for (int i = 0; i < 6; i++) { | ||
| std::string path = FLAGS_input_dir + "/" + specs[i].name + ".bin"; | ||
| input_bufs[i] = read_file(path); | ||
| input_tensors.push_back( | ||
| from_blob(input_bufs[i].data(), specs[i].shape, specs[i].dtype)); | ||
| inputs.push_back(*input_tensors.back()); | ||
| } | ||
| } else { | ||
| // Generate deterministic test inputs | ||
| auto to_bf16 = [](float f) -> uint16_t { | ||
| uint32_t bits; | ||
| std::memcpy(&bits, &f, sizeof(float)); | ||
| return static_cast<uint16_t>(bits >> 16); | ||
| }; | ||
|
|
||
| static std::vector<uint16_t> qk_data(B * T * H * K); | ||
| for (size_t i = 0; i < qk_data.size(); i++) | ||
| qk_data[i] = to_bf16(static_cast<float>(i % 100) * 0.01f - 0.5f); | ||
| static auto v_data = std::vector<uint16_t>(qk_data.begin(), qk_data.end()); | ||
| static std::vector<uint16_t> g_data(B * T * H, to_bf16(-0.5f)); | ||
| static std::vector<uint16_t> beta_data(B * T * H, to_bf16(0.5f)); | ||
| static std::vector<uint16_t> state_data(B * H * K * V, to_bf16(0.0f)); | ||
|
|
||
| static std::vector<executorch::extension::TensorPtr> default_tensors; | ||
| default_tensors.clear(); | ||
| default_tensors.push_back(from_blob( | ||
| qk_data.data(), {B, T, H, K}, exec_aten::ScalarType::BFloat16)); | ||
| default_tensors.push_back(from_blob( | ||
| qk_data.data(), {B, T, H, K}, exec_aten::ScalarType::BFloat16)); | ||
| default_tensors.push_back(from_blob( | ||
| v_data.data(), {B, T, H, V}, exec_aten::ScalarType::BFloat16)); | ||
| default_tensors.push_back( | ||
| from_blob(g_data.data(), {B, T, H}, exec_aten::ScalarType::BFloat16)); | ||
| default_tensors.push_back(from_blob( | ||
| beta_data.data(), {B, T, H}, exec_aten::ScalarType::BFloat16)); | ||
| default_tensors.push_back(from_blob( | ||
| state_data.data(), {B, H, K, V}, exec_aten::ScalarType::BFloat16)); | ||
| for (auto& t : default_tensors) | ||
| inputs.push_back(*t); | ||
| } | ||
|
|
||
| auto result = module->execute("forward", inputs); | ||
| if (!result.ok()) { | ||
| fprintf(stderr, "Forward failed: 0x%x\n", static_cast<int>(result.error())); | ||
| return 1; | ||
| } | ||
|
|
||
| auto outputs = result.get(); | ||
| for (size_t i = 0; i < outputs.size(); i++) { | ||
| if (!outputs[i].isTensor()) | ||
| continue; | ||
| const auto& t = outputs[i].toTensor(); | ||
| printf("Output %zu: [", i); | ||
| for (int d = 0; d < t.dim(); d++) | ||
| printf("%d%s", static_cast<int>(t.size(d)), d < t.dim() - 1 ? "," : ""); | ||
| printf("] dtype=%d\n", static_cast<int>(t.scalar_type())); | ||
|
|
||
| if (!FLAGS_output_dir.empty()) { | ||
| // Output tensors are on host memory (CUDA delegate copies back to CPU) | ||
| std::string path = | ||
| FLAGS_output_dir + "/output_" + std::to_string(i) + ".bin"; | ||
| write_file(path, t.const_data_ptr(), t.nbytes()); | ||
| printf(" Saved to %s (%zu bytes)\n", path.c_str(), (size_t)t.nbytes()); | ||
mergennachin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| printf("SUCCESS\n"); | ||
| return 0; | ||
| } | ||
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.