uploading model
Browse filesSigned-off-by: Jacob Renn <jacob.renn@squared.ai>
- added_tokens.json +5 -0
- config.json +54 -0
- config.json~ +47 -0
- instruct_pipeline.py +160 -0
- merges.txt +0 -0
- pytorch_model.bin +3 -0
- special_tokens_map.json +11 -0
- tokenizer_config.json +34 -0
- training_args.bin +3 -0
- vocab.json +0 -0
added_tokens.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"### End": 50257,
|
| 3 |
+
"### Instruction:": 50258,
|
| 4 |
+
"### Response:\n": 50259
|
| 5 |
+
}
|
config.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "/dbfs/FileStore/dais-model-large",
|
| 3 |
+
"activation_function": "gelu_new",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"GPT2LMHeadModel"
|
| 6 |
+
],
|
| 7 |
+
"custom_pipelines": {
|
| 8 |
+
"text-generation": {
|
| 9 |
+
"impl": "instruct_pipeline.InstructionTextGenerationPipeline",
|
| 10 |
+
"pt": "AutoModelForCausalLM",
|
| 11 |
+
"tf": "TFAutoModelForCausalLM"
|
| 12 |
+
}
|
| 13 |
+
},
|
| 14 |
+
"attn_pdrop": 0.1,
|
| 15 |
+
"bos_token_id": 50256,
|
| 16 |
+
"custom_pipelines": {
|
| 17 |
+
"text-generation": {
|
| 18 |
+
"impl": "instruct_pipeline.InstructionTextGenerationPipeline",
|
| 19 |
+
"pt": "AutoModelForCausalLM",
|
| 20 |
+
"tf": "TFAutoModelForCausalLM"
|
| 21 |
+
}
|
| 22 |
+
},
|
| 23 |
+
"embd_pdrop": 0.1,
|
| 24 |
+
"eos_token_id": 50256,
|
| 25 |
+
"initializer_range": 0.02,
|
| 26 |
+
"layer_norm_epsilon": 1e-05,
|
| 27 |
+
"model_type": "gpt2",
|
| 28 |
+
"n_ctx": 1024,
|
| 29 |
+
"n_embd": 1600,
|
| 30 |
+
"n_head": 25,
|
| 31 |
+
"n_inner": null,
|
| 32 |
+
"n_layer": 48,
|
| 33 |
+
"n_positions": 1024,
|
| 34 |
+
"output_past": true,
|
| 35 |
+
"reorder_and_upcast_attn": false,
|
| 36 |
+
"resid_pdrop": 0.1,
|
| 37 |
+
"scale_attn_by_inverse_layer_idx": false,
|
| 38 |
+
"scale_attn_weights": true,
|
| 39 |
+
"summary_activation": null,
|
| 40 |
+
"summary_first_dropout": 0.1,
|
| 41 |
+
"summary_proj_to_labels": true,
|
| 42 |
+
"summary_type": "cls_index",
|
| 43 |
+
"summary_use_proj": true,
|
| 44 |
+
"task_specific_params": {
|
| 45 |
+
"text-generation": {
|
| 46 |
+
"do_sample": true,
|
| 47 |
+
"max_length": 50
|
| 48 |
+
}
|
| 49 |
+
},
|
| 50 |
+
"torch_dtype": "float16",
|
| 51 |
+
"transformers_version": "4.25.1",
|
| 52 |
+
"use_cache": false,
|
| 53 |
+
"vocab_size": 50260
|
| 54 |
+
}
|
config.json~
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "/dbfs/FileStore/dais-model-large",
|
| 3 |
+
"activation_function": "gelu_new",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"GPT2LMHeadModel"
|
| 6 |
+
],
|
| 7 |
+
"attn_pdrop": 0.1,
|
| 8 |
+
"bos_token_id": 50256,
|
| 9 |
+
"custom_pipelines": {
|
| 10 |
+
"text-generation": {
|
| 11 |
+
"impl": "instruct_pipeline.InstructionTextGenerationPipeline",
|
| 12 |
+
"pt": "AutoModelForCausalLM",
|
| 13 |
+
"tf": "TFAutoModelForCausalLM"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"embd_pdrop": 0.1,
|
| 17 |
+
"eos_token_id": 50256,
|
| 18 |
+
"initializer_range": 0.02,
|
| 19 |
+
"layer_norm_epsilon": 1e-05,
|
| 20 |
+
"model_type": "gpt2",
|
| 21 |
+
"n_ctx": 1024,
|
| 22 |
+
"n_embd": 1600,
|
| 23 |
+
"n_head": 25,
|
| 24 |
+
"n_inner": null,
|
| 25 |
+
"n_layer": 48,
|
| 26 |
+
"n_positions": 1024,
|
| 27 |
+
"output_past": true,
|
| 28 |
+
"reorder_and_upcast_attn": false,
|
| 29 |
+
"resid_pdrop": 0.1,
|
| 30 |
+
"scale_attn_by_inverse_layer_idx": false,
|
| 31 |
+
"scale_attn_weights": true,
|
| 32 |
+
"summary_activation": null,
|
| 33 |
+
"summary_first_dropout": 0.1,
|
| 34 |
+
"summary_proj_to_labels": true,
|
| 35 |
+
"summary_type": "cls_index",
|
| 36 |
+
"summary_use_proj": true,
|
| 37 |
+
"task_specific_params": {
|
| 38 |
+
"text-generation": {
|
| 39 |
+
"do_sample": true,
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from transformers import Pipeline, PreTrainedTokenizer
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
INSTRUCTION_KEY = "### Instruction:"
|
| 8 |
+
RESPONSE_KEY = "### Response:"
|
| 9 |
+
END_KEY = "### End"
|
| 10 |
+
INTRO_BLURB = (
|
| 11 |
+
"Below is an instruction that describes a task, along with any additional context. Write a response that appropriately completes the request."
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# This is the prompt that is used for generating responses using an already trained model. It ends with the response
|
| 15 |
+
# key, where the job of the model is to provide the completion that follows it (i.e. the response itself).
|
| 16 |
+
PROMPT_FOR_GENERATION_FORMAT = """{intro}
|
| 17 |
+
|
| 18 |
+
{instruction_key}
|
| 19 |
+
{instruction}
|
| 20 |
+
|
| 21 |
+
{response_key}
|
| 22 |
+
""".format(
|
| 23 |
+
intro=INTRO_BLURB,
|
| 24 |
+
instruction_key=INSTRUCTION_KEY,
|
| 25 |
+
instruction="{instruction}",
|
| 26 |
+
response_key=RESPONSE_KEY,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_special_token_id(tokenizer: PreTrainedTokenizer, key: str) -> int:
|
| 31 |
+
"""Gets the token ID for a given string that has been added to the tokenizer as a special token.
|
| 32 |
+
|
| 33 |
+
When training, we configure the tokenizer so that the sequences like "### Instruction:" and "### End" are
|
| 34 |
+
treated specially and converted to a single, new token. This retrieves the token ID each of these keys map to.
|
| 35 |
+
|
| 36 |
+
Args:
|
| 37 |
+
tokenizer (PreTrainedTokenizer): the tokenizer
|
| 38 |
+
key (str): the key to convert to a single token
|
| 39 |
+
|
| 40 |
+
Raises:
|
| 41 |
+
RuntimeError: if more than one ID was generated
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
int: the token ID for the given key
|
| 45 |
+
"""
|
| 46 |
+
token_ids = tokenizer.encode(key)
|
| 47 |
+
if len(token_ids) > 1:
|
| 48 |
+
raise ValueError(f"Expected only a single token for '{key}' but found {token_ids}")
|
| 49 |
+
return token_ids[0]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class InstructionTextGenerationPipeline(Pipeline):
|
| 53 |
+
def __init__(
|
| 54 |
+
self, *args, do_sample: bool = True, max_new_tokens: int = 256, top_p: float = 0.92, top_k: int = 0, **kwargs
|
| 55 |
+
):
|
| 56 |
+
super().__init__(*args, do_sample=do_sample, max_new_tokens=max_new_tokens, top_p=top_p, top_k=top_k, **kwargs)
|
| 57 |
+
|
| 58 |
+
def _sanitize_parameters(self, return_instruction_text=False, **generate_kwargs):
|
| 59 |
+
preprocess_params = {}
|
| 60 |
+
|
| 61 |
+
# newer versions of the tokenizer configure the response key as a special token. newer versions still may
|
| 62 |
+
# append a newline to yield a single token. find whatever token is configured for the response key.
|
| 63 |
+
tokenizer_response_key = next(
|
| 64 |
+
(token for token in self.tokenizer.additional_special_tokens if token.startswith(RESPONSE_KEY)), None
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
response_key_token_id = None
|
| 68 |
+
end_key_token_id = None
|
| 69 |
+
if tokenizer_response_key:
|
| 70 |
+
try:
|
| 71 |
+
response_key_token_id = get_special_token_id(self.tokenizer, tokenizer_response_key)
|
| 72 |
+
end_key_token_id = get_special_token_id(self.tokenizer, END_KEY)
|
| 73 |
+
|
| 74 |
+
# Ensure generation stops once it generates "### End"
|
| 75 |
+
generate_kwargs["eos_token_id"] = end_key_token_id
|
| 76 |
+
except ValueError:
|
| 77 |
+
pass
|
| 78 |
+
|
| 79 |
+
forward_params = generate_kwargs
|
| 80 |
+
postprocess_params = {
|
| 81 |
+
"response_key_token_id": response_key_token_id,
|
| 82 |
+
"end_key_token_id": end_key_token_id,
|
| 83 |
+
"return_instruction_text": return_instruction_text,
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
return preprocess_params, forward_params, postprocess_params
|
| 87 |
+
|
| 88 |
+
def preprocess(self, instruction_text, **generate_kwargs):
|
| 89 |
+
prompt_text = PROMPT_FOR_GENERATION_FORMAT.format(instruction=instruction_text)
|
| 90 |
+
inputs = self.tokenizer(
|
| 91 |
+
prompt_text,
|
| 92 |
+
return_tensors="pt",
|
| 93 |
+
)
|
| 94 |
+
inputs["prompt_text"] = prompt_text
|
| 95 |
+
inputs["instruction_text"] = instruction_text
|
| 96 |
+
return inputs
|
| 97 |
+
|
| 98 |
+
def _forward(self, model_inputs, **generate_kwargs):
|
| 99 |
+
input_ids = model_inputs["input_ids"]
|
| 100 |
+
attention_mask = model_inputs.get("attention_mask", None)
|
| 101 |
+
generated_sequence = self.model.generate(
|
| 102 |
+
input_ids=input_ids.to(self.model.device),
|
| 103 |
+
attention_mask=attention_mask,
|
| 104 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
| 105 |
+
**generate_kwargs,
|
| 106 |
+
)[0].cpu()
|
| 107 |
+
instruction_text = model_inputs.pop("instruction_text")
|
| 108 |
+
return {"generated_sequence": generated_sequence, "input_ids": input_ids, "instruction_text": instruction_text}
|
| 109 |
+
|
| 110 |
+
def postprocess(self, model_outputs, response_key_token_id, end_key_token_id, return_instruction_text):
|
| 111 |
+
sequence = model_outputs["generated_sequence"]
|
| 112 |
+
instruction_text = model_outputs["instruction_text"]
|
| 113 |
+
|
| 114 |
+
# The response will be set to this variable if we can identify it.
|
| 115 |
+
decoded = None
|
| 116 |
+
|
| 117 |
+
# If we have token IDs for the response and end, then we can find the tokens and only decode between them.
|
| 118 |
+
if response_key_token_id and end_key_token_id:
|
| 119 |
+
# Find where "### Response:" is first found in the generated tokens. Considering this is part of the
|
| 120 |
+
# prompt, we should definitely find it. We will return the tokens found after this token.
|
| 121 |
+
response_pos = None
|
| 122 |
+
response_positions = np.where(sequence == response_key_token_id)[0]
|
| 123 |
+
if len(response_positions) == 0:
|
| 124 |
+
pass
|
| 125 |
+
else:
|
| 126 |
+
response_pos = response_positions[0]
|
| 127 |
+
|
| 128 |
+
if response_pos:
|
| 129 |
+
# Next find where "### End" is located. The model has been trained to end its responses with this
|
| 130 |
+
# sequence (or actually, the token ID it maps to, since it is a special token). We may not find
|
| 131 |
+
# this token, as the response could be truncated. If we don't find it then just return everything
|
| 132 |
+
# to the end. Note that even though we set eos_token_id, we still see the this token at the end.
|
| 133 |
+
end_pos = None
|
| 134 |
+
end_positions = np.where(sequence == end_key_token_id)[0]
|
| 135 |
+
if len(end_positions) > 0:
|
| 136 |
+
end_pos = end_positions[0]
|
| 137 |
+
|
| 138 |
+
decoded = self.tokenizer.decode(sequence[response_pos + 1 : end_pos]).strip()
|
| 139 |
+
else:
|
| 140 |
+
# Otherwise we'll decode everything and use a regex to find the response and end.
|
| 141 |
+
|
| 142 |
+
fully_decoded = self.tokenizer.decode(sequence)
|
| 143 |
+
|
| 144 |
+
# The response appears after "### Response:". The model has been trained to append "### End" at the
|
| 145 |
+
# end.
|
| 146 |
+
m = re.search(r"#+\s*Response:\s*(.+?)#+\s*End", fully_decoded, flags=re.DOTALL)
|
| 147 |
+
|
| 148 |
+
if m:
|
| 149 |
+
decoded = m.group(1).strip()
|
| 150 |
+
else:
|
| 151 |
+
# The model might not generate the "### End" sequence before reaching the max tokens. In this case,
|
| 152 |
+
# return everything after "### Response:".
|
| 153 |
+
m = re.search(r"#+\s*Response:\s*(.+)", fully_decoded, flags=re.DOTALL)
|
| 154 |
+
if m:
|
| 155 |
+
decoded = m.group(1).strip()
|
| 156 |
+
|
| 157 |
+
if return_instruction_text:
|
| 158 |
+
return {"instruction_text": instruction_text, "generated_text": decoded}
|
| 159 |
+
|
| 160 |
+
return decoded
|
merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:30a271cb3f0d38bbc53d2e86f933f3af9502818308bc6846707cdf7070966db4
|
| 3 |
+
size 3165775133
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"additional_special_tokens": [
|
| 3 |
+
"### End",
|
| 4 |
+
"### Instruction:",
|
| 5 |
+
"### Response:\n"
|
| 6 |
+
],
|
| 7 |
+
"bos_token": "<|endoftext|>",
|
| 8 |
+
"eos_token": "<|endoftext|>",
|
| 9 |
+
"pad_token": "<|endoftext|>",
|
| 10 |
+
"unk_token": "<|endoftext|>"
|
| 11 |
+
}
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_bos_token": false,
|
| 3 |
+
"add_prefix_space": false,
|
| 4 |
+
"bos_token": {
|
| 5 |
+
"__type": "AddedToken",
|
| 6 |
+
"content": "<|endoftext|>",
|
| 7 |
+
"lstrip": false,
|
| 8 |
+
"normalized": true,
|
| 9 |
+
"rstrip": false,
|
| 10 |
+
"single_word": false
|
| 11 |
+
},
|
| 12 |
+
"eos_token": {
|
| 13 |
+
"__type": "AddedToken",
|
| 14 |
+
"content": "<|endoftext|>",
|
| 15 |
+
"lstrip": false,
|
| 16 |
+
"normalized": true,
|
| 17 |
+
"rstrip": false,
|
| 18 |
+
"single_word": false
|
| 19 |
+
},
|
| 20 |
+
"errors": "replace",
|
| 21 |
+
"model_max_length": 1024,
|
| 22 |
+
"name_or_path": "/dbfs/FileStore/dais-model-large",
|
| 23 |
+
"pad_token": null,
|
| 24 |
+
"special_tokens_map_file": null,
|
| 25 |
+
"tokenizer_class": "GPT2Tokenizer",
|
| 26 |
+
"unk_token": {
|
| 27 |
+
"__type": "AddedToken",
|
| 28 |
+
"content": "<|endoftext|>",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": true,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false
|
| 33 |
+
}
|
| 34 |
+
}
|
training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b0b3f10c8b1c6eb7e6085ee8166b3eedc44b8b9255e92da9797f51d5648462c5
|
| 3 |
+
size 4603
|
vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|