初始化项目,由ModelHub XC社区提供模型
Model: verseAI/databricks-dolly-v2-3b Source: Original Platform
This commit is contained in:
34
.gitattributes
vendored
Normal file
34
.gitattributes
vendored
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
*.7z filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.arrow filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ftz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.gz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.h5 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.joblib filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.model filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npy filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.onnx filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ot filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.parquet filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pb filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pickle filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pkl filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pth filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.rar filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
||||||
|
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tflite filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tgz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.wasm filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.xz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zip filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zst filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
||||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.venv
|
||||||
181
README.md
Normal file
181
README.md
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
---
|
||||||
|
license: mit
|
||||||
|
language:
|
||||||
|
- en
|
||||||
|
library_name: transformers
|
||||||
|
inference: false
|
||||||
|
datasets:
|
||||||
|
- databricks/databricks-dolly-15k
|
||||||
|
duplicated_from: databricks/dolly-v2-3b
|
||||||
|
---
|
||||||
|
# dolly-v2-3b Model Card
|
||||||
|
|
||||||
|
# Fork of [databricks/dolly-v2-3b](https://huggingface.co/databricks/dolly-v2-3b)
|
||||||
|
|
||||||
|
Original README copied below:
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
Databricks’ `dolly-v2-3b`, an instruction-following large language model trained on the Databricks machine learning platform
|
||||||
|
that is licensed for commercial use. Based on `pythia-2.8b`, Dolly is trained on ~15k instruction/response fine tuning records
|
||||||
|
[`databricks-dolly-15k`](https://github.com/databrickslabs/dolly/tree/master/data) generated
|
||||||
|
by Databricks employees in capability domains from the InstructGPT paper, including brainstorming, classification, closed QA, generation,
|
||||||
|
information extraction, open QA and summarization. `dolly-v2-3b` is not a state-of-the-art model, but does exhibit surprisingly
|
||||||
|
high quality instruction following behavior not characteristic of the foundation model on which it is based.
|
||||||
|
|
||||||
|
Dolly v2 is also available in these larger models sizes:
|
||||||
|
|
||||||
|
* [dolly-v2-12b](https://huggingface.co/databricks/dolly-v2-12b), a 12 billion parameter based on `pythia-12b`
|
||||||
|
* [dolly-v2-7b](https://huggingface.co/databricks/dolly-v2-7b), a 6.9 billion parameter based on `pythia-6.9b`
|
||||||
|
|
||||||
|
Please refer to the [dolly GitHub repo](https://github.com/databrickslabs/dolly#getting-started-with-response-generation) for tips on
|
||||||
|
running inference for various GPU configurations.
|
||||||
|
|
||||||
|
**Owner**: Databricks, Inc.
|
||||||
|
|
||||||
|
## Model Overview
|
||||||
|
`dolly-v2-3b` is a 2.8 billion parameter causal language model created by [Databricks](https://databricks.com/) that is derived from
|
||||||
|
[EleutherAI’s](https://www.eleuther.ai/) [Pythia-2.8b](https://huggingface.co/EleutherAI/pythia-2.8b) and fine-tuned
|
||||||
|
on a [~15K record instruction corpus](https://github.com/databrickslabs/dolly/tree/master/data) generated by Databricks employees and released under a permissive license (CC-BY-SA)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
To use the model with the `transformers` library on a machine with GPUs, first make sure you have the `transformers` and `accelerate` libraries installed.
|
||||||
|
In a Databricks notebook you could run:
|
||||||
|
|
||||||
|
```python
|
||||||
|
%pip install "accelerate>=0.16.0,<1" "transformers[torch]>=4.28.1,<5" "torch>=1.13.1,<2"
|
||||||
|
```
|
||||||
|
|
||||||
|
The instruction following pipeline can be loaded using the `pipeline` function as shown below. This loads a custom `InstructionTextGenerationPipeline`
|
||||||
|
found in the model repo [here](https://huggingface.co/databricks/dolly-v2-3b/blob/main/instruct_pipeline.py), which is why `trust_remote_code=True` is required.
|
||||||
|
Including `torch_dtype=torch.bfloat16` is generally recommended if this type is supported in order to reduce memory usage. It does not appear to impact output quality.
|
||||||
|
It is also fine to remove it if there is sufficient memory.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import torch
|
||||||
|
from transformers import pipeline
|
||||||
|
|
||||||
|
generate_text = pipeline(model="databricks/dolly-v2-3b", torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto")
|
||||||
|
```
|
||||||
|
|
||||||
|
You can then use the pipeline to answer instructions:
|
||||||
|
|
||||||
|
```python
|
||||||
|
res = generate_text("Explain to me the difference between nuclear fission and fusion.")
|
||||||
|
print(res[0]["generated_text"])
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, if you prefer to not use `trust_remote_code=True` you can download [instruct_pipeline.py](https://huggingface.co/databricks/dolly-v2-3b/blob/main/instruct_pipeline.py),
|
||||||
|
store it alongside your notebook, and construct the pipeline yourself from the loaded model and tokenizer:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import torch
|
||||||
|
from instruct_pipeline import InstructionTextGenerationPipeline
|
||||||
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||||
|
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-3b", padding_side="left")
|
||||||
|
model = AutoModelForCausalLM.from_pretrained("databricks/dolly-v2-3b", device_map="auto", torch_dtype=torch.bfloat16)
|
||||||
|
|
||||||
|
generate_text = InstructionTextGenerationPipeline(model=model, tokenizer=tokenizer)
|
||||||
|
```
|
||||||
|
|
||||||
|
### LangChain Usage
|
||||||
|
|
||||||
|
To use the pipeline with LangChain, you must set `return_full_text=True`, as LangChain expects the full text to be returned
|
||||||
|
and the default for the pipeline is to only return the new text.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import torch
|
||||||
|
from transformers import pipeline
|
||||||
|
|
||||||
|
generate_text = pipeline(model="databricks/dolly-v2-3b", torch_dtype=torch.bfloat16,
|
||||||
|
trust_remote_code=True, device_map="auto", return_full_text=True)
|
||||||
|
```
|
||||||
|
|
||||||
|
You can create a prompt that either has only an instruction or has an instruction with context:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from langchain import PromptTemplate, LLMChain
|
||||||
|
from langchain.llms import HuggingFacePipeline
|
||||||
|
|
||||||
|
# template for an instrution with no input
|
||||||
|
prompt = PromptTemplate(
|
||||||
|
input_variables=["instruction"],
|
||||||
|
template="{instruction}")
|
||||||
|
|
||||||
|
# template for an instruction with input
|
||||||
|
prompt_with_context = PromptTemplate(
|
||||||
|
input_variables=["instruction", "context"],
|
||||||
|
template="{instruction}\n\nInput:\n{context}")
|
||||||
|
|
||||||
|
hf_pipeline = HuggingFacePipeline(pipeline=generate_text)
|
||||||
|
|
||||||
|
llm_chain = LLMChain(llm=hf_pipeline, prompt=prompt)
|
||||||
|
llm_context_chain = LLMChain(llm=hf_pipeline, prompt=prompt_with_context)
|
||||||
|
```
|
||||||
|
|
||||||
|
Example predicting using a simple instruction:
|
||||||
|
|
||||||
|
```python
|
||||||
|
print(llm_chain.predict(instruction="Explain to me the difference between nuclear fission and fusion.").lstrip())
|
||||||
|
```
|
||||||
|
|
||||||
|
Example predicting using an instruction with context:
|
||||||
|
|
||||||
|
```python
|
||||||
|
context = """George Washington (February 22, 1732[b] – December 14, 1799) was an American military officer, statesman,
|
||||||
|
and Founding Father who served as the first president of the United States from 1789 to 1797."""
|
||||||
|
|
||||||
|
print(llm_context_chain.predict(instruction="When was George Washington president?", context=context).lstrip())
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Known Limitations
|
||||||
|
|
||||||
|
### Performance Limitations
|
||||||
|
**`dolly-v2-3b` is not a state-of-the-art generative language model** and, though quantitative benchmarking is ongoing, is not designed to perform
|
||||||
|
competitively with more modern model architectures or models subject to larger pretraining corpuses.
|
||||||
|
|
||||||
|
The Dolly model family is under active development, and so any list of shortcomings is unlikely to be exhaustive, but we include known limitations and misfires here as a means to document and share our preliminary findings with the community.
|
||||||
|
In particular, `dolly-v2-3b` struggles with: syntactically complex prompts, programming problems, mathematical operations, factual errors,
|
||||||
|
dates and times, open-ended question answering, hallucination, enumerating lists of specific length, stylistic mimicry, having a sense of humor, etc.
|
||||||
|
Moreover, we find that `dolly-v2-3b` does not have some capabilities, such as well-formatted letter writing, present in the original model.
|
||||||
|
|
||||||
|
### Dataset Limitations
|
||||||
|
Like all language models, `dolly-v2-3b` reflects the content and limitations of its training corpuses.
|
||||||
|
|
||||||
|
- **The Pile**: GPT-J’s pre-training corpus contains content mostly collected from the public internet, and like most web-scale datasets,
|
||||||
|
it contains content many users would find objectionable. As such, the model is likely to reflect these shortcomings, potentially overtly
|
||||||
|
in the case it is explicitly asked to produce objectionable content, and sometimes subtly, as in the case of biased or harmful implicit
|
||||||
|
associations.
|
||||||
|
|
||||||
|
- **`databricks-dolly-15k`**: The training data on which `dolly-v2-3b` is instruction tuned represents natural language instructions generated
|
||||||
|
by Databricks employees during a period spanning March and April 2023 and includes passages from Wikipedia as references passages
|
||||||
|
for instruction categories like closed QA and summarization. To our knowledge it does not contain obscenity, intellectual property or
|
||||||
|
personally identifying information about non-public figures, but it may contain typos and factual errors.
|
||||||
|
The dataset may also reflect biases found in Wikipedia. Finally, the dataset likely reflects
|
||||||
|
the interests and semantic choices of Databricks employees, a demographic which is not representative of the global population at large.
|
||||||
|
|
||||||
|
Databricks is committed to ongoing research and development efforts to develop helpful, honest and harmless AI technologies that
|
||||||
|
maximize the potential of all individuals and organizations.
|
||||||
|
|
||||||
|
### Benchmark Metrics
|
||||||
|
|
||||||
|
Below you'll find various models benchmark performance on the [EleutherAI LLM Evaluation Harness](https://github.com/EleutherAI/lm-evaluation-harness);
|
||||||
|
model results are sorted by geometric mean to produce an intelligible ordering. As outlined above, these results demonstrate that `dolly-v2-3b` is not state of the art.
|
||||||
|
It underperforms `dolly-v1-6b` in the evaluation benchmarks, which is not surprising considering it has half the number of parameters.
|
||||||
|
|
||||||
|
| model | openbookqa | arc_easy | winogrande | hellaswag | arc_challenge | piqa | boolq | gmean |
|
||||||
|
| --------------------------------- | ------------ | ---------- | ------------ | ----------- | --------------- | -------- | -------- | ---------|
|
||||||
|
| EleutherAI/pythia-2.8b | 0.348 | 0.585859 | 0.589582 | 0.591217 | 0.323379 | 0.73395 | 0.638226 | 0.523431 |
|
||||||
|
| EleutherAI/pythia-6.9b | 0.368 | 0.604798 | 0.608524 | 0.631548 | 0.343857 | 0.761153 | 0.6263 | 0.543567 |
|
||||||
|
| databricks/dolly-v2-3b | 0.384 | 0.611532 | 0.589582 | 0.650767 | 0.370307 | 0.742655 | 0.575535 | 0.544886 |
|
||||||
|
| EleutherAI/pythia-12b | 0.364 | 0.627104 | 0.636148 | 0.668094 | 0.346416 | 0.760065 | 0.673394 | 0.559676 |
|
||||||
|
| EleutherAI/gpt-j-6B | 0.382 | 0.621633 | 0.651144 | 0.662617 | 0.363481 | 0.761153 | 0.655963 | 0.565936 |
|
||||||
|
| databricks/dolly-v2-12b | 0.408 | 0.63931 | 0.616417 | 0.707927 | 0.388225 | 0.757889 | 0.568196 | 0.56781 |
|
||||||
|
| databricks/dolly-v2-7b | 0.392 | 0.633838 | 0.607735 | 0.686517 | 0.406997 | 0.750816 | 0.644037 | 0.573487 |
|
||||||
|
| databricks/dolly-v1-6b | 0.41 | 0.62963 | 0.643252 | 0.676758 | 0.384812 | 0.773667 | 0.687768 | 0.583431 |
|
||||||
|
| EleutherAI/gpt-neox-20b | 0.402 | 0.683923 | 0.656669 | 0.7142 | 0.408703 | 0.784004 | 0.695413 | 0.602236 |
|
||||||
|
|
||||||
|
# Happy Hacking!
|
||||||
32
config.json
Executable file
32
config.json
Executable file
@@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"_name_or_path": "EleutherAI/pythia-2.8b",
|
||||||
|
"architectures": [
|
||||||
|
"GPTNeoXForCausalLM"
|
||||||
|
],
|
||||||
|
"custom_pipelines": {
|
||||||
|
"text-generation": {
|
||||||
|
"impl": "instruct_pipeline.InstructionTextGenerationPipeline",
|
||||||
|
"pt": "AutoModelForCausalLM",
|
||||||
|
"tf": "TFAutoModelForCausalLM"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"bos_token_id": 0,
|
||||||
|
"eos_token_id": 0,
|
||||||
|
"hidden_act": "gelu",
|
||||||
|
"hidden_size": 2560,
|
||||||
|
"initializer_range": 0.02,
|
||||||
|
"intermediate_size": 10240,
|
||||||
|
"layer_norm_eps": 1e-05,
|
||||||
|
"max_position_embeddings": 2048,
|
||||||
|
"model_type": "gpt_neox",
|
||||||
|
"num_attention_heads": 32,
|
||||||
|
"num_hidden_layers": 32,
|
||||||
|
"rotary_emb_base": 10000,
|
||||||
|
"rotary_pct": 0.25,
|
||||||
|
"tie_word_embeddings": false,
|
||||||
|
"torch_dtype": "bfloat16",
|
||||||
|
"transformers_version": "4.25.1",
|
||||||
|
"use_cache": true,
|
||||||
|
"use_parallel_residual": true,
|
||||||
|
"vocab_size": 50280
|
||||||
|
}
|
||||||
212
instruct_pipeline.py
Normal file
212
instruct_pipeline.py
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
import logging
|
||||||
|
import re
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from transformers import Pipeline, PreTrainedTokenizer
|
||||||
|
|
||||||
|
from transformers.utils import is_tf_available
|
||||||
|
|
||||||
|
if is_tf_available():
|
||||||
|
import tensorflow as tf
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
INSTRUCTION_KEY = "### Instruction:"
|
||||||
|
RESPONSE_KEY = "### Response:"
|
||||||
|
END_KEY = "### End"
|
||||||
|
INTRO_BLURB = (
|
||||||
|
"Below is an instruction that describes a task. Write a response that appropriately completes the request."
|
||||||
|
)
|
||||||
|
|
||||||
|
# This is the prompt that is used for generating responses using an already trained model. It ends with the response
|
||||||
|
# key, where the job of the model is to provide the completion that follows it (i.e. the response itself).
|
||||||
|
PROMPT_FOR_GENERATION_FORMAT = """{intro}
|
||||||
|
|
||||||
|
{instruction_key}
|
||||||
|
{instruction}
|
||||||
|
|
||||||
|
{response_key}
|
||||||
|
""".format(
|
||||||
|
intro=INTRO_BLURB,
|
||||||
|
instruction_key=INSTRUCTION_KEY,
|
||||||
|
instruction="{instruction}",
|
||||||
|
response_key=RESPONSE_KEY,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_special_token_id(tokenizer: PreTrainedTokenizer, key: str) -> int:
|
||||||
|
"""Gets the token ID for a given string that has been added to the tokenizer as a special token.
|
||||||
|
|
||||||
|
When training, we configure the tokenizer so that the sequences like "### Instruction:" and "### End" are
|
||||||
|
treated specially and converted to a single, new token. This retrieves the token ID each of these keys map to.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
tokenizer (PreTrainedTokenizer): the tokenizer
|
||||||
|
key (str): the key to convert to a single token
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
RuntimeError: if more than one ID was generated
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int: the token ID for the given key
|
||||||
|
"""
|
||||||
|
token_ids = tokenizer.encode(key)
|
||||||
|
if len(token_ids) > 1:
|
||||||
|
raise ValueError(f"Expected only a single token for '{key}' but found {token_ids}")
|
||||||
|
return token_ids[0]
|
||||||
|
|
||||||
|
|
||||||
|
class InstructionTextGenerationPipeline(Pipeline):
|
||||||
|
def __init__(
|
||||||
|
self, *args, do_sample: bool = True, max_new_tokens: int = 256, top_p: float = 0.92, top_k: int = 0, **kwargs
|
||||||
|
):
|
||||||
|
"""Initialize the pipeline
|
||||||
|
|
||||||
|
Args:
|
||||||
|
do_sample (bool, optional): Whether or not to use sampling. Defaults to True.
|
||||||
|
max_new_tokens (int, optional): Max new tokens after the prompt to generate. Defaults to 128.
|
||||||
|
top_p (float, optional): If set to float < 1, only the smallest set of most probable tokens with
|
||||||
|
probabilities that add up to top_p or higher are kept for generation. Defaults to 0.92.
|
||||||
|
top_k (int, optional): The number of highest probability vocabulary tokens to keep for top-k-filtering.
|
||||||
|
Defaults to 0.
|
||||||
|
"""
|
||||||
|
super().__init__(*args, do_sample=do_sample, max_new_tokens=max_new_tokens, top_p=top_p, top_k=top_k,
|
||||||
|
**kwargs)
|
||||||
|
|
||||||
|
def _sanitize_parameters(self,
|
||||||
|
return_full_text: bool = None,
|
||||||
|
**generate_kwargs):
|
||||||
|
preprocess_params = {}
|
||||||
|
|
||||||
|
# newer versions of the tokenizer configure the response key as a special token. newer versions still may
|
||||||
|
# append a newline to yield a single token. find whatever token is configured for the response key.
|
||||||
|
tokenizer_response_key = next(
|
||||||
|
(token for token in self.tokenizer.additional_special_tokens if token.startswith(RESPONSE_KEY)), None
|
||||||
|
)
|
||||||
|
|
||||||
|
response_key_token_id = None
|
||||||
|
end_key_token_id = None
|
||||||
|
if tokenizer_response_key:
|
||||||
|
try:
|
||||||
|
response_key_token_id = get_special_token_id(self.tokenizer, tokenizer_response_key)
|
||||||
|
end_key_token_id = get_special_token_id(self.tokenizer, END_KEY)
|
||||||
|
|
||||||
|
# Ensure generation stops once it generates "### End"
|
||||||
|
generate_kwargs["eos_token_id"] = end_key_token_id
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
forward_params = generate_kwargs
|
||||||
|
postprocess_params = {
|
||||||
|
"response_key_token_id": response_key_token_id,
|
||||||
|
"end_key_token_id": end_key_token_id
|
||||||
|
}
|
||||||
|
|
||||||
|
if return_full_text is not None:
|
||||||
|
postprocess_params["return_full_text"] = return_full_text
|
||||||
|
|
||||||
|
return preprocess_params, forward_params, postprocess_params
|
||||||
|
|
||||||
|
def preprocess(self, instruction_text, **generate_kwargs):
|
||||||
|
prompt_text = PROMPT_FOR_GENERATION_FORMAT.format(instruction=instruction_text)
|
||||||
|
inputs = self.tokenizer(
|
||||||
|
prompt_text,
|
||||||
|
return_tensors="pt",
|
||||||
|
)
|
||||||
|
inputs["prompt_text"] = prompt_text
|
||||||
|
inputs["instruction_text"] = instruction_text
|
||||||
|
return inputs
|
||||||
|
|
||||||
|
def _forward(self, model_inputs, **generate_kwargs):
|
||||||
|
input_ids = model_inputs["input_ids"]
|
||||||
|
attention_mask = model_inputs.get("attention_mask", None)
|
||||||
|
|
||||||
|
if input_ids.shape[1] == 0:
|
||||||
|
input_ids = None
|
||||||
|
attention_mask = None
|
||||||
|
in_b = 1
|
||||||
|
else:
|
||||||
|
in_b = input_ids.shape[0]
|
||||||
|
|
||||||
|
generated_sequence = self.model.generate(
|
||||||
|
input_ids=input_ids.to(self.model.device),
|
||||||
|
attention_mask=attention_mask.to(self.model.device) if attention_mask is not None else None,
|
||||||
|
pad_token_id=self.tokenizer.pad_token_id,
|
||||||
|
**generate_kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
out_b = generated_sequence.shape[0]
|
||||||
|
if self.framework == "pt":
|
||||||
|
generated_sequence = generated_sequence.reshape(in_b, out_b // in_b, *generated_sequence.shape[1:])
|
||||||
|
elif self.framework == "tf":
|
||||||
|
generated_sequence = tf.reshape(generated_sequence, (in_b, out_b // in_b, *generated_sequence.shape[1:]))
|
||||||
|
|
||||||
|
instruction_text = model_inputs.pop("instruction_text")
|
||||||
|
return {"generated_sequence": generated_sequence, "input_ids": input_ids, "instruction_text": instruction_text}
|
||||||
|
|
||||||
|
def postprocess(self, model_outputs, response_key_token_id, end_key_token_id, return_full_text: bool = False):
|
||||||
|
|
||||||
|
generated_sequence = model_outputs["generated_sequence"][0]
|
||||||
|
instruction_text = model_outputs["instruction_text"]
|
||||||
|
|
||||||
|
generated_sequence: List[List[int]] = generated_sequence.numpy().tolist()
|
||||||
|
records = []
|
||||||
|
for sequence in generated_sequence:
|
||||||
|
|
||||||
|
# The response will be set to this variable if we can identify it.
|
||||||
|
decoded = None
|
||||||
|
|
||||||
|
# If we have token IDs for the response and end, then we can find the tokens and only decode between them.
|
||||||
|
if response_key_token_id and end_key_token_id:
|
||||||
|
# Find where "### Response:" is first found in the generated tokens. Considering this is part of the
|
||||||
|
# prompt, we should definitely find it. We will return the tokens found after this token.
|
||||||
|
try:
|
||||||
|
response_pos = sequence.index(response_key_token_id)
|
||||||
|
except ValueError:
|
||||||
|
logger.warn(f"Could not find response key {response_key_token_id} in: {sequence}")
|
||||||
|
response_pos = None
|
||||||
|
|
||||||
|
if response_pos:
|
||||||
|
# Next find where "### End" is located. The model has been trained to end its responses with this
|
||||||
|
# sequence (or actually, the token ID it maps to, since it is a special token). We may not find
|
||||||
|
# this token, as the response could be truncated. If we don't find it then just return everything
|
||||||
|
# to the end. Note that even though we set eos_token_id, we still see the this token at the end.
|
||||||
|
try:
|
||||||
|
end_pos = sequence.index(end_key_token_id)
|
||||||
|
except ValueError:
|
||||||
|
end_pos = None
|
||||||
|
|
||||||
|
decoded = self.tokenizer.decode(sequence[response_pos + 1 : end_pos]).strip()
|
||||||
|
|
||||||
|
if not decoded:
|
||||||
|
# Otherwise we'll decode everything and use a regex to find the response and end.
|
||||||
|
|
||||||
|
fully_decoded = self.tokenizer.decode(sequence)
|
||||||
|
|
||||||
|
# The response appears after "### Response:". The model has been trained to append "### End" at the
|
||||||
|
# end.
|
||||||
|
m = re.search(r"#+\s*Response:\s*(.+?)#+\s*End", fully_decoded, flags=re.DOTALL)
|
||||||
|
|
||||||
|
if m:
|
||||||
|
decoded = m.group(1).strip()
|
||||||
|
else:
|
||||||
|
# The model might not generate the "### End" sequence before reaching the max tokens. In this case,
|
||||||
|
# return everything after "### Response:".
|
||||||
|
m = re.search(r"#+\s*Response:\s*(.+)", fully_decoded, flags=re.DOTALL)
|
||||||
|
if m:
|
||||||
|
decoded = m.group(1).strip()
|
||||||
|
else:
|
||||||
|
logger.warn(f"Failed to find response in:\n{fully_decoded}")
|
||||||
|
|
||||||
|
# If the full text is requested, then append the decoded text to the original instruction.
|
||||||
|
# This technically isn't the full text, as we format the instruction in the prompt the model has been
|
||||||
|
# trained on, but to the client it will appear to be the full text.
|
||||||
|
if return_full_text:
|
||||||
|
decoded = f"{instruction_text}\n{decoded}"
|
||||||
|
|
||||||
|
rec = {"generated_text": decoded}
|
||||||
|
|
||||||
|
records.append(rec)
|
||||||
|
|
||||||
|
return records
|
||||||
3
pytorch_model.bin
Executable file
3
pytorch_model.bin
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:2b4a1f9a10084b10c886c2f0a0e86f63fc56cf9e781864bf0b3f24acb095824c
|
||||||
|
size 5684548185
|
||||||
11
special_tokens_map.json
Executable file
11
special_tokens_map.json
Executable file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"additional_special_tokens": [
|
||||||
|
"### End",
|
||||||
|
"### Instruction:",
|
||||||
|
"### Response:"
|
||||||
|
],
|
||||||
|
"bos_token": "<|endoftext|>",
|
||||||
|
"eos_token": "<|endoftext|>",
|
||||||
|
"pad_token": "<|endoftext|>",
|
||||||
|
"unk_token": "<|endoftext|>"
|
||||||
|
}
|
||||||
87
tester.py
Normal file
87
tester.py
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
import pathlib
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from transformers import pipeline
|
||||||
|
|
||||||
|
from langchain import PromptTemplate, LLMChain
|
||||||
|
from langchain.llms import HuggingFacePipeline
|
||||||
|
|
||||||
|
def getText():
|
||||||
|
s = '''
|
||||||
|
A US climber has died on his way to scale Mount Everest on Monday, according to an expedition organizer.
|
||||||
|
|
||||||
|
“Jonathan Sugarman died at Camp 2 after he began to feel unwell,” Pasang Sherpa told CNN on Tuesday.
|
||||||
|
|
||||||
|
Seattle-based Sugarman was part of an expedition arranged by Washington state-based International Mountain Guides (IMG) with Beyul Adventure handling the local logistics.
|
||||||
|
|
||||||
|
Sherpa added that “his body remains at Camp 2 with the rest of the climbing team.”
|
||||||
|
|
||||||
|
This comes after Nepal has issued permits for a record 463 climbers by April 26, for this spring season’s expeditions to Mount Everest.
|
||||||
|
|
||||||
|
Following Sugarman’s death, the Embassy of the United States issued a statement. “We can confirm Dr. Jonathan Sugarman passed away while climbing Mt. Everest Monday May 1,” it said. “Our deepest sympathies go out to his family and friends.
|
||||||
|
|
||||||
|
“The Embassy is in contact with Dr. Sugarman’s family and with local authorities. Out of respect for the family’s privacy, we cannot comment further,” read a statement sent to CNN by an Embassy spokesperson.
|
||||||
|
'''
|
||||||
|
|
||||||
|
return s
|
||||||
|
|
||||||
|
def mainSimple():
|
||||||
|
print("\nIn main simple...")
|
||||||
|
# print(pathlib.Path().resolve())
|
||||||
|
# generate_text_pipline = pipeline(model="databricks/dolly-v2-3b", torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto")
|
||||||
|
# generate_text_pipline = pipeline(model="verseAI/databricks-dolly-v2-3b", torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto")
|
||||||
|
workingDir = pathlib.Path().resolve()
|
||||||
|
generate_text_pipline = pipeline(model=workingDir, torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto", return_full_text=True)
|
||||||
|
inputText = getText()
|
||||||
|
resp = generate_text_pipline(inputText)
|
||||||
|
respStr = resp[0]["generated_text"]
|
||||||
|
|
||||||
|
print(f'\nInput: {inputText}')
|
||||||
|
print(f'\nResponse: {respStr}')
|
||||||
|
|
||||||
|
print("\nAll Done!\n")
|
||||||
|
|
||||||
|
def getInstrAndContext():
|
||||||
|
context = '''George Washington (February 22, 1732[b] - December 14, 1799) was an American military officer, statesman,
|
||||||
|
and Founding Father who served as the first president of the United States from 1789 to 1797.'''
|
||||||
|
# instr = '''When was George Washington president?'''
|
||||||
|
instr = '''What do you think?'''
|
||||||
|
|
||||||
|
return instr, context
|
||||||
|
|
||||||
|
def mainContext():
|
||||||
|
print("\nIn main context...")
|
||||||
|
workingDir = pathlib.Path().resolve()
|
||||||
|
generate_text_pipline = pipeline(model=workingDir, torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto", return_full_text=True)
|
||||||
|
|
||||||
|
# template for an instrution with no input
|
||||||
|
prompt = PromptTemplate(
|
||||||
|
input_variables=["instruction"],
|
||||||
|
template="{instruction}")
|
||||||
|
|
||||||
|
# template for an instruction with input
|
||||||
|
prompt_with_context = PromptTemplate(
|
||||||
|
input_variables=["instruction", "context"],
|
||||||
|
template="{instruction}\n\nInput:\n{context}")
|
||||||
|
|
||||||
|
hf_pipeline = HuggingFacePipeline(pipeline=generate_text_pipline)
|
||||||
|
|
||||||
|
llm_chain = LLMChain(llm=hf_pipeline, prompt=prompt)
|
||||||
|
llm_context_chain = LLMChain(llm=hf_pipeline, prompt=prompt_with_context)
|
||||||
|
|
||||||
|
instr, context = getInstrAndContext()
|
||||||
|
resp = ''
|
||||||
|
|
||||||
|
if(context and not context.isspace()):
|
||||||
|
resp = llm_context_chain.predict(instruction=instr, context=context).lstrip()
|
||||||
|
else:
|
||||||
|
resp = llm_chain.predict(instruction=instr).lstrip()
|
||||||
|
|
||||||
|
print(f'\nInput-Context: {context}')
|
||||||
|
print(f'\nInput-Instr: {instr}')
|
||||||
|
print(f'\nResponse: {resp}')
|
||||||
|
|
||||||
|
print("\nAll Done!\n")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
mainContext()
|
||||||
100555
tokenizer.json
Executable file
100555
tokenizer.json
Executable file
File diff suppressed because it is too large
Load Diff
10
tokenizer_config.json
Executable file
10
tokenizer_config.json
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"add_prefix_space": false,
|
||||||
|
"bos_token": "<|endoftext|>",
|
||||||
|
"eos_token": "<|endoftext|>",
|
||||||
|
"model_max_length": 1000000000000000019884624838656,
|
||||||
|
"name_or_path": "EleutherAI/pythia-2.8b",
|
||||||
|
"special_tokens_map_file": "/admin/home-hailey/.cache/huggingface/hub/models--EleutherAI--gpt-neox-20b/snapshots/4e49eadb5d14bd22f314ec3f45b69a87b88c7691/special_tokens_map.json",
|
||||||
|
"tokenizer_class": "GPTNeoXTokenizer",
|
||||||
|
"unk_token": "<|endoftext|>"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user