forked from foundation-model-stack/fms-dgt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatsonx.py
205 lines (171 loc) · 7.28 KB
/
watsonx.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
# Standard
from typing import Any, List
import copy
import logging
# Third Party
from tqdm import tqdm
# Local
from fms_dgt.base.registry import get_resource, register_block
from fms_dgt.blocks.generators.llm import LMBlockData, LMGenerator
from fms_dgt.resources.watsonx import WatsonXResource
import fms_dgt.blocks.generators.utils as generator_utils
try:
# Third Party
from ibm_watsonx_ai import Credentials
from ibm_watsonx_ai.foundation_models import ModelInference as Model
from ibm_watsonx_ai.foundation_models.schema import (
ReturnOptionProperties,
TextGenParameters,
)
except ModuleNotFoundError:
pass
# Disable third party logging
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("ibm_watsonx_ai").setLevel(logging.WARNING)
@register_block("watsonx")
class WatsonXAIGenerator(LMGenerator):
"""WatsonX AI Generator"""
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
try:
# Third Party
import ibm_watsonx_ai
except ModuleNotFoundError as err:
raise Exception(
"attempted to use 'watsonx' LM type, but package `ibm_watsonx_ai` not installed. ",
"please install these via `pip install -r fms_dgt[watsonx]`",
) from err
# Load WatsonX Resource
self._watsonx_resource: WatsonXResource = get_resource(
"watsonx",
)
# Configure credentials for WatsonX AI service
if self._watsonx_resource.token:
self._credentials = Credentials(
url=self._watsonx_resource.url, token=self._watsonx_resource.token
)
else:
self._credentials = Credentials(
url=self._watsonx_resource.url, api_key=self._watsonx_resource.key
)
def generate_batch(
self, requests: List[LMBlockData], disable_tqdm: bool = False
) -> None:
# group requests by kwargs
grouper = generator_utils.Grouper(requests, lambda x: str(x.gen_kwargs))
pbar = tqdm(
total=len(requests),
disable=(disable_tqdm or (self.rank != 0)),
desc="Running generate_batch requests",
)
for _, reqs in grouper.get_grouped().items():
chunks: List[List[LMBlockData]] = generator_utils.chunks(
reqs, n=self._watsonx_resource.max_calls
)
for chunk in chunks:
# Prepare inputs
inputs = [instance.prompt for instance in chunk]
# all kwargs are identical within a chunk
gen_kwargs = next(iter(chunk)).gen_kwargs
if isinstance(kwargs := copy.deepcopy(gen_kwargs), dict):
# start with default params then overwrite with kwargs
kwargs = {**self._base_kwargs, **kwargs}
else:
raise ValueError(
f"Expected repr(kwargs) to be of type repr(dict) but got {kwargs}"
)
until = kwargs.get("stop_sequences", None)
# TODO: I don't believe WatsonX allows the "n" samples param
kwargs.pop("n", None)
# Initialize model
model = Model(
model_id=kwargs.pop("model_id_or_path", self.model_id_or_path),
credentials=self._credentials,
project_id=self._watsonx_resource.project_id,
params=TextGenParameters(
**kwargs,
),
)
# Execute generation routine
responses = model.generate(prompt=inputs)
# Process generated outputs
for idx, instance in enumerate(chunk):
self.update_instance_with_result(
"generate_batch",
responses[idx]["results"][0]["generated_text"],
instance,
until,
additional={
"gen_token_count": responses[idx]["results"][0][
"generated_token_count"
]
},
)
pbar.update(1)
# Clean up model object
model = None
pbar.close()
def loglikelihood_batch(
self, requests: List[LMBlockData], disable_tqdm: bool = False
) -> None:
# group requests by kwargs
grouper = generator_utils.Grouper(requests, lambda x: str(x.kwargs))
pbar = tqdm(
total=len(requests),
disable=(disable_tqdm or (self.rank != 0)),
desc="Running loglikelihood_batch requests",
)
for _, reqs in grouper.get_grouped().items():
chunks: List[List[LMBlockData]] = generator_utils.chunks(
reqs, n=self._watsonx_resource.max_calls
)
for chunk in chunks:
# Prepare inputs
to_score = ["".join(instance.args) for instance in chunk]
# all kwargs are identical within a chunk
gen_kwargs = next(iter(chunk)).kwargs
if isinstance(kwargs := copy.deepcopy(gen_kwargs), dict):
# start with default params in self.config then overwrite with kwargs
kwargs = {**self._base_kwargs, **kwargs}
else:
raise ValueError(
f"Expected repr(kwargs) to be of type repr(dict) but got {kwargs}"
)
# Initialize model
model = Model(
model_id=kwargs.pop("model_id_or_path", self.model_id_or_path),
credentials=self._credentials,
project_id=self._watsonx_resource.project_id,
params=TextGenParameters(
temperature=1.0,
decoding_method="greedy",
max_new_tokens=1,
min_new_tokens=0,
return_options=ReturnOptionProperties(
generated_tokens=True,
token_logprobs=True,
),
),
)
# Execute generation routine
score_responses = model.generate(prompt=to_score)
for idx, instance in enumerate(chunk):
score_result = score_responses[idx]["results"][0]
# tok_ct - 1 since first token in encoding is bos
generated_tokens = score_result["generated_tokens"][
-(score_result["generated_token_count"] - 1) :
]
answer = sum(
[
tok["logprob"]
for tok in generated_tokens
if tok["logprob"] is not None
]
)
self.update_instance_with_result(
"loglikelihood_batch", answer, instance
)
pbar.update(1)
# Clean up model object
model = None
pbar.close()