Skip to content

Commit

Permalink
Merge pull request #9 from probcomp/gg/poetry
Browse files Browse the repository at this point in the history
Migrate dependency management to `poetry`
  • Loading branch information
gabegrand authored Feb 10, 2024
2 parents ffb5f27 + 3821db8 commit 4c5e3d1
Show file tree
Hide file tree
Showing 5 changed files with 1,375 additions and 34 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ This repository implements LLaMPPL for use with HuggingFace Transformers.

If you just want to try out LLaMPPL, check out our [demo notebook on Colab](https://colab.research.google.com/drive/1uJEC-U8dcwsTWccCDGVexpgXexzZ642n?usp=sharing), which performs a simple constrained generation task using GPT-2. (Larger models may require more RAM or GPU resources than Colab's free version provides.)

To get started on your own machine, clone this repository and run `pip install .` (or `pip install -e .` if you plan to modify the library).
> [!NOTE]
> We use [poetry](https://python-poetry.org/) to manage dependencies. If you don't have poetry installed, you can install it with `pip install poetry`.
To get started on your own machine, clone this repository and run `poetry install` to install `hfppl` and its dependencies.

```
git clone https://github.com/probcomp/hfppl
cd hfppl
pip install .
poetry install
```

Then, try running an example. Note that this will cause the weights for Vicuna-7b-v1.5 to be downloaded.

```
python examples/hard_constraints.py
poetry run python examples/hard_constraints.py
```

If everything is working, you should see the model generate political news using words that are at most five letters long (e.g., "Dr. Jill Biden may still be a year away from the White House but she is set to make her first trip to the U.N. today.").
Expand All @@ -44,18 +47,18 @@ class MyModel(Model):
# A stateful context object for the LLM, initialized with the prompt
self.context = LMContext(lm, prompt)
self.lm = lm

# The forbidden letter
self.forbidden_tokens = [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):
# Sample a token from the LLM -- automatically extends `self.context`.
# We use `await` so that LLaMPPL can automatically batch language model calls.
token = await self.sample(self.context.next_token(),
token = await self.sample(self.context.next_token(),
proposal=self.proposal())

# Condition on the token not having the forbidden letter
Expand All @@ -65,7 +68,7 @@ class MyModel(Model):
if token.token_id == self.lm.tokenizer.eos_token_id or str(token) in ['.', '!', '?']:
# Finish generation
self.finish()

# Helper method to define a custom proposal
def proposal(self):
logits = self.context.next_token_logprobs.copy()
Expand Down
10 changes: 6 additions & 4 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ One easy way to try LLaMPPL out is to use a Colab notebook. We have [a demo note

## Installing LLaMPPL

To get started, clone the `hfppl` repository and install the `hfppl` package:
To get started, clone the `hfppl` repository and install the `hfppl` package.

```bash
git clone https://github.com/probcomp/hfppl
cd hfppl
pip install .
poetry install
```

We use [poetry](https://python-poetry.org/) to manage dependencies. If you don't have poetry installed, you can install it with `pip install poetry`.

You can then run an example. The first time you run it, the example may ask to downlaod model weights from the HuggingFace model repository.

```
python examples/hard_constraints.py
```bash
poetry run python examples/hard_constraints.py
```

Depending on your available GPU memory, you may wish to edit the example to change parameters such as the batch size, or which HuggingFace model to use. The `hard_constraints.py` example has been run successfully on an NVIDIA L4 GPU (with 24 GB of VRAM) on Google Cloud.
Expand Down
Loading

0 comments on commit 4c5e3d1

Please sign in to comment.