forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Threshold.cu
75 lines (62 loc) · 1.57 KB
/
Threshold.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
#include "THCUNN.h"
#include "TH/THHalf.h"
#include "THCHalfAutoNumerics.cuh"
#include <THC/THCApply.cuh>
template <typename T>
struct ThresholdUpdateOutput
{
const T threshold_;
const T val_;
ThresholdUpdateOutput(T threshold, T val)
: threshold_(threshold)
, val_(val)
{}
__device__ __forceinline__ void operator()(T *out, T *in)
{
T x = *in;
*out = (x <= threshold_) ? val_ : x; // this order propagates NaN
}
};
// in-place variant
template <typename T>
struct ThresholdUpdateOutputIP
{
const T threshold_;
const T val_;
ThresholdUpdateOutputIP(T threshold, T val)
: threshold_(threshold)
, val_(val)
{}
__device__ __forceinline__ void operator()(T *x)
{
*x = (*x <= threshold_) ? val_ : *x; // this order propagates NaN
}
};
template <typename T>
struct ThresholdUpdateGradInput
{
const T threshold_;
ThresholdUpdateGradInput(T threshold)
: threshold_(threshold)
{}
__device__ __forceinline__ void operator()(
T *gradInput, T *input, T *gradOutput) const
{
*gradInput = (*input <= threshold_) ? ScalarConvert<int, T>::to(0) : *gradOutput; // this order propagates NaN
}
};
template <typename T>
struct ThresholdUpdateGradInputIP
{
const T threshold_;
ThresholdUpdateGradInputIP(T threshold)
: threshold_(threshold)
{}
__device__ __forceinline__ void operator()(
T *gradOutput, T *input) const
{
*gradOutput = (*input <= threshold_) ? ScalarConvert<int, T>::to(0) : *gradOutput; // this order propagates NaN
}
};
#include "generic/Threshold.cu"
#include "THCGenerateFloatTypes.h"