Skip to content

Commit 804c5a9

Browse files
committed
The Dawn of a New Era
0 parents  commit 804c5a9

File tree

197 files changed

+19739
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

197 files changed

+19739
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build
2+
fb
3+
CHANGELOG.md
4+
TARGETS
5+

CMakeLists.txt

+333
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
cmake_minimum_required(VERSION 3.5.1)
2+
3+
project(flashlight)
4+
5+
include(CTest)
6+
7+
# ----------------------------- Setup -----------------------------
8+
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")
9+
set(CMAKE_CXX_STANDARD 11)
10+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
11+
set(CMAKE_CXX_EXTENSIONS OFF) # no compiler extensions like gnu++11
12+
set(FLASHLIGHT_PROJECT_COMPONENT_SRC_DIR "${CMAKE_SOURCE_DIR}/flashlight") # module root
13+
14+
# ----------------------------- Configuration -----------------------------
15+
# Arrayfire ML Backend
16+
set(FLASHLIGHT_BACKEND "CUDA" CACHE STRING "Backend with which to build Arrayfire ML")
17+
# Select from exactly one backend
18+
set_property(CACHE FLASHLIGHT_BACKEND PROPERTY STRINGS UNIFIED CPU CUDA OPENCL)
19+
# Map to flags
20+
set(FLASHLIGHT_USE_UNIFIED OFF)
21+
set(FLASHLIGHT_USE_CPU OFF)
22+
set(FLASHLIGHT_USE_CUDA OFF)
23+
set(FLASHLIGHT_USE_OPENCL OFF)
24+
if (FLASHLIGHT_BACKEND STREQUAL "UNIFIED")
25+
# Currently unsupported
26+
message(FATAL_ERROR "Building FLASHLIGHT with the Unified backend is not currently supported")
27+
# set(FLASHLIGHT_USE_UNIFIED ON)
28+
elseif (FLASHLIGHT_BACKEND STREQUAL "CPU")
29+
set(FLASHLIGHT_USE_CPU ON)
30+
elseif (FLASHLIGHT_BACKEND STREQUAL "CUDA")
31+
set(FLASHLIGHT_USE_CUDA ON)
32+
elseif (FLASHLIGHT_BACKEND STREQUAL "OPENCL")
33+
set(FLASHLIGHT_USE_OPENCL ON)
34+
else ()
35+
message(FATAL_ERROR "Invalid FLASHLIGHT backend specified")
36+
endif ()
37+
38+
# Distributed Training Backend
39+
set(FL_BUILD_DISTRIBUTED "ON" CACHE STRING "Whether to build and link the distributed backend with flashlight")
40+
# If building with CUDA, use NCCL to on; if using CPU or OpenCL, use GLOO
41+
set(USE_NCCL FALSE)
42+
set(USE_GLOO FALSE)
43+
if (FL_BUILD_DISTRIBUTED)
44+
if (FLASHLIGHT_USE_CUDA)
45+
set(USE_NCCL TRUE)
46+
elseif (FLASHLIGHT_USE_CPU OR FLASHLIGHT_USE_OPENCL OR FLASHLIGHT_USE_UNIFIED)
47+
set(USE_GLOO TRUE)
48+
endif ()
49+
endif ()
50+
51+
# ------------------------ Global External Dependencies ------------------------
52+
# ArrayFire
53+
find_package(ArrayFire 3.6.1 REQUIRED)
54+
if (ArrayFire_FOUND)
55+
message(STATUS "ArrayFire found (include: ${ArrayFire_INCLUDE_DIRS}, library: ${ArrayFire_LIBRARIES})")
56+
if (FLASHLIGHT_USE_UNIFIED)
57+
# Set the AF_PATH environment variable to wherever the ArrayFire libs are
58+
# located so they can be loaded in the unified backend
59+
set(ENV{AF_PATH} ${ArrayFire_LIBRARIES})
60+
endif ()
61+
else()
62+
message(FATAL_ERROR "ArrayFire not found")
63+
endif()
64+
65+
# Check the proper ArrayFire backend is present
66+
if (FLASHLIGHT_USE_CPU AND NOT ArrayFire_CPU_FOUND)
67+
message(FATAL_ERROR "ArrayFire CPU not found: cannot build CPU backend")
68+
elseif (FLASHLIGHT_USE_CUDA AND NOT ArrayFire_CUDA_FOUND)
69+
message(FATAL_ERROR "ArrayFire CUDA not found: cannot build CUDA backend")
70+
elseif (FLASHLIGHT_USE_OPENCL AND NOT ArrayFire_OpenCL_FOUND)
71+
message(FATAL_ERROR "ArrayFire OpenCL not found: cannot build OpenCL backend")
72+
elseif (FLASHLIGHT_USE_UNIFIED AND NOT ArrayFire_Unified_FOUND)
73+
message(FATAL_ERROR "ArrayFire Unified not found: cannot build unified backend")
74+
endif()
75+
76+
# TODO(@jacobkahn): remove these when glog is purged
77+
# Find GLog
78+
find_package(GLOG REQUIRED)
79+
if (GLOG_FOUND)
80+
message(STATUS "GLOG found")
81+
else()
82+
message(FATAL_ERROR "GLOG not found")
83+
endif()
84+
85+
# TODO(@jacobkahn): remove these when glog is purged
86+
# Find GFlags
87+
find_package(GFLAGS REQUIRED)
88+
if (GFLAGS_FOUND)
89+
message(STATUS "GFLAGS found")
90+
else()
91+
message(FATAL_ERROR "GFLAGS not found")
92+
endif()
93+
94+
# Find cereal
95+
find_package(cereal REQUIRED)
96+
if (cereal_FOUND)
97+
message(STATUS "cereal found")
98+
else()
99+
message(FATAL_ERROR "cereal not found")
100+
endif()
101+
102+
# -------------------- Locate Backend-specific Dependencies --------------------
103+
find_package(CUDA 9.2) # CUDA 9.2 is required for >= ArrayFire 3.6.1
104+
if (CUDA_FOUND)
105+
message(STATUS "CUDA found (library: ${CUDA_LIBRARIES} include: ${CUDA_INCLUDE_DIRS})")
106+
else()
107+
message(STATUS "CUDA not found")
108+
if (FLASHLIGHT_USE_CUDA)
109+
message(FATAL_ERROR "CUDA required to build CUDA backend")
110+
endif ()
111+
endif()
112+
113+
find_package(CUDNN 7.2 QUIET) # CUDNN 7.2 works with CUDA 9.2
114+
if (CUDNN_FOUND)
115+
message(STATUS "CUDNN found (library: ${CUDNN_LIBRARIES} include: ${CUDNN_INCLUDE_DIRS})")
116+
else()
117+
message(STATUS "CUDNN not found")
118+
if (FLASHLIGHT_USE_CUDA)
119+
message(FATAL_ERROR "CUDNN required to build CUDA backend")
120+
endif ()
121+
endif()
122+
123+
find_package(MKL)
124+
if (MKL_FOUND)
125+
message(STATUS "MKL found")
126+
else()
127+
message(STATUS "MKL not found")
128+
if (FLASHLIGHT_USE_CPU)
129+
message(FATAL_ERROR "MKL required to build CPU backend")
130+
endif()
131+
endif()
132+
133+
find_package(MKLDNN)
134+
if (MKLDNN_FOUND)
135+
message(STATUS "MKLDNN found")
136+
else()
137+
message(STATUS "MKLDNN not found")
138+
if (FLASHLIGHT_USE_CPU)
139+
message(FATAL_ERROR "MKLDNN required to build CPU backend")
140+
endif()
141+
endif()
142+
143+
find_package(OpenCL)
144+
if (OpenCL_FOUND)
145+
message(STATUS "OpenCL found (library: ${OpenCL_LIBRARIES} include: ${OpenCL_INCLUDE_DIRS})")
146+
else()
147+
message(STATUS "OpenCL not found")
148+
if (FLASHLIGHT_USE_OPENCL)
149+
message(FATAL_ERROR "OpenCL required to build OpenCL backend")
150+
endif ()
151+
endif()
152+
153+
154+
# -------------------------------- Main Library --------------------------------
155+
add_library(flashlight "")
156+
157+
set_target_properties(
158+
flashlight
159+
PROPERTIES
160+
LINKER_LANGUAGE CXX
161+
CXX_STANDARD 11
162+
)
163+
164+
set(
165+
FLASHLIGHT_MODULES
166+
Autograd
167+
Common
168+
Dataset
169+
Distributed
170+
Meter
171+
NN
172+
Optim
173+
)
174+
175+
target_link_libraries(
176+
flashlight
177+
PUBLIC # export dependency library and include paths for each module
178+
${FLASHLIGHT_MODULES}
179+
# TODO(@jacobkahn) remove this once glog is purged
180+
${GLOG_LIBRARIES}
181+
${GFLAGS_LIBRARIES}
182+
)
183+
184+
# Internal includes are impl defined as <flashlight...>
185+
target_include_directories(
186+
flashlight
187+
PRIVATE
188+
${CMAKE_SOURCE_DIR}
189+
# TODO(@jacobkahn) remove these once glog is purged
190+
${GLOG_INCLUDE_DIRS}
191+
${GFLAGS_INCLUDE_DIRS}
192+
)
193+
194+
# Link appropriate ArrayFire backend, set global compile time macros
195+
if (FLASHLIGHT_USE_UNIFIED)
196+
# For flashlight, nn ops use CUDA in unified mode
197+
target_compile_definitions(
198+
flashlight
199+
PRIVATE
200+
BUILD_ARRAYFIRE_UNIFIED_BACKEND=1
201+
RUN_UNIFIED_BACKEND_TESTS=1)
202+
target_link_libraries(flashlight ArrayFire::af)
203+
elseif (FLASHLIGHT_USE_CPU)
204+
target_compile_definitions(flashlight PRIVATE FLASHLIGHT_BUILD_CPU_BACKEND=1)
205+
target_link_libraries(flashlight PRIVATE ArrayFire::afcpu)
206+
elseif (FLASHLIGHT_USE_CUDA)
207+
target_compile_definitions(flashlight PRIVATE FLASHLIGHT_BUILD_CUDA_BACKEND=1)
208+
target_link_libraries(flashlight PRIVATE ArrayFire::afcuda)
209+
elseif (FLASHLIGHT_USE_OPENCL)
210+
target_compile_definitions(flashlight PRIVATE FLASHLIGHT_BUILD_OPENCL_BACKEND=1)
211+
target_link_libraries(flashlight PRIVATE ArrayFire::afopencl)
212+
else()
213+
message(FATAL_ERROR "flashlight backend ill-specified")
214+
endif()
215+
216+
# -------------------------------- Components --------------------------------
217+
# NOTE: each module is built as an interface library, but can't be built
218+
# using add_subdirectory and installed with a common target (flashlight) -
219+
# this is only recently supported in CMake:
220+
# https://gitlab.kitware.com/cmake/cmake/merge_requests/2152. Because
221+
# of this, each module must be included and added as a target to the
222+
# main export.
223+
224+
# Autograd
225+
include(${FLASHLIGHT_PROJECT_COMPONENT_SRC_DIR}/autograd/CMakeLists.txt)
226+
227+
# Common
228+
include(${FLASHLIGHT_PROJECT_COMPONENT_SRC_DIR}/common/CMakeLists.txt)
229+
230+
# Dataset
231+
include(${FLASHLIGHT_PROJECT_COMPONENT_SRC_DIR}/dataset/CMakeLists.txt)
232+
233+
# Dataset
234+
include(${FLASHLIGHT_PROJECT_COMPONENT_SRC_DIR}/distributed/CMakeLists.txt)
235+
236+
# Meter
237+
include(${FLASHLIGHT_PROJECT_COMPONENT_SRC_DIR}/meter/CMakeLists.txt)
238+
239+
# NN
240+
include(${FLASHLIGHT_PROJECT_COMPONENT_SRC_DIR}/nn/CMakeLists.txt)
241+
242+
# Optim
243+
include(${FLASHLIGHT_PROJECT_COMPONENT_SRC_DIR}/optim/CMakeLists.txt)
244+
245+
# ------------------------------- Install/Export -------------------------------
246+
247+
# Default directories for installation
248+
set(FL_INSTALL_INC_DIR "include" CACHE PATH "Install path for headers")
249+
set(FL_INSTALL_LIB_DIR "lib" CACHE PATH "Install path for libraries")
250+
set(FL_INSTALL_BIN_DIR "bin" CACHE PATH "Install path for binaries")
251+
# Other assets
252+
set(FL_INSTALL_ASSETS_BASE_DIR "share/flashlight")
253+
set(FL_INSTALL_CMAKE_DIR "${FL_INSTALL_ASSETS_BASE_DIR}/cmake" CACHE PATH "Install path for CMake files")
254+
set(FL_INSTALL_EXAMPLES_DIR "${FL_INSTALL_ASSETS_BASE_DIR}/examples" CACHE PATH "Install path for example files")
255+
set(FL_INSTALL_DOC_DIR "${FL_INSTALL_ASSETS_BASE_DIR}/doc" CACHE PATH "Install path for documentation")
256+
257+
# Main target
258+
install(
259+
TARGETS flashlight ${FLASHLIGHT_MODULES}
260+
EXPORT flashlightTargets
261+
# ARCHIVE DESTINATION ${FL_INSTALL_LIB_DIR}
262+
# INCLUDES DESTINATION ${FL_INSTALL_INC_DIR}
263+
COMPONENT flashlight
264+
PUBLIC_HEADER DESTINATION fl
265+
RUNTIME DESTINATION ${FL_INSTALL_BIN_DIR}
266+
LIBRARY DESTINATION ${FL_INSTALL_LIB_DIR}
267+
ARCHIVE DESTINATION ${FL_INSTALL_LIB_DIR}
268+
FRAMEWORK DESTINATION framework
269+
INCLUDES DESTINATION ${FL_INSTALL_INC_DIR}
270+
)
271+
272+
# Write and install targets file
273+
install(
274+
EXPORT
275+
flashlightTargets
276+
NAMESPACE
277+
flashlight::
278+
DESTINATION
279+
${FL_INSTALL_CMAKE_DIR}
280+
COMPONENT
281+
cmake)
282+
283+
# Move headers
284+
install(
285+
DIRECTORY
286+
${CMAKE_SOURCE_DIR}/flashlight/ # module headers in ./flashlight
287+
COMPONENT
288+
headers
289+
DESTINATION
290+
${FL_INSTALL_INC_DIR}/flashlight
291+
FILES_MATCHING # preserve directory structure
292+
PATTERN "*.h"
293+
)
294+
295+
# Move examples
296+
# Don't build examples unless FL_BUILD_EXAMPLES is set, but always move them
297+
install(
298+
DIRECTORY examples/
299+
DESTINATION ${FL_INSTALL_EXAMPLES_DIR}
300+
COMPONENT examples
301+
)
302+
303+
# Write config file (used by projects including fl, such as examples)
304+
include(CMakePackageConfigHelpers)
305+
set(INCLUDE_DIRS include)
306+
set(CMAKE_DIR ${FL_INSTALL_CMAKE_DIR})
307+
configure_package_config_file(
308+
${CMAKE_MODULE_PATH}/flashlightConfig.cmake.in
309+
cmake/install/${FL_CONFIG_CMAKE_BUILD_DIR}/flashlightConfig.cmake
310+
INSTALL_DESTINATION
311+
${FL_INSTALL_CMAKE_DIR}
312+
PATH_VARS INCLUDE_DIRS CMAKE_DIR
313+
)
314+
install(FILES
315+
${PROJECT_BINARY_DIR}/cmake/install/flashlightConfig.cmake
316+
DESTINATION ${FL_INSTALL_CMAKE_DIR}
317+
COMPONENT cmake
318+
)
319+
320+
# --------------------------- Configure Examples/Tests ---------------------------
321+
322+
# Build tests
323+
option(FL_BUILD_TESTS "Build tests for flashlight" ON)
324+
if (FL_BUILD_TESTS)
325+
enable_testing()
326+
add_subdirectory(${CMAKE_SOURCE_DIR}/tests)
327+
endif ()
328+
329+
# Build examples
330+
option(FL_BUILD_EXAMPLES "Build examples for flashlight" ON)
331+
if (FL_BUILD_EXAMPLES)
332+
add_subdirectory(${CMAKE_SOURCE_DIR}/examples)
333+
endif ()

CODE_OF_CONDUCT.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Code of Conduct
2+
3+
Facebook has adopted a Code of Conduct that we expect project participants to adhere to.
4+
Please read the [full text](https://code.fb.com/codeofconduct/)
5+
so that you can understand what actions will and will not be tolerated.

CONTRIBUTING.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Contributing to flashlight
2+
We want to make contributing to this project as easy and transparent as
3+
possible.
4+
5+
## Pull Requests
6+
We actively welcome your pull requests.
7+
8+
1. Fork the repo and create your branch from `master`.
9+
2. If you've added code that should be tested, add tests.
10+
3. If you've changed APIs, update the documentation.
11+
4. Ensure the test suite passes.
12+
5. Make sure your code lints.
13+
6. If you haven't already, complete the Contributor License Agreement ("CLA").
14+
15+
## Contributor License Agreement ("CLA")
16+
In order to accept your pull request, we need you to submit a CLA. You only need
17+
to do this once to work on any of Facebook's open source projects.
18+
19+
Complete your CLA here: <https://code.facebook.com/cla>
20+
21+
## Issues
22+
We use GitHub issues to track public bugs. Please ensure your description is
23+
clear and has sufficient instructions to be able to reproduce the issue.
24+
25+
## License
26+
By contributing to flashlight, you agree that your contributions will be licensed
27+
under the LICENSE file in the root directory of this source tree.

0 commit comments

Comments
 (0)