llm_config.py Source File

llm_config.py Source File#

Mobilint SDK qb Compiler: llm_config.py Source File
Mobilint SDK qb Compiler v0.11.0.1
MCS002-KR
llm_config.py
1import inspect
2from functools import wraps
3
4from typeguard import typechecked
5from . import ConfigABC
6
7"""
8max_data_len : maximum data length, sum of maximum sequence lengths on all NPU cores. Defaults to 4096.
9max_seq_len : maximum sequence length. Defaults to 4096.
10max_cache_len : maximum cache length. Defaults to 4096.
11random_seq_len_calib : sequence length for random calibration. Defaults to 80.
12use_full_seq_len_calib : whether to use full sequence length for calibration for the last attention layer. Defaults to False.
13use_custom_mask_input : whether to use custom mask input. Defaults to False.
14start_dram_offset : start dram offset. Defaults to 0.
15use_global_core : whether to use global core. Defaults to False.
16llm_batch_size : batch size for LLM. Defaults to 1.
17npu_core_ids : NPU core IDs to use(batched LLM). Defaults to [0]. If the llm_batch_size is greater than 1, then defaults to [0, 1, 2, 3, 4, 5, 6, 7].
18"""
19
20
21class LlmConfig(ConfigABC):
22 py_to_cpp_name = {
23 "max_data_len": "maxDataLen",
24 "max_seq_len": "maxSeqLen",
25 "max_cache_len": "maxCacheLen",
26 "random_seq_len_calib": "randomSeqLenCalib",
27 "use_full_seq_len_calib": "useFullSeqLenCalib",
28 "use_custom_mask_input": "useCustomMaskInput",
29 "start_dram_offset": "startDramOffset",
30 "use_global_core": "useGlobalCore",
31 "llm_batch_size": "llmBatchSize",
32 "npu_core_ids": "npuCoreIds",
33 }
34
35 min_max_check = {
36 "max_data_len": [0, None],
37 "max_seq_len": [0, None],
38 "max_cache_len": [0, None],
39 "random_seq_len_calib": [0, None],
40 "use_full_seq_len_calib": [0, 1],
41 "use_custom_mask_input": [0, 1],
42 "start_dram_offset": [0, None],
43 "use_global_core": [0, 1],
44 "llm_batch_size": [0, None],
45 }
46
47 @typechecked
48 def __init__(
49 self,
50 max_data_len: int,
51 max_seq_len: int,
52 max_cache_len: int,
53 random_seq_len_calib: int,
54 use_full_seq_len_calib: bool,
55 use_custom_mask_input: bool,
56 start_dram_offset: int,
57 use_global_core: bool,
58 llm_batch_size: int,
59 npu_core_ids: list[int],
60 optional: bool = True,
61 ):
62 super().__init__(optional)
63 self.max_data_len = max_data_len
64 self.max_seq_len = max_seq_len
65 self.max_cache_len = max_cache_len
66 self.random_seq_len_calib = random_seq_len_calib
67 self.use_full_seq_len_calib = use_full_seq_len_calib
68 self.use_custom_mask_input = use_custom_mask_input
69 self.start_dram_offset = start_dram_offset
70 self.use_global_core = use_global_core
71 self.llm_batch_size = llm_batch_size
72 self.npu_core_ids = npu_core_ids
73 self.check_valid()
74
75 @classmethod
76 def default_config(cls):
77 max_data_len = 4096
78 max_seq_len = 4096
79 max_cache_len = 4096
80 random_seq_len_calib = 80
81 use_full_seq_len_calib = False
82 use_custom_mask_input = False
83 start_dram_offset = 0
84 use_global_core = False
85 llm_batch_size = 1
86 npu_core_ids = [0]
87
88 return LlmConfig(
89 optional=True,
90 max_data_len=max_data_len,
91 max_seq_len=max_seq_len,
92 max_cache_len=max_cache_len,
93 random_seq_len_calib=random_seq_len_calib,
94 use_full_seq_len_calib=use_full_seq_len_calib,
95 use_custom_mask_input=use_custom_mask_input,
96 start_dram_offset=start_dram_offset,
97 use_global_core=use_global_core,
98 llm_batch_size=llm_batch_size,
99 npu_core_ids=npu_core_ids,
100 )
101
102 def to_dict(self):
103 llm_config_dict = dict()
104 llm_params = dict()
105 for py_name, cpp_name in LlmConfig.py_to_cpp_name.items():
106 value = getattr(self, py_name)
107 llm_params[cpp_name] = value
108 llm_config_dict["LLM"] = llm_params
109 return llm_config_dict
110
111
112def track_passed_args(fn):
113 @wraps(fn)
114 def wrapper(*args, **kwargs):
115 sig = inspect.signature(fn)
116 bound_args = sig.bind_partial(*args, **kwargs)
117
118 # If llm_batch_size is larger than 1, set npu_core_ids to [0, 1, 2, 3, 4, 5, 6, 7]
119 npu_core_ids = []
120 if "llm_batch_size" in bound_args.arguments:
121 llm_batch_size = bound_args.arguments["llm_batch_size"]
122 if llm_batch_size > 1 and "npu_core_ids" not in bound_args.arguments:
123 npu_core_ids = [0, 1, 2, 3, 4, 5, 6, 7]
124
125 # Update default npu_core_ids if necessary
126 bound_args.apply_defaults()
127 if npu_core_ids:
128 bound_args.arguments["npu_core_ids"] = npu_core_ids
129
130 return fn(**bound_args.arguments)
131
132 return wrapper
133
134
135@track_passed_args
136def get_llm_config(
137 max_data_len: int = 4096,
138 max_seq_len: int = 4096,
139 max_cache_len: int = 4096,
140 random_seq_len_calib: int = 80,
141 use_full_seq_len_calib: bool = False,
142 use_custom_mask_input: bool = False,
143 start_dram_offset: int = 0,
144 use_global_core: bool = False,
145 llm_batch_size: int = 1,
146 npu_core_ids: list[int] = [0],
147) -> LlmConfig:
148 return LlmConfig(
149 max_data_len=max_data_len,
150 max_seq_len=max_seq_len,
151 max_cache_len=max_cache_len,
152 random_seq_len_calib=random_seq_len_calib,
153 use_full_seq_len_calib=use_full_seq_len_calib,
154 use_custom_mask_input=use_custom_mask_input,
155 start_dram_offset=start_dram_offset,
156 use_global_core=use_global_core,
157 llm_batch_size=llm_batch_size,
158 npu_core_ids=npu_core_ids,
159 )