14 , Accelerate, CoreGraphics, CoreVideo
15 , lmdbSupport ? true, lmdb
16 , leveldbSupport ? true, leveldb, snappy
17 , cudaSupport ? config.cudaSupport, cudaPackages ? { }
18 , cudnnSupport ? cudaSupport
20 , pythonSupport ? false, python ? null, numpy ? null
25 inherit (cudaPackages) backendStdenv cudatoolkit nccl;
26 # The default for cudatoolkit 10.1 is CUDNN 8.0.5, the last version to support CUDA 10.1.
27 # However, this caffe does not build with CUDNN 8.x, so we use CUDNN 7.6.5 instead.
28 # Earlier versions of cudatoolkit use pre-8.x CUDNN, so we use the default.
30 if lib.versionOlder cudatoolkit.version "10.1"
31 then cudaPackages ? cudnn
32 else cudaPackages ? cudnn_7_6;
34 toggle = bool: if bool then "ON" else "OFF";
36 test_model_weights = fetchurl {
37 url = "http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel";
38 sha256 = "472d4a06035497b180636d8a82667129960371375bd10fcb6df5c6c7631f25e0";
42 stdenv.mkDerivation rec {
46 src = fetchFromGitHub {
50 sha256 = "104jp3cm823i3cdph7hgsnj6l77ygbwsy35mdmzhmsi4jxprd9j3";
53 nativeBuildInputs = [ cmake doxygen ];
56 # It's important that caffe is passed the major and minor version only because that's what
57 # boost_python expects
58 [ (if pythonSupport then "-Dpython_version=${python.pythonVersion}" else "-DBUILD_python=OFF")
60 ] ++ (if cudaSupport then [
61 "-DCUDA_ARCH_NAME=All"
62 "-DCUDA_HOST_COMPILER=${backendStdenv.cc}/bin/cc"
63 ] else [ "-DCPU_ONLY=ON" ])
64 ++ ["-DUSE_NCCL=${toggle ncclSupport}"]
65 ++ ["-DUSE_LEVELDB=${toggle leveldbSupport}"]
66 ++ ["-DUSE_LMDB=${toggle lmdbSupport}"];
68 buildInputs = [ boost gflags glog protobuf hdf5-cpp opencv4 blas ]
69 ++ lib.optional cudaSupport cudatoolkit
70 ++ lib.optional (lib.versionOlder cudatoolkit.version "10.1" && hasCudnn) cudaPackages.cudnn
71 ++ lib.optional (lib.versionAtLeast cudatoolkit.version "10.1" && hasCudnn) cudaPackages.cudnn_7_6
72 ++ lib.optional lmdbSupport lmdb
73 ++ lib.optional ncclSupport nccl
74 ++ lib.optionals leveldbSupport [ leveldb snappy ]
75 ++ lib.optionals pythonSupport [ python numpy ]
76 ++ lib.optionals stdenv.hostPlatform.isDarwin [ Accelerate CoreGraphics CoreVideo ]
79 propagatedBuildInputs = lib.optionals pythonSupport (
81 let pp = python.pkgs; in ([
82 pp.numpy pp.scipy pp.scikit-image pp.h5py
83 pp.matplotlib pp.ipython pp.networkx
84 pp.pandas pp.python-dateutil pp.protobuf pp.gflags
85 pp.pyyaml pp.pillow pp.six
86 ] ++ lib.optional leveldbSupport pp.leveldb)
89 outputs = [ "bin" "out" ];
90 propagatedBuildOutputs = []; # otherwise propagates out -> bin cycle
95 name = "support-opencv4";
96 url = "https://github.com/BVLC/caffe/pull/6638/commits/0a04cc2ccd37ba36843c18fea2d5cbae6e7dd2b5.patch";
97 hash = "sha256-ZegTvp0tTHlopQv+UzHDigs6XLkP2VfqLCWXl6aKJSI=";
99 ] ++ lib.optional pythonSupport (substituteAll {
100 src = ./python.patch;
101 inherit (python.sourceVersion) major minor; # Should be changed in case of PyPy
105 substituteInPlace src/caffe/util/io.cpp --replace \
106 'SetTotalBytesLimit(kProtoReadBytesLimit, 536870912)' \
107 'SetTotalBytesLimit(kProtoReadBytesLimit)'
108 '' + lib.optionalString (cudaSupport && lib.versionAtLeast cudatoolkit.version "9.0") ''
109 # CUDA 9.0 doesn't support sm_20
110 sed -i 's,20 21(20) ,,' cmake/Cuda.cmake
113 preConfigure = lib.optionalString pythonSupport ''
114 # We need this when building with Python bindings
115 export BOOST_LIBRARYDIR="${boost.out}/lib";
119 # Internal static library.
120 rm $out/lib/libproto.a
123 cp -a ../models $out/share/Caffe/models
125 moveToOutput "bin" "$bin"
126 '' + lib.optionalString pythonSupport ''
127 mkdir -p $out/${python.sitePackages}
128 mv $out/python/caffe $out/${python.sitePackages}
132 doInstallCheck = false; # build takes more than 30 min otherwise
133 installCheckPhase = ''
134 model=bvlc_reference_caffenet
135 m_path="$out/share/Caffe/models/$model"
136 $bin/bin/caffe test \
137 -model "$m_path/deploy.prototxt" \
138 -solver "$m_path/solver.prototxt" \
139 -weights "${test_model_weights}"
143 description = "Deep learning framework";
145 Caffe is a deep learning framework made with expression, speed, and
146 modularity in mind. It is developed by the Berkeley Vision and Learning
147 Center (BVLC) and by community contributors.
149 homepage = "http://caffe.berkeleyvision.org/";
152 (pythonSupport && (python.isPy310))
154 || !(leveldbSupport -> (leveldb != null && snappy != null))
155 || !(cudnnSupport -> (hasCudnn && cudaSupport))
156 || !(ncclSupport -> (cudaSupport && !nccl.meta.unsupported))
157 || !(pythonSupport -> (python != null && numpy != null))
159 license = licenses.bsd2;
160 platforms = platforms.linux ++ platforms.darwin;