forked from caikit/caikit-nlp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
peft_tgis_remote.py
361 lines (329 loc) · 13.3 KB
/
peft_tgis_remote.py
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# Copyright The Caikit Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This file contains a distributed backend implementation for leveraging the PEFT-trained
prompt vectors in TGIS generation requests.
"""
# Standard
from functools import cached_property
from typing import Iterable, List, Optional, Tuple, Union
import os
# Third Party
import numpy as np
# First Party
from caikit.config import get_config
from caikit.core import ModuleBase, ModuleConfig, ModuleSaver, modules
from caikit.core.exceptions import error_handler
from caikit.core.module_backends import BackendBase, backend_types
from caikit.interfaces.nlp.data_model import (
GeneratedTextResult,
GeneratedTextStreamResult,
TokenizationResults,
)
from caikit.interfaces.nlp.tasks import TextGenerationTask, TokenizationTask
from caikit.interfaces.runtime.data_model import RuntimeServerContextType
from caikit_tgis_backend import TGISBackend
import alog
# Local
from ...data_model import ExponentialDecayLengthPenalty
from ...toolkit.text_generation.tgis_utils import (
GENERATE_FUNCTION_TGIS_ARGS,
TGISGenerationClient,
get_route_info,
)
from ...toolkit.verbalizer_utils import render_verbalizer
from . import PeftPromptTuning
log = alog.use_channel("PEFT_PROMPT_REMOTE")
error = error_handler.get(log)
@modules.module(
backend_type=TGISBackend.backend_type,
base_module=PeftPromptTuning,
tasks=[TextGenerationTask, TokenizationTask],
)
class PeftPromptTuningTGIS(ModuleBase): # pylint: disable=too-many-instance-attributes
SUPPORTED_LOAD_BACKENDS = [TGISBackend.backend_type, backend_types.LOCAL]
## Module Interface ##
def __init__(
self,
base_model_name: str,
prompt_cache_id: str,
eos_token: str,
verbalizer: str,
enable_backend: bool = True,
tgis_backend: Optional[TGISBackend] = None,
prompt_artifacts: Optional[List[str]] = None,
) -> None:
super().__init__()
self._tgis_backend = tgis_backend
if enable_backend:
error.type_check(
"<NLP33971947E>", TGISBackend, tgis_backend=self._tgis_backend
)
# get_client will also launch a local TGIS process and get the model
# loaded when using the local TGIS backend
# Tell the backend to load all of the available prompt files
if prompt_artifacts:
tgis_backend.load_prompt_artifacts(
base_model_name, prompt_cache_id, *prompt_artifacts
)
self.base_model_name = base_model_name
self._prompt_cache_id = prompt_cache_id
self.eos_token = eos_token
self.verbalizer = verbalizer
self.enable_backend = enable_backend
self.tgis_generation_client = TGISGenerationClient(
self.base_model_name,
self.eos_token,
self._client,
self.PRODUCER_ID,
self._prompt_cache_id,
)
def __del__(self):
"""Attempt to clean up the prompt cache on deletion"""
if get_config().unload_tgis_prompt_artifacts:
tgis_backend = getattr(self, "_tgis_backend", None)
prompt_cache_id = getattr(self, "_prompt_cache_id", None)
model_id = getattr(self, "base_model_name", None)
if tgis_backend and prompt_cache_id and model_id:
tgis_backend.unload_prompt_artifacts(model_id, prompt_cache_id)
@cached_property
def _client(self):
# Configure the internal client
# NOTE: This is made optional for the cases where we do not need to execute `.run` function
# for example, bootstrapping a model to caikit format and saving.
if hasattr(self, "tgis_backend") and self._tgis_backend:
return self._tgis_backend.get_client(self.base_model_name)
@classmethod
def load(cls, model_path: str, load_backend: BackendBase) -> "PeftPromptTuningTGIS":
"""Load a TGIS Peft Prompt Tuning distributed module. Note that we do not
leverage artifacts stored within the model here, and we assume that the
prompt vector is already available at a place that the TGIS server can pick it
up.
Args:
model_path: str
Path to the model to be loaded.
load_backend: BackendBase
Backend object to be used to run inference with.
Returns:
PeftPromptTuningTGIS
Instance of this class built from the on disk model.
"""
error.type_check("<NLP85069377E>", TGISBackend, load_backend=load_backend)
config = ModuleConfig.load(model_path)
eos_token = config.eos_token
verbalizer = config.verbalizer
dir_name = os.path.basename(model_path)
# NOTE: base_model_name is used as "model_id" when calling to TGIS backend
base_model_name = config.get("base_model_name", "")
prompt_cache_id = dir_name
error.type_check("<NLP24633932E>", str, prompt_cache_id=prompt_cache_id)
# NOTE: prompt model config stores a base_model_config
# which can be used to validate if the prompt is tuned
# for the model it is being used with. However,
# we are currently not accessing or assuming the accessibilty of
# base model, thus not validating.
# NOTE: When we access base_model_config, we need to make sure
# we convert make it valid json compatible dict (aka doesn't have non string keys)
log.debug("Prompt ID: %s", prompt_cache_id)
log.debug("TGIS model ID: %s", base_model_name)
# Get all the valid prompt artifact files so they can be loaded after
# the connection is established
prompt_artifacts = [
os.path.join(model_path, config.get(config_key))
for config_key in [
PeftPromptTuning._ENCODER_KEY.name,
PeftPromptTuning._DECODER_KEY.name,
]
if config.get(config_key)
]
return cls(
base_model_name,
prompt_cache_id,
eos_token,
verbalizer,
tgis_backend=load_backend,
prompt_artifacts=prompt_artifacts,
)
def save(self, model_path: str):
"""Export the config for this model.
model_path: str
Path to which we should write our model.
"""
saver = ModuleSaver(
self,
model_path=model_path,
)
with saver:
saver.update_config(
{
"base_model_name": self.base_model_name,
"prompt_cache_id": self._prompt_cache_id,
"eos_token": self.eos_token,
"verbalizer": self.verbalizer,
}
)
# pylint: disable=duplicate-code
@TextGenerationTask.taskmethod(context_arg="context")
def run(
self,
text: str,
max_new_tokens: Optional[int] = 20,
min_new_tokens: Optional[int] = 0,
truncate_input_tokens: Optional[int] = 0,
decoding_method: Optional[str] = "GREEDY",
top_k: Optional[int] = None,
top_p: Optional[float] = None,
typical_p: Optional[float] = None,
temperature: Optional[float] = None,
repetition_penalty: Optional[float] = None,
max_time: Optional[float] = None,
exponential_decay_length_penalty: Optional[
Union[Tuple[int, float], ExponentialDecayLengthPenalty]
] = None,
stop_sequences: Optional[List[str]] = None,
seed: Optional[np.uint64] = None,
preserve_input_text: bool = False,
input_tokens: bool = False,
generated_tokens: bool = True,
token_logprobs: bool = True,
token_ranks: bool = True,
context: Optional[RuntimeServerContextType] = None,
) -> GeneratedTextResult:
f"""Run inference against the model running in TGIS.
Args:
{GENERATE_FUNCTION_TGIS_ARGS}
Returns:
GeneratedTextResult
Generated text result produced by TGIS.
"""
error.value_check(
"<NLP87360638E>",
self.enable_backend,
"Backend must be configured and loaded with this module before executing `run` call.",
)
if self._tgis_backend:
self._register_model_connection_with_context(context)
verbalized_text = render_verbalizer(self.verbalizer, {"input": text})
return self.tgis_generation_client.unary_generate(
text=verbalized_text,
preserve_input_text=preserve_input_text,
input_tokens=input_tokens,
generated_tokens=generated_tokens,
token_logprobs=token_logprobs,
token_ranks=token_ranks,
max_new_tokens=max_new_tokens,
min_new_tokens=min_new_tokens,
truncate_input_tokens=truncate_input_tokens,
decoding_method=decoding_method,
top_k=top_k,
top_p=top_p,
typical_p=typical_p,
temperature=temperature,
seed=seed,
repetition_penalty=repetition_penalty,
max_time=max_time,
exponential_decay_length_penalty=exponential_decay_length_penalty,
stop_sequences=stop_sequences,
)
@TextGenerationTask.taskmethod(output_streaming=True, context_arg="context")
def run_stream_out(
self,
text: str,
max_new_tokens: Optional[int] = 20,
min_new_tokens: Optional[int] = 0,
truncate_input_tokens: Optional[int] = 0,
decoding_method: Optional[str] = "GREEDY",
top_k: Optional[int] = None,
top_p: Optional[float] = None,
typical_p: Optional[float] = None,
temperature: Optional[float] = None,
repetition_penalty: Optional[float] = None,
max_time: Optional[float] = None,
exponential_decay_length_penalty: Optional[
Union[Tuple[int, float], ExponentialDecayLengthPenalty]
] = None,
stop_sequences: Optional[List[str]] = None,
seed: Optional[np.uint64] = None,
preserve_input_text: bool = False,
input_tokens: bool = False,
generated_tokens: bool = True,
token_logprobs: bool = True,
token_ranks: bool = True,
context: Optional[RuntimeServerContextType] = None,
) -> Iterable[GeneratedTextStreamResult]:
f"""Run output stream inferencing against the model running in TGIS
Args:
{GENERATE_FUNCTION_TGIS_ARGS}
Returns:
Iterable[GeneratedTextStreamResult]
"""
error.value_check(
"<NLP62995899E>",
self.enable_backend,
"Backend must be configured and loaded with this module \
before executing `run_stream_out` call.",
)
if self._tgis_backend:
self._register_model_connection_with_context(context)
verbalized_text = render_verbalizer(self.verbalizer, {"input": text})
return self.tgis_generation_client.stream_generate(
text=verbalized_text,
preserve_input_text=preserve_input_text,
input_tokens=input_tokens,
generated_tokens=generated_tokens,
token_logprobs=token_logprobs,
token_ranks=token_ranks,
max_new_tokens=max_new_tokens,
min_new_tokens=min_new_tokens,
truncate_input_tokens=truncate_input_tokens,
decoding_method=decoding_method,
top_k=top_k,
top_p=top_p,
typical_p=typical_p,
temperature=temperature,
seed=seed,
repetition_penalty=repetition_penalty,
max_time=max_time,
exponential_decay_length_penalty=exponential_decay_length_penalty,
stop_sequences=stop_sequences,
)
@TokenizationTask.taskmethod(context_arg="context")
def run_tokenizer(
self,
text: str,
context: Optional[RuntimeServerContextType] = None,
) -> TokenizationResults:
"""Run tokenization task against the model running in TGIS.
Args:
text: str
Text to tokenize
Returns:
TokenizationResults
The token count
"""
if self._tgis_backend:
self._register_model_connection_with_context(context)
return self.tgis_generation_client.unary_tokenize(
text=text,
)
def _register_model_connection_with_context(
self, context: Optional[RuntimeServerContextType]
):
ok, route_info = get_route_info(context)
if ok:
self._tgis_backend.register_model_connection(
self.base_model_name, {"hostname": route_info}
)
else:
self._tgis_backend.register_model_connection(self.base_model_name)
self._model_loaded = True