1 /*
2  * Copyright (c) 2024 MediaTek Inc.
3  *
4  * Licensed under the BSD License (the "License"); you may not use this file
5  * except in compliance with the License. See the license file in the root
6  * directory of this source tree for more details.
7  */
8 
9 #pragma once
10 
11 #include <stddef.h>
12 #include <strings.h>
13 
14 namespace example {
15 namespace llm_helper {
16 
17 typedef enum { INT4, INT8, INT16, FP16, INT32, FP32, INVALID } LLMType;
18 
getLLMTypeSize(const LLMType llm_type)19 inline size_t getLLMTypeSize(const LLMType llm_type) {
20 #define RETURN_LLMTYPE_SIZE(type, size) \
21   case type:                            \
22     return size;
23 
24   switch (llm_type) {
25     RETURN_LLMTYPE_SIZE(INT4, 1) // min 1 byte
26     RETURN_LLMTYPE_SIZE(INT8, 1)
27     RETURN_LLMTYPE_SIZE(INT16, 2)
28     RETURN_LLMTYPE_SIZE(FP16, 2)
29     RETURN_LLMTYPE_SIZE(INT32, 4)
30     RETURN_LLMTYPE_SIZE(FP32, 4)
31     default:
32       return 0; // invalid type
33   }
34 
35 #undef RETURN_LLMTYPE_SIZE
36 }
37 
getLLMTypeFromName(const char * llm_type_name)38 inline LLMType getLLMTypeFromName(const char* llm_type_name) {
39 #define RETURN_LLMTYPE(type)               \
40   if (!strcasecmp(llm_type_name, #type)) { \
41     return LLMType::type;                  \
42   }
43 
44   RETURN_LLMTYPE(INT4)
45   RETURN_LLMTYPE(INT8)
46   RETURN_LLMTYPE(INT16)
47   RETURN_LLMTYPE(FP16)
48   RETURN_LLMTYPE(INT32)
49   RETURN_LLMTYPE(FP32)
50 
51 #undef RETURN_LLMTYPE
52 
53   return LLMType::INVALID;
54 }
55 
getLLMTypeName(const LLMType llm_type)56 inline const char* getLLMTypeName(const LLMType llm_type) {
57 #define RETURN_TYPENAME_STR(type) \
58   case LLMType::type:             \
59     return #type;
60 
61   switch (llm_type) {
62     RETURN_TYPENAME_STR(INT4)
63     RETURN_TYPENAME_STR(INT8)
64     RETURN_TYPENAME_STR(INT16)
65     RETURN_TYPENAME_STR(FP16)
66     RETURN_TYPENAME_STR(FP32)
67     RETURN_TYPENAME_STR(INT32)
68     RETURN_TYPENAME_STR(INVALID)
69   }
70 
71 #undef RETURN_TYPENAME_STR
72 }
73 
74 } // namespace llm_helper
75 } // namespace example
76