forked from ROCm/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompareKernels.cu
64 lines (52 loc) · 1.88 KB
/
CompareKernels.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#define TORCH_ASSERT_NO_OPERATORS
#include <ATen/Dispatch.h>
#include <ATen/native/BinaryOps.h>
#include <ATen/native/DispatchStub.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/cuda/Loops.cuh>
// NOTE: CUDA on Windows requires that the enclosing function
// of a __device__ lambda not have internal linkage.
namespace at { namespace native { namespace {
enum class OpType {GE, GT, LE, LT};
template<typename scalar_t>
struct CompareFunctor{
CompareFunctor(OpType op): op_(op) {};
OpType op_;
__device__ __forceinline__ bool operator() (scalar_t a, scalar_t b) const {
if (op_ == OpType::GE) {
return a >= b;
} else if (op_ == OpType::GT) {
return a > b;
} else if (op_ == OpType::LE) {
return a <= b;
} else { //LT
return a < b;
}
}
};
}
void ge_kernel_cuda(TensorIteratorBase& iter) {
AT_DISPATCH_ALL_TYPES_AND3(kHalf, kBFloat16, kBool, iter.common_dtype(), "ge_cuda", [&]() {
gpu_kernel_with_scalars(iter, CompareFunctor<scalar_t>(OpType::GE));
});
}
void gt_kernel_cuda(TensorIteratorBase& iter) {
AT_DISPATCH_ALL_TYPES_AND3(kHalf, kBFloat16, kBool, iter.common_dtype(), "gt_cuda", [&]() {
gpu_kernel_with_scalars(iter, CompareFunctor<scalar_t>(OpType::GT));
});
}
void le_kernel_cuda(TensorIteratorBase& iter) {
AT_DISPATCH_ALL_TYPES_AND3(kHalf, kBFloat16, kBool, iter.common_dtype(), "le_cuda", [&]() {
gpu_kernel_with_scalars(iter, CompareFunctor<scalar_t>(OpType::LE));
});
}
void lt_kernel_cuda(TensorIteratorBase& iter) {
AT_DISPATCH_ALL_TYPES_AND3(kHalf, kBFloat16, kBool, iter.common_dtype(), "lt_cuda", [&]() {
gpu_kernel_with_scalars(iter, CompareFunctor<scalar_t>(OpType::LT));
});
}
REGISTER_DISPATCH(ge_stub, &ge_kernel_cuda);
REGISTER_DISPATCH(gt_stub, >_kernel_cuda);
REGISTER_DISPATCH(le_stub, &le_kernel_cuda);
REGISTER_DISPATCH(lt_stub, <_kernel_cuda);
}} // namespace at::native