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