From 05f35b182c1218aa03da00fdc79a9017a50e2440 Mon Sep 17 00:00:00 2001 From: Marc R Kellerman <9411143+mkellerman@users.noreply.github.com> Date: Fri, 21 Apr 2023 15:29:32 -0700 Subject: [PATCH] fix(makefile): fix go-gpt2 folder and add verification before git clone (#51) Co-authored-by: Ettore Di Giacinto --- .env | 1 + .gitignore | 1 + Dockerfile | 5 +-- Makefile | 75 +++++++++++++++++++++++++-------------------- docker-compose.yaml | 12 +++----- 5 files changed, 51 insertions(+), 43 deletions(-) diff --git a/.env b/.env index cd580cd0..b05dac68 100644 --- a/.env +++ b/.env @@ -2,3 +2,4 @@ # CONTEXT_SIZE=512 MODELS_PATH=/models # DEBUG=true +# BUILD_TYPE=generic diff --git a/.gitignore b/.gitignore index 25fb365b..6115e500 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # go-llama build artifacts go-llama go-gpt4all-j +go-gpt2 # LocalAI build binary LocalAI diff --git a/Dockerfile b/Dockerfile index 4c0d31ae..348d74c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,12 @@ ARG GO_VERSION=1.20 ARG DEBIAN_VERSION=11 +ARG BUILD_TYPE= + FROM golang:$GO_VERSION as builder WORKDIR /build RUN apt-get update && apt-get install -y cmake COPY . . -ARG BUILD_TYPE= -RUN make build${BUILD_TYPE} +RUN make build FROM debian:$DEBIAN_VERSION COPY --from=builder /build/local-ai /usr/bin/local-ai diff --git a/Makefile b/Makefile index 91f8de32..85649ba4 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,20 @@ WHITE := $(shell tput -Txterm setaf 7) CYAN := $(shell tput -Txterm setaf 6) RESET := $(shell tput -Txterm sgr0) +C_INCLUDE_PATH=$(shell pwd)/go-llama:$(shell pwd)/go-gpt4all-j:$(shell pwd)/go-gpt2 +LIBRARY_PATH=$(shell pwd)/go-llama:$(shell pwd)/go-gpt4all-j:$(shell pwd)/go-gpt2 + +# Use this if you want to set the default behavior +ifndef BUILD_TYPE + BUILD_TYPE:=default +endif + +ifeq ($(BUILD_TYPE), "generic") + GENERIC_PREFIX:=generic- +else + GENERIC_PREFIX:= +endif + .PHONY: all test build vendor all: help @@ -19,15 +33,18 @@ all: help ## Build: build: prepare ## Build the project - C_INCLUDE_PATH=$(shell pwd)/go-llama.cpp:$(shell pwd)/go-gpt4all-j:$(shell pwd)/go-gpt2.cpp LIBRARY_PATH=$(shell pwd)/go-llama.cpp:$(shell pwd)/go-gpt4all-j:$(shell pwd)/go-gpt2.cpp $(GOCMD) build -o $(BINARY_NAME) ./ + $(info ${GREEN}I local-ai build info:${RESET}) + $(info ${GREEN}I BUILD_TYPE: ${YELLOW}$(BUILD_TYPE)${RESET}) + C_INCLUDE_PATH=${C_INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} $(GOCMD) build -o $(BINARY_NAME) ./ -buildgeneric: prepare-generic ## Build the project - C_INCLUDE_PATH=$(shell pwd)/go-llama.cpp:$(shell pwd)/go-gpt4all-j:$(shell pwd)/go-gpt2.cpp LIBRARY_PATH=$(shell pwd)/go-llama.cpp:$(shell pwd)/go-gpt4all-j:$(shell pwd)/go-gpt2.cpp $(GOCMD) build -o $(BINARY_NAME) ./ +generic-build: ## Build the project using generic + BUILD_TYPE="generic" $(MAKE) build ## GPT4ALL-J go-gpt4all-j: - git clone --recurse-submodules https://github.com/go-skynet/go-gpt4all-j.cpp go-gpt4all-j && cd go-gpt4all-j && git checkout -b build $(GOGPT4ALLJ_VERSION) -# This is hackish, but needed as both go-llama and go-gpt4allj have their own version of ggml.. + git clone --recurse-submodules https://github.com/go-skynet/go-gpt4all-j.cpp go-gpt4all-j + cd go-gpt4all-j && git checkout -b build $(GOGPT4ALLJ_VERSION) + # This is hackish, but needed as both go-llama and go-gpt4allj have their own version of ggml.. @find ./go-gpt4all-j -type f -name "*.c" -exec sed -i'' -e 's/ggml_/ggml_gptj_/g' {} + @find ./go-gpt4all-j -type f -name "*.cpp" -exec sed -i'' -e 's/ggml_/ggml_gptj_/g' {} + @find ./go-gpt4all-j -type f -name "*.h" -exec sed -i'' -e 's/ggml_/ggml_gptj_/g' {} + @@ -38,54 +55,46 @@ go-gpt4all-j: @find ./go-gpt4all-j -type f -name "*.cpp" -exec sed -i'' -e 's/::replace/::json_gptj_replace/g' {} + go-gpt4all-j/libgptj.a: go-gpt4all-j - $(MAKE) -C go-gpt4all-j libgptj.a - -go-gpt4all-j/libgptj.a-generic: go-gpt4all-j - $(MAKE) -C go-gpt4all-j generic-libgptj.a + $(MAKE) -C go-gpt4all-j $(GENERIC_PREFIX)libgptj.a # CEREBRAS GPT -go-gpt2.cpp: - git clone --recurse-submodules https://github.com/go-skynet/go-gpt2.cpp go-gpt2.cpp && cd go-gpt2.cpp && git checkout -b build $(GOGPT2_VERSION) -# This is hackish, but needed as both go-llama and go-gpt4allj have their own version of ggml.. - @find ./go-gpt2.cpp -type f -name "*.c" -exec sed -i'' -e 's/ggml_/ggml_gpt2_/g' {} + - @find ./go-gpt2.cpp -type f -name "*.cpp" -exec sed -i'' -e 's/ggml_/ggml_gpt2_/g' {} + - @find ./go-gpt2.cpp -type f -name "*.h" -exec sed -i'' -e 's/ggml_/ggml_gpt2_/g' {} + - @find ./go-gpt2.cpp -type f -name "*.cpp" -exec sed -i'' -e 's/gpt_/gpt2_/g' {} + - @find ./go-gpt2.cpp -type f -name "*.h" -exec sed -i'' -e 's/gpt_/gpt2_/g' {} + - @find ./go-gpt2.cpp -type f -name "*.cpp" -exec sed -i'' -e 's/json_/json_gpt2_/g' {} + +go-gpt2: + git clone --recurse-submodules https://github.com/go-skynet/go-gpt2.cpp go-gpt2 + cd go-gpt2 && git checkout -b build $(GOGPT2_VERSION) + # This is hackish, but needed as both go-llama and go-gpt4allj have their own version of ggml.. + @find ./go-gpt2 -type f -name "*.c" -exec sed -i'' -e 's/ggml_/ggml_gpt2_/g' {} + + @find ./go-gpt2 -type f -name "*.cpp" -exec sed -i'' -e 's/ggml_/ggml_gpt2_/g' {} + + @find ./go-gpt2 -type f -name "*.h" -exec sed -i'' -e 's/ggml_/ggml_gpt2_/g' {} + + @find ./go-gpt2 -type f -name "*.cpp" -exec sed -i'' -e 's/gpt_/gpt2_/g' {} + + @find ./go-gpt2 -type f -name "*.h" -exec sed -i'' -e 's/gpt_/gpt2_/g' {} + + @find ./go-gpt2 -type f -name "*.cpp" -exec sed -i'' -e 's/json_/json_gpt2_/g' {} + -go-gpt2.cpp/libgpt2.a: go-gpt2.cpp - $(MAKE) -C go-gpt2.cpp libgpt2.a - -go-gpt2.cpp/libgpt2.a-generic: go-gpt2.cpp - $(MAKE) -C go-gpt2.cpp generic-libgpt2.a +go-gpt2/libgpt2.a: go-gpt2 + $(MAKE) -C go-gpt2 $(GENERIC_PREFIX)libgpt2.a + go-llama: git clone -b $(GOLLAMA_VERSION) --recurse-submodules https://github.com/go-skynet/go-llama.cpp go-llama - $(MAKE) -C go-llama libbinding.a -go-llama-generic: - git clone -b $(GOLLAMA_VERSION) --recurse-submodules https://github.com/go-skynet/go-llama.cpp go-llama - $(MAKE) -C go-llama generic-libbinding.a +go-llama/libbinding.a: go-llama + $(MAKE) -C go-llama $(GENERIC_PREFIX)libbinding.a replace: $(GOCMD) mod edit -replace github.com/go-skynet/go-llama.cpp=$(shell pwd)/go-llama $(GOCMD) mod edit -replace github.com/go-skynet/go-gpt4all-j.cpp=$(shell pwd)/go-gpt4all-j - $(GOCMD) mod edit -replace github.com/go-skynet/go-gpt2.cpp=$(shell pwd)/go-gpt2.cpp + $(GOCMD) mod edit -replace github.com/go-skynet/go-gpt2.cpp=$(shell pwd)/go-gpt2 -prepare: go-llama go-gpt4all-j/libgptj.a go-gpt2.cpp/libgpt2.a replace - -prepare-generic: go-llama-generic go-gpt4all-j/libgptj.a-generic go-gpt2.cpp/libgpt2.a-generic replace +prepare: go-llama/libbinding.a go-gpt4all-j/libgptj.a go-gpt2/libgpt2.a replace clean: ## Remove build related file rm -fr ./go-llama rm -rf ./go-gpt4all-j - rm -rf ./go-gpt2.cpp + rm -rf ./go-gpt2 rm -rf $(BINARY_NAME) ## Run: run: prepare - $(GOCMD) run ./ api + C_INCLUDE_PATH=${C_INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} $(GOCMD) run ./main.go ## Test: test: ## Run the tests of the project diff --git a/docker-compose.yaml b/docker-compose.yaml index ad061c3f..8058e467 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,14 +6,10 @@ services: build: context: . dockerfile: Dockerfile - # args: - # BUILD_TYPE: generic # Uncomment to build CPU generic code that works on most HW ports: - 8080:8080 - environment: - - MODELS_PATH=$MODELS_PATH - - CONTEXT_SIZE=$CONTEXT_SIZE - - THREADS=$THREADS - - DEBUG=$DEBUG + env_file: + - .env volumes: - - ./models:/models:cached \ No newline at end of file + - ./models:/models:cached + command: ["/usr/bin/local-ai" ] \ No newline at end of file