-
Notifications
You must be signed in to change notification settings - Fork 143
/
mistralrs.pyi
289 lines (265 loc) · 8.36 KB
/
mistralrs.pyi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
148
149
150
151
152
153
154
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from dataclasses import dataclass
from enum import Enum
from typing import Iterator
@dataclass
class ChatCompletionRequest:
"""
A ChatCompletionRequest represents a request sent to the mistral.rs engine. It encodes information
about input data, sampling, and how to return the response.
"""
messages: list[dict[str, str]] | str
model: str
logit_bias: dict[int, float] | None = None
logprobs: bool = False
top_logprobs: int | None = None
max_tokens: int | None = None
n_choices: int = 1
presence_penalty: float | None = None
frequency_penalty: float | None = None
stop_seqs: list[str] | None = None
temperature: float | None = None
top_p: float | None = None
stream: bool = False
top_k: int | None = None
grammar: str | None = None
grammar_type: str | None = None
adapters: list[str] | None = None
@dataclass
class CompletionRequest:
"""
A CompletionRequest represents a request sent to the mistral.rs engine. It encodes information
about input data, sampling, and how to return the response.
"""
prompt: str
model: str
echo_prompt: bool = False
logit_bias: dict[int, float] | None = None
max_tokens: int | None = None
n_choices: int = 1
best_of: int = 1
presence_penalty: float | None = None
frequency_penalty: float | None = None
stop_seqs: list[str] | None = None
temperature: float | None = None
top_p: float | None = None
top_k: int | None = None
suffix: str | None = None
grammar: str | None = None
grammar_type: str | None = None
adapters: list[str] | None = None
@dataclass
class Architecture(Enum):
Mistral = "mistral"
Gemma = "gemma"
Mixtral = "mixtral"
Llama = "llama"
Phi2 = "phi2"
class Which(Enum):
"""
Which model to select. See the docs for the `Which` enum in API.md for more details.
Usage:
```python
>>> Which.Plain(...)
```
"""
@dataclass
class Plain:
model_id: str
arch: Architecture
tokenizer_json: str | None = None
repeat_last_n: int = 64
@dataclass
class XLora:
arch: Architecture
xlora_model_id: str
order: str
tgt_non_granular_index: int | None = None
model_id: str | None = None
tokenizer_json: str | None = None
repeat_last_n: int = 64
@dataclass
class Lora:
arch: Architecture
adapters_model_id: str
order: str
model_id: str | None = None
tokenizer_json: str | None = None
repeat_last_n: int = 64
@dataclass
class GGUF:
tok_model_id: str
quantized_model_id: str
quantized_filename: str
repeat_last_n: int = 64
@dataclass
class XLoraGGUF:
tok_model_id: str
quantized_model_id: str
quantized_filename: str
xlora_model_id: str
order: str
tgt_non_granular_index: int | None = None
repeat_last_n: int = 64
@dataclass
class LoraGGUF:
tok_model_id: str
quantized_model_id: str
quantized_filename: str
adapters_model_id: str
order: str
repeat_last_n: int = 64
@dataclass
class GGML:
tok_model_id: str
quantized_model_id: str
quantized_filename: str
tokenizer_json: str | None = None
repeat_last_n: int = 64
@dataclass
class XLoraGGML:
tok_model_id: str
quantized_model_id: str
quantized_filename: str
xlora_model_id: str
order: str
tgt_non_granular_index: int | None = None
tokenizer_json: str | None = None
repeat_last_n: int = 64
@dataclass
class LoraGGML:
tok_model_id: str
quantized_model_id: str
quantized_filename: str
adapters_model_id: str
order: str
tokenizer_json: str | None = None
repeat_last_n: int = 64
class Runner:
def __init__(
self,
which: Which,
max_seqs: int = 16,
no_kv_cache: bool = False,
prefix_cache_n: int = 16,
token_source: str = "cache",
speculative_gamma: int = 32,
which_draft: Which | None = None,
chat_template: str | None = None,
num_device_layers: int | None = None,
in_situ_quant: str | None = None,
) -> None:
"""
Load a model.
- `which` specifies which model to load or the target model to load in the case of speculative decoding.
- `max_seqs` specifies how many sequences may be running at any time.
- `no_kv_cache` disables the KV cache.
- `prefix_cache_n` sets the number of sequences to hold in the device prefix cache, others will be evicted to CPU.
- `token_source` specifies where to load the HF token from.
The token source follows the following format: "literal:<value>", "env:<value>", "path:<value>", "cache" to use a cached token or "none" to use no token.
- `speculative_gamma` specifies the `gamma` parameter for specuative decoding, the ratio of draft tokens to generate before calling
the target model. If `which_draft` is not specified, this is ignored.
- `which_draft` specifies which draft model to load. Setting this parameter will cause a speculative decoding model to be loaded,
with `which` as the target (higher quality) model and `which_draft` as the draft (lower quality) model.
- `chat_template` specifies an optional JINJA chat template.
The JINJA template should have `messages`, `add_generation_prompt`, `bos_token`, `eos_token`, and `unk_token` as inputs.
It is used if the automatic deserialization fails. If this ends with `.json` (ie., it is a file) then that template is loaded.
- `num_device_layers` sets the number of layers to load and run on the device.
- `in_situ_quant` sets the optional in-situ quantization for models that are not quantized (not GGUF or GGML).
"""
...
def send_chat_completion_request(
self, request: ChatCompletionRequest
) -> ChatCompletionResponse | Iterator[ChatCompletionChunkResponse]:
"""
Send a chat completion request to the mistral.rs engine, returning the response object or a generator
over chunk objects.
"""
def send_completion_request(self, request: CompletionRequest) -> CompletionResponse:
"""
Send a chat completion request to the mistral.rs engine, returning the response object.
"""
def send_re_isq(self, dtype: str) -> CompletionResponse:
"""
Send a request to re-ISQ the model. If the model was loaded as GGUF or GGML then nothing will happen.
"""
def activate_adapters(self, adapter_names: list[str]) -> None:
"""
Send a request to make the specified adapters the active adapters for the model.
"""
@dataclass
class Usage:
completion_tokens: int
prompt_tokens: int
total_tokens: int
avg_tok_per_sec: float
avg_prompt_tok_per_sec: float
avg_compl_tok_per_sec: float
total_time_sec: float
total_prompt_time_sec: float
total_completion_time_sec: float
@dataclass
class ResponseMessage:
content: str
role: str
@dataclass
class TopLogprob:
token: int
logprob: float
bytes: str
@dataclass
class ResponseLogprob:
token: str
logprob: float
bytes: list[int]
top_logprobs: list[TopLogprob]
@dataclass
class Logprobs:
content: list[ResponseLogprob] | None
@dataclass
class Choice:
finish_reason: str
index: int
message: ResponseMessage
logprobs: Logprobs
@dataclass
class ChatCompletionResponse:
id: str
choices: list[Choice]
created: int
model: str
system_fingerprint: str
object: str
usage: Usage
@dataclass
class Delta:
content: str
role: str
@dataclass
class ChunkChoice:
finish_reason: str | None
index: int
delta: Delta
logprobs: ResponseLogprob | None
@dataclass
class ChatCompletionChunkResponse:
id: str
choices: list[ChunkChoice]
created: int
model: str
system_fingerprint: str
object: str
@dataclass
class CompletionChoice:
finish_reason: str
index: int
text: str
# NOTE(EricLBuehler): `logprobs` in undocumented
@dataclass
class CompletionResponse:
id: str
choices: list[CompletionChoice]
created: int
model: str
system_fingerprint: str
object: str
usage: Usage