-
Notifications
You must be signed in to change notification settings - Fork 25
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::adaptive_max_pool2d/backward and their variants #568
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3d86a49
Add aten::adaptive_max_pool2d/backward and their variants
fengyuan14 60bce0b
Add backward kernel
fengyuan14 3773d66
Merge branch 'main' into fy/adaptive_max_pool2d
fengyuan14 d05007a
Fix wrong hw idx when non-contiguous Tensor inputted
fengyuan14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
#include <ATen/ATen.h> | ||
#include <ATen/native/AdaptivePooling.h> | ||
#include <ATen/xpu/XPUNativeFunctions.h> | ||
|
||
#include <ATen/native/xpu/sycl/AdaptiveMaxPooling2dKernels.h> | ||
#include <comm/RegisterUtils.h> | ||
|
||
namespace at { | ||
|
||
void adaptive_max_pool2d_meta( | ||
const Tensor& input, | ||
IntArrayRef output_size, | ||
Tensor& output, | ||
Tensor& indices) { | ||
int ndim = input.ndimension(); | ||
TORCH_CHECK( | ||
ndim == 3 || ndim == 4, | ||
"adaptive_max_pool2d(): Expected 3D or 4D tensor, but got: ", | ||
input.sizes()); | ||
for (const auto i : c10::irange(1, ndim)) { | ||
TORCH_CHECK( | ||
input.size(i) > 0, | ||
"adaptive_max_pool2d(): Expected input to have non-zero size for non-batch dimensions, " | ||
"but input has sizes ", | ||
input.sizes(), | ||
" with dimension ", | ||
i, | ||
" being empty"); | ||
} | ||
|
||
TORCH_CHECK( | ||
output_size.size() == 2, | ||
"adaptive_max_pool2d(): internal error: output_size.size() must be 2"); | ||
|
||
int dimH = 1; | ||
int64_t sizeB = 1; | ||
int64_t sizeD = 0; | ||
|
||
if (input.ndimension() == 4) { | ||
sizeB = input.size(0); | ||
dimH++; | ||
} | ||
|
||
sizeD = input.size(dimH - 1); | ||
|
||
int64_t osizeH = output_size[0]; | ||
int64_t osizeW = output_size[1]; | ||
|
||
/* resize output */ | ||
if (input.ndimension() == 3) { | ||
if (output.defined()) { | ||
at::xpu::resize_out(output, {sizeD, osizeH, osizeW}, {}, input.options()); | ||
} else { | ||
output = | ||
at::xpu::create_out({sizeD, osizeH, osizeW}, {}, input.options()); | ||
} | ||
if (indices.defined()) { | ||
at::xpu::resize_out( | ||
indices, {sizeD, osizeH, osizeW}, {}, input.options()); | ||
} else { | ||
indices = at::xpu::create_out( | ||
{sizeD, osizeH, osizeW}, {}, input.options().dtype(kLong)); | ||
} | ||
} else { | ||
if (output.defined()) { | ||
at::xpu::resize_out( | ||
output, | ||
{sizeB, sizeD, osizeH, osizeW}, | ||
{}, | ||
input.options().memory_format(input.suggest_memory_format())); | ||
} else { | ||
output = at::xpu::create_out( | ||
{sizeB, sizeD, osizeH, osizeW}, | ||
{}, | ||
input.options().memory_format(input.suggest_memory_format())); | ||
} | ||
if (indices.defined()) { | ||
at::xpu::resize_out( | ||
indices, | ||
{sizeB, sizeD, osizeH, osizeW}, | ||
{}, | ||
input.options() | ||
.memory_format(input.suggest_memory_format()) | ||
.dtype(kLong)); | ||
} else { | ||
indices = at::xpu::create_out( | ||
{sizeB, sizeD, osizeH, osizeW}, | ||
{}, | ||
input.options() | ||
.memory_format(input.suggest_memory_format()) | ||
.dtype(kLong)); | ||
} | ||
} | ||
} | ||
|
||
std::tuple<Tensor, Tensor> XPUNativeFunctions::adaptive_max_pool2d( | ||
const Tensor& input, | ||
IntArrayRef output_size) { | ||
TensorArg input_arg{input, "input", 1}; | ||
checkAllSameGPU(__func__, {input_arg}); | ||
|
||
Tensor output, indices; | ||
adaptive_max_pool2d_meta(input, output_size, output, indices); | ||
|
||
if (input.numel() == 0) { | ||
return {output, indices}; | ||
} | ||
|
||
native::xpu::adaptive_max_pool2d_kernel(input, output_size, output, indices); | ||
return {output, indices}; | ||
} | ||
|
||
std::tuple<Tensor&, Tensor&> XPUNativeFunctions::adaptive_max_pool2d_out( | ||
const Tensor& input, | ||
IntArrayRef output_size, | ||
Tensor& output, | ||
Tensor& indices) { | ||
TensorArg output_arg{output, "output", 1}; | ||
TensorArg indices_arg{indices, "indices", 2}; | ||
TensorArg input_arg{input, "input", 3}; | ||
checkAllSameGPU(__func__, {output_arg, indices_arg, input_arg}); | ||
|
||
adaptive_max_pool2d_meta(input, output_size, output, indices); | ||
|
||
if (input.numel() == 0) { | ||
return {output, indices}; | ||
} | ||
|
||
native::xpu::adaptive_max_pool2d_kernel(input, output_size, output, indices); | ||
return {output, indices}; | ||
} | ||
|
||
void adaptive_max_pool2d_backward_meta( | ||
const Tensor& grad_output, | ||
const Tensor& input, | ||
const Tensor& indices, | ||
Tensor& grad_input) { | ||
int64_t ndim = grad_output.ndimension(); | ||
TORCH_CHECK( | ||
ndim == 3 || ndim == 4, | ||
"adaptive_max_pooling2d_backward(): Expected 3D or 4D grad_output, but got: ", | ||
grad_output.sizes()); | ||
|
||
at::native::adaptive_pool_empty_output_check( | ||
grad_output, "adaptive_max_pool2d_backward"); | ||
|
||
TORCH_CHECK( | ||
input.dtype() == grad_output.dtype(), | ||
"expected dtype ", | ||
input.dtype(), | ||
" for `grad_output` but got dtype ", | ||
grad_output.dtype()); | ||
|
||
if (grad_input.defined()) { | ||
at::xpu::resize_out( | ||
grad_input, | ||
input.sizes(), | ||
{}, | ||
input.options().memory_format(input.suggest_memory_format())); | ||
} else { | ||
grad_input = at::xpu::create_out( | ||
input.sizes(), | ||
{}, | ||
input.options().memory_format(input.suggest_memory_format())); | ||
} | ||
} | ||
|
||
Tensor XPUNativeFunctions::adaptive_max_pool2d_backward( | ||
const Tensor& grad_output, | ||
const Tensor& input, | ||
const Tensor& indices) { | ||
TensorArg grad_output_arg{grad_output, "grad_output", 1}; | ||
TensorArg input_arg{input, "input", 2}; | ||
TensorArg indices_arg{indices, "indices", 3}; | ||
|
||
checkAllSameGPU(__func__, {grad_output_arg, input_arg, indices_arg}); | ||
|
||
Tensor grad_input; | ||
adaptive_max_pool2d_backward_meta(grad_output, input, indices, grad_input); | ||
|
||
if (grad_output.numel() == 0) { | ||
return grad_input; | ||
} | ||
|
||
native::xpu::adaptive_max_pool2d_backward_kernel( | ||
grad_output, input, indices, grad_input); | ||
return grad_input; | ||
} | ||
|
||
Tensor& XPUNativeFunctions::adaptive_max_pool2d_backward_out( | ||
const Tensor& grad_output, | ||
const Tensor& input, | ||
const Tensor& indices, | ||
Tensor& grad_input) { | ||
TensorArg grad_input_arg{grad_input, "grad_input", 1}; | ||
TensorArg grad_output_arg{grad_output, "grad_output", 2}; | ||
TensorArg input_arg{input, "input", 3}; | ||
TensorArg indices_arg{indices, "indices", 4}; | ||
|
||
checkAllSameGPU( | ||
__func__, {grad_input_arg, grad_output_arg, input_arg, indices_arg}); | ||
|
||
adaptive_max_pool2d_backward_meta(grad_output, input, indices, grad_input); | ||
|
||
if (grad_output.numel() == 0) { | ||
return grad_input; | ||
} | ||
|
||
native::xpu::adaptive_max_pool2d_backward_kernel( | ||
grad_output, input, indices, grad_input); | ||
return grad_input; | ||
} | ||
|
||
} // namespace at |
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
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.
where is "alertNotDeterministic"
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.
It's the logic of kernel. So I put it in kernel level.