-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add aten::topk and its variants (#547)
Task list: - [x] topk - [x] topk.values
- Loading branch information
Showing
8 changed files
with
741 additions
and
1 deletion.
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,97 @@ | ||
#include <ATen/ATen.h> | ||
#include <ATen/core/Tensor.h> | ||
#include <ATen/native/TensorIterator.h> | ||
#include <ATen/native/xpu/sycl/TensorTopKKernel.h> | ||
#include <ATen/xpu/XPUNativeFunctions.h> | ||
#include <comm/RegisterUtils.h> | ||
|
||
namespace at { | ||
|
||
void topk_meta( | ||
const Tensor& self, | ||
int64_t k, | ||
int64_t dim_, | ||
bool largest, | ||
bool sorted, | ||
Tensor& values, | ||
Tensor& indices) { | ||
int64_t dim = maybe_wrap_dim(dim_, self.dim(), /*wrap_scalar=*/true); | ||
TORCH_CHECK( | ||
k >= 0 && k <= (self.dim() > 0 ? self.size(dim) : 1), | ||
"selected index k out of range"); | ||
int64_t sliceSize = self.dim() == 0 ? 1 : self.size(dim); | ||
TORCH_CHECK(k >= 0 && k <= sliceSize, "k not in range for dimension"); | ||
|
||
// Build the output size, which is the dim being selected set to | ||
// size k | ||
DimVector topKSize(self.sizes().vec()); | ||
if (!topKSize.empty()) { | ||
topKSize[dim] = k; | ||
} | ||
|
||
if (values.defined()) { | ||
at::xpu::resize_out(values, topKSize, {}, self.options()); | ||
} else { | ||
values = at::xpu::create_out(topKSize, {}, self.options()); | ||
} | ||
|
||
if (indices.defined()) { | ||
at::xpu::resize_out(indices, topKSize, {}, self.options().dtype(at::kLong)); | ||
} else { | ||
indices = | ||
at::xpu::create_out(topKSize, {}, self.options().dtype(at::kLong)); | ||
} | ||
} | ||
|
||
void topk_out_impl( | ||
const Tensor& self, | ||
int64_t k, | ||
int64_t dim_, | ||
bool largest, | ||
bool sorted, | ||
Tensor& values, | ||
Tensor& indices) { | ||
int64_t dim = maybe_wrap_dim(dim_, self.dim(), /*wrap_scalar=*/true); | ||
TORCH_CHECK( | ||
k >= 0 && k <= (self.dim() > 0 ? self.size(dim) : 1), | ||
"selected index k out of range"); | ||
|
||
// If k is 0 the result is an empty tensor, so we don't need to launch a kernel. | ||
if (k == 0) { | ||
return; | ||
} | ||
|
||
if (self.dim() == 0 && self.numel() == 1) { | ||
values.copy_(self); | ||
indices.zero_(); | ||
} else { | ||
native::xpu::topk_kernel(self, k, dim, largest, sorted, values, indices); | ||
} | ||
} | ||
|
||
std::tuple<Tensor, Tensor> XPUNativeFunctions::topk( | ||
const Tensor& self, | ||
int64_t k, | ||
int64_t dim, | ||
bool largest, | ||
bool sorted) { | ||
Tensor values, indices; | ||
topk_meta(self, k, dim, largest, sorted, values, indices); | ||
topk_out_impl(self, k, dim, largest, sorted, values, indices); | ||
return std::tuple<Tensor, Tensor>(values, indices); | ||
} | ||
|
||
std::tuple<Tensor&, Tensor&> XPUNativeFunctions::topk_out( | ||
const Tensor& self, | ||
int64_t k, | ||
int64_t dim, | ||
bool largest, | ||
bool sorted, | ||
Tensor& values, | ||
Tensor& indices) { | ||
topk_meta(self, k, dim, largest, sorted, values, indices); | ||
topk_out_impl(self, k, dim, largest, sorted, values, indices); | ||
return std::forward_as_tuple(values, indices); | ||
} | ||
|
||
} // 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
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.