Skip to content

Commit c73def1

Browse files
committed
Merge 'origin/master' into hipblas
2 parents d8ea75e + f0d70f1 commit c73def1

File tree

11 files changed

+145
-91
lines changed

11 files changed

+145
-91
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ models/*
2828
/result
2929
/perplexity
3030
/embedding
31-
/benchmark-q4_0-matmult
31+
/benchmark-matmult
3232
/vdot
3333
/Pipfile
3434

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ endif()
367367
add_library(llama
368368
llama.cpp
369369
llama.h
370-
llama_util.h)
370+
llama-util.h)
371371

372372
target_include_directories(llama PUBLIC .)
373373
target_compile_features(llama PUBLIC cxx_std_11) # don't bump

Makefile

+12-7
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ endif
3434
#
3535

3636
# keep standard at C11 and C++11
37-
CFLAGS = -I. -O3 -DNDEBUG -std=c11 -fPIC
38-
CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC
37+
CFLAGS = -I. -O3 -std=c11 -fPIC
38+
CXXFLAGS = -I. -I./examples -O3 -std=c++11 -fPIC
3939
LDFLAGS =
4040

41+
ifndef LLAMA_DEBUG
42+
CFLAGS += -DNDEBUG
43+
CXXFLAGS += -DNDEBUG
44+
endif
45+
4146
# warnings
4247
CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith
4348
CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-multichar
@@ -181,14 +186,14 @@ $(info )
181186
ggml.o: ggml.c ggml.h ggml-cuda.h
182187
$(CC) $(CFLAGS) -c $< -o $@
183188

184-
llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama_util.h
189+
llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama-util.h
185190
$(CXX) $(CXXFLAGS) -c $< -o $@
186191

187192
common.o: examples/common.cpp examples/common.h
188193
$(CXX) $(CXXFLAGS) -c $< -o $@
189194

190195
clean:
191-
rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-q4_0-matmult
196+
rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-matmult
192197

193198
main: examples/main/main.cpp ggml.o llama.o common.o $(OBJS)
194199
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
@@ -218,9 +223,9 @@ libllama.so: llama.o ggml.o $(OBJS)
218223
# Tests
219224
#
220225

221-
benchmark: examples/benchmark/benchmark-q4_0-matmult.c ggml.o $(OBJS)
222-
$(CXX) $(CXXFLAGS) $^ -o benchmark-q4_0-matmult $(LDFLAGS)
223-
./benchmark-q4_0-matmult
226+
benchmark-matmult: examples/benchmark/benchmark-matmult.cpp ggml.o $(OBJS)
227+
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
228+
./$@
224229

225230
.PHONY: tests
226231
tests:

examples/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ else()
3535
add_subdirectory(perplexity)
3636
add_subdirectory(embedding)
3737
add_subdirectory(save-load-state)
38+
add_subdirectory(benchmark)
3839
endif()

examples/benchmark/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(TARGET benchmark)
2+
add_executable(${TARGET} benchmark-matmult.cpp)
3+
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
4+
target_compile_features(${TARGET} PRIVATE cxx_std_11)

examples/benchmark/benchmark-q4_0-matmult.c renamed to examples/benchmark/benchmark-matmult.cpp

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/*
2-
License: MIT License
3-
4-
Changelog:
5-
- 2023-03-31 Initial version by Sebastian Apel (https://github.com/SebastianApel)
6-
7-
*/
8-
91
#include <locale.h>
102
#include "ggml.h"
113
#include <assert.h>
@@ -45,7 +37,7 @@ float tensor_sum_elements(struct ggml_tensor * tensor) {
4537

4638
#define TENSOR_TYPE_AS_STR(TYPE) TYPE == GGML_TYPE_F32 ? "FP32" : TYPE == GGML_TYPE_F16 ? "FP16" : TYPE == GGML_TYPE_Q4_0 ? "Q4_0" : TYPE == GGML_TYPE_Q4_1 ? "Q4_1" : "UNKNOWN"
4739

48-
#define TENSOR_DUMP(TENSOR) printf("%15s: type = %i (%5s) ne = %5d x %5d x %5d, nb = (%5li, %5li, %5li) - ", #TENSOR, \
40+
#define TENSOR_DUMP(TENSOR) printf("%15s: type = %i (%5s) ne = %5ld x %5ld x %5ld, nb = (%5li, %5li, %5li) - ", #TENSOR, \
4941
TENSOR->type,TENSOR_TYPE_AS_STR(TENSOR->type),\
5042
TENSOR->ne[0], TENSOR->ne[1], TENSOR->ne[2], TENSOR->nb[0], TENSOR->nb[1], TENSOR->nb[2]); \
5143
{ float sum = tensor_sum_elements(TENSOR); printf("Sum of tensor %s is %6.2f\n",#TENSOR, sum); }
@@ -98,12 +90,9 @@ int main(int argc, char ** argv) {
9890
}
9991
}
10092

101-
10293
// create the ggml context
10394
printf("Starting Test\n");
10495

105-
106-
10796
struct ggml_context * ctx;
10897
//const int sizex = 4096;
10998
//const int sizey = 11008;
@@ -125,16 +114,18 @@ int main(int argc, char ** argv) {
125114
#endif
126115

127116
//printf("Memsize required = %i\n", sizex*sizex);
128-
ggml_type wtype = GGML_TYPE_F32;
129117

130118
size_t ctx_size = 0;
131-
ctx_size += sizex*sizey*ggml_type_sizef(wtype);
132-
ctx_size += sizex*sizey*ggml_type_sizef(wtype);
133119
ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32);
134-
ctx_size += sizex*sizeof(float);
135-
ctx_size += 1024*1024*100;
120+
ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32);
121+
ctx_size += sizex*sizez*ggml_type_sizef(GGML_TYPE_F32);
122+
ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_Q4_0);
123+
ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_Q4_0);
124+
ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32); // BLAS
125+
ctx_size += sizex*sizey*ggml_type_sizef(GGML_TYPE_F32); // BLAS
126+
ctx_size += 1024*1024*16;
136127

137-
printf("Allocating Memory of size %li byes, %li MB\n",ctx_size, (ctx_size/1024/1024));
128+
printf("Allocating Memory of size %li bytes, %li MB\n",ctx_size, (ctx_size/1024/1024));
138129

139130
struct ggml_init_params params = {
140131
/*.mem_size =*/ ctx_size,
@@ -217,7 +208,7 @@ int main(int argc, char ** argv) {
217208
const int dimz = sizez;
218209
long long int flops_per_dot_product = dimy + dimy;
219210
long long int flops_per_matrix = flops_per_dot_product * dimx * dimz; ;
220-
printf("Matrix Multiplication of (%i,%i,%i) x (%i,%i,%i) - aboout %6.2f gFLOPS\n\n", sizex, sizey, 1, sizex, sizez, 1, 1.0f*flops_per_matrix / 1000 / 1000 / 1000);
211+
printf("Matrix Multiplication of (%i,%i,%i) x (%i,%i,%i) - about %6.2f gFLOPS\n\n", sizex, sizey, 1, sizex, sizez, 1, 1.0f*flops_per_matrix / 1000 / 1000 / 1000);
221212

222213

223214
// Let's use the F32 result from above as a reference for the q4_0 multiplication
@@ -234,7 +225,6 @@ int main(int argc, char ** argv) {
234225
ggml_graph_compute(ctx, &gf31);
235226
long long int stop = ggml_time_us();
236227
long long int usec = stop-start;
237-
float sec = usec/1000000;
238228
float flops_per_usec = (1.0f*flops_per_matrix)/usec;
239229
printf("%9i;%8i;%6i;%6i;%6i;%15lli;%18lli;%19.2f\n",
240230
i,
+49-31
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
#include <vector>
2-
#include <cstdio>
3-
#include <chrono>
4-
51
#include "common.h"
62
#include "llama.h"
7-
#include "llama.cpp"
83

9-
using namespace std;
4+
#include <vector>
5+
#include <cstdio>
6+
#include <chrono>
107

118
int main(int argc, char ** argv) {
129
gpt_params params;
@@ -20,21 +17,25 @@ int main(int argc, char ** argv) {
2017
return 1;
2118
}
2219

20+
if (params.n_predict < 0) {
21+
params.n_predict = 16;
22+
}
23+
2324
auto lparams = llama_context_default_params();
2425

25-
lparams.n_ctx = params.n_ctx;
26-
lparams.n_parts = params.n_parts;
27-
lparams.seed = params.seed;
28-
lparams.f16_kv = params.memory_f16;
29-
lparams.use_mmap = params.use_mmap;
30-
lparams.use_mlock = params.use_mlock;
26+
lparams.n_ctx = params.n_ctx;
27+
lparams.n_parts = params.n_parts;
28+
lparams.seed = params.seed;
29+
lparams.f16_kv = params.memory_f16;
30+
lparams.use_mmap = params.use_mmap;
31+
lparams.use_mlock = params.use_mlock;
3132

3233
auto n_past = 0;
33-
auto last_n_tokens_data = vector<llama_token>(params.repeat_last_n, 0);
34+
auto last_n_tokens_data = std::vector<llama_token>(params.repeat_last_n, 0);
3435

3536
// init
3637
auto ctx = llama_init_from_file(params.model.c_str(), lparams);
37-
auto tokens = vector<llama_token>(params.n_ctx);
38+
auto tokens = std::vector<llama_token>(params.n_ctx);
3839
auto n_prompt_tokens = llama_tokenize(ctx, params.prompt.c_str(), tokens.data(), tokens.size(), true);
3940

4041
if (n_prompt_tokens < 1) {
@@ -43,26 +44,29 @@ int main(int argc, char ** argv) {
4344
}
4445

4546
// evaluate prompt
46-
4747
llama_eval(ctx, tokens.data(), n_prompt_tokens, n_past, params.n_threads);
4848

4949
last_n_tokens_data.insert(last_n_tokens_data.end(), tokens.data(), tokens.data() + n_prompt_tokens);
5050
n_past += n_prompt_tokens;
5151

52+
const size_t state_size = llama_get_state_size(ctx);
53+
uint8_t * state_mem = new uint8_t[state_size];
54+
5255
// Save state (rng, logits, embedding and kv_cache) to file
53-
FILE *fp_write = fopen("dump_state.bin", "wb");
54-
auto state_size = llama_get_state_size(ctx);
55-
auto state_mem = new uint8_t[state_size];
56-
llama_copy_state_data(ctx, state_mem); // could also copy directly to memory mapped file
57-
fwrite(state_mem, 1, state_size, fp_write);
58-
fclose(fp_write);
56+
{
57+
FILE *fp_write = fopen("dump_state.bin", "wb");
58+
llama_copy_state_data(ctx, state_mem); // could also copy directly to memory mapped file
59+
fwrite(state_mem, 1, state_size, fp_write);
60+
fclose(fp_write);
61+
}
5962

6063
// save state (last tokens)
61-
auto last_n_tokens_data_saved = vector<llama_token>(last_n_tokens_data);
62-
auto n_past_saved = n_past;
64+
const auto last_n_tokens_data_saved = std::vector<llama_token>(last_n_tokens_data);
65+
const auto n_past_saved = n_past;
6366

6467
// first run
6568
printf("\n%s", params.prompt.c_str());
69+
6670
for (auto i = 0; i < params.n_predict; i++) {
6771
auto logits = llama_get_logits(ctx);
6872
auto n_vocab = llama_n_vocab(ctx);
@@ -75,31 +79,42 @@ int main(int argc, char ** argv) {
7579
auto next_token = llama_sample_token(ctx, &candidates_p);
7680
auto next_token_str = llama_token_to_str(ctx, next_token);
7781
last_n_tokens_data.push_back(next_token);
82+
7883
printf("%s", next_token_str);
7984
if (llama_eval(ctx, &next_token, 1, n_past, params.n_threads)) {
8085
fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
8186
return 1;
8287
}
8388
n_past += 1;
8489
}
90+
8591
printf("\n\n");
8692

8793
// free old model
8894
llama_free(ctx);
8995

9096
// load new model
91-
9297
auto ctx2 = llama_init_from_file(params.model.c_str(), lparams);
9398

9499
// Load state (rng, logits, embedding and kv_cache) from file
95-
FILE *fp_read = fopen("dump_state.bin", "rb");
96-
auto state_size2 = llama_get_state_size(ctx2);
97-
if (state_size != state_size2) {
98-
fprintf(stderr, "\n%s : failed to validate state size\n", __func__);
100+
{
101+
FILE *fp_read = fopen("dump_state.bin", "rb");
102+
if (state_size != llama_get_state_size(ctx2)) {
103+
fprintf(stderr, "\n%s : failed to validate state size\n", __func__);
104+
return 1;
105+
}
106+
107+
const size_t ret = fread(state_mem, 1, state_size, fp_read);
108+
if (ret != state_size) {
109+
fprintf(stderr, "\n%s : failed to read state\n", __func__);
110+
return 1;
111+
}
112+
113+
llama_set_state_data(ctx2, state_mem); // could also read directly from memory mapped file
114+
fclose(fp_read);
99115
}
100-
fread(state_mem, 1, state_size, fp_read);
101-
llama_set_state_data(ctx2, state_mem); // could also read directly from memory mapped file
102-
fclose(fp_read);
116+
117+
delete[] state_mem;
103118

104119
// restore state (last tokens)
105120
last_n_tokens_data = last_n_tokens_data_saved;
@@ -118,13 +133,16 @@ int main(int argc, char ** argv) {
118133
auto next_token = llama_sample_token(ctx2, &candidates_p);
119134
auto next_token_str = llama_token_to_str(ctx2, next_token);
120135
last_n_tokens_data.push_back(next_token);
136+
121137
printf("%s", next_token_str);
122138
if (llama_eval(ctx2, &next_token, 1, n_past, params.n_threads)) {
123139
fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
124140
return 1;
125141
}
126142
n_past += 1;
127143
}
144+
128145
printf("\n\n");
146+
129147
return 0;
130148
}

0 commit comments

Comments
 (0)