1{ lib
2, stdenv
3, fetchFromGitHub
4, fetchurl
5, rocmUpdateScript
6, pkg-config
7, cmake
8, rocm-cmake
9, rocblas
10, rocmlir
11, hip
12, clang-tools-extra
13, clang-ocl
14, llvm
15, miopengemm
16, composable_kernel
17, half
18, boost
19, sqlite
20, bzip2
21, nlohmann_json
22, texlive
23, doxygen
24, sphinx
25, zlib
26, gtest
27, rocm-comgr
28, python3Packages
29, buildDocs ? true
30, buildTests ? false
31, fetchKDBs ? true
32, useOpenCL ? false
33}:
34
35let
36 latex = lib.optionalAttrs buildDocs texlive.combine {
37 inherit (texlive) scheme-small
38 latexmk
39 tex-gyre
40 fncychap
41 wrapfig
42 capt-of
43 framed
44 needspace
45 tabulary
46 varwidth
47 titlesec;
48 };
49
50 kdbs = lib.optionalAttrs fetchKDBs import ./deps.nix {
51 inherit fetchurl;
52 mirror = "https://repo.radeon.com/rocm/miopen-kernel/rel-5.0";
53 };
54in stdenv.mkDerivation (finalAttrs: {
55 pname = "miopen";
56 version = "5.4.2";
57
58 outputs = [
59 "out"
60 ] ++ lib.optionals buildDocs [
61 "doc"
62 ] ++ lib.optionals buildTests [
63 "test"
64 ];
65
66 src = fetchFromGitHub {
67 owner = "ROCmSoftwarePlatform";
68 repo = "MIOpen";
69 rev = "rocm-${finalAttrs.version}";
70 hash = "sha256-GfXPCXiVJVve3d8sQCQcFLb/vEnKkVEn7xYUhHkEEVI=";
71 };
72
73 nativeBuildInputs = [
74 pkg-config
75 cmake
76 rocm-cmake
77 hip
78 clang-tools-extra
79 ];
80
81 buildInputs = [
82 llvm
83 rocblas
84 rocmlir
85 clang-ocl
86 miopengemm
87 composable_kernel
88 half
89 boost
90 sqlite
91 bzip2
92 nlohmann_json
93 ] ++ lib.optionals buildDocs [
94 latex
95 doxygen
96 sphinx
97 python3Packages.sphinx-rtd-theme
98 python3Packages.breathe
99 python3Packages.myst-parser
100 ] ++ lib.optionals buildTests [
101 zlib
102 ];
103
104 cmakeFlags = [
105 "-DMIOPEN_USE_MIOPENGEMM=ON"
106 # Manually define CMAKE_INSTALL_<DIR>
107 # See: https://github.com/NixOS/nixpkgs/pull/197838
108 "-DCMAKE_INSTALL_BINDIR=bin"
109 "-DCMAKE_INSTALL_LIBDIR=lib"
110 "-DCMAKE_INSTALL_INCLUDEDIR=include"
111 ] ++ lib.optionals (!useOpenCL) [
112 "-DCMAKE_C_COMPILER=hipcc"
113 "-DCMAKE_CXX_COMPILER=hipcc"
114 "-DMIOPEN_BACKEND=HIP"
115 ] ++ lib.optionals useOpenCL [
116 "-DMIOPEN_BACKEND=OpenCL"
117 ] ++ lib.optionals buildTests [
118 "-DBUILD_TESTS=ON"
119 "-DMIOPEN_TEST_ALL=ON"
120 "-DMIOPEN_TEST_GFX900=ON"
121 "-DMIOPEN_TEST_GFX906=ON"
122 "-DMIOPEN_TEST_GFX908=ON"
123 "-DMIOPEN_TEST_GFX90A=ON"
124 "-DMIOPEN_TEST_GFX103X=ON"
125 "-DGOOGLETEST_DIR=${gtest.src}" # Custom linker names
126 ];
127
128 postPatch = ''
129 substituteInPlace CMakeLists.txt \
130 --replace "enable_testing()" "" \
131 --replace "MIOPEN_HIP_COMPILER MATCHES \".*clang\\\\+\\\\+$\"" "true" \
132 --replace "set(MIOPEN_TIDY_ERRORS ALL)" "" # error: missing required key 'key'
133 '' + lib.optionalString buildTests ''
134 substituteInPlace test/gtest/CMakeLists.txt \
135 --replace "enable_testing()" ""
136 '' + lib.optionalString (!buildTests) ''
137 substituteInPlace CMakeLists.txt \
138 --replace "add_subdirectory(test)" ""
139 '' + lib.optionalString fetchKDBs ''
140 ln -sf ${kdbs.gfx1030_36} src/kernels/gfx1030_36.kdb
141 ln -sf ${kdbs.gfx900_56} src/kernels/gfx900_56.kdb
142 ln -sf ${kdbs.gfx900_64} src/kernels/gfx900_64.kdb
143 ln -sf ${kdbs.gfx906_60} src/kernels/gfx906_60.kdb
144 ln -sf ${kdbs.gfx906_64} src/kernels/gfx906_64.kdb
145 ln -sf ${kdbs.gfx90878} src/kernels/gfx90878.kdb
146 ln -sf ${kdbs.gfx90a68} src/kernels/gfx90a68.kdb
147 ln -sf ${kdbs.gfx90a6e} src/kernels/gfx90a6e.kdb
148 '';
149
150 # Unfortunately, it seems like we have to call make on these manually
151 postBuild = lib.optionalString buildDocs ''
152 export HOME=$(mktemp -d)
153 make -j$NIX_BUILD_CORES doc
154 '' + lib.optionalString buildTests ''
155 make -j$NIX_BUILD_CORES check
156 '';
157
158 postInstall = ''
159 rm $out/bin/install_precompiled_kernels.sh
160 '' + lib.optionalString buildDocs ''
161 mv ../doc/html $out/share/doc/miopen-${if useOpenCL then "opencl" else "hip"}
162 mv ../doc/pdf/miopen.pdf $out/share/doc/miopen-${if useOpenCL then "opencl" else "hip"}
163 '' + lib.optionalString buildTests ''
164 mkdir -p $test/bin
165 mv bin/test_* $test/bin
166 patchelf --set-rpath $out/lib:${lib.makeLibraryPath (finalAttrs.buildInputs ++
167 [ hip rocm-comgr ])} $test/bin/*
168 '' + lib.optionalString fetchKDBs ''
169 # Apparently gfx1030_40 wasn't generated so the developers suggest just renaming gfx1030_36 to it
170 # Should be fixed in the next miopen kernel generation batch
171 ln -s ${kdbs.gfx1030_36} $out/share/miopen/db/gfx1030_40.kdb
172 '';
173
174 requiredSystemFeatures = [ "big-parallel" ];
175
176 passthru.updateScript = rocmUpdateScript {
177 name = finalAttrs.pname;
178 owner = finalAttrs.src.owner;
179 repo = finalAttrs.src.repo;
180 };
181
182 meta = with lib; {
183 description = "Machine intelligence library for ROCm";
184 homepage = "https://github.com/ROCmSoftwarePlatform/MIOpen";
185 license = with licenses; [ mit ];
186 maintainers = teams.rocm.members;
187 platforms = platforms.linux;
188 broken = versions.minor finalAttrs.version != versions.minor hip.version;
189 };
190})