|
| 1 | +# Copyright 2023 Baichuan Inc. All Rights Reserved. |
| 2 | + |
| 3 | +# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. |
| 4 | +# |
| 5 | +# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX |
| 6 | +# and OPT implementations in this library. It has been modified from its |
| 7 | +# original forms to accommodate minor architectural differences compared |
| 8 | +# to GPT-NeoX and OPT used by the Meta AI team that trained the model. |
| 9 | +# |
| 10 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | +# you may not use this file except in compliance with the License. |
| 12 | +# You may obtain a copy of the License at |
| 13 | +# |
| 14 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +# |
| 16 | +# Unless required by applicable law or agreed to in writing, software |
| 17 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +# See the License for the specific language governing permissions and |
| 20 | +# limitations under the License. |
| 21 | + |
| 22 | +""" |
| 23 | +Adapted from the following sources: |
| 24 | +https://huggingface.co/baichuan-inc/Baichuan2-7B-Chat/blob/main/generation_utils.py |
| 25 | +https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat/blob/main/generation_utils.py |
| 26 | +""" |
| 27 | + |
| 28 | +from queue import Queue |
| 29 | +from typing import List |
| 30 | + |
| 31 | +import torch |
| 32 | + |
| 33 | + |
| 34 | +def build_chat_input(model, tokenizer, messages: List[dict], max_new_tokens: int = 0): |
| 35 | + def _parse_messages(messages, split_role="user"): |
| 36 | + system, rounds = "", [] |
| 37 | + round = [] |
| 38 | + for i, message in enumerate(messages): |
| 39 | + if message["role"] == "system": |
| 40 | + assert i == 0 |
| 41 | + system = message["content"] |
| 42 | + continue |
| 43 | + if message["role"] == split_role and round: |
| 44 | + rounds.append(round) |
| 45 | + round = [] |
| 46 | + round.append(message) |
| 47 | + if round: |
| 48 | + rounds.append(round) |
| 49 | + return system, rounds |
| 50 | + |
| 51 | + max_new_tokens = max_new_tokens or model.generation_config.max_new_tokens |
| 52 | + max_input_tokens = model.config.model_max_length - max_new_tokens |
| 53 | + system, rounds = _parse_messages(messages, split_role="user") |
| 54 | + system_tokens = tokenizer.encode(system) |
| 55 | + max_history_tokens = max_input_tokens - len(system_tokens) |
| 56 | + |
| 57 | + history_tokens = [] |
| 58 | + for round in rounds[::-1]: |
| 59 | + round_tokens = [] |
| 60 | + for message in round: |
| 61 | + if message["role"] == "user": |
| 62 | + round_tokens.append(model.generation_config.user_token_id) |
| 63 | + else: |
| 64 | + round_tokens.append(model.generation_config.assistant_token_id) |
| 65 | + round_tokens.extend(tokenizer.encode(message["content"])) |
| 66 | + if len(history_tokens) == 0 or len(history_tokens) + len(round_tokens) <= max_history_tokens: |
| 67 | + history_tokens = round_tokens + history_tokens # concat left |
| 68 | + if len(history_tokens) < max_history_tokens: |
| 69 | + continue |
| 70 | + break |
| 71 | + |
| 72 | + input_tokens = system_tokens + history_tokens |
| 73 | + if messages[-1]["role"] != "assistant": |
| 74 | + input_tokens.append(model.generation_config.assistant_token_id) |
| 75 | + input_tokens = input_tokens[-max_input_tokens:] # truncate left |
| 76 | + return torch.LongTensor([input_tokens]).to(model.device) |
| 77 | + |
| 78 | + |
| 79 | +class TextIterStreamer: |
| 80 | + def __init__(self, tokenizer, skip_prompt=False, skip_special_tokens=False): |
| 81 | + self.tokenizer = tokenizer |
| 82 | + self.skip_prompt = skip_prompt |
| 83 | + self.skip_special_tokens = skip_special_tokens |
| 84 | + self.tokens = [] |
| 85 | + self.text_queue = Queue() |
| 86 | + self.next_tokens_are_prompt = True |
| 87 | + |
| 88 | + def put(self, value): |
| 89 | + if self.skip_prompt and self.next_tokens_are_prompt: |
| 90 | + self.next_tokens_are_prompt = False |
| 91 | + else: |
| 92 | + if len(value.shape) > 1: |
| 93 | + value = value[0] |
| 94 | + self.tokens.extend(value.tolist()) |
| 95 | + self.text_queue.put(self.tokenizer.decode(self.tokens, skip_special_tokens=self.skip_special_tokens)) |
| 96 | + |
| 97 | + def end(self): |
| 98 | + self.text_queue.put(None) |
| 99 | + |
| 100 | + def __iter__(self): |
| 101 | + return self |
| 102 | + |
| 103 | + def __next__(self): |
| 104 | + value = self.text_queue.get() |
| 105 | + if value is None: |
| 106 | + raise StopIteration() |
| 107 | + else: |
| 108 | + return value |
0 commit comments