Skip to content
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 aten::addcdiv and its variants #486

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/ATen/native/xpu/PointwiseOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,63 @@

namespace at {

TensorIterator addcdiv_meta(
const Tensor& self,
const Tensor& tensor1,
const Tensor& tensor2,
const Scalar& value,
Tensor& out) {
if (isIntegralType(tensor1.scalar_type(), /*includeBool=*/true) &&
isIntegralType(tensor2.scalar_type(), /*includeBool=*/true)) {
TORCH_CHECK(
false,
"Integer division with addcdiv is no longer supported, and in a future ",
"release addcdiv will perform a true division of tensor1 and tensor2. ",
"The historic addcdiv behavior can be implemented as ",
"(input + value * torch.trunc(tensor1 / tensor2)).to(input.dtype) ",
"for integer inputs and as ",
"(input + value * tensor1 / tensor2) for float inputs. ",
"The future addcdiv behavior is just the latter implementation: ",
"(input + value * tensor1 / tensor2), for all dtypes.");
}

TensorIterator iter;
iter.build_ternary_op(out, self, tensor1, tensor2);
return iter;
}

Tensor& XPUNativeFunctions::addcdiv_out(
const Tensor& self,
const Tensor& tensor1,
const Tensor& tensor2,
const Scalar& value,
Tensor& out) {
auto iter = addcdiv_meta(self, tensor1, tensor2, value, out);
native::xpu::addcdiv_kernel(iter, value);
return out;
}

Tensor XPUNativeFunctions::addcdiv(
const Tensor& self,
const Tensor& tensor1,
const Tensor& tensor2,
const Scalar& value) {
Tensor out;
auto iter = addcdiv_meta(self, tensor1, tensor2, value, out);
native::xpu::addcdiv_kernel(iter, value);
return iter.output();
}

Tensor& XPUNativeFunctions::addcdiv_(
Tensor& self,
const Tensor& tensor1,
const Tensor& tensor2,
const Scalar& value) {
auto iter = addcdiv_meta(self, tensor1, tensor2, value, self);
native::xpu::addcdiv_kernel(iter, value);
return self;
}

TensorIterator addcmul_meta(
const Tensor& self,
const Tensor& tensor1,
Expand Down
1 change: 0 additions & 1 deletion src/ATen/native/xpu/XPUFallback.template
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ TORCH_LIBRARY_IMPL(aten, XPU, m) {
"adaptive_max_pool2d.out",
"adaptive_max_pool3d_backward.grad_input",
"adaptive_max_pool3d.out",
"addcdiv.out",
"aminmax.out",
"angle",
"argmin.out",
Expand Down
103 changes: 85 additions & 18 deletions src/ATen/native/xpu/sycl/PointwiseOpsKernels.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,105 @@
#include <ATen/ATen.h>
#include <ATen/AccumulateType.h>
#include <ATen/Dispatch.h>
#include <ATen/OpMathType.h>
#include <ATen/native/TensorIterator.h>

#include <ATen/native/xpu/sycl/Loops.h>

namespace at::native::xpu {

template <typename scalar_t>
struct AddcmulKernelFunctor {
using opmath_t = at::opmath_type<scalar_t>;
struct AddcmulFunctor {
using accscalar_t = at::acc_type<scalar_t, true>;
scalar_t operator()(scalar_t a, scalar_t b, scalar_t c) const {
return static_cast<opmath_t>(a) +
alpha_ * static_cast<opmath_t>(b) * static_cast<opmath_t>(c);
return static_cast<accscalar_t>(a) +
alpha_ * static_cast<accscalar_t>(b) * static_cast<accscalar_t>(c);
}

AddcmulKernelFunctor(opmath_t alpha) : alpha_(alpha) {}
AddcmulFunctor(accscalar_t alpha) : alpha_(alpha) {}

private:
opmath_t alpha_;
accscalar_t alpha_;
};

template <typename scalar_t>
struct AddcmulComplexFunctor {
scalar_t operator()(scalar_t a, scalar_t b, scalar_t c) const {
return a + alpha_ * b * c;
}

AddcmulComplexFunctor(scalar_t alpha) : alpha_(alpha) {}

private:
scalar_t alpha_;
};

void addcmul_kernel(TensorIterator& iter, Scalar value) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND2(
at::ScalarType::Half,
at::ScalarType::BFloat16,
iter.dtype(),
"addcmul_xpu",
[&]() {
using opmath_t = at::opmath_type<scalar_t>;
auto alpha = value.to<opmath_t>();
AddcmulKernelFunctor<scalar_t> f(alpha);
gpu_kernel(iter, f);
});
auto dtype = iter.common_dtype();
if (at::isComplexType(dtype)) {
AT_DISPATCH_COMPLEX_TYPES(dtype, "addcmul_xpu", [&]() {
auto alpha = value.to<scalar_t>();
gpu_kernel(iter, AddcmulComplexFunctor<scalar_t>(alpha));
});
} else {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND2(
at::ScalarType::Half,
at::ScalarType::BFloat16,
iter.dtype(),
"addcmul_xpu",
[&]() {
using accscalar_t = at::acc_type<scalar_t, true>;
auto alpha = value.to<accscalar_t>();
gpu_kernel(iter, AddcmulFunctor<scalar_t>(alpha));
});
}
}

template <typename scalar_t>
struct AddcdivFunctor {
using accscalar_t = at::acc_type<scalar_t, true>;
scalar_t operator()(scalar_t a, scalar_t b, scalar_t c) const {
return a + alpha_ * (b / static_cast<accscalar_t>(c));
}

AddcdivFunctor(accscalar_t alpha) : alpha_(alpha) {}

private:
accscalar_t alpha_;
};

template <typename scalar_t>
struct AddcdivComplexFunctor {
scalar_t operator()(scalar_t a, scalar_t b, scalar_t c) const {
return a + alpha_ * (b / c);
}

AddcdivComplexFunctor(scalar_t alpha) : alpha_(alpha) {}

private:
scalar_t alpha_;
};

void addcdiv_kernel(TensorIterator& iter, Scalar value) {
auto dtype = iter.common_dtype();
if (at::isComplexType(dtype)) {
AT_DISPATCH_COMPLEX_TYPES(dtype, "addcdiv_xpu", [&]() {
auto alpha = value.to<scalar_t>();
AddcdivComplexFunctor<scalar_t> f(alpha);
gpu_kernel(iter, f);
});
} else {
AT_DISPATCH_ALL_TYPES_AND2(
at::ScalarType::Half,
at::ScalarType::BFloat16,
iter.dtype(),
"addcdiv_xpu",
[&]() {
using accscalar_t = at::acc_type<scalar_t, true>;
auto alpha = value.to<accscalar_t>();
AddcdivFunctor<scalar_t> f(alpha);
gpu_kernel(iter, f);
});
}
}

template <typename scalar_t>
Expand Down
2 changes: 2 additions & 0 deletions src/ATen/native/xpu/sycl/PointwiseOpsKernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace at::native::xpu {

void addcmul_kernel(TensorIterator& iter, Scalar value);

void addcdiv_kernel(TensorIterator& iter, Scalar value);

void mse_backward_kernel(TensorIterator& iter, const Scalar& value);

} // namespace at::native::xpu
1 change: 1 addition & 0 deletions test/xpu/xpu_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"bitwise_or",
"bitwise_xor",
"addcmul",
"addcdiv",
"clamp",
"clamp_max",
"clamp_min",
Expand Down
3 changes: 3 additions & 0 deletions yaml/xpu_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ supported:
- avg_pool2d.out
- avg_pool2d_backward
- avg_pool2d_backward.grad_input
- addcdiv.out
- addcdiv
- addcdiv_
- addcmul.out
- addcmul
- addcmul_
Expand Down
Loading