Spaces:
Running
on
Zero
Running
on
Zero
jedick
commited on
Commit
·
09d7140
1
Parent(s):
142bd00
Attempt fix for RuntimeError: p.attn_bias_ptr is not correctly aligned
Browse files- main.py +19 -19
- mods/bm25s_retriever.py +1 -0
- pipeline.py +86 -0
main.py
CHANGED
|
@@ -1,29 +1,28 @@
|
|
| 1 |
-
from
|
| 2 |
-
from langchain_core.prompts import ChatPromptTemplate
|
| 3 |
-
from langchain_core.runnables import RunnablePassthrough
|
| 4 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
|
|
|
|
| 5 |
from langgraph.checkpoint.memory import MemorySaver
|
|
|
|
| 6 |
from langchain_core.messages import ToolMessage
|
| 7 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 8 |
-
from datetime import datetime
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
-
import
|
| 11 |
-
import glob
|
| 12 |
-
import torch
|
| 13 |
import logging
|
|
|
|
|
|
|
| 14 |
import ast
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
from langchain_openai import ChatOpenAI
|
| 18 |
-
|
| 19 |
-
# To use Hugging Face models (local)
|
| 20 |
from langchain_huggingface import ChatHuggingFace, HuggingFacePipeline
|
|
|
|
| 21 |
|
| 22 |
# Local modules
|
| 23 |
-
from
|
| 24 |
from retriever import BuildRetriever, db_dir
|
| 25 |
-
from graph import BuildGraph
|
| 26 |
from prompts import answer_prompt
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# -----------
|
| 29 |
# R-help-chat
|
|
@@ -157,16 +156,17 @@ def GetChatModel(compute_mode, ckpt_dir=None):
|
|
| 157 |
torch_dtype=torch.bfloat16,
|
| 158 |
)
|
| 159 |
|
| 160 |
-
#
|
| 161 |
-
|
| 162 |
-
pipe = pipeline(
|
| 163 |
-
"text-generation",
|
| 164 |
model=model,
|
| 165 |
tokenizer=tokenizer,
|
|
|
|
| 166 |
return_full_text=False,
|
| 167 |
# It seems that max_new_tokens has to be specified here, not in .invoke()
|
| 168 |
-
max_new_tokens=
|
| 169 |
)
|
|
|
|
|
|
|
| 170 |
|
| 171 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 172 |
chat_model = ChatHuggingFace(llm=llm)
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
| 2 |
from langchain_core.output_parsers import StrOutputParser
|
| 3 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 5 |
from langgraph.checkpoint.memory import MemorySaver
|
| 6 |
+
from langchain_core.messages import SystemMessage
|
| 7 |
from langchain_core.messages import ToolMessage
|
|
|
|
|
|
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
+
from datetime import datetime
|
|
|
|
|
|
|
| 10 |
import logging
|
| 11 |
+
import torch
|
| 12 |
+
import glob
|
| 13 |
import ast
|
| 14 |
+
import os
|
| 15 |
|
| 16 |
+
# Imports for local and remote chat models
|
|
|
|
|
|
|
|
|
|
| 17 |
from langchain_huggingface import ChatHuggingFace, HuggingFacePipeline
|
| 18 |
+
from langchain_openai import ChatOpenAI
|
| 19 |
|
| 20 |
# Local modules
|
| 21 |
+
from pipeline import MyTextGenerationPipeline
|
| 22 |
from retriever import BuildRetriever, db_dir
|
|
|
|
| 23 |
from prompts import answer_prompt
|
| 24 |
+
from index import ProcessFile
|
| 25 |
+
from graph import BuildGraph
|
| 26 |
|
| 27 |
# -----------
|
| 28 |
# R-help-chat
|
|
|
|
| 156 |
torch_dtype=torch.bfloat16,
|
| 157 |
)
|
| 158 |
|
| 159 |
+
# Use MyTextGenerationPipeline with custom preprocess() method
|
| 160 |
+
pipe = MyTextGenerationPipeline(
|
|
|
|
|
|
|
| 161 |
model=model,
|
| 162 |
tokenizer=tokenizer,
|
| 163 |
+
# ToolCallingLLM needs return_full_text=False in order to parse just the assistant response
|
| 164 |
return_full_text=False,
|
| 165 |
# It seems that max_new_tokens has to be specified here, not in .invoke()
|
| 166 |
+
max_new_tokens=2000,
|
| 167 |
)
|
| 168 |
+
# We need the task so HuggingFacePipeline can deal with our class
|
| 169 |
+
pipe.task = "text-generation"
|
| 170 |
|
| 171 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 172 |
chat_model = ChatHuggingFace(llm=llm)
|
mods/bm25s_retriever.py
CHANGED
|
@@ -155,6 +155,7 @@ class BM25SRetriever(BaseRetriever):
|
|
| 155 |
*,
|
| 156 |
run_manager: CallbackManagerForRetrieverRun,
|
| 157 |
) -> List[Document]:
|
|
|
|
| 158 |
from mods.bm25s_tokenization import tokenize as bm25s_tokenize
|
| 159 |
|
| 160 |
processed_query = bm25s_tokenize(query, return_ids=False)
|
|
|
|
| 155 |
*,
|
| 156 |
run_manager: CallbackManagerForRetrieverRun,
|
| 157 |
) -> List[Document]:
|
| 158 |
+
# from bm25s import tokenize as bm25s_tokenize
|
| 159 |
from mods.bm25s_tokenization import tokenize as bm25s_tokenize
|
| 160 |
|
| 161 |
processed_query = bm25s_tokenize(query, return_ids=False)
|
pipeline.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.pipelines.text_generation import Chat
|
| 2 |
+
from transformers import TextGenerationPipeline
|
| 3 |
+
from typing import Dict
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class MyTextGenerationPipeline(TextGenerationPipeline):
|
| 7 |
+
"""
|
| 8 |
+
This subclass overrides the preprocess method to add pad_to_multiple_of=8 to tokenizer_kwargs.
|
| 9 |
+
Fix for: "RuntimeError: p.attn_bias_ptr is not correctly aligned"
|
| 10 |
+
https://github.com/google-deepmind/gemma/issues/169
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
def preprocess(
|
| 14 |
+
self,
|
| 15 |
+
prompt_text,
|
| 16 |
+
prefix="",
|
| 17 |
+
handle_long_generation=None,
|
| 18 |
+
add_special_tokens=None,
|
| 19 |
+
truncation=None,
|
| 20 |
+
padding=None,
|
| 21 |
+
max_length=None,
|
| 22 |
+
continue_final_message=None,
|
| 23 |
+
**generate_kwargs,
|
| 24 |
+
):
|
| 25 |
+
print(f"PADDING: {padding}")
|
| 26 |
+
# Only set non-None tokenizer kwargs, so as to rely on the tokenizer's defaults
|
| 27 |
+
tokenizer_kwargs = {
|
| 28 |
+
"add_special_tokens": add_special_tokens,
|
| 29 |
+
"truncation": truncation,
|
| 30 |
+
"padding": padding,
|
| 31 |
+
"max_length": max_length,
|
| 32 |
+
"pad_to_multiple_of": 8,
|
| 33 |
+
}
|
| 34 |
+
tokenizer_kwargs = {
|
| 35 |
+
key: value for key, value in tokenizer_kwargs.items() if value is not None
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
if isinstance(prompt_text, Chat):
|
| 39 |
+
tokenizer_kwargs.pop(
|
| 40 |
+
"add_special_tokens", None
|
| 41 |
+
) # ignore add_special_tokens on chats
|
| 42 |
+
# If the user passes a chat that ends in an assistant message, we treat it as a prefill by default
|
| 43 |
+
# because very few models support multiple separate, consecutive assistant messages
|
| 44 |
+
if continue_final_message is None:
|
| 45 |
+
continue_final_message = prompt_text.messages[-1]["role"] == "assistant"
|
| 46 |
+
inputs = self.tokenizer.apply_chat_template(
|
| 47 |
+
prompt_text.messages,
|
| 48 |
+
add_generation_prompt=not continue_final_message,
|
| 49 |
+
continue_final_message=continue_final_message,
|
| 50 |
+
return_dict=True,
|
| 51 |
+
return_tensors=self.framework,
|
| 52 |
+
**tokenizer_kwargs,
|
| 53 |
+
)
|
| 54 |
+
else:
|
| 55 |
+
inputs = self.tokenizer(
|
| 56 |
+
prefix + prompt_text, return_tensors=self.framework, **tokenizer_kwargs
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
inputs["prompt_text"] = prompt_text
|
| 60 |
+
|
| 61 |
+
if handle_long_generation == "hole":
|
| 62 |
+
cur_len = inputs["input_ids"].shape[-1]
|
| 63 |
+
if "max_new_tokens" in generate_kwargs:
|
| 64 |
+
new_tokens = generate_kwargs["max_new_tokens"]
|
| 65 |
+
else:
|
| 66 |
+
new_tokens = (
|
| 67 |
+
generate_kwargs.get("max_length", self.generation_config.max_length)
|
| 68 |
+
- cur_len
|
| 69 |
+
)
|
| 70 |
+
if new_tokens < 0:
|
| 71 |
+
raise ValueError("We cannot infer how many new tokens are expected")
|
| 72 |
+
if cur_len + new_tokens > self.tokenizer.model_max_length:
|
| 73 |
+
keep_length = self.tokenizer.model_max_length - new_tokens
|
| 74 |
+
if keep_length <= 0:
|
| 75 |
+
raise ValueError(
|
| 76 |
+
"We cannot use `hole` to handle this generation the number of desired tokens exceeds the"
|
| 77 |
+
" models max length"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
inputs["input_ids"] = inputs["input_ids"][:, -keep_length:]
|
| 81 |
+
if "attention_mask" in inputs:
|
| 82 |
+
inputs["attention_mask"] = inputs["attention_mask"][
|
| 83 |
+
:, -keep_length:
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
return inputs
|