Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging and Sampling outlines.processors #35

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

lapp0
Copy link
Owner

@lapp0 lapp0 commented Jun 25, 2024

No description provided.

@lapp0 lapp0 changed the base branch from main to logits-processor-integrations-fix June 25, 2024 02:17
@lapp0 lapp0 force-pushed the logits-processor-integrations-fix branch 2 times, most recently from c07de55 to c3e8673 Compare June 29, 2024 13:14
@lapp0 lapp0 changed the title Logging processor Logging and Sampling outlines.processors Jul 20, 2024
@rlouf
Copy link

rlouf commented Jul 20, 2024

The idea is interesting. Do we really want to increase the surface area of the library? There's a risk of spreading ourselves too thinly by adding extra code to maintain.

@lapp0 lapp0 changed the base branch from logits-processor-integrations-fix to main September 9, 2024 21:13
Copy link

@cpfiffer cpfiffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments here and there. Was able to get this to work with a modified example:

import outlines
import outlines.processors as processors
model = outlines.models.transformers(
    "openaccess-ai-collective/tiny-mistral",
)
# Create a chained logits processor
logits_processor = (
    processors.sequence_logging(model.tokenizer) |  # Log the generated sequence
	processors.logits_logging(model.tokenizer) |  # Log the raw logits
	processors.regex(r"[0-9]*", model.tokenizer) |  # Restrict the logits to match the pattern
	processors.temperature(0.5) |  # Set temperature to 0.5
	processors.logits_logging(model.tokenizer)  # Log the restricted, temperature-augmentent, sampled logits
)
generator = outlines.generate.base(model, logits_processor)
generator("What is your favorite number? ")

We'll also need to export the base generator method, i.e. __init__.py should include this line:

from .api import SequenceGenerator
from .base import base
from .cfg import cfg
from .choice import choice
from .format import format
from .fsm import fsm
from .json import json
from .regex import regex
from .text import text

processors.logits_logging(model.tokenizer) # Log the restricted, temperature-augmentent, sampled logits
)

generator = outlines.generate.base(model, logits_process)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
generator = outlines.generate.base(model, logits_process)
generator = outlines.generate.text(model, logits_processor)
  • should be logits_processor
  • Is base defined here? I haven't been able to find it (yet)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +30 to +33
model = outlines.models.llamacpp(
repo_id="M4-ai/TinyMistral-248M-v2-Instruct-GGUF",
filename="TinyMistral-248M-v2-Instruct.Q4_K_M.gguf"
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to work with llamacpp, but it does work with transformers:

import outlines
import outlines.processors as processors
model = outlines.models.transformers(
    "openaccess-ai-collective/tiny-mistral",
)
# Create a chained logits processor
logits_processor = (
    processors.sequence_logging(model.tokenizer) |  # Log the generated sequence
	processors.logits_logging(model.tokenizer) |  # Log the raw logits
	processors.regex(r"[0-9]*", model.tokenizer) |  # Restrict the logits to match the pattern
	processors.temperature(0.5) |  # Set temperature to 0.5
	processors.logits_logging(model.tokenizer)  # Log the restricted, temperature-augmentent, sampled logits
)
generator = outlines.generate.base(model, logits_processor)
generator("What is your favorite number? ")

The error was

Traceback (most recent call last):
  File "/home/cameron/dottxt/outlines/demo-logging.py", line 16, in <module>
    generator("What is your favorite number? ")
  File "/home/cameron/dottxt/outlines/outlines/generate/api.py", line 503, in __call__
    completions = self.model.generate(
  File "/home/cameron/dottxt/outlines/outlines/models/llamacpp.py", line 288, in generate
    completion = self.model(prompts, **llama_cpp_params)
  File "/home/cameron/dottxt/outlines/.venv/lib/python3.10/site-packages/llama_cpp/llama.py", line 1799, in __call__
    return self.create_completion(
  File "/home/cameron/dottxt/outlines/.venv/lib/python3.10/site-packages/llama_cpp/llama.py", line 1732, in create_completion
    completion: Completion = next(completion_or_chunks)  # type: ignore
  File "/home/cameron/dottxt/outlines/.venv/lib/python3.10/site-packages/llama_cpp/llama.py", line 1216, in _create_completion
    for token in self.generate(
  File "/home/cameron/dottxt/outlines/.venv/lib/python3.10/site-packages/llama_cpp/llama.py", line 810, in generate
    token = self.sample(
  File "/home/cameron/dottxt/outlines/.venv/lib/python3.10/site-packages/llama_cpp/llama.py", line 704, in sample
    else logits_processor(self._input_ids[: idx + 1], logits)
  File "/home/cameron/dottxt/outlines/.venv/lib/python3.10/site-packages/llama_cpp/llama.py", line 2250, in __call__
    scores = processor(input_ids, scores)
  File "/home/cameron/dottxt/outlines/.venv/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "/home/cameron/dottxt/outlines/outlines/processors/base_logits_processor.py", line 82, in __call__
    processed_logits = self.process_logits(
  File "/home/cameron/dottxt/outlines/outlines/processors/base_logits_processor.py", line 168, in process_logits
    result = processor.process_logits(input_ids, result)
  File "/home/cameron/dottxt/outlines/outlines/processors/logging.py", line 63, in process_logits
    self.logger.info(self.tokenizer.decode(input_ids))
  File "/home/cameron/dottxt/outlines/outlines/models/llamacpp.py", line 56, in decode
    decoded_bytes = self.tokenizer.detokenize(token_ids)
  File "/home/cameron/dottxt/outlines/.venv/lib/python3.10/site-packages/llama_cpp/llama_tokenizer.py", line 52, in detokenize
    return self._model.detokenize(tokens)
  File "/home/cameron/dottxt/outlines/.venv/lib/python3.10/site-packages/llama_cpp/_internals.py", line 224, in detokenize
    self.model, llama_cpp.llama_token(token), buffer, size, 0, special
TypeError: 'list' object cannot be interpreted as an integer

self.logger = logger
else:
self.logger = logging.getLogger("logits_logger")
self.logger.setLevel(logging.info)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should be

Suggested change
self.logger.setLevel(logging.info)
self.logger.setLevel(logging.INFO)

self.logger = logger
else:
self.logger = logging.getLogger("sequence_logger")
self.logger.setLevel(logging.info)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.logger.setLevel(logging.info)
self.logger.setLevel(logging.INFO)

@cpfiffer
Copy link

cpfiffer commented Sep 9, 2024

Oh, also, the transformers output I think is the token indices, maybe

['What is your favorite number? ']
[{4130: tensor(0.0001), 2: tensor(1.8356e-05), 22315: tensor(0.0002), 26286: tensor(0.0002), 21646: tensor(0.0002), 22835: tensor(0.0002), 8473: tensor(0.0002), 16349: tensor(0.0001), 20126: tensor(0.0002)}]
[{4130: tensor(0.0001), 2: tensor(1.8356e-05), 22315: tensor(0.0002), 26286: tensor(0.0002), 21646: tensor(0.0002), 22835: tensor(0.0002), 8473: tensor(0.0002), 16349: tensor(0.0001), 20126: tensor(0.0002)}]
[{28770: tensor(0.1335), 2: tensor(0.0203), 51: tensor(0.0793), 28787: tensor(0.0984), 54: tensor(0.0466), 57: tensor(0.0612), 58: tensor(0.1677), 59: tensor(0.0338), 28734: tensor(0.1308)}]
[{28770: tensor(0.1335), 2: tensor(0.0203), 51: tensor(0.0793), 28787: tensor(0.0984), 54: tensor(0.0466), 57: tensor(0.0612), 58: tensor(0.1677), 59: tensor(0.0338), 28734: tensor(0.1308)}]
['What is your favorite number? 2']
[{2: tensor(2.8889e-05), 11651: tensor(0.0001), 27974: tensor(0.0002), 4650: tensor(0.0002), 13322: tensor(0.0001), 16362: tensor(0.0001), 12301: tensor(0.0001), 18383: tensor(0.0001), 16753: tensor(0.0001)}]
[{2: tensor(2.8889e-05), 11651: tensor(0.0001), 27974: tensor(0.0002), 4650: tensor(0.0002), 13322: tensor(0.0001), 16362: tensor(0.0001), 12301: tensor(0.0001), 18383: tensor(0.0001), 16753: tensor(0.0001)}]
[{2: tensor(0.0412), 28740: tensor(0.0566), 28750: tensor(0.1070), 28787: tensor(0.1236), 51: tensor(0.0709), 52: tensor(0.0612), 53: tensor(0.0479), 59: tensor(0.1091), 28734: tensor(0.1091)}]
[{2: tensor(0.0412), 28740: tensor(0.0566), 28750: tensor(0.1070), 28787: tensor(0.1236), 51: tensor(0.0709), 52: tensor(0.0612), 53: tensor(0.0479), 59: tensor(0.1091), 28734: tensor(0.1091)}]
['What is your favorite number? 26']
[{2: tensor(2.2782e-05), 24968: tensor(0.0001), 7438: tensor(0.0001), 29904: tensor(0.0002), 11856: tensor(0.0001), 2484: tensor(0.0001), 12731: tensor(0.0001), 1629: tensor(0.0001), 19135: tensor(0.0001)}]
[{2: tensor(2.2782e-05), 24968: tensor(0.0001), 7438: tensor(0.0001), 29904: tensor(0.0002), 11856: tensor(0.0001), 2484: tensor(0.0001), 12731: tensor(0.0001), 1629: tensor(0.0001), 19135: tensor(0.0001)}]
[{28770: tensor(0.0952), 2: tensor(0.0239), 28740: tensor(0.0407), 28784: tensor(0.0665), 28787: tensor(0.1372), 53: tensor(0.0650), 58: tensor(0.1083), 60: tensor(0.0717), 28734: tensor(0.1166)}]
[{28770: tensor(0.0952), 2: tensor(0.0239), 28740: tensor(0.0407), 28784: tensor(0.0665), 28787: tensor(0.1372), 53: tensor(0.0650), 58: tensor(0.1083), 60: tensor(0.0717), 28734: tensor(0.1166)}]
['What is your favorite number? 260']
[{2: tensor(1.9624e-05), 14182: tensor(0.0002), 1514: tensor(0.0001), 3024: tensor(0.0001), 2867: tensor(0.0001), 10966: tensor(0.0002), 6423: tensor(0.0001), 12028: tensor(0.0002), 12799: tensor(0.0001)}]
[{2: tensor(1.9624e-05), 14182: tensor(0.0002), 1514: tensor(0.0001), 3024: tensor(0.0001), 2867: tensor(0.0001), 10966: tensor(0.0002), 6423: tensor(0.0001), 12028: tensor(0.0002), 12799: tensor(0.0001)}]
[{28770: tensor(0.0558), 2: tensor(0.0160), 28782: tensor(0.1015), 28784: tensor(0.2751), 28787: tensor(0.0615), 52: tensor(0.0761), 57: tensor(0.0305), 60: tensor(0.0684), 28734: tensor(0.1016)}]
[{28770: tensor(0.0558), 2: tensor(0.0160), 28782: tensor(0.1015), 28784: tensor(0.2751), 28787: tensor(0.0615), 52: tensor(0.0761), 57: tensor(0.0305), 60: tensor(0.0684), 28734: tensor(0.1016)}]
['What is your favorite number? 2607']
[{2: tensor(1.3555e-05), 9032: tensor(0.0001), 12556: tensor(0.0001), 8974: tensor(0.0001), 8911: tensor(0.0001), 918: tensor(0.0001), 11287: tensor(0.0002), 26521: tensor(0.0002), 25274: tensor(0.0002)}]
[{2: tensor(1.3555e-05), 9032: tensor(0.0001), 12556: tensor(0.0001), 8974: tensor(0.0001), 8911: tensor(0.0001), 918: tensor(0.0001), 11287: tensor(0.0002), 26521: tensor(0.0002), 25274: tensor(0.0002)}]
[{28770: tensor(0.0809), 2: tensor(0.0092), 28740: tensor(0.0617), 28781: tensor(0.0459), 28782: tensor(0.0592), 28787: tensor(0.1464), 52: tensor(0.0830), 57: tensor(0.1384), 58: tensor(0.0557)}]
[{28770: tensor(0.0809), 2: tensor(0.0092), 28740: tensor(0.0617), 28781: tensor(0.0459), 28782: tensor(0.0592), 28787: tensor(0.1464), 52: tensor(0.0830), 57: tensor(0.1384), 58: tensor(0.0557)}]
['What is your favorite number? 26076']

@cpfiffer cpfiffer mentioned this pull request Sep 9, 2024
3 tasks
@lapp0
Copy link
Owner Author

lapp0 commented Sep 10, 2024

Thanks for the review @cpfiffer ! It's a good refresher since I haven't looked at this in a while.

I'll see if I have some free time this weekend and can get this ready for review in outlines-dev


And indeed, it is token indices. Decoded tokens would be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants