Skip to content

Commit

Permalink
Merge branch 'main' into rebase_ollama_main
Browse files Browse the repository at this point in the history
  • Loading branch information
zhewang1-intc committed Mar 19, 2024
2 parents 892a74f + 0c5346e commit d815e78
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ RUN mkdir /tmp/scratch && \
(cd /tmp/scratch/ && tar czvf /go/src/github.com/jmorganca/ollama/dist/deps/ollama-linux-amd64-rocm.tgz . )


FROM --platform=linux/amd64 intel/oneapi-basekit:2024.0.1-devel-rockylinux9 AS oneapi-build-amd64
ARG CMAKE_VERSION
COPY ./scripts/rh_linux_deps.sh /
RUN CMAKE_VERSION=${CMAKE_VERSION} sh /rh_linux_deps.sh
COPY --from=llm-code / /go/src/github.com/jmorganca/ollama/
WORKDIR /go/src/github.com/jmorganca/ollama/llm/generate
ARG CGO_CFLAGS
RUN OLLAMA_SKIP_CPU_GENERATE=1 sh gen_linux.sh

FROM --platform=linux/amd64 intel/oneapi-basekit:2024.0.1-devel-rockylinux9 AS oneapi-build-amd64
ARG CMAKE_VERSION
COPY ./scripts/rh_linux_deps.sh /
Expand Down
54 changes: 54 additions & 0 deletions gpu/gpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ var OneapiLinuxGlobs = []string{
"/usr/lib*/libze_intel_gpu.so*",
}

var OneapiLinuxGlobs = []string{
"/usr/lib/x86_64-linux-gnu/libze_intel_gpu.so*",
"/usr/lib*/libze_intel_gpu.so*",
}

// Note: gpuMutex must already be held
func initGPUHandles() {

Expand Down Expand Up @@ -105,6 +110,16 @@ func initGPUHandles() {
return
}
}

oneapiLibPaths := FindGPULibs(oneapiMgmtName, oneapiMgmtPatterns)
if len(oneapiLibPaths) > 0 {
oneapi := LoadOneapiMgmt(oneapiLibPaths)
if oneapi != nil {
slog.Info("Intel GPU detected")
gpuHandles.oneapi = oneapi
return
}
}
}

func GetGPUInfo() GpuInfo {
Expand Down Expand Up @@ -170,6 +185,28 @@ func GetGPUInfo() GpuInfo {
if resp.Library != "" {
return resp
}
} else if gpuHandles.oneapi != nil && (cpuVariant != "" || runtime.GOARCH != "amd64") {

Check failure on line 188 in gpu/gpu.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest, amd64)

expected ';', found 'else' (typecheck)

Check failure on line 188 in gpu/gpu.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest, amd64)

expected ';', found 'else' (typecheck)

Check failure on line 188 in gpu/gpu.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest, amd64)

expected ';', found 'else') (typecheck)

Check failure on line 188 in gpu/gpu.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest, amd64)

expected ';', found 'else') (typecheck)

Check failure on line 188 in gpu/gpu.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, amd64)

expected ';', found 'else'
C.oneapi_check_vram(*gpuHandles.oneapi, &memInfo)
if memInfo.err != nil {
slog.Info(fmt.Sprintf("error looking up OneAPI GPU memory: %s", C.GoString(memInfo.err)))
C.free(unsafe.Pointer(memInfo.err))
} else if memInfo.igpu_index >= 0 && memInfo.count == 1 {
// Only one GPU detected and it appears to be an integrated GPU - skip it
slog.Info("OneAPI unsupported integrated GPU detected")
} else if memInfo.count > 0 {
if memInfo.igpu_index >= 0 {
// We have multiple GPUs reported, and one of them is an integrated GPU
// so we have to set the env var to bypass it
// If the user has specified their own SYCL_DEVICE_ALLOWLIST, don't clobber it
val := os.Getenv("SYCL_DEVICE_ALLOWLIST")
if val == "" {
val = "DeviceType:gpu"
os.Setenv("SYCL_DEVICE_ALLOWLIST", val)
}
slog.Info(fmt.Sprintf("oneAPI integrated GPU detected - SYCL_DEVICE_ALLOWLIST=%s", val))
}
resp.Library = "oneapi"
}
}
if resp.Library == "" {
C.cpu_check_ram(&memInfo)
Expand Down Expand Up @@ -315,6 +352,23 @@ func LoadOneapiMgmt(rocmLibPaths []string) *C.oneapi_handle_t {
return nil
}

func LoadOneapiMgmt(rocmLibPaths []string) *C.oneapi_handle_t {
var resp C.oneapi_init_resp_t
resp.oh.verbose = getVerboseState()
for _, libPath := range rocmLibPaths {
lib := C.CString(libPath)
defer C.free(unsafe.Pointer(lib))
C.oneapi_init(lib, &resp)
if resp.err != nil {
slog.Info(fmt.Sprintf("Unable to load oneAPI management library %s: %s", libPath, C.GoString(resp.err)))
C.free(unsafe.Pointer(resp.err))
} else {
return &resp.oh
}
}
return nil
}

func getVerboseState() C.uint16_t {
if debug := os.Getenv("OLLAMA_DEBUG"); debug != "" {
return C.uint16_t(1)
Expand Down
21 changes: 21 additions & 0 deletions llm/generate/gen_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,25 @@ if [ -d "${ONEAPI_ROOT}" ]; then
compress_libs
fi

if [ -z "${ONEAPI_ROOT}" ]; then
# Try the default location in case it exists
ONEAPI_ROOT=/opt/intel/oneapi
fi

if [ -d "${ONEAPI_ROOT}" ]; then
echo "OneAPI libraries detected - building dynamic OneAPI library"
init_vars
source ${ONEAPI_ROOT}/setvars.sh --force # set up environment variables for oneAPI
CC=icx
CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DLLAMA_SYCL=ON -DLLAMA_SYCL_F16=OFF"
BUILD_DIR="${LLAMACPP_DIR}/build/linux/${ARCH}/oneapi"
EXTRA_LIBS="-fsycl -Wl,-rpath,${ONEAPI_ROOT}/compiler/latest/lib,-rpath,${ONEAPI_ROOT}/mkl/latest/lib,-rpath,${ONEAPI_ROOT}/tbb/latest/lib,-rpath,${ONEAPI_ROOT}/compiler/latest/opt/oclfpga/linux64/lib -lOpenCL -lmkl_core -lmkl_sycl_blas -lmkl_intel_ilp64 -lmkl_tbb_thread -ltbb"
DEBUG_FLAGS="" # icx compiles with -O0 if we pass -g, so we must remove it
build

# Note: the OneAPI libs and runtime library files are too large to embed, so we depend on
# them being present at runtime on the host
compress_libs
fi

cleanup
10 changes: 10 additions & 0 deletions scripts/build_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ docker build \
-f Dockerfile \
-t ollama/ollama:$VERSION-oneapi \
.

docker build \
--load \
--platform=linux/amd64 \
--build-arg=VERSION \
--build-arg=GOFLAGS \
--target runtime-oneapi \
-f Dockerfile \
-t ollama/ollama:$VERSION-oneapi \
.

0 comments on commit d815e78

Please sign in to comment.