-
Notifications
You must be signed in to change notification settings - Fork 233
Add sycl examples #114
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
ZhaoqiongZ
wants to merge
9
commits into
pytorch:master
Choose a base branch
from
ZhaoqiongZ:add_sycl_examples/zzq
base: master
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
Add sycl examples #114
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e7dcf6d
update sycl extension for muladd op
ZhaoqiongZ e77e626
update setup for cuda/sycl extension
ZhaoqiongZ f9a38a7
update setup and README for SyclExtension examples
ZhaoqiongZ ebaeb31
Update extension_cpp/csrc/sycl/muladd.sycl
ZhaoqiongZ 1e186ec
add back long description
ZhaoqiongZ 156bb4b
remove debug printout
ZhaoqiongZ 0dd3d29
update comments for py_limited_api compatibility
ZhaoqiongZ a3d54f3
Update README.md
ZhaoqiongZ 39c4423
Update README.md with sycl description
ZhaoqiongZ 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
// Copyright (c) 2025 Intel Corporation | ||
|
||
#include <c10/xpu/XPUStream.h> | ||
ZhaoqiongZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <sycl/sycl.hpp> | ||
#include <ATen/Operators.h> | ||
#include <torch/all.h> | ||
#include <torch/library.h> | ||
|
||
namespace extension_cpp { | ||
|
||
|
||
// MulAdd Kernel: result = a * b + c | ||
static void muladd_kernel( | ||
int numel, const float* a, const float* b, float c, float* result, | ||
const sycl::nd_item<1>& item) { | ||
int idx = item.get_global_id(0); | ||
if (idx < numel) { | ||
result[idx] = a[idx] * b[idx] + c; | ||
} | ||
} | ||
|
||
// Mul Kernel: result = a * b | ||
static void mul_kernel( | ||
int numel, const float* a, const float* b, float* result, | ||
const sycl::nd_item<1>& item) { | ||
int idx = item.get_global_id(0); | ||
if (idx < numel) { | ||
result[idx] = a[idx] * b[idx]; | ||
} | ||
} | ||
|
||
// Add Kernel: result = a + b | ||
static void add_kernel( | ||
int numel, const float* a, const float* b, float* result, | ||
const sycl::nd_item<1>& item) { | ||
int idx = item.get_global_id(0); | ||
if (idx < numel) { | ||
result[idx] = a[idx] + b[idx]; | ||
} | ||
} | ||
|
||
|
||
class MulAddKernelFunctor { | ||
public: | ||
MulAddKernelFunctor(int _numel, const float* _a, const float* _b, float _c, float* _result) | ||
: numel(_numel), a(_a), b(_b), c(_c), result(_result) {} | ||
|
||
void operator()(const sycl::nd_item<1>& item) const { | ||
muladd_kernel(numel, a, b, c, result, item); | ||
} | ||
|
||
private: | ||
int numel; | ||
const float* a; | ||
const float* b; | ||
float c; | ||
float* result; | ||
}; | ||
|
||
class MulKernelFunctor { | ||
public: | ||
MulKernelFunctor(int _numel, const float* _a, const float* _b, float* _result) | ||
: numel(_numel), a(_a), b(_b), result(_result) {} | ||
|
||
void operator()(const sycl::nd_item<1>& item) const { | ||
mul_kernel(numel, a, b, result, item); | ||
} | ||
|
||
private: | ||
int numel; | ||
const float* a; | ||
const float* b; | ||
float* result; | ||
}; | ||
|
||
class AddKernelFunctor { | ||
public: | ||
AddKernelFunctor(int _numel, const float* _a, const float* _b, float* _result) | ||
: numel(_numel), a(_a), b(_b), result(_result) {} | ||
|
||
void operator()(const sycl::nd_item<1>& item) const { | ||
add_kernel(numel, a, b, result, item); | ||
} | ||
|
||
private: | ||
int numel; | ||
const float* a; | ||
const float* b; | ||
float* result; | ||
}; | ||
|
||
|
||
at::Tensor mymuladd_xpu(const at::Tensor& a, const at::Tensor& b, double c) { | ||
TORCH_CHECK(a.sizes() == b.sizes(), "a and b must have the same shape"); | ||
TORCH_CHECK(a.dtype() == at::kFloat, "a must be a float tensor"); | ||
TORCH_CHECK(b.dtype() == at::kFloat, "b must be a float tensor"); | ||
TORCH_CHECK(a.device().is_xpu(), "a must be an XPU tensor"); | ||
TORCH_CHECK(b.device().is_xpu(), "b must be an XPU tensor"); | ||
|
||
at::Tensor a_contig = a.contiguous(); | ||
at::Tensor b_contig = b.contiguous(); | ||
at::Tensor result = at::empty_like(a_contig); | ||
|
||
const float* a_ptr = a_contig.data_ptr<float>(); | ||
const float* b_ptr = b_contig.data_ptr<float>(); | ||
float* res_ptr = result.data_ptr<float>(); | ||
int numel = a_contig.numel(); | ||
|
||
sycl::queue& queue = c10::xpu::getCurrentXPUStream().queue(); | ||
constexpr int threads = 256; | ||
int blocks = (numel + threads - 1) / threads; | ||
|
||
queue.submit([&](sycl::handler& cgh) { | ||
cgh.parallel_for<MulAddKernelFunctor>( | ||
sycl::nd_range<1>(blocks * threads, threads), | ||
MulAddKernelFunctor(numel, a_ptr, b_ptr, static_cast<float>(c), res_ptr) | ||
); | ||
}); | ||
return result; | ||
} | ||
|
||
at::Tensor mymul_xpu(const at::Tensor& a, const at::Tensor& b) { | ||
TORCH_CHECK(a.sizes() == b.sizes(), "a and b must have the same shape"); | ||
TORCH_CHECK(a.dtype() == at::kFloat, "a must be a float tensor"); | ||
TORCH_CHECK(b.dtype() == at::kFloat, "b must be a float tensor"); | ||
TORCH_CHECK(a.device().is_xpu(), "a must be an XPU tensor"); | ||
TORCH_CHECK(b.device().is_xpu(), "b must be an XPU tensor"); | ||
|
||
at::Tensor a_contig = a.contiguous(); | ||
at::Tensor b_contig = b.contiguous(); | ||
at::Tensor result = at::empty_like(a_contig); | ||
|
||
const float* a_ptr = a_contig.data_ptr<float>(); | ||
const float* b_ptr = b_contig.data_ptr<float>(); | ||
float* res_ptr = result.data_ptr<float>(); | ||
int numel = a_contig.numel(); | ||
|
||
sycl::queue& queue = c10::xpu::getCurrentXPUStream().queue(); | ||
constexpr int threads = 256; | ||
int blocks = (numel + threads - 1) / threads; | ||
|
||
queue.submit([&](sycl::handler& cgh) { | ||
cgh.parallel_for<MulKernelFunctor>( | ||
sycl::nd_range<1>(blocks * threads, threads), | ||
MulKernelFunctor(numel, a_ptr, b_ptr, res_ptr) | ||
); | ||
}); | ||
return result; | ||
} | ||
|
||
void myadd_out_xpu(const at::Tensor& a, const at::Tensor& b, at::Tensor& out) { | ||
TORCH_CHECK(a.sizes() == b.sizes(), "a and b must have the same shape"); | ||
TORCH_CHECK(b.sizes() == out.sizes(), "b and out must have the same shape"); | ||
TORCH_CHECK(a.dtype() == at::kFloat, "a must be a float tensor"); | ||
TORCH_CHECK(b.dtype() == at::kFloat, "b must be a float tensor"); | ||
TORCH_CHECK(out.is_contiguous(), "out must be contiguous"); | ||
TORCH_CHECK(a.device().is_xpu(), "a must be an XPU tensor"); | ||
TORCH_CHECK(b.device().is_xpu(), "b must be an XPU tensor"); | ||
TORCH_CHECK(out.device().is_xpu(), "out must be an XPU tensor"); | ||
|
||
at::Tensor a_contig = a.contiguous(); | ||
at::Tensor b_contig = b.contiguous(); | ||
|
||
const float* a_ptr = a_contig.data_ptr<float>(); | ||
const float* b_ptr = b_contig.data_ptr<float>(); | ||
float* out_ptr = out.data_ptr<float>(); | ||
int numel = a_contig.numel(); | ||
|
||
sycl::queue& queue = c10::xpu::getCurrentXPUStream().queue(); | ||
constexpr int threads = 256; | ||
int blocks = (numel + threads - 1) / threads; | ||
|
||
queue.submit([&](sycl::handler& cgh) { | ||
cgh.parallel_for<AddKernelFunctor>( | ||
sycl::nd_range<1>(blocks * threads, threads), | ||
AddKernelFunctor(numel, a_ptr, b_ptr, out_ptr) | ||
); | ||
}); | ||
} | ||
|
||
// ================================================== | ||
// Register Sycl Implementations to Torch Library | ||
// ================================================== | ||
|
||
TORCH_LIBRARY_IMPL(extension_cpp, XPU, m) { | ||
m.impl("mymuladd", mymuladd_xpu); | ||
m.impl("mymul", mymul_xpu); | ||
m.impl("myadd_out", myadd_out_xpu); | ||
} | ||
|
||
} // namespace extension_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
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.
@ZhaoqiongZ : change a title, it reads "C++/CUDA" only.
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.
done