Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Openai compatible gauntlet #1017

Draft
wants to merge 167 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
167 commits
Select commit Hold shift + click to select a range
cd18e74
start
bmosaicml Jan 23, 2024
1fffbad
still need to migrate fixtures
bmosaicml Feb 2, 2024
5a6e81c
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Feb 2, 2024
4aac81e
wip onboarding tests
bmosaicml Feb 2, 2024
946a4af
still workin'
bmosaicml Feb 2, 2024
289ca55
still wip
bmosaicml Feb 2, 2024
3696f8d
maybe done; test out on mcli now
bmosaicml Feb 2, 2024
a20877d
mcli
bmosaicml Feb 2, 2024
53da3ea
remove calibration error
bmosaicml Feb 2, 2024
16b8e32
merge
bmosaicml Feb 7, 2024
a90766e
migration
bmosaicml Feb 7, 2024
72ce793
migration
bmosaicml Feb 7, 2024
667bdec
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Feb 9, 2024
ceff0c4
full migration
bmosaicml Feb 9, 2024
5bb06cc
precommit
bmosaicml Feb 12, 2024
fe83828
fix
bmosaicml Feb 12, 2024
b54a12b
fix pytests
bmosaicml Feb 12, 2024
71e8391
refactor QA
bmosaicml Feb 16, 2024
414153e
update
bmosaicml Feb 22, 2024
a3f5a31
restore
bmosaicml Feb 23, 2024
820069a
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Feb 23, 2024
4a1cd79
add
bmosaicml Feb 23, 2024
d265979
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Feb 23, 2024
6cbaad4
wip
bmosaicml Feb 26, 2024
ddfd7b5
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Feb 26, 2024
71f77e3
fix
bmosaicml Feb 26, 2024
cb3725b
wip
bmosaicml Feb 27, 2024
5135152
update readme
bmosaicml Feb 27, 2024
18bae17
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Feb 27, 2024
c6162dd
final pyright
bmosaicml Feb 27, 2024
25d431e
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Feb 27, 2024
f1b334d
done
bmosaicml Feb 27, 2024
c4ed644
pass prelimiter into ALL the ICL task datasets
eitanturok Feb 27, 2024
2516c24
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Mar 4, 2024
f213a40
allow QA task name stil lfor backward compatibility
bmosaicml Mar 4, 2024
35fd2f1
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Mar 4, 2024
d570e5d
fix
bmosaicml Mar 5, 2024
a5cd308
fix test
bmosaicml Mar 5, 2024
0fb37cd
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Mar 5, 2024
901fc69
add generation length
bmosaicml Mar 5, 2024
a313499
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Mar 5, 2024
df19c0d
remove max_new_tokens
bmosaicml Mar 5, 2024
54bb4c7
fix cpu trsts
bmosaicml Mar 6, 2024
9ebeaa0
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Mar 6, 2024
ca9816c
Merge branch 'main' into migrate_subclasses_to_foundry
maxisawesome Mar 6, 2024
b9d6cd1
try and fix lm eval test
bmosaicml Mar 7, 2024
691ab20
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Mar 7, 2024
c207cd9
temp disable lm task eval test
bmosaicml Mar 7, 2024
0662d59
add data
bmosaicml Mar 7, 2024
7b06a9f
Merge branch 'main' into openai_compatible_gauntlet
bmosaicml Mar 7, 2024
ee440b5
test
bmosaicml Mar 7, 2024
6083891
Merge branch 'main' into openai_compatible_gauntlet
bmosaicml Mar 7, 2024
d6c1ae7
finish
bmosaicml Mar 8, 2024
c85813b
fix test?
bmosaicml Mar 8, 2024
110eaf4
push dataset
bmosaicml Mar 8, 2024
4642578
fix
bmosaicml Mar 8, 2024
b6beb49
finish
bmosaicml Mar 8, 2024
c3ef3a7
fix bug
bmosaicml Mar 11, 2024
08ef908
fix tet
bmosaicml Mar 11, 2024
f1e50dc
remove human eval
bmosaicml Mar 11, 2024
aca0e63
finish
bmosaicml Mar 11, 2024
30fcedd
fix
bmosaicml Mar 12, 2024
877e5eb
modify mlci
bmosaicml Mar 12, 2024
59daa26
Merge branch 'main' into migrate_subclasses_to_foundry
maxisawesome Mar 13, 2024
4217a78
Update scripts/eval/README.md
bmosaicml Mar 13, 2024
6f597a9
fix comments
bmosaicml Mar 13, 2024
8c6e622
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Mar 13, 2024
f387a73
fix bug with seq len
bmosaicml Mar 14, 2024
cbfa3da
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Mar 14, 2024
2f405d9
restore mcli
bmosaicml Mar 14, 2024
76e600a
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Mar 14, 2024
898928e
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Mar 15, 2024
d1c1b33
fix
bmosaicml Mar 15, 2024
dd95f2d
add output logging
bmosaicml Mar 15, 2024
4ad2b2c
fix generation length
bmosaicml Mar 15, 2024
132a179
fix generation length
bmosaicml Mar 15, 2024
650de02
add example dep deployment eval
bmosaicml Mar 15, 2024
8178589
finish
bmosaicml Mar 15, 2024
dbfebba
update dep deployment mcli
bmosaicml Mar 15, 2024
72cffea
update dep deployment mcli
bmosaicml Mar 16, 2024
07fb59e
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Mar 18, 2024
587971f
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Mar 18, 2024
eed61da
change temp
bmosaicml Mar 18, 2024
6abc07b
change mcli
bmosaicml Mar 18, 2024
cb49cf6
dep deployment is kinda jank
bmosaicml Mar 18, 2024
0dd6897
finish merge
bmosaicml Mar 18, 2024
0684aef
add init file
bmosaicml Mar 19, 2024
8278298
add init file
bmosaicml Mar 19, 2024
d651ed5
finish
bmosaicml Mar 19, 2024
b83761b
finish
bmosaicml Mar 19, 2024
53b812c
modify formatting to match eleuther
bmosaicml Mar 20, 2024
79660c7
modify long context
bmosaicml Mar 20, 2024
8904d27
add custom api keys
bmosaicml Mar 20, 2024
8b3328b
add custom api keys
bmosaicml Mar 20, 2024
1ca727b
fix api key
bmosaicml Mar 20, 2024
a585bdf
fix api key
bmosaicml Mar 20, 2024
5129dd4
fix api key
bmosaicml Mar 20, 2024
8e1159c
fix api key
bmosaicml Mar 20, 2024
4d83efa
fix yaml
bmosaicml Mar 21, 2024
c425390
add 16k tasks
bmosaicml Mar 21, 2024
6f580ef
add 16k tasks
bmosaicml Mar 21, 2024
4b2aa6a
add 16k tasks
bmosaicml Mar 21, 2024
cc7bc1e
add 16k tasks
bmosaicml Mar 21, 2024
1455833
add api key to config
bmosaicml Mar 22, 2024
b0dbbb3
fix
bmosaicml Mar 22, 2024
c70c7f7
fix ctor
bmosaicml Mar 22, 2024
09017ff
gemini chat
bmosaicml Mar 25, 2024
c601eba
gemini
bmosaicml Mar 25, 2024
1f04738
restore tasks
bmosaicml Mar 25, 2024
47bb064
fault tolerance for content filter
bmosaicml Mar 25, 2024
e307dd7
fix
bmosaicml Mar 25, 2024
18efa86
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Mar 29, 2024
7faeb78
merge
bmosaicml Apr 1, 2024
343e115
fix builder
bmosaicml Apr 1, 2024
bf6231e
add deprecation warning
bmosaicml Apr 1, 2024
58859a3
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Apr 1, 2024
501d4cc
add deprecation warning
bmosaicml Apr 1, 2024
22f6759
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Apr 1, 2024
414467a
merge
bmosaicml Apr 1, 2024
b60066e
finish merge
bmosaicml Apr 1, 2024
65fbbed
merge
bmosaicml Apr 1, 2024
5696f09
add logging necessities to nlp.py
maxisawesome Apr 1, 2024
91a2b18
add attention_mask test update
maxisawesome Apr 1, 2024
79877ee
fix generation_length in tests
maxisawesome Apr 1, 2024
9c50795
Merge branch 'main' into migrate_subclasses_to_foundry
maxisawesome Apr 1, 2024
eac919a
fix bug
maxisawesome Apr 1, 2024
965c20c
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
maxisawesome Apr 1, 2024
6db05bd
rm conflicts
maxisawesome Apr 2, 2024
ce3e19c
rm extra arg
maxisawesome Apr 2, 2024
57e902a
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Apr 2, 2024
7c0996f
Merge branch 'main' into migrate_subclasses_to_foundry
maxisawesome Apr 2, 2024
599695c
Merge branch 'main' into migrate_subclasses_to_foundry
maxisawesome Apr 2, 2024
e10086f
restore yamls
bmosaicml Apr 3, 2024
0b5da4b
merge
bmosaicml Apr 3, 2024
c889fd2
fix?
bmosaicml Apr 3, 2024
1b2f6bc
fix yaml; testing
bmosaicml Apr 3, 2024
7afee25
fix api key
maxisawesome Apr 3, 2024
5720676
add formatting
bmosaicml Apr 3, 2024
c303b91
Merge branch 'openai_compatible_gauntlet' of github.com:mosaicml/llm-…
bmosaicml Apr 3, 2024
a60ef1d
Merge branch 'main' into migrate_subclasses_to_foundry
bmosaicml Apr 4, 2024
d78d783
Merge branch 'main' into migrate_subclasses_to_foundry
maxisawesome Apr 4, 2024
1ddf194
Merge branch 'main' into migrate_subclasses_to_foundry
maxisawesome Apr 9, 2024
78ac2d9
Merge branch 'main' into openai_compatible_gauntlet
bmosaicml Apr 10, 2024
34c967b
working
bmosaicml Apr 10, 2024
d5aebc8
fix typos
bmosaicml Apr 10, 2024
d7272b1
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
bmosaicml Apr 10, 2024
a5082b0
add deprecation warning for code
maxisawesome Apr 10, 2024
3c8ac56
Merge branch 'migrate_subclasses_to_foundry' of github.com:mosaicml/l…
maxisawesome Apr 10, 2024
642ad40
pyright wip
maxisawesome Apr 10, 2024
f30db14
Merge branch 'main' into migrate_subclasses_to_foundry
maxisawesome Apr 11, 2024
de321b2
fix pyright
bmosaicml Apr 11, 2024
019c58a
fix pyright error again
bmosaicml Apr 11, 2024
779f490
fix pyright
bmosaicml Apr 11, 2024
03f7e91
fix pyright
bmosaicml Apr 11, 2024
bb2728b
Merge branch 'openai_compatible_gauntlet' of github.com:mosaicml/llm-…
bmosaicml Apr 12, 2024
65f1a3e
Merge branch 'migrate_subclasses_to_foundry' into openai_compatible_g…
bmosaicml Apr 12, 2024
f493e35
Merge branch 'main' into openai_compatible_gauntlet
maxisawesome Apr 12, 2024
e62f584
Merge branch 'main' into openai_compatible_gauntlet
maxisawesome Apr 13, 2024
d23aa5f
add api key to test
maxisawesome Apr 19, 2024
558538b
Merge branch 'main' into openai_compatible_gauntlet
maxisawesome Apr 19, 2024
47a0cb9
put openai imports behind typechecking
maxisawesome Apr 19, 2024
21de07a
correct typecheckign import
maxisawesome Apr 19, 2024
7c1c32c
Merge branch 'openai_compatible_gauntlet' of github.com:mosaicml/llm-…
maxisawesome Apr 19, 2024
5dce21d
linting
maxisawesome Apr 19, 2024
619e2ce
fix gemni import errors
maxisawesome Apr 19, 2024
eec82a1
fix hf_eval
maxisawesome Apr 19, 2024
6fc714b
linting
maxisawesome Apr 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions llmfoundry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@
from llmfoundry.data import (ConcatTokensDataset, NoConcatDataset,
Seq2SeqFinetuningCollator,
build_finetuning_dataloader)
from llmfoundry.eval import (InContextLearningCodeEvalAccuracy,
InContextLearningCodeEvalDataset,
InContextLearningDataset,
InContextLearningGenerationExactMatchAccuracy,
InContextLearningGenerationTaskWithAnswersDataset,
InContextLearningLMAccuracy,
InContextLearningLMExpectedCalibrationError,
InContextLearningLMTaskDataset,
InContextLearningMCExpectedCalibrationError,
InContextLearningMetric,
InContextLearningMultipleChoiceAccuracy,
InContextLearningMultipleChoiceTaskDataset,
InContextLearningSchemaTaskDataset,
get_continuation_span, get_fewshot_sample_idxs,
get_icl_task_dataloader, make_padded_input,
strip_data, tokenizer_needs_prefix_space,
trim_context)
from llmfoundry.models.hf import ComposerHFCausalLM, ComposerHFT5
from llmfoundry.models.layers.attention import (
MultiheadAttention, attn_bias_shape, build_alibi_bias, build_attn_bias,
Expand Down Expand Up @@ -59,6 +76,26 @@
'algorithms',
'callbacks',
'TiktokenTokenizerWrapper',
'InContextLearningLMAccuracy',
'InContextLearningMultipleChoiceAccuracy',
'InContextLearningGenerationExactMatchAccuracy',
'InContextLearningMCExpectedCalibrationError',
'InContextLearningLMExpectedCalibrationError',
'InContextLearningMetric',
'InContextLearningCodeEvalAccuracy',
'InContextLearningDataset',
'InContextLearningGenerationTaskWithAnswersDataset',
'InContextLearningLMTaskDataset',
'InContextLearningCodeEvalDataset',
'InContextLearningMultipleChoiceTaskDataset',
'InContextLearningSchemaTaskDataset',
'get_icl_task_dataloader',
'strip_data',
'tokenizer_needs_prefix_space',
'trim_context',
'get_continuation_span',
'get_fewshot_sample_idxs',
'make_padded_input',
'registry',
]

Expand Down
39 changes: 39 additions & 0 deletions llmfoundry/eval/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
# Copyright 2024 MosaicML LLM Foundry authors
# SPDX-License-Identifier: Apache-2.0

"""Natively supported datasets."""

from llmfoundry.eval.datasets import (
InContextLearningCodeEvalDataset, InContextLearningDataset,
InContextLearningGenerationTaskWithAnswersDataset,
InContextLearningLMTaskDataset, InContextLearningMultipleChoiceTaskDataset,
InContextLearningSchemaTaskDataset, get_continuation_span,
get_fewshot_sample_idxs, get_icl_task_dataloader, make_padded_input,
strip_data, tokenizer_needs_prefix_space, trim_context)
from llmfoundry.eval.metrics import (
InContextLearningCodeEvalAccuracy,
InContextLearningGenerationExactMatchAccuracy, InContextLearningLMAccuracy,
InContextLearningLMExpectedCalibrationError,
InContextLearningMCExpectedCalibrationError, InContextLearningMetric,
InContextLearningMultipleChoiceAccuracy)

__all__ = [
'InContextLearningLMAccuracy',
'InContextLearningMultipleChoiceAccuracy',
'InContextLearningGenerationExactMatchAccuracy',
'InContextLearningMCExpectedCalibrationError',
'InContextLearningLMExpectedCalibrationError',
'InContextLearningMetric',
'InContextLearningCodeEvalAccuracy',
'InContextLearningDataset',
'InContextLearningGenerationTaskWithAnswersDataset',
'InContextLearningLMTaskDataset',
'InContextLearningCodeEvalDataset',
'InContextLearningMultipleChoiceTaskDataset',
'InContextLearningSchemaTaskDataset',
'get_icl_task_dataloader',
'strip_data',
'tokenizer_needs_prefix_space',
'trim_context',
'get_continuation_span',
'get_fewshot_sample_idxs',
'make_padded_input',
]
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

class InContextLearningDataset(Dataset):
r"""A base dataset that constructs batches for in-context learning task.

evaluations. The dataset format is expected to be a local jsonl file, a
cloud link to a jsonl file, or a Hugging Face dataset link. 'context' refers
to the input a model will recieve before generating an output. For example,
Expand Down
2 changes: 2 additions & 0 deletions llmfoundry/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from llmfoundry.models.hf import ComposerHFCausalLM, ComposerHFT5
from llmfoundry.models.inference_api_wrapper import (FMAPICasualLMEvalWrapper,
FMAPIChatAPIEvalWrapper,
GeminiChatAPIEvalrapper,
OpenAICausalLMEvalWrapper,
OpenAIChatAPIEvalWrapper)
from llmfoundry.models.mpt import (ComposerMPTCausalLM, MPTConfig,
Expand All @@ -17,6 +18,7 @@
models.register('fmapi_causal_lm', func=FMAPICasualLMEvalWrapper)
models.register('openai_chat', func=OpenAIChatAPIEvalWrapper)
models.register('fmapi_chat', func=FMAPIChatAPIEvalWrapper)
models.register('gemini_chat', func=GeminiChatAPIEvalrapper)

__all__ = [
'ComposerHFCausalLM',
Expand Down
3 changes: 3 additions & 0 deletions llmfoundry/models/inference_api_wrapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

from llmfoundry.models.inference_api_wrapper.fmapi import (
FMAPICasualLMEvalWrapper, FMAPIChatAPIEvalWrapper)
from llmfoundry.models.inference_api_wrapper.gemini_chat import \
GeminiChatAPIEvalrapper
from llmfoundry.models.inference_api_wrapper.interface import \
InferenceAPIEvalWrapper
from llmfoundry.models.inference_api_wrapper.openai_causal_lm import (
OpenAICausalLMEvalWrapper, OpenAIChatAPIEvalWrapper)

__all__ = [
'OpenAICausalLMEvalWrapper',
'GeminiChatAPIEvalrapper',
'OpenAIChatAPIEvalWrapper',
'InferenceAPIEvalWrapper',
'FMAPICasualLMEvalWrapper',
Expand Down
3 changes: 2 additions & 1 deletion llmfoundry/models/inference_api_wrapper/fmapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def block_until_ready(self, base_url: str):

def __init__(self, om_model_config: DictConfig, tokenizer: AutoTokenizer):
is_local = om_model_config.pop('local', False)
api_key = om_model_config.pop('api_key', None)
if is_local:
base_url = os.environ.get('MOSAICML_MODEL_ENDPOINT',
'http://0.0.0.0:8080/v2')
Expand All @@ -60,7 +61,7 @@ def __init__(self, om_model_config: DictConfig, tokenizer: AutoTokenizer):
'Must specify base_url or use local=True in model_cfg for FMAPIsEvalWrapper'
)

super().__init__(om_model_config, tokenizer)
super().__init__(om_model_config, tokenizer, api_key)


class FMAPICasualLMEvalWrapper(FMAPIEvalInterface, OpenAICausalLMEvalWrapper):
Expand Down
153 changes: 153 additions & 0 deletions llmfoundry/models/inference_api_wrapper/gemini_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Copyright 2022 MosaicML LLM Foundry authors
# SPDX-License-Identifier: Apache-2.0

import logging
import os
import random
from time import sleep
from typing import TYPE_CHECKING, Any, List, Optional, Union

from composer.core.types import Batch
from composer.utils.import_helpers import MissingConditionalImportError
from omegaconf import DictConfig
from transformers import AutoTokenizer

log = logging.getLogger(__name__)

from transformers import AutoTokenizer

from llmfoundry.models.inference_api_wrapper.interface import \
InferenceAPIEvalWrapper

if TYPE_CHECKING:
from openai.types.chat.chat_completion import ChatCompletion

MAX_RETRIES = 3

__all__ = [
'GeminiChatAPIEvalrapper',
]

log = logging.getLogger(__name__)


class GeminiChatAPIEvalrapper(InferenceAPIEvalWrapper):
"""Databricks Foundational Model API wrapper for causal LM models."""

def __init__(self, om_model_config: DictConfig,
tokenizer: AutoTokenizer) -> None:
api_key = om_model_config.pop('api_key', None)
if api_key is None:
api_key = os.environ.get('GEMINI_API_KEY')
try:
import google.generativeai as google_genai
except ImportError as e:
# TODO: should google-generativeai be grouped with openai in setup.py?
raise MissingConditionalImportError(
extra_deps_group='openai',
conda_package='google-generativeai',
conda_channel='conda-forge') from e
google_genai.configure(api_key=api_key)
super().__init__(om_model_config, tokenizer)
self.model_cfg = om_model_config
self.model = google_genai.GenerativeModel(
om_model_config.get('version', ''))
ignore = [
google_genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT,
google_genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
google_genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
google_genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
]
self.safety_settings = {
category: google_genai.types.HarmBlockThreshold.BLOCK_NONE
for category in ignore
}

def generate_completion(
self,
prompt: str, #
num_tokens: int,
generation_kwargs: Optional[dict] = None) -> 'ChatCompletion':
if generation_kwargs is None:
generation_kwargs = {}
if isinstance(prompt, str):
generation_config = google_genai.types.GenerationConfig(
candidate_count=1,
max_output_tokens=num_tokens,
temperature=generation_kwargs.get('temperature', 0))
response = self.model.generate_content(
prompt,
safety_settings=self.safety_settings,
generation_config=generation_config)
return response
else:
raise ValueError(f'Prompt must be str: {prompt}')

def completion_to_string(self, completion: 'ChatCompletion'):
try:
# sometimes gemini will block outputs due to content filters
return [completion.text]
except:
return ['']

def eval_forward(self, batch: Batch, outputs: Optional[Any] = None):
# Override the base class because Chat's API always strips spacing from model outputs resulting in different tokens
# than what the continuation would expect.
# Get around this issue by retokenizing the batch to remove spacing from the continuation as well as
# decoding the whole continuation at once.
padding_tok = self.tokenizer.pad_token_id if self.tokenizer.pad_token_id else self.tokenizer.eos_token_id
if batch.get('mode', '') == 'generate':
outputs = []
# generate-based implementation
for tokens, _ in zip(batch['input_ids'], batch['labels']):

tokens = tokens.tolist()
tokens = [t for t in tokens if t != padding_tok]
prompt = self.tokenizer.decode(tokens)

if 'generation_length' in batch:
num_tokens = batch['generation_length']
elif 'generation_kwargs' in batch:
num_tokens = batch['generation_kwargs'].get(
'max_new_tokens', 2)

for _ in range(
0,
batch.get('generation_kwargs',
{}).get('num_return_sequences', 1)):
api_output = self.try_generate_completion( #
prompt,
num_tokens=num_tokens,
generation_kwargs=batch.get('generation_kwargs', {}))

assert api_output is not None
sample_output = self.completion_to_string(
api_output)[ # pyright: ignore
0]
outputs.append(sample_output)
return outputs
else:
raise ValueError("Only 'generate' tasks are supported.")

def try_generate_completion(self,
prompt: Union[str, List],
num_tokens: int,
generation_kwargs: Optional[dict] = None):
if generation_kwargs is None:
generation_kwargs = {}

tries = 0
completion = None
delay = 1
while tries < MAX_RETRIES:
tries += 1
try:
completion = self.generate_completion(prompt, num_tokens,
generation_kwargs)
break
except Exception as e:
delay *= 2 * (1 + random.random())
sleep(delay)
continue

return completion
Loading
Loading