Repository navigation
Expand file tree
/
Copy pathMax.c
More file actions
108 lines (90 loc) · 3.26 KB
/
Copy pathMax.c
File metadata and controls
108 lines (90 loc) · 3.26 KB
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
104
105
106
107
108
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/Max.c"
#else
static int nn_(Max_updateOutput)(lua_State *L)
{
THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
int dimension = luaT_getfieldcheckint(L, 1, "dimension")-1;
THTensor *indices = luaT_getfieldcheckudata(L, 1, "indices", torch_Tensor);
THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_Tensor);
THLongStorage *dim;
long i;
luaL_argcheck(L, dimension >= 0 && dimension < input->nDimension, 2, "dimension out of range");
dim = THLongStorage_newWithSize(input->nDimension);
for(i = 0; i < input->nDimension; i++)
dim->data[i] = input->size[i];
dim->data[dimension] = 1;
THTensor_(resize)(output, dim, NULL);
THTensor_(resize)(indices, dim, NULL);
THLongStorage_free(dim);
TH_TENSOR_DIM_APPLY3(real, output, real, input, real, indices, dimension,
long theIndex = 0;
real theMax = input_data[0];
for(i = 1; i < input_size; i++)
{
if(input_data[i*input_stride] > theMax)
{
theIndex = i;
theMax = input_data[i*input_stride];
}
}
*indices_data = theIndex+1;
*output_data = theMax;)
if(output->nDimension > 1)
THTensor_(select)(output, NULL, dimension, 0);
return 1;
}
static int nn_(Max_updateGradInput)(lua_State *L)
{
THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
THTensor *gradOutput = luaT_checkudata(L, 3, torch_Tensor);
THTensor *indices = luaT_getfieldcheckudata(L, 1, "indices", torch_Tensor);
int dimension = luaT_getfieldcheckint(L, 1, "dimension")-1;
THTensor *gradInput = luaT_getfieldcheckudata(L, 1, "gradInput", torch_Tensor);
THTensor *gradOutputPlusOneDim;
THLongStorage *dim, *str;
int i, j;
THTensor_(resizeAs)(gradInput, input);
THTensor_(zero)(gradInput);
if(input->nDimension > 1)
{
dim = THLongStorage_newWithSize(gradOutput->nDimension+1);
str = THLongStorage_newWithSize(gradOutput->nDimension+1);
for(i = 0, j = 0; j < gradOutput->nDimension+1; j++)
{
if(j == dimension)
{
dim->data[j] = input->size[dimension];
str->data[j] = 0;
continue;
}
dim->data[j] = gradOutput->size[i];
str->data[j] = gradOutput->stride[i];
i++;
}
gradOutputPlusOneDim = THTensor_(newWithStorage)(gradOutput->storage, gradOutput->storageOffset, dim, str);
THLongStorage_free(dim);
THLongStorage_free(str);
}
else
{
THTensor_(retain)(gradOutput);
gradOutputPlusOneDim = gradOutput;
}
TH_TENSOR_DIM_APPLY3(real, gradInput, real, gradOutputPlusOneDim, real, indices, dimension,
gradInput_data[ ((long)(*indices_data)-1)*gradInput_stride ] = *gradOutputPlusOneDim_data;)
THTensor_(free)(gradOutputPlusOneDim);
return 1;
}
static const struct luaL_Reg nn_(Max__) [] = {
{"Max_updateOutput", nn_(Max_updateOutput)},
{"Max_updateGradInput", nn_(Max_updateGradInput)},
{NULL, NULL}
};
static void nn_(Max_init)(lua_State *L)
{
luaT_pushmetatable(L, torch_Tensor);
luaT_registeratname(L, nn_(Max__), "nn");
lua_pop(L,1);
}
#endif