Skip to content

Commit 3104687

Browse files
authored
Merge branch 'ggerganov:master' into master
2 parents afdd774 + 815fe72 commit 3104687

File tree

11 files changed

+374
-157
lines changed

11 files changed

+374
-157
lines changed

examples/server/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3259,7 +3259,7 @@ int main(int argc, char ** argv) {
32593259
ctx_server.queue_tasks.terminate();
32603260
};
32613261

3262-
LOG_INF("%s: server is listening on %s:%d - starting the main loop\n", __func__, params.hostname.c_str(), params.port);
3262+
LOG_INF("%s: server is listening on http://%s:%d - starting the main loop\n", __func__, params.hostname.c_str(), params.port);
32633263

32643264
ctx_server.queue_tasks.start_loop();
32653265

ggml/include/ggml.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@
217217

218218
#define GGML_MAX_DIMS 4
219219
#define GGML_MAX_PARAMS 2048
220-
#define GGML_MAX_CONTEXTS 64
221220
#define GGML_MAX_SRC 10
222221
#define GGML_MAX_N_THREADS 512
223222
#define GGML_MAX_OP_PARAMS 64
@@ -657,6 +656,7 @@ extern "C" {
657656
};
658657

659658
// scratch buffer
659+
// TODO: deprecate and remove
660660
struct ggml_scratch {
661661
size_t offs;
662662
size_t size;
@@ -760,8 +760,9 @@ extern "C" {
760760

761761
// main
762762

763-
GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
764-
GGML_API void ggml_free(struct ggml_context * ctx);
763+
GGML_API struct ggml_context * ggml_init (struct ggml_init_params params);
764+
GGML_API void ggml_reset(struct ggml_context * ctx);
765+
GGML_API void ggml_free (struct ggml_context * ctx);
765766

766767
GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
767768

ggml/src/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ if (GGML_KOMPUTE)
810810
kompute-shaders/op_mul_mat_q8_0.comp
811811
kompute-shaders/op_mul_mat_q4_0.comp
812812
kompute-shaders/op_mul_mat_q4_1.comp
813+
kompute-shaders/op_mul_mat_q4_k.comp
813814
kompute-shaders/op_mul_mat_q6_k.comp
814815
kompute-shaders/op_getrows_f32.comp
815816
kompute-shaders/op_getrows_f16.comp
@@ -843,6 +844,7 @@ if (GGML_KOMPUTE)
843844
shaderop_mul_mat_q8_0.h
844845
shaderop_mul_mat_q4_0.h
845846
shaderop_mul_mat_q4_1.h
847+
shaderop_mul_mat_q4_k.h
846848
shaderop_mul_mat_q6_k.h
847849
shaderop_getrows_f32.h
848850
shaderop_getrows_f16.h
@@ -1410,7 +1412,7 @@ list(APPEND GGML_EXTRA_LIBS_PRIVATE Threads::Threads)
14101412

14111413
find_library(MATH_LIBRARY m)
14121414
if (MATH_LIBRARY)
1413-
if (NOT WIN32 OR NOT GGML_SYCL)
1415+
if (NOT WIN32 OR NOT DEFINED ENV{ONEAPI_ROOT})
14141416
list(APPEND GGML_EXTRA_LIBS_PRIVATE m)
14151417
endif()
14161418
endif()

ggml/src/ggml-backend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ static int ggml_backend_sched_backend_from_buffer(ggml_backend_sched_t sched, co
15081508
return -1;
15091509
}
15101510

1511-
#if 1
1511+
#if 0
15121512
#define GGML_SCHED_MAX_SPLITS_DEBUG 4096
15131513
static char causes[GGML_DEFAULT_GRAPH_SIZE*16 + GGML_SCHED_MAX_SPLITS_DEBUG*GGML_SCHED_MAX_SPLIT_INPUTS][128]; // debug only
15141514
#define SET_CAUSE(node, ...) sprintf(causes[hash_id(node)], __VA_ARGS__)

ggml/src/ggml-cuda.cu

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,18 +3107,20 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
31073107
}
31083108
return false;
31093109
} break;
3110+
case GGML_OP_NORM:
3111+
case GGML_OP_RMS_NORM:
3112+
return ggml_is_contiguous(op->src[0]) && op->ne[0] % WARP_SIZE == 0;
3113+
break;
31103114
case GGML_OP_NONE:
31113115
case GGML_OP_RESHAPE:
31123116
case GGML_OP_VIEW:
31133117
case GGML_OP_PERMUTE:
31143118
case GGML_OP_TRANSPOSE:
3115-
case GGML_OP_NORM:
31163119
case GGML_OP_ADD:
31173120
case GGML_OP_ADD1:
31183121
case GGML_OP_SUB:
31193122
case GGML_OP_MUL:
31203123
case GGML_OP_DIV:
3121-
case GGML_OP_RMS_NORM:
31223124
case GGML_OP_SCALE:
31233125
case GGML_OP_SQR:
31243126
case GGML_OP_SQRT:

ggml/src/ggml-kompute.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "shaderop_mul_mat_q8_0.h"
2121
#include "shaderop_mul_mat_q4_0.h"
2222
#include "shaderop_mul_mat_q4_1.h"
23+
#include "shaderop_mul_mat_q4_k.h"
2324
#include "shaderop_mul_mat_q6_k.h"
2425
#include "shaderop_mul_mat_mat_f32.h"
2526
#include "shaderop_getrows_f32.h"
@@ -1067,6 +1068,40 @@ static void ggml_vk_mul_mat_q8_0(Args&&... args) {
10671068
ggml_vk_mul_mat_impl(spirv, "q8_0", 1/*We access blocks unaligned*/, std::forward<Args>(args)...);
10681069
}
10691070

1071+
static void ggml_vk_mul_mat_q4_k(
1072+
kp::Sequence& seq,
1073+
const std::shared_ptr<kp::Tensor>& inA,
1074+
const std::shared_ptr<kp::Tensor>& inB,
1075+
const std::shared_ptr<kp::Tensor>& out,
1076+
uint32_t inAOff, uint32_t inBOff, uint32_t outOff,
1077+
int32_t ne00, int32_t ne01, int32_t ne02, int32_t ne10,
1078+
int32_t ne11, int32_t ne12, int32_t ne13, int32_t ne0,
1079+
int32_t ne1, int32_t r2, int32_t r3
1080+
) {
1081+
const static auto spirv = getSpirvShader(kp::shader_data::op_mul_mat_q4_k_comp_spv,
1082+
kp::shader_data::op_mul_mat_q4_k_comp_spv_len);
1083+
1084+
struct PushConstants {
1085+
uint32_t inAOff, inBOff, outOff;
1086+
int32_t ne00, ne10, ne0, ne1, ne01, ne02, ne12, r2, r3;
1087+
} pushConsts {
1088+
0, 0, 0,
1089+
ne00, ne10, ne0, ne1, ne01, ne02, ne12, r2, r3
1090+
};
1091+
1092+
std::shared_ptr<kp::Algorithm> s_algo = nullptr;
1093+
if (!komputeManager()->hasAlgorithm(__func__)) {
1094+
s_algo = komputeManager()->algorithm<uint32_t, PushConstants>(__func__, s_kompute_context->pool.get(), {inA, inB, out}, spirv, {unsigned((ne01 + 3)/4), unsigned(ne11), unsigned(ne12) * unsigned(ne13)}, {}, {pushConsts});
1095+
} else {
1096+
s_algo = komputeManager()->getAlgorithm(__func__);
1097+
s_algo->setTensors({inA, inB, out});
1098+
s_algo->setWorkgroup({unsigned((ne01 + 3)/4), unsigned(ne11), unsigned(ne12) * unsigned(ne13)});
1099+
s_algo->setPushConstants<PushConstants>({pushConsts});
1100+
s_algo->updateDescriptors(s_kompute_context->pool.get());
1101+
}
1102+
seq.record<kp::OpAlgoDispatch>(s_algo);
1103+
}
1104+
10701105
static void ggml_vk_mul_mat_q6_k(
10711106
kp::Sequence& seq,
10721107
const std::shared_ptr<kp::Tensor>& inA,
@@ -1384,6 +1419,7 @@ static bool ggml_backend_kompute_device_supports_op(ggml_backend_dev_t dev, cons
13841419
case GGML_TYPE_Q8_0:
13851420
case GGML_TYPE_Q4_0:
13861421
case GGML_TYPE_Q4_1:
1422+
case GGML_TYPE_Q4_K:
13871423
return true;
13881424
default:
13891425
;
@@ -1635,6 +1671,12 @@ static void ggml_vk_graph_compute(struct ggml_kompute_context * ctx, struct ggml
16351671
ne00, ne01, ne02, ne10, ne11, ne12, ne13, ne0, ne1, r2, r3
16361672
);
16371673
break;
1674+
case GGML_TYPE_Q4_K:
1675+
ggml_vk_mul_mat_q4_k(
1676+
seq, id_src0, id_src1, id_dst, off_src0, off_src1, off_dst,
1677+
ne00, ne01, ne02, ne10, ne11, ne12, ne13, ne0, ne1, ne12/ne02, ne13/ne03
1678+
);
1679+
break;
16381680
case GGML_TYPE_Q6_K:
16391681
ggml_vk_mul_mat_q6_k(
16401682
seq, id_src0, id_src1, id_dst, off_src0, off_src1, off_dst,

ggml/src/ggml.c

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ void ggml_abort(const char * file, int line, const char * fmt, ...) {
306306
}
307307

308308
#define GGML_DEBUG 0
309+
309310
#define GGML_GELU_FP16
310311
#define GGML_GELU_QUICK_FP16
311312

@@ -2014,7 +2015,7 @@ static const size_t GGML_OBJECT_SIZE = sizeof(struct ggml_object);
20142015

20152016
struct ggml_context {
20162017
size_t mem_size;
2017-
void* mem_buffer;
2018+
void * mem_buffer;
20182019
bool mem_buffer_owned;
20192020
bool no_alloc;
20202021
bool no_alloc_save; // this is used to save the no_alloc state when using scratch buffers
@@ -3263,7 +3264,6 @@ struct ggml_numa_nodes {
32633264
//
32643265

32653266
struct ggml_state {
3266-
struct ggml_context_container contexts[GGML_MAX_CONTEXTS];
32673267
struct ggml_numa_nodes numa;
32683268
};
32693269

@@ -3845,7 +3845,6 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
38453845
const uint64_t t_start = ggml_time_us(); UNUSED(t_start);
38463846

38473847
g_state = (struct ggml_state) {
3848-
/*.contexts =*/ { { 0 } },
38493848
/*.numa =*/ {
38503849
.n_nodes = 0,
38513850
.total_cpus = 0,
@@ -3864,26 +3863,9 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
38643863
is_first_call = false;
38653864
}
38663865

3867-
// find non-used context in g_state
3868-
struct ggml_context * ctx = NULL;
3869-
3870-
for (int i = 0; i < GGML_MAX_CONTEXTS; i++) {
3871-
if (!g_state.contexts[i].used) {
3872-
g_state.contexts[i].used = true;
3873-
ctx = &g_state.contexts[i].context;
3874-
3875-
GGML_PRINT_DEBUG("%s: found unused context %d\n", __func__, i);
3876-
break;
3877-
}
3878-
}
3879-
3880-
if (ctx == NULL) {
3881-
GGML_PRINT_DEBUG("%s: no unused context found\n", __func__);
3882-
3883-
ggml_critical_section_end();
3866+
ggml_critical_section_end();
38843867

3885-
return NULL;
3886-
}
3868+
struct ggml_context * ctx = GGML_MALLOC(sizeof(struct ggml_context));
38873869

38883870
// allow to call ggml_init with 0 size
38893871
if (params.mem_size == 0) {
@@ -3911,42 +3893,31 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
39113893

39123894
GGML_PRINT_DEBUG("%s: context initialized\n", __func__);
39133895

3914-
ggml_critical_section_end();
3915-
39163896
return ctx;
39173897
}
39183898

3919-
void ggml_free(struct ggml_context * ctx) {
3899+
void ggml_reset(struct ggml_context * ctx) {
39203900
if (ctx == NULL) {
39213901
return;
39223902
}
39233903

3924-
// make this function thread safe
3925-
ggml_critical_section_start();
3926-
3927-
bool found = false;
3928-
3929-
for (int i = 0; i < GGML_MAX_CONTEXTS; i++) {
3930-
if (&g_state.contexts[i].context == ctx) {
3931-
g_state.contexts[i].used = false;
3932-
3933-
GGML_PRINT_DEBUG("%s: context %d has been freed. memory used = %zu\n",
3934-
__func__, i, ggml_used_mem(ctx));
3935-
3936-
if (ctx->mem_buffer_owned) {
3937-
ggml_aligned_free(ctx->mem_buffer, ctx->mem_size);
3938-
}
3904+
ctx->n_objects = 0;
3905+
ctx->objects_begin = NULL;
3906+
ctx->objects_end = NULL;
3907+
ctx->scratch = (struct ggml_scratch) { 0, 0, NULL, };
3908+
ctx->scratch_save = (struct ggml_scratch) { 0, 0, NULL, };
3909+
}
39393910

3940-
found = true;
3941-
break;
3942-
}
3911+
void ggml_free(struct ggml_context * ctx) {
3912+
if (ctx == NULL) {
3913+
return;
39433914
}
39443915

3945-
if (!found) {
3946-
GGML_PRINT_DEBUG("%s: context not found\n", __func__);
3916+
if (ctx->mem_buffer_owned) {
3917+
ggml_aligned_free(ctx->mem_buffer, ctx->mem_size);
39473918
}
39483919

3949-
ggml_critical_section_end();
3920+
GGML_FREE(ctx);
39503921
}
39513922

39523923
size_t ggml_used_mem(const struct ggml_context * ctx) {
@@ -7272,6 +7243,7 @@ struct ggml_tensor * ggml_ssm_conv(
72727243
const int64_t n_s = sx->ne[2];
72737244

72747245
// TODO: maybe support other strides than 1?
7246+
// FIXME: this is always true?
72757247
GGML_ASSERT(sx->ne[0] == d_conv - 1 + n_t);
72767248
GGML_ASSERT(sx->ne[1] == d_inner);
72777249
GGML_ASSERT(n_t >= 0);
@@ -22102,18 +22074,46 @@ static size_t gguf_type_size(enum gguf_type type) {
2210222074
return GGUF_TYPE_SIZE[type];
2210322075
}
2210422076

22105-
static void gguf_tensor_info_sanitize(struct gguf_tensor_info * info) {
22106-
GGML_ASSERT(info->n_dims <= GGML_MAX_DIMS);
22107-
GGML_ASSERT(0 <= info->type && info->type < GGML_TYPE_COUNT);
22077+
static bool gguf_tensor_info_sanitize(struct gguf_tensor_info * info) {
22078+
if (info->n_dims > GGML_MAX_DIMS) {
22079+
fprintf(stderr, "%s: invalid number of dimensions (%" PRIu32 ")\n", __func__, info->n_dims);
22080+
return false;
22081+
}
22082+
22083+
if (info->type < 0 || info->type >= GGML_TYPE_COUNT) {
22084+
fprintf(stderr, "%s: invalid type (%d)\n", __func__, info->type);
22085+
return false;
22086+
}
22087+
22088+
if (strlen(info->name.data) >= GGML_MAX_NAME) {
22089+
fprintf(stderr, "%s: tensor '%s' name is too long\n", __func__, info->name.data);
22090+
return false;
22091+
}
2210822092

2210922093
for (uint32_t i = 0; i < info->n_dims; ++i) {
22110-
GGML_ASSERT(info->ne[i] > 0);
22094+
if (info->ne[i] <= 0) {
22095+
fprintf(stderr, "%s: invalid number of elements (%" PRIu64 ")\n", __func__, info->ne[i]);
22096+
return false;
22097+
}
2211122098
}
2211222099

2211322100
// prevent overflow for total number of elements
22114-
GGML_ASSERT(INT64_MAX/info->ne[1] > info->ne[0]);
22115-
GGML_ASSERT(INT64_MAX/info->ne[2] > info->ne[0]*info->ne[1]);
22116-
GGML_ASSERT(INT64_MAX/info->ne[3] > info->ne[0]*info->ne[1]*info->ne[2]);
22101+
if (INT64_MAX/info->ne[1] <= info->ne[0]) {
22102+
fprintf(stderr, "%s: invalid number of elements (%" PRIu64 ")\n", __func__, info->ne[1]);
22103+
return false;
22104+
}
22105+
22106+
if (INT64_MAX/info->ne[2] <= info->ne[0]*info->ne[1]) {
22107+
fprintf(stderr, "%s: invalid number of elements (%" PRIu64 ")\n", __func__, info->ne[2]);
22108+
return false;
22109+
}
22110+
22111+
if (INT64_MAX/info->ne[3] <= info->ne[0]*info->ne[1]*info->ne[2]) {
22112+
fprintf(stderr, "%s: invalid number of elements (%" PRIu64 ")\n", __func__, info->ne[3]);
22113+
return false;
22114+
}
22115+
22116+
return true;
2211722117
}
2211822118

2211922119
static bool gguf_fread_el(FILE * file, void * dst, size_t size, size_t * offset) {
@@ -22414,8 +22414,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
2241422414
ok = ok && gguf_fread_el (file, &info->type, sizeof(info->type), &offset);
2241522415
ok = ok && gguf_fread_el (file, &info->offset, sizeof(info->offset), &offset);
2241622416

22417-
// TODO: return an error instead of crashing with GGML_ASSERT
22418-
gguf_tensor_info_sanitize(info);
22417+
ok = ok && gguf_tensor_info_sanitize(info);
2241922418

2242022419
// make sure there is no duplicated tensor names
2242122420
for (uint64_t j = 0; j < i && ok; ++j) {

ggml/src/kompute-shaders/common.comp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define TWOPI_F 6.283185307179586f
1616

1717
#define QK_K 256
18+
#define K_SCALE_SIZE 12
1819

1920
#define u8BufToU16(buf, idx) (((uint16_t(buf[idx + 1]) << 8)) | buf[idx])
2021
#define u8BufToFloat16(buf, idx) uint16BitsToHalf u8BufToU16(buf, idx)
@@ -64,6 +65,14 @@ mat4 dequantize_q4_1(const block_q4_1 xb, uint il) {
6465
return reg;
6566
}
6667

68+
#define sizeof_block_q4_k 144
69+
struct block_q4_k {
70+
float16_t d;
71+
float16_t dmin;
72+
uint8_t scales[K_SCALE_SIZE];
73+
uint8_t qs[QK_K/2];
74+
};
75+
6776
#define sizeof_block_q6_k 210
6877
struct block_q6_k {
6978
uint8_t ql[QK_K/2]; // quants, lower 4 bits

0 commit comments

Comments
 (0)