2024-12-13 23:54:25 +01:00
{
lib ,
stdenv ,
cmake ,
apple-sdk_11 ,
ninja ,
fetchFromGitHub ,
SDL2 ,
wget ,
which ,
autoAddDriverRunpath ,
makeWrapper ,
metalSupport ? stdenv . hostPlatform . isDarwin && stdenv . hostPlatform . isAarch64 ,
coreMLSupport ? stdenv . hostPlatform . isDarwin && false , # FIXME currently broken
config ,
cudaSupport ? config . cudaSupport ,
cudaPackages ? { } ,
rocmSupport ? config . rocmSupport ,
rocmPackages ? { } ,
rocmGpuTargets ? builtins . concatStringsSep " ; " rocmPackages . clr . gpuTargets ,
vulkanSupport ? false ,
shaderc ,
vulkan-headers ,
vulkan-loader ,
withSDL ? true ,
2022-12-07 20:56:15 +05:30
} :
2024-12-13 23:54:25 +01:00
assert metalSupport -> stdenv . hostPlatform . isDarwin ;
assert coreMLSupport -> stdenv . hostPlatform . isDarwin ;
2024-01-19 00:39:31 +08:00
let
# It's necessary to consistently use backendStdenv when building with CUDA support,
# otherwise we get libstdc++ errors downstream.
# cuda imposes an upper bound on the gcc version, e.g. the latest gcc compatible with cudaPackages_11 is gcc11
effectiveStdenv = if cudaSupport then cudaPackages . backendStdenv else stdenv ;
2024-12-13 23:54:25 +01:00
inherit ( lib )
cmakeBool
cmakeFeature
optional
optionals
optionalString
forEach
;
darwinBuildInputs = [ apple-sdk_11 ] ;
cudaBuildInputs = with cudaPackages ; [
cuda_cccl # <nv/target>
# A temporary hack for reducing the closure size, remove once cudaPackages
# have stopped using lndir: https://github.com/NixOS/nixpkgs/issues/271792
cuda_cudart
libcublas
] ;
rocmBuildInputs = with rocmPackages ; [
clr
hipblas
rocblas
] ;
vulkanBuildInputs = [
shaderc
vulkan-headers
vulkan-loader
] ;
2024-01-19 00:39:31 +08:00
in
effectiveStdenv . mkDerivation ( finalAttrs : {
2022-12-07 20:56:15 +05:30
pname = " w h i s p e r - c p p " ;
2024-11-20 22:58:10 +00:00
version = " 1 . 7 . 2 " ;
2022-12-07 20:56:15 +05:30
src = fetchFromGitHub {
owner = " g g e r g a n o v " ;
repo = " w h i s p e r . c p p " ;
2024-12-13 23:54:25 +01:00
rev = " r e f s / t a g s / v ${ finalAttrs . version } " ;
2024-11-20 22:58:10 +00:00
hash = " s h a 2 5 6 - y 3 0 Z c c p F 3 S C d R G a + P 3 d d F 1 t T 1 K n v l I 4 F e x x 8 1 w Z x f T k = " ;
2022-12-07 20:56:15 +05:30
} ;
# The upstream download script tries to download the models to the
# directory of the script, which is not writable due to being
# inside the nix store. This patch changes the script to download
# the models to the current directory of where it is being run from.
patches = [ ./download-models.patch ] ;
2024-12-13 23:54:25 +01:00
postPatch = ''
for target in examples / { bench , command , main , quantize , server , stream , talk } /CMakeLists.txt ; do
if ! grep - q - F ' install ( ' ; then
echo ' install ( TARGETS '' ${ TARGET } R U N T I M E ) ' > > $t a r g e t
fi
done
'' ;
nativeBuildInputs =
[
cmake
ninja
2024-01-24 17:55:58 -05:00
which
2024-01-19 00:39:31 +08:00
makeWrapper
2024-12-13 23:54:25 +01:00
]
++ lib . optionals cudaSupport [
2024-03-30 11:06:48 -07:00
cudaPackages . cuda_nvcc
2024-01-17 16:32:24 +01:00
autoAddDriverRunpath
2024-03-30 11:06:48 -07:00
] ;
2024-01-19 00:39:31 +08:00
2024-12-13 23:54:25 +01:00
buildInputs =
optional withSDL SDL2
++ optionals effectiveStdenv . hostPlatform . isDarwin darwinBuildInputs
++ optionals cudaSupport cudaBuildInputs
++ optionals rocmSupport rocmBuildInputs
++ optionals vulkanSupport vulkanBuildInputs ;
cmakeFlags =
[
( cmakeBool " W H I S P E R _ B U I L D _ E X A M P L E S " true )
( cmakeBool " G G M L _ C U D A " cudaSupport )
( cmakeBool " G G M L _ H I P B L A S " rocmSupport )
( cmakeBool " G G M L _ V U L K A N " vulkanSupport )
( cmakeBool " W H I S P E R _ S D L 2 " withSDL )
( cmakeBool " G G M L _ L T O " true )
( cmakeBool " G G M L _ N A T I V E " false )
( cmakeBool " B U I L D _ S H A R E D _ L I B S " ( ! effectiveStdenv . hostPlatform . isStatic ) )
]
++ optionals ( effectiveStdenv . hostPlatform . isx86 && ! effectiveStdenv . hostPlatform . isStatic ) [
( cmakeBool " G G M L _ B A C K E N D _ D L " true )
( cmakeBool " G G M L _ C P U _ A L L _ V A R I A N T S " true )
]
++ optionals cudaSupport [
( cmakeFeature " C M A K E _ C U D A _ A R C H I T E C T U R E S " cudaPackages . flags . cmakeCudaArchitecturesString )
]
++ optionals rocmSupport [
( cmakeFeature " C M A K E _ C _ C O M P I L E R " " h i p c c " )
( cmakeFeature " C M A K E _ C X X _ C O M P I L E R " " h i p c c " )
# Build all targets supported by rocBLAS. When updating search for TARGET_LIST_ROCM
# in https://github.com/ROCmSoftwarePlatform/rocBLAS/blob/develop/CMakeLists.txt
# and select the line that matches the current nixpkgs version of rocBLAS.
" - D A M D G P U _ T A R G E T S = ${ rocmGpuTargets } "
]
++ optionals coreMLSupport [
( cmakeBool " W H I S P E R _ C O R E M L " true )
( cmakeBool " W H I S P E R _ C O R E M L _ A L L O W _ F A L L B A C K " true )
]
++ optionals metalSupport [
( cmakeFeature " C M A K E _ C _ F L A G S " " - D _ _ A R M _ F E A T U R E _ D O T P R O D = 1 " )
( cmakeBool " G G M L _ M E T A L " true )
( cmakeBool " G G M L _ M E T A L _ E M B E D _ L I B R A R Y " true )
] ;
2024-10-19 01:26:09 +03:00
2024-12-13 23:54:25 +01:00
postInstall = ''
# Add "whisper-cpp" prefix before every command
mv - v $ out/bin / { main , whisper-cpp }
2024-10-19 01:26:09 +03:00
2024-12-13 23:54:25 +01:00
for file in $ out/bin /* ; d o
if [ [ - x " $ f i l e " && - f " $ f i l e " && " $ ( b a s e n a m e $ f i l e ) " != " w h i s p e r - c p p " ] ] ; then
mv - v " $ f i l e " " $ o u t / b i n / w h i s p e r - c p p - $ ( b a s e n a m e $ f i l e ) "
2024-10-19 01:26:09 +03:00
fi
done
2022-12-07 20:56:15 +05:30
2024-12-13 23:54:25 +01:00
install - v - D - m755 $ src/models/download-ggml-model.sh $ out/bin/whisper-cpp-download-ggml-model
2022-12-07 20:56:15 +05:30
wrapProgram $ out/bin/whisper-cpp-download-ggml-model \
2024-12-13 23:54:25 +01:00
- - prefix PATH : $ { lib . makeBinPath [ wget ] }
'' ;
requiredSystemFeatures = optionals rocmSupport [ " b i g - p a r a l l e l " ] ; # rocmSupport multiplies build time by the number of GPU targets, which takes arround 30 minutes on a 16-cores system to build
2022-12-07 20:56:15 +05:30
2024-12-13 23:54:25 +01:00
doInstallCheck = true ;
installCheckPhase = ''
runHook preInstallCheck
$ out/bin/whisper-cpp - - help > /dev/null
runHook postInstallCheck
2022-12-07 20:56:15 +05:30
'' ;
2024-12-13 23:54:25 +01:00
meta = {
2022-12-07 20:56:15 +05:30
description = " P o r t o f O p e n A I ' s W h i s p e r m o d e l i n C / C + + " ;
longDescription = ''
To download the models as described in the project's readme , you may
use the ` whisper-cpp-download-ggml-model ` binary from this package .
'' ;
homepage = " h t t p s : / / g i t h u b . c o m / g g e r g a n o v / w h i s p e r . c p p " ;
2024-12-13 23:54:25 +01:00
license = lib . licenses . mit ;
mainProgram = " w h i s p e r - c p p " ;
platforms = lib . platforms . all ;
broken = coreMLSupport ;
badPlatforms = optionals cudaSupport lib . platforms . darwin ;
maintainers = with lib . maintainers ; [
dit7ya
hughobrien
aviallon
] ;
2022-12-07 20:56:15 +05:30
} ;
2024-01-19 00:39:31 +08:00
} )