Update instruct_pipeline.py
Browse files- instruct_pipeline.py +96 -49
instruct_pipeline.py
CHANGED
|
@@ -1,9 +1,15 @@
|
|
| 1 |
import logging
|
| 2 |
import re
|
|
|
|
| 3 |
|
| 4 |
import numpy as np
|
| 5 |
from transformers import Pipeline, PreTrainedTokenizer
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
INSTRUCTION_KEY = "### Instruction:"
|
|
@@ -55,9 +61,22 @@ class InstructionTextGenerationPipeline(Pipeline):
|
|
| 55 |
def __init__(
|
| 56 |
self, *args, do_sample: bool = True, max_new_tokens: int = 256, top_p: float = 0.92, top_k: int = 0, **kwargs
|
| 57 |
):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
preprocess_params = {}
|
| 62 |
|
| 63 |
# newer versions of the tokenizer configure the response key as a special token. newer versions still may
|
|
@@ -81,10 +100,12 @@ class InstructionTextGenerationPipeline(Pipeline):
|
|
| 81 |
forward_params = generate_kwargs
|
| 82 |
postprocess_params = {
|
| 83 |
"response_key_token_id": response_key_token_id,
|
| 84 |
-
"end_key_token_id": end_key_token_id
|
| 85 |
-
"return_instruction_text": return_instruction_text,
|
| 86 |
}
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
return preprocess_params, forward_params, postprocess_params
|
| 89 |
|
| 90 |
def preprocess(self, instruction_text, **generate_kwargs):
|
|
@@ -100,66 +121,92 @@ class InstructionTextGenerationPipeline(Pipeline):
|
|
| 100 |
def _forward(self, model_inputs, **generate_kwargs):
|
| 101 |
input_ids = model_inputs["input_ids"]
|
| 102 |
attention_mask = model_inputs.get("attention_mask", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
generated_sequence = self.model.generate(
|
| 104 |
input_ids=input_ids.to(self.model.device),
|
| 105 |
attention_mask=attention_mask,
|
| 106 |
pad_token_id=self.tokenizer.pad_token_id,
|
| 107 |
**generate_kwargs,
|
| 108 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
instruction_text = model_inputs.pop("instruction_text")
|
| 110 |
return {"generated_sequence": generated_sequence, "input_ids": input_ids, "instruction_text": instruction_text}
|
| 111 |
|
| 112 |
-
def postprocess(self, model_outputs, response_key_token_id, end_key_token_id,
|
| 113 |
-
|
|
|
|
| 114 |
instruction_text = model_outputs["instruction_text"]
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
# sequence (or actually, the token ID it maps to, since it is a special token). We may not find
|
| 133 |
-
# this token, as the response could be truncated. If we don't find it then just return everything
|
| 134 |
-
# to the end. Note that even though we set eos_token_id, we still see the this token at the end.
|
| 135 |
-
end_pos = None
|
| 136 |
-
end_positions = np.where(sequence == end_key_token_id)[0]
|
| 137 |
-
if len(end_positions) > 0:
|
| 138 |
-
end_pos = end_positions[0]
|
| 139 |
-
|
| 140 |
-
decoded = self.tokenizer.decode(sequence[response_pos + 1 : end_pos]).strip()
|
| 141 |
-
else:
|
| 142 |
-
# Otherwise we'll decode everything and use a regex to find the response and end.
|
| 143 |
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
if m:
|
| 151 |
-
decoded = m.group(1).strip()
|
| 152 |
-
else:
|
| 153 |
-
# The model might not generate the "### End" sequence before reaching the max tokens. In this case,
|
| 154 |
-
# return everything after "### Response:".
|
| 155 |
-
m = re.search(r"#+\s*Response:\s*(.+)", fully_decoded, flags=re.DOTALL)
|
| 156 |
if m:
|
| 157 |
decoded = m.group(1).strip()
|
| 158 |
else:
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
return {"instruction_text": instruction_text, "generated_text": decoded}
|
| 163 |
|
| 164 |
-
|
| 165 |
|
|
|
|
|
|
| 1 |
import logging
|
| 2 |
import re
|
| 3 |
+
from typing import List
|
| 4 |
|
| 5 |
import numpy as np
|
| 6 |
from transformers import Pipeline, PreTrainedTokenizer
|
| 7 |
|
| 8 |
+
from transformers.utils import is_tf_available
|
| 9 |
+
|
| 10 |
+
if is_tf_available():
|
| 11 |
+
import tensorflow as tf
|
| 12 |
+
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
INSTRUCTION_KEY = "### Instruction:"
|
|
|
|
| 61 |
def __init__(
|
| 62 |
self, *args, do_sample: bool = True, max_new_tokens: int = 256, top_p: float = 0.92, top_k: int = 0, **kwargs
|
| 63 |
):
|
| 64 |
+
"""Initialize the pipeline
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
do_sample (bool, optional): Whether or not to use sampling. Defaults to True.
|
| 68 |
+
max_new_tokens (int, optional): Max new tokens after the prompt to generate. Defaults to 128.
|
| 69 |
+
top_p (float, optional): If set to float < 1, only the smallest set of most probable tokens with
|
| 70 |
+
probabilities that add up to top_p or higher are kept for generation. Defaults to 0.92.
|
| 71 |
+
top_k (int, optional): The number of highest probability vocabulary tokens to keep for top-k-filtering.
|
| 72 |
+
Defaults to 0.
|
| 73 |
+
"""
|
| 74 |
+
super().__init__(*args, do_sample=do_sample, max_new_tokens=max_new_tokens, top_p=top_p, top_k=top_k,
|
| 75 |
+
**kwargs)
|
| 76 |
+
|
| 77 |
+
def _sanitize_parameters(self,
|
| 78 |
+
return_full_text: bool = None,
|
| 79 |
+
**generate_kwargs):
|
| 80 |
preprocess_params = {}
|
| 81 |
|
| 82 |
# newer versions of the tokenizer configure the response key as a special token. newer versions still may
|
|
|
|
| 100 |
forward_params = generate_kwargs
|
| 101 |
postprocess_params = {
|
| 102 |
"response_key_token_id": response_key_token_id,
|
| 103 |
+
"end_key_token_id": end_key_token_id
|
|
|
|
| 104 |
}
|
| 105 |
|
| 106 |
+
if return_full_text is not None:
|
| 107 |
+
postprocess_params["return_full_text"] = return_full_text
|
| 108 |
+
|
| 109 |
return preprocess_params, forward_params, postprocess_params
|
| 110 |
|
| 111 |
def preprocess(self, instruction_text, **generate_kwargs):
|
|
|
|
| 121 |
def _forward(self, model_inputs, **generate_kwargs):
|
| 122 |
input_ids = model_inputs["input_ids"]
|
| 123 |
attention_mask = model_inputs.get("attention_mask", None)
|
| 124 |
+
|
| 125 |
+
if input_ids.shape[1] == 0:
|
| 126 |
+
input_ids = None
|
| 127 |
+
attention_mask = None
|
| 128 |
+
in_b = 1
|
| 129 |
+
else:
|
| 130 |
+
in_b = input_ids.shape[0]
|
| 131 |
+
|
| 132 |
generated_sequence = self.model.generate(
|
| 133 |
input_ids=input_ids.to(self.model.device),
|
| 134 |
attention_mask=attention_mask,
|
| 135 |
pad_token_id=self.tokenizer.pad_token_id,
|
| 136 |
**generate_kwargs,
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
out_b = generated_sequence.shape[0]
|
| 140 |
+
if self.framework == "pt":
|
| 141 |
+
generated_sequence = generated_sequence.reshape(in_b, out_b // in_b, *generated_sequence.shape[1:])
|
| 142 |
+
elif self.framework == "tf":
|
| 143 |
+
generated_sequence = tf.reshape(generated_sequence, (in_b, out_b // in_b, *generated_sequence.shape[1:]))
|
| 144 |
+
|
| 145 |
instruction_text = model_inputs.pop("instruction_text")
|
| 146 |
return {"generated_sequence": generated_sequence, "input_ids": input_ids, "instruction_text": instruction_text}
|
| 147 |
|
| 148 |
+
def postprocess(self, model_outputs, response_key_token_id, end_key_token_id, return_full_text: bool = False):
|
| 149 |
+
|
| 150 |
+
generated_sequence = model_outputs["generated_sequence"][0]
|
| 151 |
instruction_text = model_outputs["instruction_text"]
|
| 152 |
|
| 153 |
+
generated_sequence: List[List[int]] = generated_sequence.numpy().tolist()
|
| 154 |
+
records = []
|
| 155 |
+
for sequence in generated_sequence:
|
| 156 |
+
|
| 157 |
+
# The response will be set to this variable if we can identify it.
|
| 158 |
+
decoded = None
|
| 159 |
+
|
| 160 |
+
# If we have token IDs for the response and end, then we can find the tokens and only decode between them.
|
| 161 |
+
if response_key_token_id and end_key_token_id:
|
| 162 |
+
# Find where "### Response:" is first found in the generated tokens. Considering this is part of the
|
| 163 |
+
# prompt, we should definitely find it. We will return the tokens found after this token.
|
| 164 |
+
try:
|
| 165 |
+
response_pos = sequence.index(response_key_token_id)
|
| 166 |
+
except ValueError:
|
| 167 |
+
logger.warn(f"Could not find response key {response_key_token_id} in: {sequence}")
|
| 168 |
+
response_pos = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
if response_pos:
|
| 171 |
+
# Next find where "### End" is located. The model has been trained to end its responses with this
|
| 172 |
+
# sequence (or actually, the token ID it maps to, since it is a special token). We may not find
|
| 173 |
+
# this token, as the response could be truncated. If we don't find it then just return everything
|
| 174 |
+
# to the end. Note that even though we set eos_token_id, we still see the this token at the end.
|
| 175 |
+
try:
|
| 176 |
+
end_pos = sequence.index(end_key_token_id)
|
| 177 |
+
except ValueError:
|
| 178 |
+
end_pos = None
|
| 179 |
|
| 180 |
+
decoded = self.tokenizer.decode(sequence[response_pos + 1 : end_pos]).strip()
|
| 181 |
+
|
| 182 |
+
if not decoded:
|
| 183 |
+
# Otherwise we'll decode everything and use a regex to find the response and end.
|
| 184 |
+
|
| 185 |
+
fully_decoded = self.tokenizer.decode(sequence)
|
| 186 |
+
|
| 187 |
+
# The response appears after "### Response:". The model has been trained to append "### End" at the
|
| 188 |
+
# end.
|
| 189 |
+
m = re.search(r"#+\s*Response:\s*(.+?)#+\s*End", fully_decoded, flags=re.DOTALL)
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
if m:
|
| 192 |
decoded = m.group(1).strip()
|
| 193 |
else:
|
| 194 |
+
# The model might not generate the "### End" sequence before reaching the max tokens. In this case,
|
| 195 |
+
# return everything after "### Response:".
|
| 196 |
+
m = re.search(r"#+\s*Response:\s*(.+)", fully_decoded, flags=re.DOTALL)
|
| 197 |
+
if m:
|
| 198 |
+
decoded = m.group(1).strip()
|
| 199 |
+
else:
|
| 200 |
+
logger.warn(f"Failed to find response in:\n{fully_decoded}")
|
| 201 |
+
|
| 202 |
+
# If the full text is requested, then append the decoded text to the original instruction.
|
| 203 |
+
# This technically isn't the full text, as we format the instruction in the prompt the model has been
|
| 204 |
+
# trained on, but to the client it will appear to be the full text.
|
| 205 |
+
if return_full_text:
|
| 206 |
+
decoded = f"{instruction_text}\n{decoded}"
|
| 207 |
|
| 208 |
+
rec = {"generated_text": decoded}
|
|
|
|
| 209 |
|
| 210 |
+
records.append(rec)
|
| 211 |
|
| 212 |
+
return records
|