-
Notifications
You must be signed in to change notification settings - Fork 496
[AdvancedCompiler]Zeros(cpp wrapper) #721
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d14bc7b
zeros-wrapper is successful at first
you-and-you bc80a64
Merge branch 'FlagOpen:master' into zeros(cpp-wrapper)
AdvancedCompiler d283ec3
pre-commit is ok
you-and-you 9d321af
again
you-and-you 35ade20
update CMakelist
you-and-you 22e0dd3
Resolved merge conflict in ctests/CMakeLists.txt
you-and-you df1fd61
Merge branch 'FlagOpen:master' into zeros(cpp-wrapper)
AdvancedCompiler 7688e91
all is ok
you-and-you 5e189dd
update zeros_tensor to zeros
you-and-you b19d609
Resolved merge conflict in ctests/CMakeLists.txt
you-and-you ec96817
update all parameters and support multiple dim
you-and-you 0fa50ec
Merge branch 'master' into zeros(cpp-wrapper)
you-and-you 45e11de
ctests/CMakeLists.txt
you-and-you 1b22781
update CMakeLists.txt
you-and-you 76efe39
update zeros
you-and-you 6f7f55e
Merge branch 'master' into zeros(cpp-wrapper)
you-and-you b20e32b
Resolved merge conflict in ctest
you-and-you 1eb7757
lib/CMakeLists.txt
you-and-you 372471f
Merge branch 'master' into zeros(cpp-wrapper)
you-and-you ee5fc74
Resolved merge conflict in CMakeLists.txt
you-and-you 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,44 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "flag_gems/operators.h" | ||
| #include "torch/torch.h" | ||
|
|
||
| TEST(zeros_op_test, 2d_tensor) { | ||
| const torch::Device device(torch::kCUDA, 0); | ||
| std::vector<int64_t> shape_0 = {31}; | ||
| std::vector<int64_t> shape_1 = {11, 7}; | ||
| std::vector<int64_t> shape = {7, 7, 7}; | ||
| auto options = torch::TensorOptions().device(device).dtype(torch::kFloat32); | ||
|
|
||
| torch::Tensor ref_empty = torch::empty(shape, options); | ||
| torch::Tensor ref_empty_0 = torch::empty(shape_0, options); | ||
| torch::Tensor ref_empty_1 = torch::empty(shape_1, options); | ||
| ref_empty.fill_(0); | ||
| ref_empty_0.fill_(0); | ||
| ref_empty_1.fill_(0); | ||
| torch::Tensor out_triton = flag_gems::zeros(torch::IntArrayRef(shape), // size | ||
| torch::kFloat32, // dtype | ||
| c10::nullopt, // layout | ||
| device // device | ||
| ); | ||
|
|
||
| torch::Tensor out_triton_0 = flag_gems::zeros(torch::IntArrayRef(shape_0), // size | ||
| torch::kFloat32, // dtype | ||
| c10::nullopt, // layout | ||
| device // device | ||
| ); | ||
| torch::Tensor out_triton_1 = flag_gems::zeros(torch::IntArrayRef(shape_1), // size | ||
| torch::kFloat32, // dtype | ||
| c10::nullopt, // layout | ||
| device // device | ||
| ); | ||
|
|
||
| EXPECT_TRUE(torch::all(out_triton == 0).item<bool>()); | ||
| EXPECT_TRUE(torch::allclose(out_triton, ref_empty)); | ||
|
|
||
| EXPECT_TRUE(torch::all(out_triton_0 == 0).item<bool>()); | ||
| EXPECT_TRUE(torch::allclose(out_triton_0, ref_empty_0)); | ||
|
|
||
| EXPECT_TRUE(torch::all(out_triton_1 == 0).item<bool>()); | ||
| EXPECT_TRUE(torch::allclose(out_triton_1, ref_empty_1)); | ||
| } |
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| add_library(operators | ||
| SHARED | ||
| zeros.cpp | ||
| utils.cpp | ||
| add.cpp | ||
| sum.cpp | ||
|
|
||
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,61 @@ | ||
| #include "flag_gems/operators.h" | ||
| #include "flag_gems/utils.h" | ||
|
|
||
| #include <iostream> | ||
| #include "c10/cuda/CUDAStream.h" | ||
| #include "triton_jit/triton_jit_function.h" | ||
|
|
||
| namespace flag_gems { | ||
| using namespace triton_jit; | ||
|
|
||
| at::Tensor zeros(at::IntArrayRef size, | ||
| c10::optional<at::ScalarType> dtype, | ||
| c10::optional<at::Layout> layout, | ||
| c10::optional<at::Device> device, | ||
| c10::optional<bool> pin_memory) { | ||
| int64_t n_elements = 1; | ||
| for (auto dim : size) { | ||
| n_elements *= dim; | ||
| } | ||
|
|
||
| auto options = | ||
| at::TensorOptions() | ||
| .dtype(dtype.value_or(at::typeMetaToScalarType(at::get_default_dtype()))) | ||
| .layout(layout.value_or(at::kStrided)) | ||
| .device(device.value_or(torch::cuda::is_available() ? at::Device(at::kCUDA) : at::Device(at::kCPU))) | ||
| .pinned_memory(pin_memory.value_or(false)); | ||
|
|
||
| TORCH_CHECK(n_elements >= 0, "Total elements must be non-negative"); | ||
|
|
||
| if (n_elements == 0) { | ||
| return at::empty(size, options); | ||
| } | ||
|
|
||
| at::Tensor out = at::empty(size, options); | ||
|
|
||
| int64_t tile_size = 1024; | ||
| const int num_warps = 8; | ||
| const int num_stages = 1; | ||
|
|
||
| const uint64_t num_blocks = (static_cast<uint64_t>(n_elements) + tile_size - 1) / tile_size; | ||
|
|
||
| const TritonJITFunction &f = | ||
| TritonJITFunction::getInstance(std::string(utils::get_triton_src_path() / "zeros.py"), "zeros_kernel"); | ||
|
|
||
| c10::DeviceGuard guard(out.device()); | ||
| c10::cuda::CUDAStream stream = c10::cuda::getCurrentCUDAStream(); | ||
| CUstream raw_stream = static_cast<CUstream>(stream.stream()); | ||
|
|
||
| f(stream, | ||
| num_blocks, | ||
| /* grid_y = */ 1, | ||
| /* grid_z = */ 1, | ||
| /* num_warps = */ num_warps, | ||
| /* num_stages = */ num_stages, | ||
| out, | ||
| n_elements, | ||
| tile_size); | ||
|
|
||
| return out; | ||
| } | ||
| } // namespace flag_gems |
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,17 @@ | ||
| import triton | ||
| import triton.language as tl | ||
|
|
||
| from flag_gems.utils import triton_lang_extension as tle | ||
|
|
||
|
|
||
| @triton.jit | ||
| def zeros_kernel( | ||
| output_ptr, | ||
| n_elements, | ||
| BLOCK_SIZE: tl.constexpr, | ||
| ): | ||
| pid = tle.program_id(axis=0) | ||
| block_start = pid * BLOCK_SIZE | ||
| offsets = block_start + tl.arange(0, BLOCK_SIZE) | ||
| mask = offsets < n_elements | ||
| tl.store(output_ptr + offsets, 0.0, mask=mask) |
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.