2020-04-24 23:36:52 +00:00
|
|
|
{ config, stdenv, lib
|
|
|
|
, fetchFromGitHub
|
|
|
|
, fetchurl
|
2023-11-16 04:20:00 +00:00
|
|
|
, fetchpatch
|
2020-04-24 23:36:52 +00:00
|
|
|
, cmake
|
|
|
|
, boost
|
|
|
|
, gflags
|
|
|
|
, glog
|
|
|
|
, hdf5-cpp
|
2023-11-16 04:20:00 +00:00
|
|
|
, opencv4
|
2020-04-24 23:36:52 +00:00
|
|
|
, protobuf
|
|
|
|
, doxygen
|
|
|
|
, blas
|
|
|
|
, Accelerate, CoreGraphics, CoreVideo
|
|
|
|
, lmdbSupport ? true, lmdb
|
|
|
|
, leveldbSupport ? true, leveldb, snappy
|
2023-08-04 22:07:22 +00:00
|
|
|
, cudaSupport ? config.cudaSupport, cudaPackages ? { }
|
2022-04-15 01:41:22 +00:00
|
|
|
, cudnnSupport ? cudaSupport
|
|
|
|
, ncclSupport ? false
|
2020-04-24 23:36:52 +00:00
|
|
|
, pythonSupport ? false, python ? null, numpy ? null
|
|
|
|
, substituteAll
|
|
|
|
}:
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
let
|
2023-03-04 12:14:45 +00:00
|
|
|
inherit (cudaPackages) cudatoolkit nccl;
|
|
|
|
# The default for cudatoolkit 10.1 is CUDNN 8.0.5, the last version to support CUDA 10.1.
|
|
|
|
# However, this caffe does not build with CUDNN 8.x, so we use CUDNN 7.6.5 instead.
|
|
|
|
# Earlier versions of cudatoolkit use pre-8.x CUDNN, so we use the default.
|
2024-01-02 11:29:13 +00:00
|
|
|
hasCudnn =
|
|
|
|
if lib.versionOlder cudatoolkit.version "10.1"
|
|
|
|
then cudaPackages ? cudnn
|
|
|
|
else cudaPackages ? cudnn_7_6;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
toggle = bool: if bool then "ON" else "OFF";
|
|
|
|
|
|
|
|
test_model_weights = fetchurl {
|
|
|
|
url = "http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel";
|
|
|
|
sha256 = "472d4a06035497b180636d8a82667129960371375bd10fcb6df5c6c7631f25e0";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
stdenv.mkDerivation rec {
|
|
|
|
pname = "caffe";
|
|
|
|
version = "1.0";
|
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "BVLC";
|
|
|
|
repo = "caffe";
|
|
|
|
rev = version;
|
|
|
|
sha256 = "104jp3cm823i3cdph7hgsnj6l77ygbwsy35mdmzhmsi4jxprd9j3";
|
|
|
|
};
|
|
|
|
|
|
|
|
nativeBuildInputs = [ cmake doxygen ];
|
|
|
|
|
|
|
|
cmakeFlags =
|
|
|
|
# It's important that caffe is passed the major and minor version only because that's what
|
|
|
|
# boost_python expects
|
|
|
|
[ (if pythonSupport then "-Dpython_version=${python.pythonVersion}" else "-DBUILD_python=OFF")
|
|
|
|
"-DBLAS=open"
|
|
|
|
] ++ (if cudaSupport then [
|
|
|
|
"-DCUDA_ARCH_NAME=All"
|
|
|
|
"-DCUDA_HOST_COMPILER=${cudatoolkit.cc}/bin/cc"
|
|
|
|
] else [ "-DCPU_ONLY=ON" ])
|
|
|
|
++ ["-DUSE_NCCL=${toggle ncclSupport}"]
|
|
|
|
++ ["-DUSE_LEVELDB=${toggle leveldbSupport}"]
|
|
|
|
++ ["-DUSE_LMDB=${toggle lmdbSupport}"];
|
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
buildInputs = [ boost gflags glog protobuf hdf5-cpp opencv4 blas ]
|
2020-04-24 23:36:52 +00:00
|
|
|
++ lib.optional cudaSupport cudatoolkit
|
2024-01-02 11:29:13 +00:00
|
|
|
++ lib.optional (lib.versionOlder cudatoolkit.version "10.1" && hasCudnn) cudaPackages.cudnn
|
|
|
|
++ lib.optional (lib.versionAtLeast cudatoolkit.version "10.1" && hasCudnn) cudaPackages.cudnn_7_6
|
2020-04-24 23:36:52 +00:00
|
|
|
++ lib.optional lmdbSupport lmdb
|
|
|
|
++ lib.optional ncclSupport nccl
|
|
|
|
++ lib.optionals leveldbSupport [ leveldb snappy ]
|
|
|
|
++ lib.optionals pythonSupport [ python numpy ]
|
|
|
|
++ lib.optionals stdenv.isDarwin [ Accelerate CoreGraphics CoreVideo ]
|
|
|
|
;
|
|
|
|
|
|
|
|
propagatedBuildInputs = lib.optionals pythonSupport (
|
|
|
|
# requirements.txt
|
|
|
|
let pp = python.pkgs; in ([
|
2023-05-24 13:37:59 +00:00
|
|
|
pp.numpy pp.scipy pp.scikit-image pp.h5py
|
2020-04-24 23:36:52 +00:00
|
|
|
pp.matplotlib pp.ipython pp.networkx pp.nose
|
2021-07-04 02:40:35 +00:00
|
|
|
pp.pandas pp.python-dateutil pp.protobuf pp.gflags
|
2020-04-24 23:36:52 +00:00
|
|
|
pp.pyyaml pp.pillow pp.six
|
|
|
|
] ++ lib.optional leveldbSupport pp.leveldb)
|
|
|
|
);
|
|
|
|
|
|
|
|
outputs = [ "bin" "out" ];
|
|
|
|
propagatedBuildOutputs = []; # otherwise propagates out -> bin cycle
|
|
|
|
|
|
|
|
patches = [
|
|
|
|
./darwin.patch
|
2023-11-16 04:20:00 +00:00
|
|
|
(fetchpatch {
|
|
|
|
name = "support-opencv4";
|
|
|
|
url = "https://github.com/BVLC/caffe/pull/6638/commits/0a04cc2ccd37ba36843c18fea2d5cbae6e7dd2b5.patch";
|
|
|
|
hash = "sha256-ZegTvp0tTHlopQv+UzHDigs6XLkP2VfqLCWXl6aKJSI=";
|
|
|
|
})
|
2020-04-24 23:36:52 +00:00
|
|
|
] ++ lib.optional pythonSupport (substituteAll {
|
|
|
|
src = ./python.patch;
|
|
|
|
inherit (python.sourceVersion) major minor; # Should be changed in case of PyPy
|
|
|
|
});
|
|
|
|
|
2021-09-26 12:46:18 +00:00
|
|
|
postPatch = ''
|
|
|
|
substituteInPlace src/caffe/util/io.cpp --replace \
|
|
|
|
'SetTotalBytesLimit(kProtoReadBytesLimit, 536870912)' \
|
|
|
|
'SetTotalBytesLimit(kProtoReadBytesLimit)'
|
|
|
|
'' + lib.optionalString (cudaSupport && lib.versionAtLeast cudatoolkit.version "9.0") ''
|
2020-04-24 23:36:52 +00:00
|
|
|
# CUDA 9.0 doesn't support sm_20
|
|
|
|
sed -i 's,20 21(20) ,,' cmake/Cuda.cmake
|
|
|
|
'';
|
|
|
|
|
|
|
|
preConfigure = lib.optionalString pythonSupport ''
|
|
|
|
# We need this when building with Python bindings
|
|
|
|
export BOOST_LIBRARYDIR="${boost.out}/lib";
|
|
|
|
'';
|
|
|
|
|
|
|
|
postInstall = ''
|
|
|
|
# Internal static library.
|
|
|
|
rm $out/lib/libproto.a
|
|
|
|
|
|
|
|
# Install models
|
|
|
|
cp -a ../models $out/share/Caffe/models
|
|
|
|
|
|
|
|
moveToOutput "bin" "$bin"
|
|
|
|
'' + lib.optionalString pythonSupport ''
|
|
|
|
mkdir -p $out/${python.sitePackages}
|
|
|
|
mv $out/python/caffe $out/${python.sitePackages}
|
|
|
|
rm -rf $out/python
|
|
|
|
'';
|
|
|
|
|
|
|
|
doInstallCheck = false; # build takes more than 30 min otherwise
|
|
|
|
installCheckPhase = ''
|
|
|
|
model=bvlc_reference_caffenet
|
|
|
|
m_path="$out/share/Caffe/models/$model"
|
|
|
|
$bin/bin/caffe test \
|
|
|
|
-model "$m_path/deploy.prototxt" \
|
|
|
|
-solver "$m_path/solver.prototxt" \
|
|
|
|
-weights "${test_model_weights}"
|
|
|
|
'';
|
|
|
|
|
2021-01-15 22:18:51 +00:00
|
|
|
meta = with lib; {
|
2020-04-24 23:36:52 +00:00
|
|
|
description = "Deep learning framework";
|
|
|
|
longDescription = ''
|
|
|
|
Caffe is a deep learning framework made with expression, speed, and
|
|
|
|
modularity in mind. It is developed by the Berkeley Vision and Learning
|
|
|
|
Center (BVLC) and by community contributors.
|
|
|
|
'';
|
|
|
|
homepage = "http://caffe.berkeleyvision.org/";
|
2022-04-15 01:41:22 +00:00
|
|
|
maintainers = with maintainers; [ ];
|
2024-01-02 11:29:13 +00:00
|
|
|
broken =
|
|
|
|
(pythonSupport && (python.isPy310))
|
|
|
|
|| cudaSupport
|
|
|
|
|| !(leveldbSupport -> (leveldb != null && snappy != null))
|
|
|
|
|| !(cudnnSupport -> (hasCudnn && cudaSupport))
|
2024-01-13 08:15:51 +00:00
|
|
|
|| !(ncclSupport -> (cudaSupport && !nccl.meta.unsupported))
|
2024-01-02 11:29:13 +00:00
|
|
|
|| !(pythonSupport -> (python != null && numpy != null))
|
|
|
|
;
|
2020-04-24 23:36:52 +00:00
|
|
|
license = licenses.bsd2;
|
|
|
|
platforms = platforms.linux ++ platforms.darwin;
|
|
|
|
};
|
|
|
|
}
|