Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SINGA-257 Convnet Benchmark #263

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ ENDIF()

IF(USE_OPENCL)
FIND_PACKAGE(OpenCL REQUIRED)
IF(NOT OpenCL_FOUND)
IF(NOT OPENCL_FOUND)
MESSAGE(SEND_ERROR "OpenCL was requested, but not found.")
ELSE()
#MESSAGE(STATUS "Found OpenCL headers at ${OpenCL_INCLUDE_DIRS}")
INCLUDE_DIRECTORIES(SYSTEM ${OpenCL_INCLUDE_DIR})
LIST(APPEND SINGA_LINKER_LIBS ${OpenCL_LIBRARIES})
INCLUDE_DIRECTORIES(SYSTEM ${OPENCL_INCLUDE_DIR})
LIST(APPEND SINGA_LINKER_LIBS ${OPENCL_LIBRARIES})
FIND_PACKAGE(ViennaCL REQUIRED)
IF(NOT ViennaCL_FOUND)
MESSAGE(SEND_ERROR "ViennaCL is required if OpenCL is enabled.")
Expand Down
88 changes: 88 additions & 0 deletions cmake/Thirdparty/FindOpenCL.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# - Find the OpenCL headers and library
#
# Defines the following if found:
# OPENCL_FOUND : TRUE if found, FALSE otherwise
# OPENCL_INCLUDE_DIRS : Include directories for OpenCL
# OPENCL_LIBRARIES : The libraries to link against
#
# The user can set the OPENCLROOT environment variable to help finding OpenCL
# if it is installed in a non-standard place.

set(ENV_ATISTREAMSDKROOT "$ENV{ATISTREAMSDKROOT}")
if(ENV_ATISTREAMSDKROOT)
set(ENV_OPENCLROOT "$ENV{ATISTREAMSDKROOT}")
endif(ENV_ATISTREAMSDKROOT)

set(ENV_AMDAPPSDKROOT "$ENV{AMDAPPSDKROOT}")
if(ENV_AMDAPPSDKROOT)
set(ENV_OPENCLROOT "$ENV{AMDAPPSDKROOT}")
endif(ENV_AMDAPPSDKROOT)

set(ENV_INTELOCLSDKROOT "$ENV{INTELOCLSDKROOT}")
if(ENV_INTELOCLSDKROOT)
set(ENV_OPENCLROOT "$ENV{INTELOCLSDKROOT}")
endif(ENV_INTELOCLSDKROOT)

set(ENV_OPENCLROOT2 "$ENV{OPENCLROOT}")
if(ENV_OPENCLROOT2)
set(ENV_OPENCLROOT "$ENV{OPENCLROOT}")
endif(ENV_OPENCLROOT2)

if(ENV_OPENCLROOT)
find_path(
OPENCL_INCLUDE_DIR
NAMES CL/cl.h OpenCL/cl.h
PATHS "${ENV_OPENCLROOT}/include"
#NO_DEFAULT_PATH #uncomment this is you wish to surpress the use of default paths for OpenCL
)

if (("${CMAKE_SYSTEM_NAME}" MATCHES "Linux") OR ("${CMAKE_SYSTEM_NAME}" MATCHES "Windows"))
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(OPENCL_LIB_SEARCH_PATH
"${OPENCL_LIB_SEARCH_PATH}"
"${ENV_OPENCLROOT}/lib/x86")
else(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(OPENCL_LIB_SEARCH_PATH
"${OPENCL_LIB_SEARCH_PATH}"
"${ENV_OPENCLROOT}/lib/x86_64")
endif(CMAKE_SIZEOF_VOID_P EQUAL 4)
endif(("${CMAKE_SYSTEM_NAME}" MATCHES "Linux") OR ("${CMAKE_SYSTEM_NAME}" MATCHES "Windows"))
find_library(
OPENCL_LIBRARY
NAMES OpenCL
PATHS "${OPENCL_LIB_SEARCH_PATH}"
#NO_DEFAULT_PATH #uncomment this is you wish to surpress the use of default paths for OpenCL
)
else(ENV_OPENCLROOT)
find_path(
OPENCL_INCLUDE_DIR
NAMES CL/cl.h OpenCL/cl.h
PATHS "${PROJECT_SOURCE_DIR}" #use the CL/ include folder provided with ViennaCL
)

find_library(
OPENCL_LIBRARY
NAMES OpenCL
)
endif(ENV_OPENCLROOT)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
OPENCL
DEFAULT_MSG
OPENCL_LIBRARY OPENCL_INCLUDE_DIR
)

if(OPENCL_FOUND)
set(OPENCL_INCLUDE_DIRS "${OPENCL_INCLUDE_DIR}")
set(OPENCL_LIBRARIES "${OPENCL_LIBRARY}")
else(OPENCL_FOUND)
set(OPENCL_INCLUDE_DIRS)
set(OPENCL_LIBRARIES)
endif(OPENCL_FOUND)

mark_as_advanced(
OPENCL_INCLUDE_DIR
OPENCL_LIBRARY
)

11 changes: 10 additions & 1 deletion cmake/Thirdparty/FindViennaCL.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# This file is retrieved from caffe/cmake/Modules/FindViennaCL.cmake.
# from the opencl branch on BVLC/Caffe.

SET(ViennaCL_WITH_OPENCL TRUE)

SET(VIENNACL_INCLUDE_SEARCH_PATHS
viennacl
viennacl-dev
..
../viennacl
../viennacl-dev
/usr/include
/usr/local/include
/opt/ViennaCL/include
Expand All @@ -15,7 +20,7 @@ FIND_PATH(ViennaCL_INCLUDE_DIR NAMES viennacl/forwards.h PATHS ${VIENNACL_INCLUD

SET(ViennaCL_FOUND ON)

# Check include files
# Check include files
IF(NOT ViennaCL_INCLUDE_DIR)
SET(ViennaCL_FOUND OFF)
MESSAGE(STATUS "Could not find ViennaCL include. Turning ViennaCL_FOUND off")
Expand All @@ -33,6 +38,10 @@ ENDIF (ViennaCL_FOUND)

IF(ViennaCL_WITH_OPENCL)
find_package(OpenCL REQUIRED)
IF(NOT OPENCL_INCLUDE_DIRS)
MESSAGE(FATAL_ERROR "Could not find OpenCL include.")
ENDIF()
MESSAGE(STATUS "Found OpenCL include: ${OPENCL_INCLUDE_DIRS}")
ENDIF(ViennaCL_WITH_OPENCL)

set(ViennaCL_INCLUDE_DIRS ${ViennaCL_INCLUDE_DIR} ${OPENCL_INCLUDE_DIRS})
Expand Down
15 changes: 15 additions & 0 deletions examples/benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Benchmark scripts

These scripts will test the efficiency of SINGA by training benchmark models pecified in
[convnet-benchmarks](https://github.com/soumith/convnet-benchmarks/tree/master/caffe/imagenet_winners)
over different devices (e.g., CPU and GPU).

To run them, create a python pip virtualenv or anaconda virtual environment as
guided by [this article](http://singa.apache.org/en/docs/installation.html#pip-and-anaconda-for-pysinga).
Then, execute the `run.py` as

$ python run.py

Different models and devices could be tested, please refer to the command line help message,

$ python run.py -h
77 changes: 77 additions & 0 deletions examples/benchmark/alexnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
''' This model is created following the structure from
https://github.com/soumith/convnet-benchmarks/blob/master/caffe/imagenet_winners/alexnet.prototxt
'''

from singa import layer
from singa import loss
from singa import metric
from singa import net as ffnet


def create_net(input_shape, use_cpu=False, use_ocl=False):
if use_cpu:
layer.engine = 'singacpp'
if use_ocl:
layer.engine = 'singacl'

net = ffnet.FeedForwardNet(loss.SoftmaxCrossEntropy(), metric.Accuracy())

# Conv 1
net.add(layer.Conv2D("conv1", 64, 11, 4, pad=2,
input_sample_shape=input_shape))
net.add(layer.Activation("conv1/relu" ))
net.add(layer.MaxPooling2D("pool1/3x3_s2", 3, 2))

# Conv 2
net.add(layer.Conv2D("conv1/5x5_s1", 192, 5, 1, pad=2))
net.add(layer.Activation("conv2/relu"))
net.add(layer.MaxPooling2D("pool2/3x3_s2", 3, 2))

# Conv 3
net.add(layer.Conv2D("conv3/3x3_s1", 384, 3, 1, pad=1))
net.add(layer.Activation("conv3/relu"))

# Conv 4
net.add(layer.Conv2D("conv4/3x3_s1", 256, 3, 1, pad=1))
net.add(layer.Activation("conv4/relu"))

# Conv 5
net.add(layer.Conv2D("conv5/3x3_s1", 256, 3, 1, pad=1))
net.add(layer.Activation("conv5/relu"))
net.add(layer.MaxPooling2D("pool5/3x3_s2", 3, 2))

# L2 Norm -> Inner product
net.add(layer.Flatten("flat"))
net.add(layer.Dense("fc6", 4096))
net.add(layer.Activation("fc6/relu6"))

net.add(layer.Dense("fc7", 4096))
net.add(layer.Activation("fc7/relu7"))

net.add(layer.Dense("fc8", 1000))

for (val, spec) in zip(net.param_values(), net.param_specs()):
filler = spec.filler
if filler.type == 'gaussian':
val.gaussian(filler.mean, filler.std)
else:
val.set_value(0)
print spec.name, filler.type, val.l1()

return net
76 changes: 76 additions & 0 deletions examples/benchmark/overfeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
''' This model is created following the structure from
https://github.com/soumith/convnet-benchmarks/blob/master/caffe/imagenet_winners/overfeat.prototxt
'''

from singa import layer
from singa import loss
from singa import metric
from singa import net as ffnet


def create_net(input_shape, use_cpu=False, use_ocl=False):
if use_cpu:
layer.engine = 'singacpp'
if use_ocl:
layer.engine = 'singacl'

net = ffnet.FeedForwardNet(loss.SoftmaxCrossEntropy(), metric.Accuracy())

# Conv 1
net.add(layer.Conv2D("conv1", 96, 11, 4, input_sample_shape=input_shape))
net.add(layer.Activation("conv1/relu" ))
net.add(layer.MaxPooling2D("pool1/2x2_s2", 2, 2, border_mode='valid'))

# Conv 2
net.add(layer.Conv2D("conv1/5x5_s1", 256, 5, 1))
net.add(layer.Activation("conv2/relu"))
net.add(layer.MaxPooling2D("pool2/2x2_s2", 2, 2, border_mode='valid'))

# Conv 3
net.add(layer.Conv2D("conv3/3x3_s1", 512, 3, 1, pad=1))
net.add(layer.Activation("conv3/relu"))

# Conv 4
net.add(layer.Conv2D("conv4/3x3_s1", 1024, 3, 1, pad=1))
net.add(layer.Activation("conv4/relu"))

# Conv 5
net.add(layer.Conv2D("conv5/3x3_s1", 1024, 3, 1, pad=1))
net.add(layer.Activation("conv5/relu"))
net.add(layer.MaxPooling2D("pool5/2x2_s2", 2, 2, border_mode='valid'))

# L2 Norm -> Inner product
net.add(layer.Flatten("flat"))
net.add(layer.Dense("fc6", 3072))
net.add(layer.Activation("fc6/relu6"))

net.add(layer.Dense("fc7", 4096))
net.add(layer.Activation("fc7/relu7"))

net.add(layer.Dense("fc8", 1000))

for (val, spec) in zip(net.param_values(), net.param_specs()):
filler = spec.filler
if filler.type == 'gaussian':
val.gaussian(filler.mean, filler.std)
else:
val.set_value(0)
print spec.name, filler.type, val.l1()

return net
Loading