From 494a70366332bca74a859e017c45b84c1c9f5fad Mon Sep 17 00:00:00 2001 From: Boris Fomitchev Date: Mon, 16 Jan 2017 03:17:02 -0800 Subject: [PATCH 1/3] Reduced type set math --- init.lua | 3 +-- lib/THCUNN/THCGenerateFloatTypes.h | 35 ++++++++++++++++++++++++++++++ test.lua | 15 +++++++------ 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 lib/THCUNN/THCGenerateFloatTypes.h diff --git a/init.lua b/init.lua index fd1e319b..6a1ceb6b 100644 --- a/init.lua +++ b/init.lua @@ -9,5 +9,4 @@ require('cunn.DataParallelTable') nn.Module._flattenTensorBuffer['torch.CudaTensor'] = torch.FloatTensor.new nn.Module._flattenTensorBuffer['torch.CudaDoubleTensor'] = torch.DoubleTensor.new --- FIXME: change this to torch.HalfTensor when available -nn.Module._flattenTensorBuffer['torch.CudaHalfTensor'] = torch.FloatTensor.new +nn.Module._flattenTensorBuffer['torch.CudaHalfTensor'] = torch.HalfTensor.new diff --git a/lib/THCUNN/THCGenerateFloatTypes.h b/lib/THCUNN/THCGenerateFloatTypes.h new file mode 100644 index 00000000..a197eed7 --- /dev/null +++ b/lib/THCUNN/THCGenerateFloatTypes.h @@ -0,0 +1,35 @@ +#ifndef THC_GENERIC_FILE +#error "You must define THC_GENERIC_FILE before including THGenerateFloatTypes.h" +#endif + +#define THCGenerateFloatTypes + +#define THCTypeIdxByte 1 +#define THCTypeIdxChar 2 +#define THCTypeIdxShort 3 +#define THCTypeIdxInt 4 +#define THCTypeIdxLong 5 +#define THCTypeIdxFloat 6 +#define THCTypeIdxDouble 7 +#define THCTypeIdxHalf 8 +#define THCTypeIdx_(T) TH_CONCAT_2(THCTypeIdx,T) + +# ifndef THC_MIN_MATH +# include "THCGenerateHalfType.h" +# include "THCGenerateDoubleType.h" +# endif + +#include "THCGenerateFloatType.h" + +#undef THCTypeIdxByte +#undef THCTypeIdxChar +#undef THCTypeIdxShort +#undef THCTypeIdxInt +#undef THCTypeIdxLong +#undef THCTypeIdxFloat +#undef THCTypeIdxDouble +#undef THCTypeIdxHalf +#undef THCTypeIdx_ + +#undef THCGenerateFloatTypes +#undef THC_GENERIC_FILE diff --git a/test.lua b/test.lua index c3ed9bb2..4a137232 100644 --- a/test.lua +++ b/test.lua @@ -11,20 +11,21 @@ local THC = ffi.os == 'Windows' and ffi.load('THC') or ffi.C --e.g.: th -lcunn -e "nn.testcuda{'Sigmoid_forward'}" local typenames = { - 'torch.CudaTensor', - 'torch.CudaDoubleTensor', + 'torch.CudaTensor' } local t2cpu = { - ['torch.CudaTensor'] = 'torch.FloatTensor', - ['torch.CudaDoubleTensor'] = 'torch.DoubleTensor', - + ['torch.CudaTensor'] = 'torch.FloatTensor' } local function checkHalf() - if cutorch.hasHalf then + if not cutorch.minMath then + if cutorch.hasHalf then table.insert(typenames, 'torch.CudaHalfTensor') - t2cpu['torch.CudaHalfTensor'] = 'torch.FloatTensor' + t2cpu['torch.CudaHalfTensor'] = 'torch.HalfTensor' + end + table.insert(typenames, 'torch.CudaDoubleTensor') + t2cpu['torch.CudaDoubleTensor'] = 'torch.DoubleTensor' end end From 8a8b7adb6bba04763f46e1bc89b30a8dfd41d428 Mon Sep 17 00:00:00 2001 From: Boris Fomitchev Date: Tue, 17 Jan 2017 01:07:19 -0800 Subject: [PATCH 2/3] Built on Android --- CMakeLists.txt | 4 ++++ lib/THCUNN/SparseLinear.cu | 5 ++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 501a2de3..999c8fea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,10 @@ FILE(APPEND THCUNN_generic_h.lua "]]") FILE(GLOB luasrc *.lua) +IF (ANDROID) + ADD_DEFINITIONS(-DTHC_MIN_MATH) +ENDIF() + ADD_SUBDIRECTORY(lib) INSTALL( diff --git a/lib/THCUNN/SparseLinear.cu b/lib/THCUNN/SparseLinear.cu index a7ffa1e2..b1b756ac 100644 --- a/lib/THCUNN/SparseLinear.cu +++ b/lib/THCUNN/SparseLinear.cu @@ -82,6 +82,5 @@ void THNN_CudaHalfSparseLinear_updateParameters( #endif #include "generic/SparseLinear.cu" -#include "THCGenerateFloatType.h" -#include "generic/SparseLinear.cu" -#include "THCGenerateDoubleType.h" +#include "THCGenerateFloatTypes.h" + From b8e8ba17d892c10b07c125761a2a9cdb036bc21b Mon Sep 17 00:00:00 2001 From: Boris Fomitchev Date: Wed, 18 Jan 2017 17:02:49 -0800 Subject: [PATCH 3/3] Adding guards for double --- THCUNN.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/THCUNN.lua b/THCUNN.lua index 6776a238..9dd90e5d 100644 --- a/THCUNN.lua +++ b/THCUNN.lua @@ -129,8 +129,10 @@ local function_names_generic = extract_function_names_generic(THCUNN_generic_h) THNN.kernels['torch.CudaTensor'] = THNN.bind(THCUNN.C, function_names_generic, 'Cuda', THCUNN.getState) torch.getmetatable('torch.CudaTensor').THNN = THNN.kernels['torch.CudaTensor'] -THNN.kernels['torch.CudaDoubleTensor'] = THNN.bind(THCUNN.C, function_names_generic, 'CudaDouble', THCUNN.getState) -torch.getmetatable('torch.CudaDoubleTensor').THNN = THNN.kernels['torch.CudaDoubleTensor'] +if not cutorch.minMath then + THNN.kernels['torch.CudaDoubleTensor'] = THNN.bind(THCUNN.C, function_names_generic, 'CudaDouble', THCUNN.getState) + torch.getmetatable('torch.CudaDoubleTensor').THNN = THNN.kernels['torch.CudaDoubleTensor'] +end if cutorch.hasHalf then -- in order to call 'half' functions from lua, convert real arguments from @@ -164,7 +166,10 @@ local function Module__converter(type) end end -rawset(torch.getmetatable('nn.Module'), 'cudaDouble', Module__converter('torch.CudaDoubleTensor')) +if not cutorch.minMath then + rawset(torch.getmetatable('nn.Module'), 'cudaDouble', Module__converter('torch.CudaDoubleTensor')) +end + if cutorch.hasHalf then rawset(torch.getmetatable('nn.Module'), 'cudaHalf', Module__converter('torch.CudaHalfTensor')) end