forked from torch/cunn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreshold.cu
83 lines (66 loc) · 2.87 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
76
77
78
79
80
81
82
struct thresholdupdateOutput_functor
{
const double threshold;
const double val;
thresholdupdateOutput_functor(double threshold_, double val_) : threshold(threshold_), val(val_) {}
__host__ __device__ float operator()(const float& input) const
{
return (input > threshold) ? input : val;
}
};
static int cunn_Threshold_updateOutput(lua_State *L)
{
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *output = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "output", "torch.CudaTensor");
double val = luaT_getfieldchecknumber(L, 1, "val");
double threshold = luaT_getfieldchecknumber(L, 1, "threshold");
long size = THCudaTensor_nElement(input);
input = THCudaTensor_newContiguous(input);
THCudaTensor_resizeAs(output, input);
thrust::device_ptr<float> output_data(THCudaTensor_data(output));
thrust::device_ptr<float> input_data(THCudaTensor_data(input));
thrust::transform(input_data, input_data+size, output_data,
thresholdupdateOutput_functor(threshold, val));
THCudaTensor_free(input);
return 1;
}
struct thresholdupdateGradInput_functor
{
const double threshold;
const double val;
thresholdupdateGradInput_functor(double threshold_, double val_) : threshold(threshold_), val(val_) {}
__host__ __device__ float operator()(const float& input, const float& gradOutput) const
{
return (input > threshold) ? gradOutput : 0;
}
};
static int cunn_Threshold_updateGradInput(lua_State *L)
{
THCudaTensor *output = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "output", "torch.CudaTensor");
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *gradOutput = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
THCudaTensor *gradInput = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "gradInput", "torch.CudaTensor");
double val = luaT_getfieldchecknumber(L, 1, "val");
double threshold = luaT_getfieldchecknumber(L, 1, "threshold");
long size = THCudaTensor_nElement(output);
gradOutput = THCudaTensor_newContiguous(gradOutput);
THCudaTensor_resizeAs(gradInput, output);
thrust::device_ptr<float> input_data(THCudaTensor_data(input));
thrust::device_ptr<float> gradOutput_data(THCudaTensor_data(gradOutput));
thrust::device_ptr<float> gradInput_data(THCudaTensor_data(gradInput));
thrust::transform(input_data, input_data+size, gradOutput_data, gradInput_data,
thresholdupdateGradInput_functor(threshold, val));
THCudaTensor_free(gradOutput);
return 1;
}
static const struct luaL_Reg cunn_Threshold__ [] = {
{"Threshold_updateOutput", cunn_Threshold_updateOutput},
{"Threshold_updateGradInput", cunn_Threshold_updateGradInput},
{NULL, NULL}
};
static void cunn_Threshold_init(lua_State *L)
{
luaT_pushmetatable(L, "torch.CudaTensor");
luaT_registeratname(L, cunn_Threshold__, "nn");
lua_pop(L,1);
}