config.py 8.71 KB
Newer Older
chenxl's avatar
chenxl committed
1
2
#!/usr/bin/env python
# coding=utf-8
3
4
"""
Description  :
chenxl's avatar
chenxl committed
5
6
7
Author       : unicornchan
Date         : 2024-06-11 16:35:42
Version      : 1.0.0
8
LastEditors  : WuHao
chenxl's avatar
chenxl committed
9
LastEditTime : 2024-08-12 06:31:14
10
"""
chenxl's avatar
chenxl committed
11
import os
chenxl's avatar
chenxl committed
12
import shutil
chenxl's avatar
chenxl committed
13
14
15
import yaml

from ktransformers.server.config.singleton import Singleton
16
from typing import Optional
chenxl's avatar
chenxl committed
17
18
19


class Config(metaclass=Singleton):
20
21
    """Singleton pattern Config class, used to get all configurations."""

chenxl's avatar
chenxl committed
22
23
24
25
26
27
28
29
30
    CONFIG_FILE_NAME = "config.yaml"

    @staticmethod
    def load() -> dict:
        """load config file

        Returns:
            dict: all configs
        """
31
32
33
34
35
36
        base_path: str = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        config_yaml: str = os.path.join(base_path, "configs", Config.CONFIG_FILE_NAME)

        user_path: str = os.path.expanduser("~")
        localstore_path: str = os.path.join(user_path, ".ktransformers")
        config_path: str = os.path.join(localstore_path, Config.CONFIG_FILE_NAME)
chenxl's avatar
chenxl committed
37
38
39
        if not os.path.exists(config_yaml):
            print(f"Can't find config file, {config_yaml}")
            exit(-1)
chenxl's avatar
chenxl committed
40
41
42
        if not os.path.exists(localstore_path):
            os.mkdir(localstore_path)
        if not os.path.exists(config_path):
43
44
            shutil.copyfile(config_yaml, config_path)
        with open(config_path, "r", encoding="utf-8") as fp:
chenxl's avatar
chenxl committed
45
46
47
48
49
50
51
52
53
            config = yaml.safe_load(fp)
        return config

    @staticmethod
    def to_path(path: str) -> str:
        """
        process file path
        """
        base_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
54
        real_path = path if os.path.isabs(path) else os.path.join(base_path, path)
chenxl's avatar
chenxl committed
55
56
57
58
        return real_path

    def __init__(self):
        cfg = Config.load()
59
60
61
        self.base_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        self.user_path: str = os.path.expanduser("~")
        self.localstore_path: str = os.path.join(self.user_path, ".ktransformers")
chenxl's avatar
chenxl committed
62
63
64
65
66
67
68
        # log configs
        self.log_dir = os.path.join(self.base_path, Config.to_path(cfg["log"]["dir"]))
        self.log_file = cfg["log"]["file"]
        self.log_level = cfg["log"]["level"]
        self.backup_count = cfg["log"]["backup_count"]

        # server configs
69
        self.server: dict = cfg.get("server", {})
chenxl's avatar
chenxl committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        self.server_ip = self.server.get("ip", "0.0.0.0")
        self.server_port = self.server.get("port", 9016)

        # db configs
        self.db_configs: dict = cfg.get("db", {})
        self.db_type = self.db_configs.get("type", "")
        self.db_host = os.path.join(self.base_path, self.db_configs.get("host", ""))
        self.db_port = self.db_configs.get("port", "")
        self.db_name = self.db_configs.get("database", "")
        self.db_pool_size = self.db_configs.get("pool_size")
        self.db_database = self.db_configs.get("database", "")

        # user config
        self.user_config: dict = cfg.get("user", {})
        self.user_secret_key = self.user_config.get("secret_key", "")
        self.user_algorithm = self.user_config.get("algorithm", "")
86

chenxl's avatar
chenxl committed
87
        # model config
88
        self.model: dict = cfg.get("model", {})
chenxl's avatar
chenxl committed
89
        self.backend_type: str = self.model.get("type", "transformers")
90
        self.model_dir: str = self.model.get("path", "")
91
92
        # to make sure it consistent with previous version
        self.model_path: str = self.model_dir
chenxl's avatar
chenxl committed
93
94
        self.model_name: str = self.model.get("name", "")
        self.model_device: str = self.model.get("device", "cuda:0")
95
96
97
        self.gguf_path: Optional[str] = self.model.get("gguf_path", None)
        # self.model_cache_lens = self.model.get("cache_lens")
        self.optimize_config_path: Optional[str] = self.model.get(
98
            "optimize_config_path", None
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        )
        self.paged = self.model.get("paged", True)

        self.total_context = self.model.get("total_context", 2**18)
        self.max_batch_size = self.model.get("max_batch_size", 20 if self.paged else 1)
        self.max_chunk_size = self.model.get("max_chunk_size", 2048)
        self.max_new_tokens = self.model.get("max_new_tokens", 500)
        self.json_mode = self.model.get("json_mode", False)
        self.healing = self.model.get("healing", False)
        self.ban_strings: Optional[list] = self.model.get("ban_strings", None)
        self.gpu_split: Optional[str] = self.model.get("gpu_split", None)
        self.length: Optional[int] = self.model.get("length", None)
        self.rope_scale: Optional[float] = self.model.get("rope_scale", None)
        self.rope_alpha: Optional[float] = self.model.get("rope_alpha", None)
        self.no_flash_attn = self.model.get("no_flash_attn", False)
        self.low_mem = self.model.get("low_mem", False)
        self.experts_per_token: Optional[int] = self.model.get("experts_per_token", None)
        self.load_q4 = self.model.get("load_q4", False)
        self.fast_safetensors = self.model.get("fast_safetensors", False)
        self.draft_model_dir: Optional[str] = self.model.get("draft_model_dir", None)
        self.no_draft_scale = self.model.get("no_draft_scale", False)
        self.modes = self.model.get("modes", False)
        self.mode = self.model.get("mode", "llama")
        self.username = self.model.get("username", "User")
        self.botname = self.model.get("botname", "Chatbort")
        self.system_prompt: Optional[str] = self.model.get("system_prompt", None)
        self.temperature = self.model.get("temperature", 0.95)
        self.smoothing_factor = self.model.get("smoothing_factor", 0.0)
        self.dynamic_temperature: Optional[str] = self.model.get("dynamic_temperature", None)
        self.top_k = self.model.get("top_k", 50)
        self.top_p = self.model.get("top_p", 0.8)
        self.top_a = self.model.get("top_a", 0.0)
        self.skew = self.model.get("skew", 0.0)
        self.typical = self.model.get("typical", 0.0)
        self.repetition_penalty = self.model.get("repetition_penalty", 1.01)
        self.frequency_penalty = self.model.get("frequency_penalty", 0.0)
        self.presence_penalty = self.model.get("presence_penalty", 0.0)
        self.max_response_tokens = self.model.get("max_response_tokens", 300)
        self.response_chunk = self.model.get("response_chunk", 250)
        self.no_code_formatting = self.model.get("no_code_formatting", False)
        self.cache_8bit = self.model.get("cache_8bit", False)
        self.cache_q4 = self.model.get("cache_q4", True)
        self.ngram_decoding = self.model.get("ngram_decoding", False)
        self.print_timings = self.model.get("print_timings", False)
        self.amnesia = self.model.get("amnesia", False)
        self.batch_size = self.model.get("batch_size", 1)
        self.cache_lens = self.model.get("cache_lens", 4096)
        self.device = self.model.get("device", "cuda:2")

chenxl's avatar
chenxl committed
148
149
150
151
        # web config
        self.web: dict = cfg.get("web", {})
        self.web_cross_domain: bool = self.web.get("open_cross_domain", True)
        self.mount_web: bool = self.web.get("mount", False)
chenxl's avatar
chenxl committed
152

chenxl's avatar
chenxl committed
153
154
        self.ext: dict = cfg.get("ext", {})
        self.cpu_infer = self.ext.get("cpu_infer", 10)
chenxl's avatar
chenxl committed
155

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
        # file config
        self.local_store_configs: dict = cfg.get("local_store", {})
        self.file_upload_dir: str = os.path.join(
            self.localstore_path, self.local_store_configs.get("file_upload_dir", "")
        )
        self.assistant_store_dir: str = os.path.join(
            self.localstore_path, self.local_store_configs.get("assistant_store_dir", "")
        )

        # long context config
        self.long_context_config: dict = cfg.get("long_context", {})
        self.chunk_size = self.long_context_config.get("chunk_size", 4096)
        self.max_seq_len = self.long_context_config.get("max_seq_len", 32000)
        self.block_size = self.long_context_config.get("block_size", 128)
        self.local_windows_len = self.long_context_config.get("local_windows_len", 4096)
        self.second_select_num = self.long_context_config.get("second_select_num", 32)
        self.anchor_type = self.long_context_config.get("anchor_type", "DYNAMIC")
        self.kv_type = self.long_context_config.get("kv_type", "FP16")
        self.dense_layer_num = self.long_context_config.get("dense_layer_num", 2)
        self.anchor_num = self.long_context_config.get("anchor_num", 1)
        self.preselect_block = self.long_context_config.get("preselect_block", True)
        self.head_select_mode = self.long_context_config.get("head_select_mode", "SHARED")
        self.preselect_block_count = self.long_context_config.get("preselect_block_count", 32)
        self.layer_step = self.long_context_config.get("layer_step", 1)
        self.token_step = self.long_context_config.get("token_step", 100)
chenxl's avatar
chenxl committed
181

182
183
184
        # local chat
        self.local_chat_config: dict = cfg.get("local_chat", {})
        self.prompt_file = self.local_chat_config.get("prompt_file", None)