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 | class ModelChallenger:
def __init__(self, challenger: ModelCard):
"""
Instantiates ModelChallenger class
Args:
challenger:
ModelCard of challenger
"""
self._challenger = challenger
self._challenger_metric: Optional[Metric] = None
self._registries = CardRegistries()
@property
def challenger_metric(self) -> Metric:
if self._challenger_metric is not None:
return self._challenger_metric
raise ValueError("Challenger metric not set")
@challenger_metric.setter
def challenger_metric(self, metric: Metric) -> None:
self._challenger_metric = metric
def _get_last_champion_record(self) -> Optional[Dict[str, Any]]:
"""Gets the previous champion record"""
champion_records = self._registries.model.list_cards(
name=self._challenger.name,
repository=self._challenger.repository,
)
if not champion_records:
return None
for record in champion_records:
if record.get("runcard_uid") is not None and record.get("version") != self._challenger.version:
return record
logger.info("No previous version associated with a RunCard. Challenger wins")
return None
def _get_runcard_metric(self, runcard_uid: str, metric_name: str) -> Optional[Metric]:
"""
Loads a RunCard from uid
Args:
runcard_uid:
RunCard uid
metric_name:
Name of metric
"""
runcard = cast(RunCard, self._registries.run.load_card(uid=runcard_uid))
metric = runcard.get_metric(name=metric_name)
if not metric:
return None
return metric[0]
def _battle(self, champion: CardInfo, champion_metric: Metric, lower_is_better: bool) -> BattleReport:
"""
Runs a battle between champion and current challenger
Args:
champion:
Champion record
champion_metric:
Champion metric from a runcard
lower_is_better:
Whether lower metric is preferred
Returns:
`BattleReport`
"""
if lower_is_better:
challenger_win = self.challenger_metric.value < champion_metric.value
else:
challenger_win = self.challenger_metric.value > champion_metric.value
return BattleReport.model_construct(
champion_name=str(champion.name),
champion_version=str(champion.version),
champion_metric=champion_metric,
challenger_metric=self.challenger_metric.model_copy(deep=True),
challenger_win=challenger_win,
)
def _battle_last_model_version(self, metric_name: str, lower_is_better: bool) -> BattleReport:
"""Compares the last champion model to the current challenger"""
champion_record = self._get_last_champion_record()
if champion_record is None:
logger.info("No previous model found. Challenger wins")
return BattleReport(
champion_name="No model",
champion_version="No version",
challenger_win=True,
)
runcard_id = champion_record.get("runcard_uid")
assert runcard_id is not None, "RunCard uid is None"
champion_metric = self._get_runcard_metric(runcard_uid=runcard_id, metric_name=metric_name)
if champion_metric is None:
logger.info("Metric not found in RunCard. Challenger wins")
return BattleReport(
champion_name=champion_record.get("name"),
champion_version=champion_record.get("version"),
challenger_win=True,
)
return self._battle(
champion=CardInfo(
name=champion_record.get("name"),
version=champion_record.get("version"),
),
champion_metric=champion_metric,
lower_is_better=lower_is_better,
)
def _battle_champions(
self,
champions: List[CardInfo],
metric_name: str,
lower_is_better: bool,
) -> List[BattleReport]:
"""Loops through and creates a `BattleReport` for each champion"""
battle_reports = []
for champion in champions:
champion_record = self._registries.model.list_cards(info=champion)
if not bool(champion_record):
logger.error(f"Champion model does not exist. {champion.name} {champion.version}")
battle_reports.append(
BattleReport(
champion_name=champion.name,
champion_version="model not found",
challenger_win=True,
)
)
continue
champion_card = champion_record[0]
runcard_uid = champion_card.get("runcard_uid")
if runcard_uid is None:
logger.error(f"No RunCard associated with champion: {champion.name} {champion.version}")
battle_reports.append(
BattleReport(
champion_name=champion.name,
champion_version="no runcard",
challenger_win=True,
)
)
continue
champion_metric = self._get_runcard_metric(
runcard_uid=runcard_uid,
metric_name=metric_name,
)
if champion_metric is None:
logger.error(f"No metric found in RunCard associated with champion: {champion.name} {champion.version}")
battle_reports.append(
BattleReport(
champion_name=champion.name,
champion_version="metric not found",
challenger_win=True,
)
)
continue
# update name, repository and version in case of None
champion.name = champion.name or champion_card.get("name")
champion.repository = champion.repository or champion_card.get("repository")
champion.version = champion.version or champion_card.get("version")
battle_reports.append(
self._battle(
champion=champion,
champion_metric=champion_metric,
lower_is_better=lower_is_better,
)
)
return battle_reports
def challenge_champion(
self,
metric_name: MetricName,
metric_value: Optional[MetricValue] = None,
champions: Optional[List[CardInfo]] = None,
lower_is_better: Union[bool, List[bool]] = True,
) -> Dict[str, List[BattleReport]]:
"""
Challenges n champion models against the challenger model. If no champion is provided,
the latest model version is used as a champion.
Args:
champions:
Optional list of champion CardInfo
metric_name:
Name of metric to evaluate
metric_value:
Challenger metric value
lower_is_better:
Whether a lower metric value is better or not
Returns
`BattleReport`
"""
# validate inputs
inputs = ChallengeInputs(
metric_name=metric_name,
metric_value=metric_value,
lower_is_better=lower_is_better,
)
report_dict = {}
for name, value, _lower_is_better in zip(
inputs.metric_names,
inputs.metric_values,
inputs.thresholds,
):
# get challenger metric
if value is None:
if self._challenger.metadata.runcard_uid is not None:
metric = self._get_runcard_metric(self._challenger.metadata.runcard_uid, metric_name=name)
assert metric is not None, "Challenger metric not found"
self.challenger_metric = metric
else:
raise ValueError("Challenger and champions must be associated with a registered RunCard")
else:
self.challenger_metric = Metric(name=name, value=value)
if champions is None:
report_dict[name] = [
self._battle_last_model_version(
metric_name=name,
lower_is_better=_lower_is_better,
)
]
else:
report_dict[name] = self._battle_champions(
champions=champions,
metric_name=name,
lower_is_better=_lower_is_better,
)
return report_dict
|