Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge branch 'cudaconv'
- Loading branch information
Showing
8 changed files
with
700 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
static int cunn_SpatialConvolutionMap_updateOutput(lua_State *L) | ||
{ | ||
THCudaTensor *input = (THCudaTensor*) | ||
luaT_checkudata(L, 2, torch_CudaTensor_id); | ||
int dW = luaT_getfieldcheckint(L, 1, "dW"); | ||
int dH = luaT_getfieldcheckint(L, 1, "dH"); | ||
THCudaTensor *weight = (THCudaTensor*) | ||
luaT_getfieldcheckudata(L, 1, "weight", torch_CudaTensor_id); | ||
THCudaTensor *bias = (THCudaTensor*) | ||
luaT_getfieldcheckudata(L, 1, "bias", torch_CudaTensor_id); | ||
THCudaTensor *output = (THCudaTensor*) | ||
luaT_getfieldcheckudata(L, 1, "output", torch_CudaTensor_id); | ||
THCudaTensor *connTableRev = (THCudaTensor*) | ||
luaT_getfieldcheckudata(L, 1, "connTableRev", torch_CudaTensor_id); | ||
luaL_argcheck(L, connTableRev->nDimension == 3, 2, | ||
"Reverse table not generated (is table fixed fanin?)"); | ||
luaL_argcheck(L, input->nDimension == 3, 2, "3D tensor is expected"); | ||
|
||
int dimw = 2; | ||
int dimh = 1; | ||
|
||
// long nOutputPlane = weight->size[0]; | ||
int nOutputPlane = luaT_getfieldcheckint(L, 1, "nOutputPlane"); | ||
long kW = weight->size[2]; | ||
long kH = weight->size[1]; | ||
long inputWidth = input->size[dimw]; | ||
long inputHeight = input->size[dimh]; | ||
long outputWidth = (inputWidth - kW) / dW + 1; | ||
long outputHeight = (inputHeight - kH) / dH + 1; | ||
long fanin = weight->size[0] / nOutputPlane; | ||
THCudaTensor_resize3d(output, nOutputPlane, outputHeight, outputWidth); | ||
/* add bias first */ | ||
long k; | ||
THCudaTensor *outputPlane = THCudaTensor_new(); | ||
for(k=0; k<nOutputPlane; k++) { | ||
THCudaTensor_select(outputPlane, output, 0, k); | ||
THCudaTensor_fill(outputPlane, THCudaTensor_get1d(bias, k)); | ||
} | ||
THCudaTensor_free(outputPlane); | ||
/* do convolutions */ | ||
THCudaTensor_conv2Dmap(output, input, weight, dW, dH, | ||
connTableRev,fanin); | ||
return 1; | ||
} | ||
|
||
static const struct luaL_Reg cunn_SpatialConvolutionMap__ [] = { | ||
{"SpatialConvolutionMap_updateOutput", cunn_SpatialConvolutionMap_updateOutput}, | ||
|
||
{NULL, NULL} | ||
}; | ||
|
||
static void cunn_SpatialConvolutionMap_init(lua_State *L) | ||
{ | ||
luaT_pushmetaclass(L, torch_CudaTensor_id); | ||
luaT_registeratname(L, cunn_SpatialConvolutionMap__, "nn"); | ||
lua_pop(L,1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
struct sqrtupdateOutput_functor | ||
{ | ||
const double bias; | ||
|
||
sqrtupdateOutput_functor(double bias_) : bias(bias_) {} | ||
|
||
__host__ __device__ float operator()(const float& input) const | ||
{ | ||
return sqrt(input+bias); | ||
} | ||
}; | ||
|
||
static int cunn_Sqrt_updateOutput(lua_State *L) | ||
{ | ||
double bias = luaT_getfieldchecknumber(L,1,"eps"); | ||
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, torch_CudaTensor_id); | ||
THCudaTensor *output = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "output", torch_CudaTensor_id); | ||
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, sqrtupdateOutput_functor(bias)); | ||
|
||
THCudaTensor_free(input); | ||
return 1; | ||
} | ||
|
||
struct sqrtupdateGradInput_functor | ||
{ | ||
const double bias; | ||
|
||
sqrtupdateGradInput_functor(double bias_) : bias(bias_) {} | ||
|
||
__host__ __device__ float operator()(const float& output, const float& gradOutput) const | ||
{ | ||
return 0.5 * gradOutput / output; | ||
} | ||
}; | ||
|
||
static int cunn_Sqrt_updateGradInput(lua_State *L) | ||
{ | ||
double bias = luaT_getfieldchecknumber(L,1,"eps"); | ||
THCudaTensor *output = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "output", torch_CudaTensor_id); | ||
THCudaTensor *gradOutput = (THCudaTensor*)luaT_checkudata(L, 3, torch_CudaTensor_id); | ||
THCudaTensor *gradInput = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "gradInput", torch_CudaTensor_id); | ||
long size = THCudaTensor_nElement(output); | ||
|
||
gradOutput = THCudaTensor_newContiguous(gradOutput); | ||
THCudaTensor_resizeAs(gradInput, output); | ||
|
||
thrust::device_ptr<float> output_data(THCudaTensor_data(output)); | ||
thrust::device_ptr<float> gradOutput_data(THCudaTensor_data(gradOutput)); | ||
thrust::device_ptr<float> gradInput_data(THCudaTensor_data(gradInput)); | ||
thrust::transform(output_data, output_data+size, gradOutput_data, gradInput_data, sqrtupdateGradInput_functor(bias)); | ||
|
||
THCudaTensor_free(gradOutput); | ||
return 1; | ||
} | ||
|
||
static const struct luaL_Reg cunn_Sqrt__ [] = { | ||
{"Sqrt_updateOutput", cunn_Sqrt_updateOutput}, | ||
{"Sqrt_updateGradInput", cunn_Sqrt_updateGradInput}, | ||
{NULL, NULL} | ||
}; | ||
|
||
static void cunn_Sqrt_init(lua_State *L) | ||
{ | ||
luaT_pushmetaclass(L, torch_CudaTensor_id); | ||
luaT_registeratname(L, cunn_Sqrt__, "nn"); | ||
lua_pop(L,1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
struct squareupdateOutput_functor | ||
{ | ||
__host__ __device__ float operator()(const float& input) const | ||
{ | ||
return input*input; | ||
} | ||
}; | ||
|
||
static int cunn_Square_updateOutput(lua_State *L) | ||
{ | ||
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, torch_CudaTensor_id); | ||
THCudaTensor *output = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "output", torch_CudaTensor_id); | ||
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, squareupdateOutput_functor()); | ||
|
||
THCudaTensor_free(input); | ||
return 1; | ||
} | ||
|
||
struct squareupdateGradInput_functor | ||
{ | ||
__host__ __device__ float operator()(const float& input, const float& gradOutput) const | ||
{ | ||
return 2.0 * gradOutput * input; | ||
} | ||
}; | ||
|
||
static int cunn_Square_updateGradInput(lua_State *L) | ||
{ | ||
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 3, torch_CudaTensor_id); | ||
THCudaTensor *gradOutput = (THCudaTensor*)luaT_checkudata(L, 3, torch_CudaTensor_id); | ||
THCudaTensor *gradInput = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "gradInput", torch_CudaTensor_id); | ||
long size = THCudaTensor_nElement(input); | ||
|
||
gradOutput = THCudaTensor_newContiguous(gradOutput); | ||
THCudaTensor_resizeAs(gradInput, input); | ||
|
||
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, squareupdateGradInput_functor()); | ||
|
||
THCudaTensor_free(gradOutput); | ||
return 1; | ||
} | ||
|
||
static const struct luaL_Reg cunn_Square__ [] = { | ||
{"Square_updateOutput", cunn_Square_updateOutput}, | ||
{"Square_updateGradInput", cunn_Square_updateGradInput}, | ||
{NULL, NULL} | ||
}; | ||
|
||
static void cunn_Square_init(lua_State *L) | ||
{ | ||
luaT_pushmetaclass(L, torch_CudaTensor_id); | ||
luaT_registeratname(L, cunn_Square__, "nn"); | ||
lua_pop(L,1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters