Skip to content

Commit 9090160

Browse files
author
Wang Wei
committed
SINGA-346 Update cudnn from V5 to V7
Export numpy path by cmake; no need to export it into a env var now. replace PYTHON3 with USE_PYTHON3 one bug left due to the unicode and byte string problem from py2 to py3. which could be resolved by defining SWIG_PYTHON_2_UNICODE in *.i files. It converts byte string in python2 into unicode and then to c++ std::string.
1 parent 4ccb72e commit 9090160

18 files changed

Lines changed: 91 additions & 128 deletions

File tree

CMakeLists.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@ SET(SINGA_INCLUDE_DIR
5252
"${CMAKE_SOURCE_DIR}/include;${PROJECT_BINARY_DIR}")
5353
INCLUDE_DIRECTORIES(${SINGA_INCLUDE_DIR})
5454

55-
#OPTION(USE_CBLAS "Use CBlas libs" ON)
55+
5656
OPTION(USE_CUDA "Use Cuda libs" OFF)
57+
OPTION(ENABLE_TEST "Enable unit test" OFF)
58+
OPTION(USE_PYTHON "Generate py wrappers" ON)
59+
OPTION(USE_PYTHON3 "Python 3x" OFF)
60+
5761
OPTION(USE_CUDNN "Use Cudnn libs" ON)
5862
OPTION(USE_OPENCV "Use opencv" OFF)
5963
OPTION(USE_LMDB "Use LMDB libs" OFF)
60-
OPTION(USE_PYTHON "Generate py wrappers" ON)
6164
OPTION(USE_JAVA "Generate java wrappers" OFF)
6265
OPTION(USE_OPENCL "Use OpenCL" OFF)
6366
OPTION(ENABLE_DIST "Enable distributed training" OFF)
64-
OPTION(ENABLE_TEST "Enable unit test" OFF)
6567
OPTION(DISABLE_WARNINGS "Disable warnings under windows" ON)
6668
OPTION(USE_MODULES "Compile dependent libs as submodules together with singa" OFF)
67-
#OPTION(USE_SHARED_LIBS "Use shared library" OFF)
69+
6870

6971
# TODO: remove all USE_CBLAS in codes
7072
SET(USE_CBLAS ON)
@@ -192,7 +194,7 @@ IF(PACKAGE)
192194
SET(CORE_DEPENDENCIES "libgoogle-glog-dev, libprotobuf-dev, libopenblas-dev, libstdc++6, libc6")
193195
ENDIF()
194196

195-
IF(PYTHON3)
197+
IF(USE_PYTHON3)
196198
SET(PYTHON_DEPENDENCIES "${CORE_DEPENDENCIES}, python3, python3-dev, python3-pip, python3-numpy, python3-pillow, python3-matplotlib")
197199
ELSE()
198200
SET(PYTHON_DEPENDENCIES "${CORE_DEPENDENCIES}, python-dev, libpython2.7, python-pip, python-numpy, python-pillow")
@@ -210,7 +212,7 @@ IF(PACKAGE)
210212
SET(CPACK_DEBIAN_PACKAGE_DEPENDS ${PYTHON_DEPENDENCIES})
211213
SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_SOURCE_DIR}/tool/debian/postinst" )
212214
SET(CPACK_DEBIAN_PACKAGE_PREDEPENDS "ca-certificates")
213-
IF(PYTHON3)
215+
IF(USE_PYTHON3)
214216
SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_SOURCE_DIR}/tool/debian/postinst" )
215217
IF (USE_CUDA)
216218
SET(CPACK_DEBIAN_PACKAGE_NAME "python3-singa-cuda")

cmake/Dependencies.cmake

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ ENDIF()
124124
#MESSAGE(STATUS "link lib : " ${SINGA_LINKER_LIBS})
125125

126126
IF(USE_PYTHON)
127-
IF(PYTHON3)
128-
set(Python_ADDITIONAL_VERSIONS 3.6)
129-
FIND_PACKAGE(PythonLibs 3 REQUIRED)
127+
IF(USE_PYTHON3)
128+
set(Python_ADDITIONAL_VERSIONS 3.6 3.5 3.4)
130129
FIND_PACKAGE(PythonInterp 3 REQUIRED)
131-
FIND_PACKAGE(SWIG 3.0.10 REQUIRED)
132-
ELSE()
133-
FIND_PACKAGE(PythonLibs 2.7 REQUIRED)
130+
FIND_PACKAGE(PythonLibs 3 REQUIRED)
131+
FIND_PACKAGE(SWIG 3.0.8 REQUIRED)
132+
ELSE()
134133
FIND_PACKAGE(PythonInterp 2.7 REQUIRED)
134+
FIND_PACKAGE(PythonLibs 2.7 REQUIRED)
135135
FIND_PACKAGE(SWIG 3.0.8 REQUIRED)
136136
ENDIF()
137137
ENDIF()

doc/en/docs/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ The following libraries are optional
109109
* `USE_CUDA=ON`, used if CUDA and cuDNN is available
110110
* `USE_PYTHON=ON`, used for compiling PySINGA
111111
* `USE_OPENCL=ON`, used for compiling with OpenCL support
112-
* `PYTHON3=ON`, used for compiling with Python 3 support. (The default is Python 2)
112+
* `USE_PYTHON3=ON`, used for compiling with Python 3 support. (The default is Python 2)
113113
* `PACKAGE=ON`, used for building the Debian package
114114

115115
3. compile the code, e.g., `make`

python/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/python/singa/proto)
9494
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/python/rafiki)
9595
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/src/api)
9696

97-
IF(PYTHON3)
97+
IF(USE_PYTHON3)
9898
SET(SWIG_PYTHON3 "-py3")
9999
ELSE()
100100
SET(SWIG_PYTHON3 "")
@@ -113,6 +113,12 @@ file(GLOB_RECURSE python_source_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.py)
113113
create_symlinks(${python_source_files})
114114

115115

116+
execute_process(
117+
COMMAND ${PYTHON_EXECUTABLE} -c "from __future__ import print_function; import numpy; print(numpy.get_include())"
118+
OUTPUT_VARIABLE NUMPY_INCLUDE_DIR)
119+
120+
#message(status "numpy path ${NUMPY_INCLUDE_DIR}")
121+
116122
IF(USE_CUDA)
117123
# remain this custom command to avoid cuda objs can't find
118124
ADD_CUSTOM_COMMAND(
@@ -123,7 +129,7 @@ ENDIF(USE_CUDA)
123129

124130
ADD_LIBRARY(_singa_wrap SHARED $<TARGET_OBJECTS:singa_objects> ${python_srcs} ${proto_pys} ${global_cuda_objs})
125131
TARGET_LINK_LIBRARIES(_singa_wrap ${SINGA_LINKER_LIBS} ${PYTHON_LIBRARIES})
126-
TARGET_INCLUDE_DIRECTORIES(_singa_wrap PRIVATE ${PYTHON_INCLUDE_DIRS})
132+
TARGET_INCLUDE_DIRECTORIES(_singa_wrap PRIVATE ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR})
127133
SET_TARGET_PROPERTIES(_singa_wrap
128134
PROPERTIES PREFIX ""
129135
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/python/singa

python/singa/layer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def __init__(self, name, conf=None, **kwargs):
102102
self.param_specs = []
103103
else:
104104
self.conf = conf
105-
self.name = str(conf.name)
105+
self.name = conf.name
106106
self.caffe_layer()
107107
self.param_specs = []
108108

@@ -154,7 +154,7 @@ def caffe_layer(self):
154154
if self.conf.type == 'InnerProduct' or self.conf.type == 14:
155155
self.layer = _create_layer(engine, 'Dense')
156156
else:
157-
self.layer = _create_layer(engine, str(self.conf.type))
157+
self.layer = _create_layer(engine, self.conf.type)
158158

159159
def get_output_sample_shape(self):
160160
'''Called after setup to get the shape of the output sample(s).

python/singa/net.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Nerual net class for constructing the nets using layers and providing access
1919
functions for net info, e.g., parameters.
2020
21+
2122
Example usages::
2223
2324
from singa import net as ffnet
@@ -175,11 +176,11 @@ def param_names(self):
175176
return [spec.name for spec in self.param_specs()]
176177

177178
def train(self, x, y):
178-
'''Run BP for one iteration.
179-
This method is deprecated. It is only kept for backward compatibility.
180-
The name of this method is confusing since it does not update parameters.
181-
Please use backprob() instead.
182-
The back progagation algorithm computes gradients but it does not train.
179+
'''Run BP for one iteration.
180+
This method is deprecated. It is only kept for backward compatibility.
181+
The name of this method is confusing since it does not update parameters.
182+
Please use backprob() instead.
183+
The back progagation algorithm computes gradients but it does not train.
183184
'''
184185
return backprob(x, y)
185186

@@ -295,7 +296,8 @@ def forward(self, flag, x, output=[], freeze=None):
295296
dictionary: layer name -> output tensor(s)
296297
'''
297298
if self.ordered_layers is None:
298-
self.ordered_layers = self.topo_sort(self.layers, self.src_of_layer)
299+
self.ordered_layers = self.topo_sort(
300+
self.layers, self.src_of_layer)
299301
if type(x) is dict:
300302
input_of_layer = x
301303
else:
@@ -326,7 +328,7 @@ def forward(self, flag, x, output=[], freeze=None):
326328
outs = output_of_layer[src.name]
327329
if type(outs) == list:
328330
assert len(outs) > 0, \
329-
'the output from layer %s is empty' % src.name
331+
'the output from layer %s is empty' % src.name
330332
inputs.append(outs[0])
331333
outs.pop(0)
332334
if len(outs) == 0:
@@ -406,7 +408,7 @@ def backward(self, dy, output=[], freeze=None):
406408
outputs = output_of_layer[dst.name]
407409
if type(outputs) == list:
408410
assert len(outputs) > 0, \
409-
'the gradient from layer %s is empty' % dst.name
411+
'the gradient from layer %s is empty' % dst.name
410412
inputs.append(outputs[0])
411413
outputs.pop(0)
412414
else:
@@ -418,7 +420,7 @@ def backward(self, dy, output=[], freeze=None):
418420
outs, pgrads = cur.backward(kTrain, inputs)
419421
if verbose:
420422
disp_src = '+'.join(
421-
[dst.name for dst in self.dst_of_layer[cur.name]])
423+
[dst.name for dst in self.dst_of_layer[cur.name]])
422424
disp_src += '-->' + cur.name
423425
if type(outs) is list:
424426
print('%s: %s' % (disp_src,
@@ -477,7 +479,7 @@ def get_name(name):
477479
if version < 1101:
478480
idx = name.rfind('/')
479481
assert idx > 0, '/ must be in the parameter name'
480-
name = name[:idx] + '_' + name[idx+1:]
482+
name = name[:idx] + '_' + name[idx + 1:]
481483
return name
482484

483485
if use_pickle:
@@ -515,5 +517,5 @@ def get_name(name):
515517
except AssertionError as err:
516518
print('Error from copying values for param: %s' % name)
517519
print(('shape of param vs checkpoint',
518-
val.shape, params[name].shape))
520+
val.shape, params[name].shape))
519521
raise err

python/singa/optimizer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def __init__(self, lr=None, momentum=None, weight_decay=None,
206206
if self.momentum is not None:
207207
conf.momentum = self.momentum
208208
conf.type = 'sgd'
209-
self.opt = singa.CreateOptimizer('SGD'.encode())
209+
self.opt = singa.CreateOptimizer('SGD')
210210
self.opt.Setup(conf.SerializeToString())
211211

212212
def apply_with_lr(self, epoch, lr, grad, value, name, step=-1):
@@ -216,7 +216,7 @@ def apply_with_lr(self, epoch, lr, grad, value, name, step=-1):
216216
epoch, value, grad, name, step)
217217
if name is not None and name in self.learning_rate_multiplier:
218218
lr = lr * self.learning_rate_multiplier[name]
219-
self.opt.Apply(epoch, lr, name.encode(), grad.data,
219+
self.opt.Apply(epoch, lr, name, grad.data,
220220
value.data)
221221
return value
222222

@@ -235,7 +235,7 @@ def __init__(self, lr=None, momentum=0.9, weight_decay=None,
235235
if self.momentum is not None:
236236
conf.momentum = momentum
237237
conf.type = 'nesterov'
238-
self.opt = singa.CreateOptimizer('Nesterov'.encode())
238+
self.opt = singa.CreateOptimizer('Nesterov')
239239
self.opt.Setup(conf.SerializeToString())
240240

241241
def apply_with_lr(self, epoch, lr, grad, value, name, step=-1):
@@ -246,7 +246,7 @@ def apply_with_lr(self, epoch, lr, grad, value, name, step=-1):
246246
epoch, value, grad, name, step)
247247
if name is not None and name in self.learning_rate_multiplier:
248248
lr = lr * self.learning_rate_multiplier[name]
249-
self.opt.Apply(epoch, lr, name.encode(), grad.data,
249+
self.opt.Apply(epoch, lr, name, grad.data,
250250
value.data)
251251
return value
252252

@@ -268,7 +268,7 @@ def __init__(self, rho=0.9, epsilon=1e-8, lr=None, weight_decay=None,
268268
conf = model_pb2.OptimizerConf()
269269
conf.rho = rho
270270
conf.delta = epsilon
271-
self.opt = singa.CreateOptimizer('RMSProp'.encode())
271+
self.opt = singa.CreateOptimizer('RMSProp')
272272
self.opt.Setup(conf.SerializeToString())
273273

274274
def apply_with_lr(self, epoch, lr, grad, value, name, step=-1):
@@ -279,7 +279,7 @@ def apply_with_lr(self, epoch, lr, grad, value, name, step=-1):
279279
epoch, value, grad, name, step)
280280
if name is not None and name in self.learning_rate_multiplier:
281281
lr = lr * self.learning_rate_multiplier[name]
282-
self.opt.Apply(step, lr, name.encode(), grad.data,
282+
self.opt.Apply(step, lr, name, grad.data,
283283
value.data)
284284
return value
285285

@@ -300,7 +300,7 @@ def __init__(self, epsilon=1e-8, lr=None, weight_decay=None, lr_gen=None,
300300
conf = model_pb2.OptimizerConf()
301301
conf.delta = epsilon
302302
conf.type = 'adagrad'
303-
self.opt = singa.CreateOptimizer('AdaGrad'.encode())
303+
self.opt = singa.CreateOptimizer('AdaGrad')
304304
self.opt.Setup(conf.SerializeToString())
305305

306306
def apply_with_lr(self, epoch, lr, grad, value, name, step=-1):
@@ -311,7 +311,7 @@ def apply_with_lr(self, epoch, lr, grad, value, name, step=-1):
311311
epoch, value, grad, name, step)
312312
if name is not None and name in self.learning_rate_multiplier:
313313
lr = lr * self.learning_rate_multiplier[name]
314-
self.opt.Apply(epoch, lr, name.encode(), grad.data,
314+
self.opt.Apply(epoch, lr, name, grad.data,
315315
value.data)
316316
return value
317317

tool/conda/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ this folder should include a subfolder `include/cudnn.h` for the header file, an
2424

2525
After exporting the environment variables, execute the following command to compile Singa and package it
2626

27-
conda-build .
27+
conda-build . --python 3.6 (or 2.7)
2828

2929
You will see the package path from the screen output.
3030

tool/conda/build.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,28 @@
1616
#
1717

1818
# to compile swig api files which depdend on numpy.i
19-
export CPLUS_INCLUDE_PATH=`python -c "from __future__ import print_function; import numpy; print(numpy.get_include())"`:$CPLUS_INCLUDE_PATH
19+
# export CPLUS_INCLUDE_PATH=`python -c "from __future__ import print_function; import numpy; print(numpy.get_include())"`:$CPLUS_INCLUDE_PATH
2020

2121
# to let cmake use the dependent libs installed by conda, including python
2222
export CMAKE_PREFIX_PATH=$PREFIX:$CMAKE_PREFIX_PATH
2323
export CMAKE_INCLUDE_PATH=$PREFIX/include:$CMAKE_INCLUDE_PATH
2424
export CMAKE_LIBRARY_PATH=$PREFIX/lib:$CMAKE_LIBRARY_PATH
2525

26-
mkdir build
27-
cd build
26+
2827
USE_CUDA=OFF
2928
if [ -z ${CUDNN_PATH+x} ]; then
3029
USE_CUDA=ON
3130
cp $CUDNN_PATH/include $PREFIX/include
3231
cp -P $CUDNN_PATH/lib64/libcudnn.so* $PREFIX/lib/
3332
fi
3433

35-
PYTHON3=OFF
34+
USE_PYTHON3=OFF
3635
# PY3K is set by conda
37-
if [ "$PY3K" == "1" ]; then PYTHON3=ON; fi
38-
echo "PY3K = $PY3K"
39-
cmake -DCMAKE_INSTALL_PREFIX=$PREFIX -DUSE_CUDA=$USE_CUDA -DPYTHON3=$PYTHON3 ..
36+
if [ "$PY3K" == "1" ]; then USE_PYTHON3=ON; fi
37+
38+
39+
mkdir build
40+
cd build
41+
cmake -DCMAKE_INSTALL_PREFIX=$PREFIX -DUSE_CUDA=$USE_CUDA -DUSE_PYTHON3=$USE_PYTHON3 ..
4042
make
4143
make install

tool/docker/README.md

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,23 @@
22

33
## Availabe images
44

5-
TO BE UPDATED.
65

7-
| Tag | OS version | devel/runtime | Device|CUDA/CUDNN|
8-
|:----|:-----------|:--------------|:------|:---------|
9-
|runtime| Ubuntu16.04|runtime|CPU|-|
10-
|runtime| Ubuntu16.04|runtime|CPU|-|
11-
|runtime-cuda| Ubuntu16.04|runtime|GPU|CUDA8.0+CUDNN5|
12-
|devel| Ubuntu16.04|devel|CPU|-|
13-
|devel-cuda| Ubuntu16.04|devel|GPU|CUDA8.0+CUDNN5|
6+
| Tag | OS version | devel/runtime | Device|CUDA/CUDNN|Python|
7+
|:----|:-----------|:--------------|:------|:---------|:-----|
8+
|runtime| Ubuntu16.04|runtime|CPU|-|3.6|
9+
|conda-cuda9.0| Ubuntu16.04|devel|GPU|CUDA9.0+CUDNN7.1.2|3.6|
10+
|cuda9.0-py2| Ubuntu16.04|devel|GPU|CUDA9.0+CUDNN7.1.2|2.7|
11+
|cuda9.0-py3| Ubuntu16.04|devel|GPU|CUDA9.0+CUDNN7.1.2|3.6|
12+
13+
runtime and conda-xxx image has installed miniconda3;
14+
cudaxxx images have installed all depedent libs using apt-get.
1415

1516
## Usage
1617

1718
docker pull nusdbsystem/singa:<Tag>
1819
docker run -it nusdbsystem/singa:<Tag> /bin/bash
20+
nvidia-docker run -it nusdbsystem/singa:<Tag> /bin/bash
1921

20-
* For the *devel* images, the container has a `incubator-singa` folder in the root directory,
21-
which has the latest SINGA code. The code has been compiled into `incubator-singa/build` directory and PySINGA has been installed.
22-
* For the *runtime* images, the container has only installed the PySINGA.
23-
24-
## Tag naming style
2522

26-
singa:devel|runtime[-OS][-CUDA|OPENCL][-CUDNN]
2723

28-
* devel: development images with all dependent libs' header files installed and SINGA's source code;
29-
* runtime: the minimal images which can run SINGA programs.
30-
* OS: ubuntu, ubuntu14.04, centos, centos6
31-
* CUDA: cuda, cuda8.0, cuda7.0
32-
* CUDNN: cudnn, cudnn5, cudnn4
33-
* OPENCL: opencl, opencl1.2
3424

35-
By default, if the version is not included in the tag, the latest stable version is used.
36-
The default OS is ubuntu. The version is the latest stable version (e.g., 16.04 for now).
37-
For -cuda version, the **cudnn** is included by default. Their versions are also the latest stable version, i.e., cuda-8.0 and cudnn-5 for now.

0 commit comments

Comments
 (0)