Skip to content

Commit 37592cc

Browse files
committed
[cmn/ggml/llama] reduced enum size using scoped enums (before C23 keyword is ignored)
This optimization technique reduces size structures where there is an scoped enum field, the smaller the size of the structure, the higher chance hitting to CPU cache L2 or L3.
1 parent 658987c commit 37592cc

File tree

22 files changed

+59
-58
lines changed

22 files changed

+59
-58
lines changed

common/chat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ struct common_chat_tool {
3535
std::string parameters;
3636
};
3737

38-
enum common_chat_tool_choice {
38+
enum common_chat_tool_choice : uint8_t {
3939
COMMON_CHAT_TOOL_CHOICE_AUTO,
4040
COMMON_CHAT_TOOL_CHOICE_REQUIRED,
4141
COMMON_CHAT_TOOL_CHOICE_NONE,
4242
};
4343

44-
enum common_chat_format {
44+
enum common_chat_format : uint8_t {
4545
COMMON_CHAT_FORMAT_CONTENT_ONLY,
4646
COMMON_CHAT_FORMAT_GENERIC,
4747
COMMON_CHAT_FORMAT_MISTRAL_NEMO,

common/common.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int32_t cpu_get_num_math();
6262
// Common params
6363
//
6464

65-
enum llama_example {
65+
enum llama_example : uint8_t {
6666
LLAMA_EXAMPLE_COMMON,
6767
LLAMA_EXAMPLE_SPECULATIVE,
6868
LLAMA_EXAMPLE_MAIN,
@@ -84,7 +84,7 @@ enum llama_example {
8484
LLAMA_EXAMPLE_COUNT,
8585
};
8686

87-
enum common_sampler_type {
87+
enum common_sampler_type : uint8_t {
8888
COMMON_SAMPLER_TYPE_NONE = 0,
8989
COMMON_SAMPLER_TYPE_DRY = 1,
9090
COMMON_SAMPLER_TYPE_TOP_K = 2,
@@ -99,18 +99,18 @@ enum common_sampler_type {
9999
};
100100

101101
// dimensionality reduction methods, used by cvector-generator
102-
enum dimre_method {
102+
enum dimre_method : uint8_t {
103103
DIMRE_METHOD_PCA,
104104
DIMRE_METHOD_MEAN,
105105
};
106106

107-
enum common_conversation_mode {
107+
enum common_conversation_mode : uint8_t {
108108
COMMON_CONVERSATION_MODE_DISABLED = 0,
109109
COMMON_CONVERSATION_MODE_ENABLED = 1,
110110
COMMON_CONVERSATION_MODE_AUTO = 2,
111111
};
112112

113-
enum common_grammar_trigger_type {
113+
enum common_grammar_trigger_type : uint8_t {
114114
COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN,
115115
COMMON_GRAMMAR_TRIGGER_TYPE_WORD,
116116
COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN,
@@ -211,7 +211,7 @@ struct common_params_vocoder {
211211
bool use_guide_tokens = false; // enable guide tokens to improve TTS accuracy // NOLINT
212212
};
213213

214-
enum common_reasoning_format {
214+
enum common_reasoning_format : uint8_t {
215215
COMMON_REASONING_FORMAT_NONE,
216216
COMMON_REASONING_FORMAT_DEEPSEEK, // Extract thinking tag contents and return as `message.reasoning_content`
217217
};

common/console.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
#pragma once
44

55
#include <string>
6+
#include <cstdint>
67

78
namespace console {
8-
enum display_t {
9+
enum display_t : uint8_t {
910
reset = 0,
1011
prompt,
1112
user_input,

common/log.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static int64_t t_us() {
2020
}
2121

2222
// colors
23-
enum common_log_col : int {
23+
enum common_log_col : uint8_t {
2424
COMMON_LOG_COL_DEFAULT = 0,
2525
COMMON_LOG_COL_BOLD,
2626
COMMON_LOG_COL_RED,

ggml/include/ggml-backend.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extern "C" {
4646
// Backend buffer
4747
//
4848

49-
enum ggml_backend_buffer_usage {
49+
enum ggml_backend_buffer_usage : uint8_t {
5050
GGML_BACKEND_BUFFER_USAGE_ANY = 0,
5151
GGML_BACKEND_BUFFER_USAGE_WEIGHTS = 1,
5252
GGML_BACKEND_BUFFER_USAGE_COMPUTE = 2,
@@ -127,7 +127,7 @@ extern "C" {
127127
// Backend device
128128
//
129129

130-
enum ggml_backend_dev_type {
130+
enum ggml_backend_dev_type : uint8_t {
131131
// CPU device using system memory
132132
GGML_BACKEND_DEVICE_TYPE_CPU,
133133
// GPU device using dedicated memory

ggml/include/ggml-cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern "C" {
2222
};
2323

2424
// numa strategies
25-
enum ggml_numa_strategy {
25+
enum ggml_numa_strategy : uint8_t {
2626
GGML_NUMA_STRATEGY_DISABLED = 0,
2727
GGML_NUMA_STRATEGY_DISTRIBUTE = 1,
2828
GGML_NUMA_STRATEGY_ISOLATE = 2,

ggml/include/ggml-opt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern "C" {
2727

2828
// built-in loss types, i.e. the built-in quantities minimized by the optimizer
2929
// custom loss types can be defined via mean or sum which simply reduce the outputs for all datapoints to a single value
30-
enum ggml_opt_loss_type {
30+
enum ggml_opt_loss_type : uint8_t {
3131
GGML_OPT_LOSS_TYPE_MEAN,
3232
GGML_OPT_LOSS_TYPE_SUM,
3333
GGML_OPT_LOSS_TYPE_CROSS_ENTROPY,
@@ -59,7 +59,7 @@ extern "C" {
5959

6060
// ====== Model / Context ======
6161

62-
enum ggml_opt_build_type {
62+
enum ggml_opt_build_type : uint8_t {
6363
GGML_OPT_BUILD_TYPE_FORWARD,
6464
GGML_OPT_BUILD_TYPE_GRAD,
6565
GGML_OPT_BUILD_TYPE_OPT,

ggml/include/ggml.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ extern "C" {
317317
GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4)
318318
GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...);
319319

320-
enum ggml_status {
320+
enum ggml_status : int8_t {
321321
GGML_STATUS_ALLOC_FAILED = -2,
322322
GGML_STATUS_FAILED = -1,
323323
GGML_STATUS_SUCCESS = 0,
@@ -348,7 +348,7 @@ extern "C" {
348348
struct ggml_cgraph;
349349

350350
// NOTE: always add types at the end of the enum to keep backward compatibility
351-
enum ggml_type {
351+
enum ggml_type : uint8_t {
352352
GGML_TYPE_F32 = 0,
353353
GGML_TYPE_F16 = 1,
354354
GGML_TYPE_Q4_0 = 2,
@@ -392,13 +392,13 @@ extern "C" {
392392
};
393393

394394
// precision
395-
enum ggml_prec {
395+
enum ggml_prec : uint8_t {
396396
GGML_PREC_DEFAULT,
397397
GGML_PREC_F32,
398398
};
399399

400400
// model file types
401-
enum ggml_ftype {
401+
enum ggml_ftype : int8_t {
402402
GGML_FTYPE_UNKNOWN = -1,
403403
GGML_FTYPE_ALL_F32 = 0,
404404
GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
@@ -426,7 +426,7 @@ extern "C" {
426426
};
427427

428428
// available tensor operations:
429-
enum ggml_op {
429+
enum ggml_op : uint8_t {
430430
GGML_OP_NONE = 0,
431431

432432
GGML_OP_DUP,
@@ -520,7 +520,7 @@ extern "C" {
520520
GGML_OP_COUNT,
521521
};
522522

523-
enum ggml_unary_op {
523+
enum ggml_unary_op : uint8_t {
524524
GGML_UNARY_OP_ABS,
525525
GGML_UNARY_OP_SGN,
526526
GGML_UNARY_OP_NEG,
@@ -539,13 +539,13 @@ extern "C" {
539539
GGML_UNARY_OP_COUNT,
540540
};
541541

542-
enum ggml_object_type {
542+
enum ggml_object_type : uint8_t {
543543
GGML_OBJECT_TYPE_TENSOR,
544544
GGML_OBJECT_TYPE_GRAPH,
545545
GGML_OBJECT_TYPE_WORK_BUFFER
546546
};
547547

548-
enum ggml_log_level {
548+
enum ggml_log_level : uint8_t {
549549
GGML_LOG_LEVEL_NONE = 0,
550550
GGML_LOG_LEVEL_DEBUG = 1,
551551
GGML_LOG_LEVEL_INFO = 2,
@@ -555,7 +555,7 @@ extern "C" {
555555
};
556556

557557
// this tensor...
558-
enum ggml_tensor_flag {
558+
enum ggml_tensor_flag : uint8_t {
559559
GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
560560
GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
561561
GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
@@ -1678,7 +1678,7 @@ extern "C" {
16781678
struct ggml_tensor * b,
16791679
int stride);
16801680

1681-
enum ggml_op_pool {
1681+
enum ggml_op_pool : uint8_t {
16821682
GGML_OP_POOL_MAX,
16831683
GGML_OP_POOL_AVG,
16841684
GGML_OP_POOL_COUNT,
@@ -1717,7 +1717,7 @@ extern "C" {
17171717
float p0,
17181718
float p1);
17191719

1720-
enum ggml_scale_mode {
1720+
enum ggml_scale_mode : uint8_t {
17211721
GGML_SCALE_MODE_NEAREST = 0,
17221722
GGML_SCALE_MODE_BILINEAR = 1,
17231723
};
@@ -1767,7 +1767,7 @@ extern "C" {
17671767
int max_period);
17681768

17691769
// sort rows
1770-
enum ggml_sort_order {
1770+
enum ggml_sort_order : uint8_t {
17711771
GGML_SORT_ORDER_ASC,
17721772
GGML_SORT_ORDER_DESC,
17731773
};
@@ -2137,7 +2137,7 @@ extern "C" {
21372137
// the goal should be to create an API that other backends can use move everything to the ggml base
21382138

21392139
// scheduling priorities
2140-
enum ggml_sched_priority {
2140+
enum ggml_sched_priority : uint8_t {
21412141
GGML_SCHED_PRIO_NORMAL,
21422142
GGML_SCHED_PRIO_MEDIUM,
21432143
GGML_SCHED_PRIO_HIGH,

ggml/include/gguf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extern "C" {
5050
#endif
5151

5252
// types that can be stored as GGUF KV data
53-
enum gguf_type {
53+
enum gguf_type : uint8_t {
5454
GGUF_TYPE_UINT8 = 0,
5555
GGUF_TYPE_INT8 = 1,
5656
GGUF_TYPE_UINT16 = 2,

ggml/src/ggml-cpu/ggml-cpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ typedef atomic_int atomic_flag;
114114

115115
#define ATOMIC_FLAG_INIT 0
116116

117-
typedef enum {
117+
typedef enum : uint8_t {
118118
memory_order_relaxed,
119119
memory_order_consume,
120120
memory_order_acquire,

0 commit comments

Comments
 (0)