LLAMPPLは、言語モデルの確率的プログラミングの研究プロトタイプです。LLMS、シンボリックプログラムロジック、および確率的条件付けへの呼び出しを組み合わせた確率的プログラムを作成することにより、言語生成タスクを指定します。これらのタスクを解決するために、LLAMPPLは特殊なシーケンシャルモンテカルロ推論アルゴリズムを使用します。この手法であるSMCステアリングは、最近のワークショップの要約で説明されています。
このリポジトリは、huggingfaceトランスで使用するためにllampplを実装します。
LAMPPLを試してみたい場合は、GPT-2を使用して単純な制約付き生成タスクを実行するDemo Notebook on Colabをご覧ください。 (大規模なモデルでは、Colabの無料版が提供するよりも多くのRAMまたはGPUリソースが必要になる場合があります。)
注記
詩を使用して依存関係を管理します。詩がインストールされていない場合は、 pip install poetryでインストールできます。
独自のマシンを開始するには、このリポジトリをクローンし、 poetry installをインストールしてhfpplとその依存関係をインストールします。
git clone https://github.com/probcomp/hfppl
cd hfppl
poetry install
次に、例を実行してみてください。これにより、Vicuna-7B-V1.5の重みがダウンロードされることに注意してください。
poetry run python examples/hard_constraints.py
すべてが機能している場合は、モデルが最大5文字の長さの言葉を使用して政治ニュースを生成するのを見る必要があります(たとえば、「ジル・バイデン博士はまだホワイトハウスから1年離れているかもしれませんが、彼女は今日国連に初めて旅行する予定です」)。
LLAMPPLプログラムは、 hfppl.Modelクラスのサブクラスです。
from hfppl import Model , LMContext , CachedCausalLM
# A LLaMPPL model subclasses the Model class
class MyModel ( Model ):
# The __init__ method is used to process arguments
# and initialize instance variables.
def __init__ ( self , lm , prompt , forbidden_letter ):
super (). __init__ ()
# A stateful context object for the LLM, initialized with the prompt
self . context = LMContext ( lm , prompt )
self . eos_token = lm . tokenizer . eos_token_id
# The forbidden letter
self . forbidden_tokens = set ( i for ( i , v ) in enumerate ( lm . vocab )
if forbidden_letter in v )
# The step method is used to perform a single 'step' of generation.
# This might be a single token, a single phrase, or any other division.
# Here, we generate one token at a time.
async def step ( self ):
# Condition on the next token *not* being a forbidden token.
await self . observe ( self . context . mask_dist ( self . forbidden_tokens ), False )
# Sample the next token from the LLM -- automatically extends `self.context`.
token = await self . sample ( self . context . next_token ())
# Check for EOS or end of sentence
if token . token_id == self . eos_token or str ( token ) in [ '.' , '!' , '?' ]:
# Finish generation
self . finish ()
# To improve performance, a hint that `self.forbidden_tokens` is immutable
def immutable_properties ( self ):
return set ([ 'forbidden_tokens' ])モデルクラスは、LLAMPPLプログラムを指定するための多くの有用な方法を提供します。
self.sample(dist[, proposal])指定された分布からサンプルします。提案を提供しても、タスクの説明は変更されませんが、推論を改善できます。ここでは、たとえば、禁じられた文字を先制的に回避する提案を使用します。self.condition(cond)条件。self.finish()生成が完全であることを示します。self.observe(dist, obs)指定された分布で「ソフトコンディショニング」の形式を実行します。値vをdistからサンプリングし、すぐに実行condition(v == obs)と同等です(よりも効率的です)。推論を実行するには、 smc_steerまたはsmc_standardメソッドを使用します。
import asyncio
from hfppl import smc_steer
# Initialize the HuggingFace model
lm = CachedCausalLM . from_pretrained ( "meta-llama/Llama-2-7b-hf" , auth_token = < YOUR_HUGGINGFACE_API_TOKEN_HERE > )
# Create a model instance
model = MyModel ( lm , "The weather today is expected to be" , "e" )
# Run inference
particles = asyncio . run ( smc_steer ( model , 5 , 3 )) # number of particles N, and beam factor Kサンプル出力:
sunny.
sunny and cool.
34° (81°F) in Chicago with winds at 5mph.
34° (81°F) in Chicago with winds at 2-9 mph.
hot and humid with a possibility of rain, which is not uncommon for this part of Mississippi.
さらなるドキュメントは、https://probcomp.github.io/hfpplをご覧ください。