backprompt
1.0.0
backprompt提供了一個數據結構,該數據結構允許用戶在避免重複的LLM計算的同時動態構建提示。
在LLMS執行的許多大規模任務中,多次使用了一個特定的提示,即對於任務的每個實例。在這種情況下,可以通過緩存和重新使用LLM的提示表示來減少未來LLM調用的計算量。
backprompt通過在提示符中的中間文本的額外緩存LLM表示,將這個知名的想法進一步邁進了一步。當需要動態調整提示而無需重新計算LLM的表示情況時,中間緩存可能會很有用。 backprompt將迅速構造和緩存的複雜過程作為普通的字符串串聯。
有關更現實的用例,請參見筆記本demos/minimal_example.ipynb 。這是一個玩具演示:
from transformers import AutoModelForCausalLM , AutoTokenizer
from backprompt import Text
# Load a GPT model and its tokenizer
model_name = 'gpt2'
model = AutoModelForCausalLM . from_pretrained ( model_name )
tokenizer = AutoTokenizer . from_pretrained ( model_name )
if tokenizer . pad_token is None :
tokenizer . pad_token = tokenizer . eos_token
mt = ( model , tokenizer )
# Wrap strings in Text and construct them via concatenation
context = Text ( 'Hello there.' , mt )
choices = [ Text ( ' Senator' , mt ), Text ( ' General' , mt )]
endings = [ Text ( ' Amidala' , mt ), Text ( ' Kenobi...' , mt )]
texts = [ context + choice + ending for choice in choices for ending in endings ]
print ( texts [ - 1 ]. string )
# Hello there. General Kenobi...
# Get next-token logits by calling every text obj
# The punchline is that you don't have to worry about repeated computation
for text in texts :
text ()
texts [ - 1 ]. model_repr [ 1 ]. logits [:, - 1 , :] python -m pip install git+https://github.com/kddubey/backprompt.git
如果您基本上知道Backprop的工作原理(觀看此YouTube視頻),並且基本上知道僅解碼器的自動回歸語言模型是如何工作的(觀看此YouTube視頻),那麼您知道backprompt如何工作:-)
類比:
backprompt →注意塊鍵和值。backprompt →令牌logits。backprompt →張量串聯。TODO:圖形可視化
TODO:擴展測試用例
pytest
ModelRepr Dataclass為方便起見token_logprobs屬性到LM輸出OBJ