forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompareKernels.cu
103 lines (86 loc) · 2.97 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#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{
constexpr 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;
}
}
};
// Reflects the comparison operator, so reflect(op)(a, b) == op(b, a)
OpType reflect(OpType x) {
switch (x) {
case OpType::GE: return OpType::LE;
case OpType::GT: return OpType::LT;
case OpType::LE: return OpType::GE;
case OpType::LT: return OpType::GT;
}
TORCH_INTERNAL_ASSERT(false, "Invalid OpType");
}
} // namespace (anonymous)
template <typename scalar_t>
void compare_scalar_kernel(TensorIteratorBase &iter, OpType op, scalar_t rhs) {
CompareFunctor<scalar_t> f(op);
gpu_kernel(iter, [=] GPU_LAMBDA (scalar_t lhs) -> bool {
return f(lhs, rhs);
});
}
template <typename scalar_t>
void compare_kernel_impl(TensorIteratorBase &iter, OpType op) {
// If either input is a cpu scalar, perform the equivalent comparison
// where the scalar is on the right hand side. This saves us from
// generating two otherwise identical kernels with mirrored
// arguments.
if (iter.is_cpu_scalar(1)) {
const scalar_t lhs = iter.scalar_value<scalar_t>(1);
iter.remove_operand(1);
const DeviceGuard device_guard(iter.device(1));
compare_scalar_kernel(iter, reflect(op), lhs);
} else if (iter.is_cpu_scalar(2)) {
const scalar_t rhs = iter.scalar_value<scalar_t>(2);
iter.remove_operand(2);
compare_scalar_kernel(iter, op, rhs);
} else {
CompareFunctor<scalar_t> f(op);
gpu_kernel(iter, f);
}
}
C10_NOINLINE void compare_kernel_with_scalars(TensorIteratorBase &iter, OpType op) {
AT_DISPATCH_ALL_TYPES_AND3(kHalf, kBFloat16, kBool, iter.common_dtype(), "compare_cuda", [&]() {
compare_kernel_impl<scalar_t>(iter, op);
});
}
void ge_kernel_cuda(TensorIteratorBase& iter) {
compare_kernel_with_scalars(iter, OpType::GE);
}
void gt_kernel_cuda(TensorIteratorBase& iter) {
compare_kernel_with_scalars(iter, OpType::GT);
}
void le_kernel_cuda(TensorIteratorBase& iter) {
compare_kernel_with_scalars(iter, OpType::LE);
}
void lt_kernel_cuda(TensorIteratorBase& iter) {
compare_kernel_with_scalars(iter, 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