Skip to content

Commit 23c8da2

Browse files
authored
Merge pull request #2 from Weaxs/dev
(#1) support chatglm.cpp v0.3.0
2 parents ec1e093 + 700131f commit 23c8da2

16 files changed

+776
-258
lines changed

.github/workflows/codeql.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
name: "CodeQL"
1313

1414
on:
15-
push:
16-
branches: [ "main" ]
1715
pull_request:
16+
push:
1817
# The branches below must be a subset of the branches above
1918
branches: [ "main" ]
2019
schedule:

.github/workflows/test.yaml

+28-52
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,6 @@ jobs:
3636
run: |
3737
make test
3838
39-
windows-latest:
40-
runs-on: windows-latest
41-
strategy:
42-
matrix:
43-
go-version: [ 'stable' ]
44-
steps:
45-
- name: Checkout repository
46-
uses: actions/checkout@v3
47-
with:
48-
submodules: recursive
49-
- name: Setup Go ${{ matrix.go-version }}
50-
uses: actions/setup-go@v4
51-
with:
52-
go-version: ${{ matrix.go-version }}
53-
- name: Display Go version
54-
run: go version
55-
- name: Display GCC version
56-
run: gcc --version
57-
- name: Display CMake version
58-
run: cmake --version
59-
- name: Install dependencies
60-
run: go mod tidy
61-
- name: Test
62-
run: |
63-
make test
64-
6539
macOS-latest:
6640
runs-on: macOS-latest
6741
strategy:
@@ -88,29 +62,31 @@ jobs:
8862
run: |
8963
make test
9064
91-
macOS-metal-latest:
92-
runs-on: macOS-latest
93-
strategy:
94-
matrix:
95-
go-version: ['stable']
96-
steps:
97-
- name: Checkout repository
98-
uses: actions/checkout@v3
99-
with:
100-
submodules: recursive
101-
- name: Setup Go ${{ matrix.go-version }}
102-
uses: actions/setup-go@v4
103-
with:
104-
go-version: ${{ matrix.go-version }}
105-
# You can test your matrix by printing the current Go version
106-
- name: Display Go version
107-
run: go version
108-
- name: Display GCC version
109-
run: gcc --version
110-
- name: Display CMake version
111-
run: cmake --version
112-
- name: Install dependencies
113-
run: go mod tidy
114-
- name: Test
115-
run: |
116-
make BUILD_TYPE=metal test
65+
# arm not support https://github.com/actions/runner-images/issues/8610
66+
# Apple Silicon powered macOS runners are now available in public beta! https://github.com/actions/runner-images/issues/8439
67+
# macOS-arm64-metal-latest:
68+
# runs-on: macos-13-arm64
69+
# strategy:
70+
# matrix:
71+
# go-version: ['stable']
72+
# steps:
73+
# - name: Checkout repository
74+
# uses: actions/checkout@v3
75+
# with:
76+
# submodules: recursive
77+
# - name: Setup Go ${{ matrix.go-version }}
78+
# uses: actions/setup-go@v4
79+
# with:
80+
# go-version: ${{ matrix.go-version }}
81+
# # You can test your matrix by printing the current Go version
82+
# - name: Display Go version
83+
# run: go version
84+
# - name: Display GCC version
85+
# run: gcc --version
86+
# - name: Display CMake version
87+
# run: cmake --version
88+
# - name: Install dependencies
89+
# run: go mod tidy
90+
# - name: Test
91+
# run: |
92+
# make BUILD_TYPE=metal test

Makefile

+38-99
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,16 @@ INCLUDE_PATH := $(abspath ./)
22
LIBRARY_PATH := $(abspath ./)
33

44
ifndef UNAME_S
5-
ifeq ($(OS),Windows_NT)
6-
UNAME_S := $(shell ver)
7-
else
8-
UNAME_S := $(shell uname -s)
9-
endif
5+
UNAME_S := $(shell uname -s)
106
endif
11-
127
ifndef UNAME_P
13-
ifeq ($(OS),Windows_NT)
14-
UNAME_P := $(shell wmic cpu get caption)
15-
else
16-
UNAME_P := $(shell uname -p)
17-
endif
8+
UNAME_P := $(shell uname -p)
189
endif
19-
2010
ifndef UNAME_M
21-
ifeq ($(OS),Windows_NT)
22-
UNAME_M := $(PROCESSOR_ARCHITECTURE)
23-
else
24-
UNAME_M := $(shell uname -s)
25-
endif
11+
UNAME_M := $(shell uname -m)
2612
endif
2713

14+
2815
CCV := $(shell $(CC) --version | head -n 1)
2916
CXXV := $(shell $(CXX) --version | head -n 1)
3017

@@ -34,8 +21,8 @@ ifeq ($(UNAME_S),Darwin)
3421
ifneq ($(UNAME_P),arm)
3522
SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
3623
ifeq ($(SYSCTL_M),1)
37-
# UNAME_P := arm
38-
# UNAME_M := arm64
24+
# UNAME_P := arm
25+
# UNAME_M := arm64
3926
warn := $(warning Your arch is announced as x86_64, but it seems to actually be ARM64. Not fixing that can lead to bad performance. For more info see: https://github.com/ggerganov/whisper.cpp/issues/66\#issuecomment-1282546789)
4027
endif
4128
endif
@@ -47,30 +34,25 @@ endif
4734

4835
BUILD_TYPE?=
4936
# keep standard at C17 and C++17
50-
CFLAGS = -I. -O3 -DNDEBUG -std=c17 -fPIC -pthread
5137
CXXFLAGS = -I. -O3 -DNDEBUG -std=c++17 -fPIC -pthread
52-
LDFLAGS =
53-
CMAKE_ARGS = -DCMAKE_C_COMPILER=$(shell which gcc) -DCMAKE_CXX_COMPILER=$(shell which g++)
5438

5539
# warnings
56-
CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith -Wno-unused-function
57-
CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
40+
CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -pedantic-errors
5841

5942
# GPGPU specific
6043
GGML_CUDA_OBJ_PATH=third_party/ggml/src/CMakeFiles/ggml.dir/ggml-cuda.cu.o
6144

6245

6346
# Architecture specific
64-
# TODO: probably these flags need to be tweaked on some architectures
65-
# feel free to update the Makefile for your architecture and send a pull request or issue
47+
# feel free to update the Makefile for your architecture and send a pull request or issue
6648
ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
6749
# Use all CPU extensions that are available:
68-
CFLAGS += -march=native -mtune=native
50+
CMAKE_ARGS += -DCMAKE_C_FLAGS=-march=native -DGGML_F16C=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF
51+
CXXFLAGS += -march=native -mtune=native
6952
endif
7053
ifneq ($(filter ppc64%,$(UNAME_M)),)
7154
POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
7255
ifneq (,$(findstring POWER9,$(POWER9_M)))
73-
CFLAGS += -mcpu=power9
7456
CXXFLAGS += -mcpu=power9
7557
endif
7658
# Require c++23's std::byteswap for big-endian support.
@@ -79,61 +61,49 @@ ifneq ($(filter ppc64%,$(UNAME_M)),)
7961
endif
8062
endif
8163
ifdef CHATGLM_GPROF
82-
CFLAGS += -pg
8364
CXXFLAGS += -pg
8465
endif
8566
ifneq ($(filter aarch64%,$(UNAME_M)),)
86-
CFLAGS += -mcpu=native
8767
CXXFLAGS += -mcpu=native
8868
endif
8969
ifneq ($(filter armv6%,$(UNAME_M)),)
9070
# Raspberry Pi 1, 2, 3
91-
CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
71+
CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
9272
endif
9373
ifneq ($(filter armv7%,$(UNAME_M)),)
9474
# Raspberry Pi 4
95-
CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
75+
CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
9676
endif
9777
ifneq ($(filter armv8%,$(UNAME_M)),)
9878
# Raspberry Pi 4
99-
CFLAGS += -mfp16-format=ieee -mno-unaligned-access
79+
CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
10080
endif
10181

102-
# Build Acceleration
10382
ifeq ($(BUILD_TYPE),cublas)
104-
EXTRA_LIBS=
10583
CMAKE_ARGS+=-DGGML_CUBLAS=ON
106-
EXTRA_TARGETS+=ggml.dir/ggml-cuda.o
10784
endif
10885
ifeq ($(BUILD_TYPE),openblas)
109-
EXTRA_LIBS=
11086
CMAKE_ARGS+=-DGGML_OPENBLAS=ON
111-
CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas
112-
LDFLAGS += -lopenblas
87+
CXXFLAGS += -I/usr/local/include/openblas -lopenblas
11388
CGO_TAGS=-tags openblas
11489
endif
11590
ifeq ($(BUILD_TYPE),hipblas)
11691
ROCM_HOME ?= "/opt/rocm"
11792
CXX="$(ROCM_HOME)"/llvm/bin/clang++
11893
CC="$(ROCM_HOME)"/llvm/bin/clang
119-
EXTRA_LIBS=
12094
GPU_TARGETS ?= gfx900,gfx90a,gfx1030,gfx1031,gfx1100
12195
AMDGPU_TARGETS ?= "$(GPU_TARGETS)"
12296
CMAKE_ARGS+=-DGGML_HIPBLAS=ON -DAMDGPU_TARGETS="$(AMDGPU_TARGETS)" -DGPU_TARGETS="$(GPU_TARGETS)"
123-
EXTRA_TARGETS+=ggml.dir/ggml-cuda.o
12497
GGML_CUDA_OBJ_PATH=CMakeFiles/ggml-rocm.dir/ggml-cuda.cu.o
12598
endif
12699
ifeq ($(BUILD_TYPE),clblas)
127-
EXTRA_LIBS=
128100
CMAKE_ARGS+=-DGGML_CLBLAST=ON
129-
EXTRA_TARGETS+=ggml.dir/ggml-opencl.o
130101
CGO_TAGS=-tags cublas
131102
endif
132103
ifeq ($(BUILD_TYPE),metal)
133-
EXTRA_LIBS=
134104
CMAKE_ARGS+=-DGGML_METAL=ON
135-
EXTRA_TARGETS+=ggml.dir/ggml-metal.o
136105
CGO_TAGS=-tags metal
106+
EXTRA_TARGETS+=ggml-metal
137107
endif
138108

139109
ifdef CLBLAST_DIR
@@ -147,14 +117,13 @@ $(info I chatglm.cpp build info: )
147117
$(info I UNAME_S: $(UNAME_S))
148118
$(info I UNAME_P: $(UNAME_P))
149119
$(info I UNAME_M: $(UNAME_M))
150-
$(info I CFLAGS: $(CFLAGS))
151120
$(info I CXXFLAGS: $(CXXFLAGS))
152-
$(info I LDFLAGS: $(LDFLAGS))
153121
$(info I BUILD_TYPE: $(BUILD_TYPE))
154122
$(info I CMAKE_ARGS: $(CMAKE_ARGS))
155123
$(info I EXTRA_TARGETS: $(EXTRA_TARGETS))
156124
$(info I CC: $(CCV))
157125
$(info I CXX: $(CXXV))
126+
$(info I CGO_TAGS: $(CGO_TAGS))
158127
$(info )
159128

160129
# Use this if you want to set the default behavior
@@ -164,93 +133,63 @@ prepare:
164133

165134
# build chatglm.cpp
166135
build/chatglm.cpp: prepare
167-
cd build && CC="$(CC)" CXX="$(CXX)" cmake ../chatglm.cpp $(CMAKE_ARGS) && VERBOSE=1 cmake --build . -j --config Release
136+
cd build && CC="$(CC)" CXX="$(CXX)" cmake $(CMAKE_ARGS) ../chatglm.cpp && VERBOSE=1 cmake --build . -j --config Release
168137

169138
# chatglm.dir
170139
chatglm.dir: build/chatglm.cpp
171-
cd out && mkdir -p chatglm.dir && cd ../build && \
172-
cp -rp CMakeFiles/chatglm.dir/chatglm.cpp.o ../out/chatglm.dir/chatglm.o
140+
cd out && mkdir -p chatglm.dir
141+
cp build/CMakeFiles/chatglm.dir/chatglm.cpp.o out/chatglm.dir/
173142

174143
# ggml.dir
175144
ggml.dir: build/chatglm.cpp
176-
cd out && mkdir -p ggml.dir && cd ../build && \
177-
cp -rf third_party/ggml/src/CMakeFiles/ggml.dir/ggml.c.o ../out/ggml.dir/ggml.o && \
178-
cp -rf third_party/ggml/src/CMakeFiles/ggml.dir/ggml-alloc.c.o ../out/ggml.dir/ggml-alloc.o
145+
cd out && mkdir -p ggml.dir
146+
cp build/third_party/ggml/src/CMakeFiles/ggml.dir/*.o out/ggml.dir/
179147

180148
# sentencepiece.dir
181149
sentencepiece.dir: build/chatglm.cpp
182-
cd out && mkdir -p sentencepiece.dir && cd ../build && \
183-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/sentencepiece_processor.cc.o ../out/sentencepiece.dir/sentencepiece_processor.o && \
184-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/error.cc.o ../out/sentencepiece.dir/error.o && \
185-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/model_factory.cc.o ../out/sentencepiece.dir/model_factory.o && \
186-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/model_interface.cc.o ../out/sentencepiece.dir/model_interface.o && \
187-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/bpe_model.cc.o ../out/sentencepiece.dir/bpe_model.o && \
188-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/char_model.cc.o ../out/sentencepiece.dir/char_model.o && \
189-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/word_model.cc.o ../out/sentencepiece.dir/word_model.o && \
190-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/unigram_model.cc.o ../out/sentencepiece.dir/unigram_model.o && \
191-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/util.cc.o ../out/sentencepiece.dir/util.o && \
192-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/normalizer.cc.o ../out/sentencepiece.dir/normalizer.o && \
193-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/filesystem.cc.o ../out/sentencepiece.dir/filesystem.o && \
194-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/builtin_pb/sentencepiece.pb.cc.o ../out/sentencepiece.dir/sentencepiece.pb.o && \
195-
cp -rf third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/builtin_pb/sentencepiece_model.pb.cc.o ../out/sentencepiece.dir/sentencepiece_model.pb.o
150+
cd out && mkdir -p sentencepiece.dir
151+
cp build/third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/*.cc.o out/sentencepiece.dir/
152+
cp build/third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/builtin_pb/*.cc.o out/sentencepiece.dir/
196153

197154
# protobuf-lite.dir
198155
protobuf-lite.dir: sentencepiece.dir
199-
cd out && mkdir -p protobuf-lite.dir && cd ../build && \
200-
find third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/__/third_party/protobuf-lite -name '*.cc.o' -exec cp {} ../out/protobuf-lite.dir/ \;
156+
cd out && mkdir -p protobuf-lite.dir
157+
cp build/third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/__/third_party/protobuf-lite/*.cc.o out/protobuf-lite.dir/
201158

202159
# absl.dir
203160
absl.dir: sentencepiece.dir
204-
cd out && mkdir -p absl.dir && cd ../build && \
205-
find third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/__/third_party/absl/flags/ -name '*.cc.o' -exec cp {} ../out/absl.dir/ \;
161+
cd out && mkdir -p absl.dir
162+
cp build/third_party/sentencepiece/src/CMakeFiles/sentencepiece-static.dir/__/third_party/absl/flags/flag.cc.o out/absl.dir/
163+
164+
# ggml-metal
165+
ggml-metal: ggml.dir
166+
cp build/bin/ggml-metal.metal .
206167

207168
# binding
208169
binding.o: prepare build/chatglm.cpp chatglm.dir ggml.dir sentencepiece.dir protobuf-lite.dir absl.dir
209170
$(CXX) $(CXXFLAGS) \
210171
-I./chatglm.cpp \
211172
-I./chatglm.cpp/third_party/ggml/include/ggml \
212173
-I./chatglm.cpp/third_party/sentencepiece/src \
213-
binding.cpp -o binding.o -c $(LDFLAGS)
214-
215-
# ggml-cuda
216-
ggml.dir/ggml-cuda.o: ggml.dir
217-
cd build && cp -rf "$(GGML_CUDA_OBJ_PATH)" ../out/ggml.dir/ggml-cuda.o
218-
219-
# ggml-opencl
220-
ggml.dir/ggml-opencl.o: ggml.dir
221-
cd build && cp -rf third_party/ggml/src/CMakeFiles/ggml.dir/ggml-opencl.cpp.o ../out/ggml.dir/ggml-opencl.o
222-
223-
# ggml-metal
224-
ggml.dir/ggml-metal.o: ggml.dir ggml.dir/ggml-backend.o
225-
cd build && cp -rf bin/ggml-metal.metal ../ggml-metal.metal && \
226-
cp -rf third_party/ggml/src/CMakeFiles/ggml.dir/ggml-metal.m.o ../out/ggml.dir/ggml-metal.o
227-
228-
# ggml-backend
229-
ggml.dir/ggml-backend.o:
230-
cd build && cp -rf third_party/ggml/src/CMakeFiles/ggml.dir/ggml-backend.c.o ../out/ggml.dir/ggml-backend.o
174+
binding.cpp -MD -MT binding.o -MF binding.d -o binding.o -c
231175

232176
libbinding.a: prepare binding.o $(EXTRA_TARGETS)
233177
ar src libbinding.a \
234-
out/chatglm.dir/chatglm.o \
178+
out/chatglm.dir/*.o \
235179
out/ggml.dir/*.o out/sentencepiece.dir/*.o \
236180
out/protobuf-lite.dir/*.o out/absl.dir/*.o \
237181
binding.o
238182

239183
clean:
240184
rm -rf *.o
185+
rm -rf *.d
241186
rm -rf *.a
242187
rm -rf out
243188
rm -rf build
244189

245-
DOWNLOAD_TARGETS=ggllm-test-model.bin
246-
ifeq ($(OS),Windows_NT)
247-
DOWNLOAD_TARGETS:=windows/ggllm-test-model.bin
248-
endif
249-
250190
ggllm-test-model.bin:
251191
wget -q -N https://huggingface.co/Xorbits/chatglm3-6B-GGML/resolve/main/chatglm3-ggml-q4_0.bin -O ggllm-test-model.bin
252-
windows/ggllm-test-model.bin:
253-
powershell -Command "Invoke-WebRequest -Uri 'https://huggingface.co/Xorbits/chatglm3-6B-GGML/resolve/main/chatglm3-ggml-q4_0.bin' -OutFile 'ggllm-test-model.bin'"
254192

255-
test: $(DOWNLOAD_TARGETS) libbinding.a
256-
TEST_MODEL=ggllm-test-model.bin go test ${CGO_TAGS} .
193+
test: ggllm-test-model.bin libbinding.a
194+
go test ${CGO_TAGS} -timeout 1800s -o go-chatglm.cpp.test -c -cover
195+
TEST_MODEL=ggllm-test-model.bin ./go-chatglm.cpp.test

0 commit comments

Comments
 (0)