-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xusenlin
committed
Aug 15, 2023
1 parent
625b876
commit 141a5bd
Showing
5 changed files
with
168 additions
and
112 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
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,80 @@ | ||
from typing import List | ||
from typing import Tuple | ||
|
||
from transformers.generation.logits_process import ( | ||
LogitsProcessorList, | ||
RepetitionPenaltyLogitsProcessor, | ||
TemperatureLogitsWarper, | ||
TopKLogitsWarper, | ||
TopPLogitsWarper, | ||
) | ||
|
||
from api.utils.protocol import ChatMessage, Role | ||
|
||
|
||
def parse_messages(messages: List[ChatMessage], split_role=Role.USER) -> Tuple[str, List[List[ChatMessage]]]: | ||
system, rounds = "", [] | ||
round = [] | ||
for i, message in enumerate(messages): | ||
if message.role == Role.SYSTEM: | ||
system = message.content | ||
continue | ||
if message.role == split_role and round: | ||
rounds.append(round) | ||
round = [] | ||
round.append(message) | ||
if round: | ||
rounds.append(round) | ||
return system, rounds | ||
|
||
|
||
def prepare_logits_processor( | ||
temperature: float, repetition_penalty: float, top_p: float, top_k: int | ||
) -> LogitsProcessorList: | ||
processor_list = LogitsProcessorList() | ||
# TemperatureLogitsWarper doesn't accept 0.0, 1.0 makes it a no-op, so we skip two cases. | ||
if temperature >= 1e-5 and temperature != 1.0: | ||
processor_list.append(TemperatureLogitsWarper(temperature)) | ||
if repetition_penalty > 1.0: | ||
processor_list.append(RepetitionPenaltyLogitsProcessor(repetition_penalty)) | ||
if 1e-8 <= top_p < 1.0: | ||
processor_list.append(TopPLogitsWarper(top_p)) | ||
if top_k > 0: | ||
processor_list.append(TopKLogitsWarper(top_k)) | ||
return processor_list | ||
|
||
|
||
def is_partial_stop(output: str, stop_str: str): | ||
"""Check whether the output contains a partial stop str.""" | ||
for i in range(0, min(len(output), len(stop_str))): | ||
if stop_str.startswith(output[-i:]): | ||
return True | ||
return False | ||
|
||
|
||
# Models don't use the same configuration key for determining the maximum | ||
# sequence length. Store them here so we can sanely check them. | ||
# NOTE: The ordering here is important. Some models have two of these and we | ||
# have a preference for which value gets used. | ||
SEQUENCE_LENGTH_KEYS = [ | ||
"max_sequence_length", | ||
"seq_length", | ||
"max_position_embeddings", | ||
"max_seq_len", | ||
"model_max_length", | ||
] | ||
|
||
|
||
def get_context_length(config): | ||
"""Get the context length of a model from a huggingface model config.""" | ||
rope_scaling = getattr(config, "rope_scaling", None) | ||
if rope_scaling: | ||
rope_scaling_factor = config.rope_scaling["factor"] | ||
else: | ||
rope_scaling_factor = 1 | ||
|
||
for key in SEQUENCE_LENGTH_KEYS: | ||
val = getattr(config, key, None) | ||
if val is not None: | ||
return int(rope_scaling_factor * val) | ||
return 2048 |
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