-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Foundational Model API eval wrapper (#849)
* FMAPI model wrapper * add chat wrapper too * revert * end line * formatting * less verbose * better error messages
- Loading branch information
Showing
4 changed files
with
104 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright 2022 MosaicML LLM Foundry authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import logging | ||
import os | ||
import time | ||
from typing import Dict | ||
|
||
import requests | ||
from transformers import AutoTokenizer | ||
|
||
from llmfoundry.models.inference_api_wrapper.openai_causal_lm import ( | ||
OpenAICausalLMEvalWrapper, OpenAIChatAPIEvalWrapper, OpenAIEvalInterface) | ||
|
||
__all__ = [ | ||
'FMAPICasualLMEvalWrapper', | ||
'FMAPIChatAPIEvalWrapper', | ||
] | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def block_until_ready(base_url: str): | ||
"""Block until the endpoint is ready.""" | ||
sleep_s = 5 | ||
timout_s = 5 * 60 # At max, wait 5 minutes | ||
|
||
ping_url = f'{base_url}/ping' | ||
|
||
waited_s = 0 | ||
while True: | ||
try: | ||
requests.get(ping_url) | ||
log.info(f'Endpoint {ping_url} is ready') | ||
break | ||
except requests.exceptions.ConnectionError: | ||
log.debug( | ||
f'Endpoint {ping_url} not ready yet. Sleeping {sleep_s} seconds' | ||
) | ||
time.sleep(sleep_s) | ||
waited_s += sleep_s | ||
|
||
if waited_s >= timout_s: | ||
raise TimeoutError( | ||
f'Endpoint {ping_url} did not become read after {waited_s:,} seconds, exiting' | ||
) | ||
|
||
|
||
class FMAPIEvalInterface(OpenAIEvalInterface): | ||
|
||
def __init__(self, model_cfg: Dict, tokenizer: AutoTokenizer): | ||
is_local = model_cfg.pop('local', False) | ||
if is_local: | ||
base_url = os.environ.get('MOSAICML_MODEL_ENDPOINT', | ||
'http://0.0.0.0:8080/v2') | ||
model_cfg['base_url'] = base_url | ||
block_until_ready(base_url) | ||
|
||
if 'base_url' not in model_cfg: | ||
raise ValueError( | ||
'Must specify base_url or use local=True in model_cfg for FMAPIsEvalWrapper' | ||
) | ||
|
||
super().__init__(model_cfg, tokenizer) | ||
|
||
|
||
class FMAPICasualLMEvalWrapper(FMAPIEvalInterface, OpenAICausalLMEvalWrapper): | ||
"""Databricks Foundational Model API wrapper for causal LM models.""" | ||
|
||
|
||
class FMAPIChatAPIEvalWrapper(FMAPIEvalInterface, OpenAIChatAPIEvalWrapper): | ||
"""Databricks Foundational Model API wrapper for chat models.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters