gpu.make 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Generalized GPU runner build
  2. ifndef GPU_RUNNER_NAME
  3. dummy:
  4. $(error This makefile is not meant to build directly, but instead included in other Makefiles that set required variables)
  5. endif
  6. ifeq ($(OS),windows)
  7. GPU_COMPILER:=$(GPU_COMPILER_WIN)
  8. GPU_LIB_DIR:=$(GPU_LIB_DIR_WIN)
  9. CGO_EXTRA_LDFLAGS:=$(CGO_EXTRA_LDFLAGS_WIN)
  10. GPU_COMPILER_CFLAGS = $(GPU_COMPILER_CFLAGS_WIN)
  11. GPU_COMPILER_CXXFLAGS = $(GPU_COMPILER_CXXFLAGS_WIN)
  12. else ifeq ($(OS),linux)
  13. GPU_COMPILER:=$(GPU_COMPILER_LINUX)
  14. GPU_LIB_DIR:=$(GPU_LIB_DIR_LINUX)
  15. CGO_EXTRA_LDFLAGS:=$(CGO_EXTRA_LDFLAGS_LINUX)
  16. GPU_COMPILER_CFLAGS = $(GPU_COMPILER_CFLAGS_LINUX)
  17. GPU_COMPILER_CXXFLAGS = $(GPU_COMPILER_CXXFLAGS_LINUX)
  18. endif
  19. GPU_GOFLAGS="-ldflags=-w -s \"-X=github.com/ollama/ollama/version.Version=$(VERSION)\" \"-X=github.com/ollama/ollama/llama.CpuFeatures=$(subst $(space),$(comma),$(GPU_RUNNER_CPU_FLAGS))\" $(TARGET_LDFLAGS)"
  20. # TODO Unify how we handle dependencies in the dist/packaging and install flow
  21. # today, cuda is bundled, but rocm is split out. Should split them each out by runner
  22. DIST_GPU_RUNNER_DEPS_DIR = $(DIST_LIB_DIR)
  23. ifeq ($(OS),windows)
  24. _OS_GPU_RUNNER_CPU_FLAGS=$(call uc,$(GPU_RUNNER_CPU_FLAGS))
  25. else ifeq ($(OS),linux)
  26. _OS_GPU_RUNNER_CPU_FLAGS=$(GPU_RUNNER_CPU_FLAGS)
  27. endif
  28. GPU_RUNNER_LIBS = $(wildcard $(addsuffix .$(SHARED_EXT).*,$(addprefix $(GPU_LIB_DIR)/$(SHARED_PREFIX),$(GPU_RUNNER_LIBS_SHORT))))
  29. DIST_GPU_RUNNER_LIB_DEPS = $(addprefix $(DIST_GPU_RUNNER_DEPS_DIR)/,$(notdir $(GPU_RUNNER_LIBS)))
  30. GPU_RUNNER_SRCS := \
  31. ggml-cuda.cu \
  32. $(filter-out $(wildcard ggml-cuda/fattn*.cu),$(wildcard ggml-cuda/*.cu)) \
  33. $(wildcard ggml-cuda/template-instances/mmq*.cu) \
  34. ggml.c ggml-backend.c ggml-alloc.c ggml-quants.c sgemm.cpp ggml-aarch64.c
  35. GPU_RUNNER_HDRS := \
  36. $(wildcard ggml-cuda/*.cuh)
  37. # Conditional flags and components to speed up developer builds
  38. ifneq ($(OLLAMA_FAST_BUILD),)
  39. GPU_COMPILER_CUFLAGS += \
  40. -DGGML_DISABLE_FLASH_ATTN
  41. else
  42. GPU_RUNNER_SRCS += \
  43. $(wildcard ggml-cuda/fattn*.cu) \
  44. $(wildcard ggml-cuda/template-instances/fattn-wmma*.cu) \
  45. $(wildcard ggml-cuda/template-instances/fattn-vec*q4_0-q4_0.cu) \
  46. $(wildcard ggml-cuda/template-instances/fattn-vec*q8_0-q8_0.cu) \
  47. $(wildcard ggml-cuda/template-instances/fattn-vec*f16-f16.cu)
  48. endif
  49. GPU_RUNNER_OBJS := $(GPU_RUNNER_SRCS:.cu=.$(GPU_RUNNER_NAME).$(OBJ_EXT))
  50. GPU_RUNNER_OBJS := $(GPU_RUNNER_OBJS:.c=.$(GPU_RUNNER_NAME).$(OBJ_EXT))
  51. GPU_RUNNER_OBJS := $(addprefix $(BUILD_DIR)/,$(GPU_RUNNER_OBJS:.cpp=.$(GPU_RUNNER_NAME).$(OBJ_EXT)))
  52. DIST_RUNNERS = $(addprefix $(RUNNERS_DIST_DIR)/,$(addsuffix /ollama_llama_server$(EXE_EXT),$(GPU_RUNNER_NAME)))
  53. ifneq ($(OS),windows)
  54. PAYLOAD_RUNNERS = $(addprefix $(RUNNERS_PAYLOAD_DIR)/,$(addsuffix /ollama_llama_server$(EXE_EXT).gz,$(GPU_RUNNER_NAME)))
  55. endif
  56. BUILD_RUNNERS = $(addprefix $(RUNNERS_BUILD_DIR)/,$(addsuffix /ollama_llama_server$(EXE_EXT),$(GPU_RUNNER_NAME)))
  57. $(GPU_RUNNER_NAME): $(BUILD_RUNNERS) $(DIST_RUNNERS) $(PAYLOAD_RUNNERS)
  58. # Build targets
  59. $(BUILD_DIR)/%.$(GPU_RUNNER_NAME).$(OBJ_EXT): %.cu
  60. @-mkdir -p $(dir $@)
  61. $(CCACHE) $(GPU_COMPILER) -c $(GPU_COMPILER_CFLAGS) $(GPU_COMPILER_CUFLAGS) $(GPU_RUNNER_ARCH_FLAGS) -o $@ $<
  62. $(BUILD_DIR)/%.$(GPU_RUNNER_NAME).$(OBJ_EXT): %.c
  63. @-mkdir -p $(dir $@)
  64. $(CCACHE) $(GPU_COMPILER) -c $(GPU_COMPILER_CFLAGS) -o $@ $<
  65. $(BUILD_DIR)/%.$(GPU_RUNNER_NAME).$(OBJ_EXT): %.cpp
  66. @-mkdir -p $(dir $@)
  67. $(CCACHE) $(GPU_COMPILER) -c $(GPU_COMPILER_CXXFLAGS) -o $@ $<
  68. $(RUNNERS_BUILD_DIR)/$(GPU_RUNNER_NAME)/ollama_llama_server$(EXE_EXT): TARGET_CGO_LDFLAGS = -L"$(RUNNERS_BUILD_DIR)/$(GPU_RUNNER_NAME)/" $(CGO_EXTRA_LDFLAGS)
  69. $(RUNNERS_BUILD_DIR)/$(GPU_RUNNER_NAME)/ollama_llama_server$(EXE_EXT): $(RUNNERS_BUILD_DIR)/$(GPU_RUNNER_NAME)/$(SHARED_PREFIX)ggml_$(GPU_RUNNER_NAME).$(SHARED_EXT) *.go ./runner/*.go $(COMMON_SRCS) $(COMMON_HDRS)
  70. @-mkdir -p $(dir $@)
  71. GOARCH=$(ARCH) CGO_LDFLAGS="$(TARGET_CGO_LDFLAGS)" go build -buildmode=pie $(GPU_GOFLAGS) -trimpath -tags $(subst $(space),$(comma),$(GPU_RUNNER_CPU_FLAGS) $(GPU_RUNNER_GO_TAGS)) -o $@ ./runner
  72. $(RUNNERS_BUILD_DIR)/$(GPU_RUNNER_NAME)/$(SHARED_PREFIX)ggml_$(GPU_RUNNER_NAME).$(SHARED_EXT): $(GPU_RUNNER_OBJS) $(DIST_GPU_RUNNER_LIB_DEPS) $(COMMON_HDRS) $(GPU_RUNNER_HDRS)
  73. @-mkdir -p $(dir $@)
  74. $(CCACHE) $(GPU_COMPILER) --shared -L$(GPU_LIB_DIR) $(GPU_RUNNER_DRIVER_LIB_LINK) -L${DIST_GPU_RUNNER_DEPS_DIR} $(foreach lib, $(GPU_RUNNER_LIBS_SHORT), -l$(lib)) $(GPU_RUNNER_OBJS) -o $@
  75. # Distribution targets
  76. $(RUNNERS_DIST_DIR)/%: $(RUNNERS_BUILD_DIR)/%
  77. @-mkdir -p $(dir $@)
  78. $(CP) $< $@
  79. $(RUNNERS_DIST_DIR)/$(GPU_RUNNER_NAME)/ollama_llama_server$(EXE_EXT): $(DIST_LIB_DIR)/$(SHARED_PREFIX)ggml_$(GPU_RUNNER_NAME).$(SHARED_EXT) $(GPU_DIST_DEPS_LIBS)
  80. $(DIST_LIB_DIR)/$(SHARED_PREFIX)ggml_$(GPU_RUNNER_NAME).$(SHARED_EXT): $(RUNNERS_BUILD_DIR)/$(GPU_RUNNER_NAME)/$(SHARED_PREFIX)ggml_$(GPU_RUNNER_NAME).$(SHARED_EXT)
  81. @-mkdir -p $(dir $@)
  82. $(CP) $< $@
  83. $(DIST_GPU_RUNNER_LIB_DEPS):
  84. @-mkdir -p $(dir $@)
  85. $(CP) $(GPU_LIB_DIR)/$(notdir $@) $(dir $@)
  86. $(GPU_DIST_DEPS_LIBS):
  87. @-mkdir -p $(dir $@)
  88. $(CP) $(dir $(filter %$(notdir $@),$(GPU_LIBS) $(GPU_TRANSITIVE_LIBS)))/$(notdir $@) $(dir $@)
  89. # Payload targets
  90. $(RUNNERS_PAYLOAD_DIR)/%/ollama_llama_server.gz: $(RUNNERS_BUILD_DIR)/%/ollama_llama_server
  91. @-mkdir -p $(dir $@)
  92. ${GZIP} --best -c $< > $@
  93. $(RUNNERS_PAYLOAD_DIR)/$(GPU_RUNNER_NAME)/%.gz: $(RUNNERS_BUILD_DIR)/$(GPU_RUNNER_NAME)/%
  94. @-mkdir -p $(dir $@)
  95. ${GZIP} --best -c $< > $@
  96. clean:
  97. rm -f $(GPU_RUNNER_OBJS) $(BUILD_RUNNERS) $(DIST_RUNNERS) $(PAYLOAD_RUNNERS)
  98. .PHONY: clean $(GPU_RUNNER_NAME)
  99. # Handy debugging for make variables
  100. print-%:
  101. @echo '$*=$($*)'