Skip to main content

Restrict LLM generations to a context-free grammar

Project description

torch-grammar

Alpha Quality: This might do what you want. It's likely to not do what you want. Please open issues!

Torch-Grammar restricts a model to output a token sequence that conforms to a provided EBNF grammar.

For example:

import torch
from torch_grammar import GrammarSampler
from transformers import LlamaTokenizer
tokenizer = LlamaTokenizer.from_pretrained("huggyllama/llama-7b")

with open("examples/grammar.ebnf", "r") as file:
    input_text = file.read()
grammar = GrammarSampler(input_text, "root", tokenizer)

ids = [[1]]
logits_processor = grammar.logits_processor()

vocab_size = len(tokenizer.get_vocab())
for i in range(10):
  logits = torch.randn((1, vocab_size))
  logits = logits_processor(ids, logits)
  token = torch.argmax(logits).item()
  # logits_processor.accept_token(token)
  ids[0].append(token)
print(f"\x1b[1mfirst 10 tokens: \x1b[1;35m{tokenizer.decode(ids[0])}\x1b[0m")

logits_processor is meant to be passed to model.generate in a HuggingFace transformers model but this integration is not yet super clean.

TODO / possible features

  • UTF-8 support... a bit of fiddling but not terribly hard
  • More expressive grammars... lookahead would be challenging.
  • Easier integration with various tokenizers... LLaMA works well; T5 presents significant challenges; haven't even tried others.
  • Testing and automatic benchmarking.
  • Binary parse is probably not carrying its weight with all the caching and precomputation we're doing now; it should be rewritten to something less confusing. In fact it might work to just hijack Lark or something?

Broken seeds

  • 11833882144218229242

Related Work

The code was originally adapted from https://github.com/ggerganov/llama.cpp/pull/1773. In particular, the grammar parser is a pretty straightforward mechanical translation and the binary grammar format is identical.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_grammar-0.3.3.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

torch_grammar-0.3.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file torch_grammar-0.3.3.tar.gz.

File metadata

  • Download URL: torch_grammar-0.3.3.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.3 Darwin/22.5.0

File hashes

Hashes for torch_grammar-0.3.3.tar.gz
Algorithm Hash digest
SHA256 6d59affce8cb937af6d0dbe9605afcafa2283b3270ab60cd30a4455479bb7530
MD5 5ae5b2d0e7108d984ba294301274663d
BLAKE2b-256 cfd56cf14ea60bf678ba224c375cf469a44b21ca933ead8315e5a88bd908302e

See more details on using hashes here.

File details

Details for the file torch_grammar-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: torch_grammar-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.3 Darwin/22.5.0

File hashes

Hashes for torch_grammar-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 245312dcfa29fb9072a74c5ecd385a35081b9ce1df60448af5e7b27250ca9192
MD5 b4be367b5f425a85524506aa338b095e
BLAKE2b-256 2b33bca15f46a86414730e6f86ff8d6ef773f7d48ad7d2ae749cb4e40dc0b6fe

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page