forked from NVIDIA/cutlass
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Add a tool for debugging copies #224
Open
t4c1
wants to merge
8
commits into
codeplaysoftware:sycl-develop
Choose a base branch
from
t4c1:copy_reverse_engineer
base: sycl-develop
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 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2888f89
add a tool for reverse engineering copies
t4c1 b73418b
Merge branch 'sycl-develop' into copy_reverse_engineer
t4c1 9fe0ac4
addressed review comments
t4c1 25b486f
Merge branch 'copy_reverse_engineer' of https://github.com/t4c1/cutla…
t4c1 13abf3f
try fixing CI
t4c1 b0238da
Merge branch 'sycl-develop' into copy_reverse_engineer
t4c1 797f731
fix CI
t4c1 d7fcedf
Merge branch 'sycl-develop' into copy_reverse_engineer
t4c1 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 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 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,133 @@ | ||
/*************************************************************************************************** | ||
* Copyright (c) 2025 - 2025 Codeplay Software Ltd. All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
**************************************************************************************************/ | ||
|
||
#include "cutlass/detail/layout.hpp" | ||
|
||
#include <cute/tensor.hpp> | ||
#include <sycl/sycl.hpp> | ||
#include <syclcompat.hpp> | ||
|
||
#include <cutlass/util/device_memory.h> | ||
#include <syclcompat/syclcompat.hpp> | ||
#include <cutlass/cutlass.h> | ||
|
||
using namespace syclcompat::experimental; | ||
|
||
#define SUBGROUP_SIZE (16) | ||
|
||
template <class CopyInstruction, class TensorS, class fragment_size> | ||
void copy_kernel(TensorS S) { | ||
using namespace cute; | ||
using Element = typename TensorS::value_type; | ||
|
||
// initialization | ||
if(thread(0)){ | ||
for(int i=0; i<size(S); i++){ | ||
S(i) = static_cast<Element>(i); | ||
} | ||
} | ||
syncthreads(); | ||
|
||
using CopyThreadShape = Shape<_1, Int<SUBGROUP_SIZE>>; | ||
using traits_load = Copy_Traits<CopyInstruction, decltype(S)>; | ||
using Atom_load = Copy_Atom<traits_load, Element>; | ||
auto tiled_copy_load = make_tiled_copy(Atom_load{}.with(S), | ||
Layout<CopyThreadShape>{}, | ||
make_layout(shape_div(typename traits_load::BlockShape{}, CopyThreadShape{}))); | ||
|
||
auto thr_copy_load = tiled_copy_load.get_slice(cutlass::get_sub_group_local_id()); | ||
|
||
using actual_fragment_size = std::conditional_t<std::is_same_v<fragment_size, void>, C<Atom_load::NumValDst>, fragment_size>; | ||
Tensor fragment = make_tensor<Element>(make_shape(actual_fragment_size{},_1{},_1{})); | ||
clear(fragment); | ||
|
||
static_assert(actual_fragment_size::value >= Atom_load::NumValDst, "fragment is too small to hold all results!"); | ||
Tensor fragment_copy_view = make_tensor(fragment.data(), make_shape(C<Atom_load::NumValDst>{},_1{},_1{})); | ||
auto blk_load_S = tiled_copy_load.get_pvc_tensor(make_coord(0, 0, 0),make_shape(_1{},_1{},_1{})); | ||
copy(tiled_copy_load, blk_load_S, fragment_copy_view); | ||
|
||
if(thread(0)){ | ||
print("fragment: "); print(fragment); print("\n"); | ||
} | ||
|
||
for(int i=0;i<SUBGROUP_SIZE;i++){ | ||
if(thread(i)){ | ||
print("thread "); | ||
if(i<10) print(" "); | ||
print(i); print(" "); | ||
} | ||
} | ||
for(int i = 0;i < size(fragment); i++){ | ||
if(thread(0)){ | ||
print("\n "); | ||
} | ||
for(int j=0;j<SUBGROUP_SIZE;j++){ | ||
if(thread(j)){ | ||
if(fragment(i)<10) print(" "); | ||
if(fragment(i)<100) print(" "); | ||
if(fragment(i)<1000) print(" "); | ||
print(static_cast<int>(fragment(i))); print(" "); | ||
} | ||
} | ||
} | ||
if(thread(0)){ | ||
print("\n"); | ||
} | ||
} | ||
|
||
// by default select fragment size to match copy size. This can be set manually to a bigger value as copy size might be wrong | ||
template <class CopyInstruction, class dtype, class fragment_size = void> | ||
void copy(int global_M, int global_N) { | ||
using namespace cute; | ||
|
||
auto tensor_shape = make_shape(global_M, global_N); | ||
int tensor_size = size(tensor_shape); | ||
cutlass::DeviceAllocation<dtype> src(tensor_size); | ||
|
||
Tensor tensor_S = make_tensor(make_gmem_ptr(src.get()), make_layout(tensor_shape, LayoutLeft{})); | ||
|
||
auto gridDim = syclcompat::dim3(SUBGROUP_SIZE); | ||
auto blockDim = syclcompat::dim3(SUBGROUP_SIZE); | ||
launch<copy_kernel<CopyInstruction, decltype(tensor_S), fragment_size>>( | ||
launch_policy{gridDim, blockDim, | ||
kernel_properties{sycl_exp::sub_group_size<SUBGROUP_SIZE>}}, | ||
tensor_S); | ||
|
||
syclcompat::wait_and_throw(); | ||
} | ||
|
||
int main(){ | ||
// for 16b copies use integers as floating point types could lose precision for bigger indices | ||
// for 8b copies you have to work with overflow | ||
copy<XE_2D_U16x32x32_LD_V, int16_t>(256, 256); | ||
copy<XE_2D_U16x32x32_LD_N, int16_t>(256, 256); | ||
return 0; | ||
} |
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.
This launches 16x16 threads. Is that your intention?
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.
I dont think so (or I would have multiple printouts). I think this is the relevant dim3 constructor, right?