nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 fetchpatch,
6 rocmUpdateScript,
7 runCommand,
8 pkg-config,
9 cmake,
10 rocm-cmake,
11 rocblas,
12 rocmlir,
13 rocrand,
14 rocm-runtime,
15 rocm-merged-llvm,
16 hipblas-common,
17 hipblas,
18 hipblaslt,
19 clr,
20 composable_kernel,
21 frugally-deep,
22 rocm-docs-core,
23 half,
24 boost,
25 sqlite,
26 bzip2,
27 lbzip2,
28 nlohmann_json,
29 texliveSmall,
30 doxygen,
31 sphinx,
32 zlib,
33 gtest,
34 rocm-comgr,
35 roctracer,
36 python3Packages,
37 # FIXME: should be able to use all clr targets
38 gpuTargets ? [
39 "gfx900"
40 "gfx906"
41 "gfx908"
42 "gfx90a"
43 "gfx942"
44 "gfx1030"
45 "gfx1100"
46 "gfx1101"
47 "gfx1102"
48 ], # clr.gpuTargets
49 buildDocs ? false, # Needs internet because of rocm-docs-core
50 buildTests ? false,
51 withComposableKernel ? composable_kernel.anyGfx9Target,
52}:
53
54let
55 # FIXME: cmake files need patched to include this properly
56 cFlags = "-O3 -DNDEBUG -Wno-documentation-pedantic --offload-compress -I${hipblas-common}/include -I${hipblas}/include -I${roctracer}/include -I${nlohmann_json}/include -I${sqlite.dev}/include -I${rocrand}/include";
57 version = "6.3.3";
58
59 src = fetchFromGitHub {
60 owner = "ROCm";
61 repo = "MIOpen";
62 rev = "rocm-${version}";
63 hash = "sha256-rX+BE6wBDMnLyc6eai3bDVvmfahomDO0s10n6HhWu7c=";
64 fetchLFS = true;
65 fetchSubmodules = true;
66 # WORKAROUND: .lfsconfig is incorrectly set to exclude everything upstream
67 leaveDotGit = true;
68 postFetch = ''
69 export HOME=$(mktemp -d)
70 cd $out
71 set -x
72 git remote add origin $url
73 git fetch origin +refs/tags/rocm-${version}:refs/tags/rocm-${version}
74 git clean -fdx
75 git switch -c rocm-${version} refs/tags/rocm-${version}
76 git config lfs.fetchexclude "none"
77 rm .lfsconfig
78 git lfs install
79 git lfs track "*.kdb.bz2"
80 GIT_TRACE=1 git lfs fetch --include="src/kernels/**"
81 GIT_TRACE=1 git lfs pull --include="src/kernels/**"
82 git lfs checkout
83
84 rm -rf .git
85 '';
86 };
87
88 latex = lib.optionalAttrs buildDocs (
89 texliveSmall.withPackages (
90 ps: with ps; [
91 latexmk
92 tex-gyre
93 fncychap
94 wrapfig
95 capt-of
96 framed
97 needspace
98 tabulary
99 varwidth
100 titlesec
101 ]
102 )
103 );
104
105 gfx900 = runCommand "miopen-gfx900.kdb" { preferLocalBuild = true; } ''
106 ${lbzip2}/bin/lbzip2 -ckd ${src}/src/kernels/gfx900.kdb.bz2 > $out
107 '';
108
109 gfx906 = runCommand "miopen-gfx906.kdb" { preferLocalBuild = true; } ''
110 ${lbzip2}/bin/lbzip2 -ckd ${src}/src/kernels/gfx906.kdb.bz2 > $out
111 '';
112
113 gfx908 = runCommand "miopen-gfx908.kdb" { preferLocalBuild = true; } ''
114 ${lbzip2}/bin/lbzip2 -ckd ${src}/src/kernels/gfx908.kdb.bz2 > $out
115 '';
116
117 gfx90a = runCommand "miopen-gfx90a.kdb" { preferLocalBuild = true; } ''
118 ${lbzip2}/bin/lbzip2 -ckd ${src}/src/kernels/gfx90a.kdb.bz2 > $out
119 '';
120
121 gfx1030 = runCommand "miopen-gfx1030.kdb" { preferLocalBuild = true; } ''
122 ${lbzip2}/bin/lbzip2 -ckd ${src}/src/kernels/gfx1030.kdb.bz2 > $out
123 '';
124in
125stdenv.mkDerivation (finalAttrs: {
126 inherit version src;
127 pname = "miopen";
128
129 env.CFLAGS = cFlags;
130 env.CXXFLAGS = cFlags;
131
132 # Find zstd and add to target. Mainly for torch.
133 patches = [
134 ./skip-preexisting-dbs.patch
135 ./fix-isnan.patch # https://github.com/ROCm/MIOpen/pull/3448
136 (fetchpatch {
137 url = "https://github.com/ROCm/MIOpen/commit/e608b4325646afeabb5e52846997b926d2019d19.patch";
138 hash = "sha256-oxa3qlIC2bzbwGxrQOZXoY/S7CpLsMrnWRB7Og0tk0M=";
139 })
140 (fetchpatch {
141 url = "https://github.com/ROCm/MIOpen/commit/3413d2daaeb44b7d6eadcc03033a5954a118491e.patch";
142 hash = "sha256-ST4snUcTmmSI1Ogx815KEX9GdMnmubsavDzXCGJkiKs=";
143 })
144 # FIXME: We need to rebase or drop this arch compat patch
145 # https://github.com/ROCm/MIOpen/issues/3540 suggests that
146 # arch compat patching doesn't work correctly for gfx1031
147 # (fetchpatch {
148 # name = "Extend-MIOpen-ISA-compatibility.patch";
149 # url = "https://github.com/GZGavinZhao/MIOpen/commit/416088b534618bd669a765afce59cfc7197064c1.patch";
150 # hash = "sha256-OwONCA68y8s2GqtQj+OtotXwUXQ5jM8tpeM92iaD4MU=";
151 # })
152 ];
153
154 outputs = [
155 "out"
156 ]
157 ++ lib.optionals buildDocs [
158 "doc"
159 ]
160 ++ lib.optionals buildTests [
161 "test"
162 ];
163 enableParallelBuilding = true;
164 env.ROCM_PATH = clr;
165 env.LD_LIBRARY_PATH = lib.makeLibraryPath [ rocm-runtime ];
166 env.HIP_CLANG_PATH = "${rocm-merged-llvm}/bin";
167
168 nativeBuildInputs = [
169 pkg-config
170 cmake
171 rocm-cmake
172 clr
173 ];
174
175 buildInputs = [
176 hipblas
177 hipblas-common
178 rocblas
179 rocmlir
180 half
181 boost
182 sqlite
183 bzip2
184 nlohmann_json
185 frugally-deep
186 roctracer
187 rocrand
188 hipblaslt
189 ]
190 ++ lib.optionals withComposableKernel [
191 composable_kernel
192 ]
193 ++ lib.optionals buildDocs [
194 latex
195 doxygen
196 sphinx
197 rocm-docs-core
198 python3Packages.sphinx-rtd-theme
199 python3Packages.breathe
200 python3Packages.myst-parser
201 ]
202 ++ lib.optionals buildTests [
203 gtest
204 zlib
205 ];
206
207 cmakeFlags = [
208 "-DAMDGPU_TARGETS=${lib.concatStringsSep ";" gpuTargets}"
209 "-DGPU_TARGETS=${lib.concatStringsSep ";" gpuTargets}"
210 "-DGPU_ARCHS=${lib.concatStringsSep ";" gpuTargets}"
211 "-DMIOPEN_USE_SQLITE_PERFDB=ON"
212 "-DCMAKE_VERBOSE_MAKEFILE=ON"
213 "-DCMAKE_MODULE_PATH=${clr}/hip/cmake"
214 "-DCMAKE_BUILD_TYPE=Release"
215
216 # needs to stream to stdout so bzcat rather than bunzip2
217 "-DUNZIPPER=${bzip2}/bin/bzcat"
218
219 "-DCMAKE_C_COMPILER=amdclang"
220 "-DCMAKE_CXX_COMPILER=amdclang++"
221 "-DROCM_PATH=${clr}"
222 "-DHIP_ROOT_DIR=${clr}"
223 (lib.cmakeBool "MIOPEN_USE_ROCBLAS" true)
224 (lib.cmakeBool "MIOPEN_USE_HIPBLASLT" true)
225 (lib.cmakeBool "MIOPEN_USE_COMPOSABLEKERNEL" withComposableKernel)
226 (lib.cmakeBool "MIOPEN_USE_HIPRTC" true)
227 (lib.cmakeBool "MIOPEN_USE_COMGR" true)
228 "-DCMAKE_HIP_COMPILER_ROCM_ROOT=${clr}"
229 # Manually define CMAKE_INSTALL_<DIR>
230 # See: https://github.com/NixOS/nixpkgs/pull/197838
231 "-DCMAKE_INSTALL_BINDIR=bin"
232 "-DCMAKE_INSTALL_LIBDIR=lib"
233 "-DCMAKE_INSTALL_INCLUDEDIR=include"
234 "-DMIOPEN_BACKEND=HIP"
235 ]
236 ++ lib.optionals buildTests [
237 "-DBUILD_TESTS=ON"
238 "-DMIOPEN_TEST_ALL=ON"
239 ];
240
241 postPatch = ''
242 substituteInPlace cmake/ClangTidy.cmake \
243 --replace-fail 'macro(enable_clang_tidy)' 'macro(enable_clang_tidy)
244 endmacro()
245 macro(enable_clang_tidy_unused)' \
246 --replace-fail 'function(clang_tidy_check TARGET)' 'function(clang_tidy_check TARGET)
247 return()'
248
249 patchShebangs test src/composable_kernel fin utils install_deps.cmake
250
251 ln -sf ${gfx900} src/kernels/gfx900.kdb
252 ln -sf ${gfx906} src/kernels/gfx906.kdb
253 ln -sf ${gfx908} src/kernels/gfx908.kdb
254 ln -sf ${gfx90a} src/kernels/gfx90a.kdb
255 ln -sf ${gfx1030} src/kernels/gfx1030.kdb
256 mkdir -p build/share/miopen/db/
257 ln -sf ${gfx900} build/share/miopen/db/gfx900.kdb
258 ln -sf ${gfx906} build/share/miopen/db/gfx906.kdb
259 ln -sf ${gfx908} build/share/miopen/db/gfx908.kdb
260 ln -sf ${gfx90a} build/share/miopen/db/gfx90a.kdb
261 ln -sf ${gfx1030} build/share/miopen/db/gfx1030.kdb
262 '';
263
264 # Unfortunately, it seems like we have to call make on these manually
265 postBuild =
266 lib.optionalString buildDocs ''
267 python -m sphinx -T -E -b html -d _build/doctrees -D language=en ../docs _build/html
268 ''
269 + lib.optionalString buildTests ''
270 make -j$NIX_BUILD_CORES check
271 '';
272
273 postInstall = ''
274 rm $out/bin/install_precompiled_kernels.sh
275 ln -sf ${gfx900} $out/share/miopen/db/gfx900.kdb
276 ln -sf ${gfx906} $out/share/miopen/db/gfx906.kdb
277 ln -sf ${gfx908} $out/share/miopen/db/gfx908.kdb
278 ln -sf ${gfx90a} $out/share/miopen/db/gfx90a.kdb
279 ln -sf ${gfx1030} $out/share/miopen/db/gfx1030.kdb
280 ''
281 + lib.optionalString buildDocs ''
282 mv ../doc/html $out/share/doc/miopen-hip
283 ''
284 + lib.optionalString buildTests ''
285 mkdir -p $test/bin
286 mv bin/test_* $test/bin
287 patchelf --set-rpath $out/lib:${
288 lib.makeLibraryPath (
289 finalAttrs.buildInputs
290 ++ [
291 clr
292 rocm-comgr
293 ]
294 )
295 } $test/bin/*
296 '';
297
298 requiredSystemFeatures = [ "big-parallel" ];
299
300 passthru.updateScript = rocmUpdateScript {
301 name = finalAttrs.pname;
302 inherit (finalAttrs.src) owner;
303 inherit (finalAttrs.src) repo;
304 };
305
306 meta = with lib; {
307 description = "Machine intelligence library for ROCm";
308 homepage = "https://github.com/ROCm/MIOpen";
309 license = with licenses; [ mit ];
310 teams = [ teams.rocm ];
311 platforms = platforms.linux;
312 };
313})