|
| 1 | +# Copyright 2024-2025 Gentoo Authors |
| 2 | +# Distributed under the terms of the GNU General Public License v2 |
| 3 | + |
| 4 | +EAPI=8 |
| 5 | + |
| 6 | +# supports ROCM/HIP >=5.5, but we define 6.1 due to the eclass |
| 7 | +ROCM_VERSION="6.1" |
| 8 | +inherit cuda rocm |
| 9 | +inherit cmake |
| 10 | +inherit flag-o-matic go-module linux-info systemd toolchain-funcs |
| 11 | + |
| 12 | +DESCRIPTION="Get up and running with Llama 3, Mistral, Gemma, and other language models." |
| 13 | +HOMEPAGE="https://ollama.com" |
| 14 | + |
| 15 | +if [[ ${PV} == *9999* ]]; then |
| 16 | + inherit git-r3 |
| 17 | + EGIT_REPO_URI="https://github.com/ollama/ollama.git" |
| 18 | +else |
| 19 | + SRC_URI=" |
| 20 | + https://github.com/ollama/${PN}/archive/refs/tags/v${PV}.tar.gz -> ${P}.gh.tar.gz |
| 21 | + https://github.com/gentoo-golang-dist/${PN}/releases/download/v${PV}/${P}-deps.tar.xz |
| 22 | + " |
| 23 | + KEYWORDS="~amd64" |
| 24 | +fi |
| 25 | + |
| 26 | +LICENSE="MIT" |
| 27 | +SLOT="0" |
| 28 | + |
| 29 | +X86_CPU_FLAGS=( |
| 30 | + sse4_2 |
| 31 | + avx |
| 32 | + f16c |
| 33 | + avx2 |
| 34 | + bmi2 |
| 35 | + fma3 |
| 36 | + avx512f |
| 37 | + avx512vbmi |
| 38 | + avx512_vnni |
| 39 | + avx_vnni |
| 40 | +) |
| 41 | +CPU_FLAGS=( "${X86_CPU_FLAGS[@]/#/cpu_flags_x86_}" ) |
| 42 | +IUSE="blas ${CPU_FLAGS[*]} cuda mkl rocm vulkan" |
| 43 | +# IUSE+=" opencl" |
| 44 | + |
| 45 | +RESTRICT="mirror test" |
| 46 | + |
| 47 | +COMMON_DEPEND=" |
| 48 | + blas? ( |
| 49 | + !mkl? ( |
| 50 | + virtual/blas |
| 51 | + ) |
| 52 | + mkl? ( |
| 53 | + sci-libs/mkl |
| 54 | + ) |
| 55 | + ) |
| 56 | + cuda? ( |
| 57 | + dev-util/nvidia-cuda-toolkit:= |
| 58 | + ) |
| 59 | + rocm? ( |
| 60 | + >=dev-util/hip-${ROCM_VERSION}:= |
| 61 | + >=sci-libs/hipBLAS-${ROCM_VERSION}:= |
| 62 | + >=sci-libs/rocBLAS-${ROCM_VERSION}:= |
| 63 | + ) |
| 64 | +" |
| 65 | + |
| 66 | +DEPEND=" |
| 67 | + ${COMMON_DEPEND} |
| 68 | + >=dev-lang/go-1.23.4 |
| 69 | +" |
| 70 | +BDEPEND=" |
| 71 | + vulkan? ( |
| 72 | + dev-util/vulkan-headers |
| 73 | + media-libs/shaderc |
| 74 | + ) |
| 75 | +" |
| 76 | + |
| 77 | +RDEPEND=" |
| 78 | + ${COMMON_DEPEND} |
| 79 | + acct-group/${PN} |
| 80 | + >=acct-user/${PN}-3[cuda?] |
| 81 | +" |
| 82 | + |
| 83 | +PATCHES=( |
| 84 | + "${FILESDIR}/${PN}-9999-use-GNUInstallDirs.patch" |
| 85 | +) |
| 86 | + |
| 87 | +pkg_pretend() { |
| 88 | + if use amd64; then |
| 89 | + if use cpu_flags_x86_f16c && use cpu_flags_x86_avx2 && use cpu_flags_x86_fma3 && ! use cpu_flags_x86_bmi2; then |
| 90 | + ewarn |
| 91 | + ewarn "CPU_FLAGS_X86: bmi2 not enabled." |
| 92 | + ewarn " Not building haswell runner." |
| 93 | + ewarn " Not building skylakex runner." |
| 94 | + ewarn " Not building icelake runner." |
| 95 | + ewarn " Not building alderlake runner." |
| 96 | + ewarn |
| 97 | + if grep bmi2 /proc/cpuinfo > /dev/null; then |
| 98 | + ewarn "bmi2 found in /proc/cpuinfo" |
| 99 | + ewarn |
| 100 | + fi |
| 101 | + fi |
| 102 | + fi |
| 103 | +} |
| 104 | + |
| 105 | +pkg_setup() { |
| 106 | + if use rocm; then |
| 107 | + linux-info_pkg_setup |
| 108 | + if linux-info_get_any_version && linux_config_exists; then |
| 109 | + if ! linux_chkconfig_present HSA_AMD_SVM; then |
| 110 | + ewarn "To use ROCm/HIP, you need to have HSA_AMD_SVM option enabled in your kernel." |
| 111 | + fi |
| 112 | + fi |
| 113 | + fi |
| 114 | +} |
| 115 | + |
| 116 | +src_unpack() { |
| 117 | + # Already filter lto flags for ROCM |
| 118 | + # 963401 |
| 119 | + if use rocm; then |
| 120 | + # copied from _rocm_strip_unsupported_flags |
| 121 | + strip-unsupported-flags |
| 122 | + export CXXFLAGS="$(test-flags-HIPCXX "${CXXFLAGS}")" |
| 123 | + fi |
| 124 | + |
| 125 | + if [[ "${PV}" == *9999* ]]; then |
| 126 | + git-r3_src_unpack |
| 127 | + go-module_live_vendor |
| 128 | + else |
| 129 | + go-module_src_unpack |
| 130 | + fi |
| 131 | +} |
| 132 | + |
| 133 | +src_prepare() { |
| 134 | + cmake_src_prepare |
| 135 | + |
| 136 | + sed \ |
| 137 | + -e "/set(GGML_CCACHE/s/ON/OFF/g" \ |
| 138 | + -e "/PRE_INCLUDE_REGEXES.*cu/d" \ |
| 139 | + -e "/PRE_INCLUDE_REGEXES.*hip/d" \ |
| 140 | + -i CMakeLists.txt || die "bundle headers sed failed" |
| 141 | + |
| 142 | + # TODO see src_unpack? |
| 143 | + sed \ |
| 144 | + -e "s/ -O3//g" \ |
| 145 | + -i \ |
| 146 | + ml/backend/ggml/ggml/src/ggml-cpu/cpu.go \ |
| 147 | + || die "-O3 sed failed" |
| 148 | + |
| 149 | + # grep -Rl -e 'lib/ollama' -e '"..", "lib"' --include '*.go' |
| 150 | + sed \ |
| 151 | + -e "s/\"..\", \"lib\"/\"..\", \"$(get_libdir)\"/" \ |
| 152 | + -e "s#\"lib/ollama\"#\"$(get_libdir)/ollama\"#" \ |
| 153 | + -i \ |
| 154 | + ml/backend/ggml/ggml/src/ggml.go \ |
| 155 | + ml/path.go \ |
| 156 | + || die "libdir sed failed" |
| 157 | + |
| 158 | + if use amd64; then |
| 159 | + if |
| 160 | + ! use cpu_flags_x86_sse4_2; then |
| 161 | + sed -e "/ggml_add_cpu_backend_variant(sse42/s/^/# /g" -i ml/backend/ggml/ggml/src/CMakeLists.txt || die |
| 162 | + # SSE42) |
| 163 | + fi |
| 164 | + if |
| 165 | + ! use cpu_flags_x86_sse4_2 || |
| 166 | + ! use cpu_flags_x86_avx; then |
| 167 | + sed -e "/ggml_add_cpu_backend_variant(sandybridge/s/^/# /g" -i ml/backend/ggml/ggml/src/CMakeLists.txt || die |
| 168 | + # SSE42 AVX) |
| 169 | + fi |
| 170 | + if |
| 171 | + ! use cpu_flags_x86_sse4_2 || |
| 172 | + ! use cpu_flags_x86_avx || |
| 173 | + ! use cpu_flags_x86_f16c || |
| 174 | + ! use cpu_flags_x86_avx2 || |
| 175 | + ! use cpu_flags_x86_bmi2 || |
| 176 | + ! use cpu_flags_x86_fma3; then |
| 177 | + sed -e "/ggml_add_cpu_backend_variant(haswell/s/^/# /g" -i ml/backend/ggml/ggml/src/CMakeLists.txt || die |
| 178 | + # SSE42 AVX F16C AVX2 BMI2 FMA) |
| 179 | + fi |
| 180 | + if |
| 181 | + ! use cpu_flags_x86_sse4_2 || |
| 182 | + ! use cpu_flags_x86_avx || |
| 183 | + ! use cpu_flags_x86_f16c || |
| 184 | + ! use cpu_flags_x86_avx2 || |
| 185 | + ! use cpu_flags_x86_bmi2 || |
| 186 | + ! use cpu_flags_x86_fma3 || |
| 187 | + ! use cpu_flags_x86_avx512f; then |
| 188 | + sed -e "/ggml_add_cpu_backend_variant(skylakex/s/^/# /g" -i ml/backend/ggml/ggml/src/CMakeLists.txt || die |
| 189 | + # SSE42 AVX F16C AVX2 BMI2 FMA AVX512) |
| 190 | + fi |
| 191 | + if |
| 192 | + ! use cpu_flags_x86_sse4_2 || |
| 193 | + ! use cpu_flags_x86_avx || |
| 194 | + ! use cpu_flags_x86_f16c || |
| 195 | + ! use cpu_flags_x86_avx2 || |
| 196 | + ! use cpu_flags_x86_bmi2 || |
| 197 | + ! use cpu_flags_x86_fma3 || |
| 198 | + ! use cpu_flags_x86_avx512f || |
| 199 | + ! use cpu_flags_x86_avx512vbmi || |
| 200 | + ! use cpu_flags_x86_avx512_vnni; then |
| 201 | + sed -e "/ggml_add_cpu_backend_variant(icelake/s/^/# /g" -i ml/backend/ggml/ggml/src/CMakeLists.txt || die |
| 202 | + # SSE42 AVX F16C AVX2 BMI2 FMA AVX512 AVX512_VBMI AVX512_VNNI) |
| 203 | + fi |
| 204 | + if |
| 205 | + ! use cpu_flags_x86_sse4_2 || |
| 206 | + ! use cpu_flags_x86_avx || |
| 207 | + ! use cpu_flags_x86_f16c || |
| 208 | + ! use cpu_flags_x86_avx2 || |
| 209 | + ! use cpu_flags_x86_bmi2 || |
| 210 | + ! use cpu_flags_x86_fma3 || |
| 211 | + ! use cpu_flags_x86_avx_vnni; then |
| 212 | + sed -e "/ggml_add_cpu_backend_variant(alderlake/s/^/# /g" -i ml/backend/ggml/ggml/src/CMakeLists.txt || die |
| 213 | + # SSE42 AVX F16C AVX2 BMI2 FMA AVX_VNNI) |
| 214 | + fi |
| 215 | + |
| 216 | + # ml/backend/ggml/ggml/src/CMakeLists.txt |
| 217 | + fi |
| 218 | + |
| 219 | + if use cuda; then |
| 220 | + cuda_src_prepare |
| 221 | + fi |
| 222 | + |
| 223 | + if use rocm; then |
| 224 | + # --hip-version gets appended to the compile flags which isn't a known flag. |
| 225 | + # This causes rocm builds to fail because -Wunused-command-line-argument is turned on. |
| 226 | + # Use nuclear option to fix this. |
| 227 | + # Disable -Werror's from go modules. |
| 228 | + find "${S}" -name ".go" -exec sed -i "s/ -Werror / /g" {} + || die |
| 229 | + fi |
| 230 | +} |
| 231 | + |
| 232 | +src_configure() { |
| 233 | + local mycmakeargs=( |
| 234 | + -DGGML_CCACHE="no" |
| 235 | + |
| 236 | + # -DGGML_CPU="yes" |
| 237 | + -DGGML_BLAS="$(usex blas)" |
| 238 | + # -DGGML_CUDA="$(usex cuda)" |
| 239 | + # -DGGML_HIP="$(usex rocm)" |
| 240 | + |
| 241 | + # -DGGML_METAL="yes" # apple |
| 242 | + # missing from ml/backend/ggml/ggml/src/ |
| 243 | + # -DGGML_CANN="yes" |
| 244 | + # -DGGML_MUSA="yes" |
| 245 | + # -DGGML_RPC="yes" |
| 246 | + # -DGGML_SYCL="yes" |
| 247 | + # -DGGML_KOMPUTE="$(usex kompute)" |
| 248 | + # -DGGML_OPENCL="$(usex opencl)" |
| 249 | + # -DGGML_VULKAN="$(usex vulkan)" |
| 250 | + "$(cmake_use_find_package vulkan Vulkan)" |
| 251 | + ) |
| 252 | + |
| 253 | + if use blas; then |
| 254 | + if use mkl; then |
| 255 | + mycmakeargs+=( |
| 256 | + -DGGML_BLAS_VENDOR="Intel" |
| 257 | + ) |
| 258 | + else |
| 259 | + mycmakeargs+=( |
| 260 | + -DGGML_BLAS_VENDOR="Generic" |
| 261 | + ) |
| 262 | + fi |
| 263 | + fi |
| 264 | + |
| 265 | + if use cuda; then |
| 266 | + local -x CUDAHOSTCXX CUDAHOSTLD |
| 267 | + CUDAHOSTCXX="$(cuda_gccdir)" |
| 268 | + CUDAHOSTLD="$(tc-getCXX)" |
| 269 | + |
| 270 | + cuda_add_sandbox -w |
| 271 | + addpredict "/dev/char/" |
| 272 | + else |
| 273 | + mycmakeargs+=( |
| 274 | + -DCMAKE_CUDA_COMPILER="NOTFOUND" |
| 275 | + ) |
| 276 | + fi |
| 277 | + |
| 278 | + if use rocm; then |
| 279 | + mycmakeargs+=( |
| 280 | + -DCMAKE_HIP_ARCHITECTURES="$(get_amdgpu_flags)" |
| 281 | + -DCMAKE_HIP_PLATFORM="amd" |
| 282 | + ) |
| 283 | + |
| 284 | + local -x HIP_PATH="${ESYSROOT}/usr" |
| 285 | + else |
| 286 | + mycmakeargs+=( |
| 287 | + -DCMAKE_HIP_COMPILER="NOTFOUND" |
| 288 | + ) |
| 289 | + fi |
| 290 | + |
| 291 | + cmake_src_configure |
| 292 | +} |
| 293 | + |
| 294 | +src_compile() { |
| 295 | + # export version information |
| 296 | + # https://github.com/gentoo/guru/pull/205 |
| 297 | + # https://forums.gentoo.org/viewtopic-p-8831646.html |
| 298 | + local VERSION |
| 299 | + if [[ "${PV}" == *9999* ]]; then |
| 300 | + VERSION="$( |
| 301 | + git describe --tags --first-parent --abbrev=7 --long --dirty --always \ |
| 302 | + | sed -e "s/^v//g" |
| 303 | + )" |
| 304 | + else |
| 305 | + VERSION="${PVR}" |
| 306 | + fi |
| 307 | + local EXTRA_GOFLAGS_LD=( |
| 308 | + # "-w" # disable DWARF generation |
| 309 | + # "-s" # disable symbol table |
| 310 | + "-X=github.com/ollama/ollama/version.Version=${VERSION}" |
| 311 | + "-X=github.com/ollama/ollama/server.mode=release" |
| 312 | + ) |
| 313 | + GOFLAGS+=" '-ldflags=${EXTRA_GOFLAGS_LD[*]}'" |
| 314 | + |
| 315 | + ego build |
| 316 | + |
| 317 | + cmake_src_compile |
| 318 | +} |
| 319 | + |
| 320 | +src_install() { |
| 321 | + dobin ollama |
| 322 | + |
| 323 | + cmake_src_install |
| 324 | + |
| 325 | + newinitd "${FILESDIR}/ollama.init" "${PN}" |
| 326 | + newconfd "${FILESDIR}/ollama.confd" "${PN}" |
| 327 | + |
| 328 | + systemd_dounit "${FILESDIR}/ollama.service" |
| 329 | +} |
| 330 | + |
| 331 | +pkg_preinst() { |
| 332 | + keepdir /var/log/ollama |
| 333 | + fperms 750 /var/log/ollama |
| 334 | + fowners "${PN}:${PN}" /var/log/ollama |
| 335 | +} |
| 336 | + |
| 337 | +pkg_postinst() { |
| 338 | + if [[ -z ${REPLACING_VERSIONS} ]] ; then |
| 339 | + einfo "Quick guide:" |
| 340 | + einfo "\tollama serve" |
| 341 | + einfo "\tollama run llama3:70b" |
| 342 | + einfo |
| 343 | + einfo "See available models at https://ollama.com/library" |
| 344 | + fi |
| 345 | + |
| 346 | + if use cuda ; then |
| 347 | + einfo "When using cuda the user running ${PN} has to be in the video group or it won't detect devices." |
| 348 | + einfo "The ebuild ensures this for user ${PN} via acct-user/${PN}[cuda]" |
| 349 | + fi |
| 350 | +} |
0 commit comments