dottxt-ai / outlines

Structured Text Generation
https://dottxt-ai.github.io/outlines/
Apache License 2.0
9.18k stars 466 forks source link

Running outlines in Docker throws a beartype.roar.BeartypeDecorHintPep484Exception #288

Closed MaartenvanMeeuwen closed 1 year ago

MaartenvanMeeuwen commented 1 year ago

Outlines (0.0.8) won't run inside my Docker container. It worked great last week, but on a build this week, it no longer does.

Steps to reproduce

Here's my minimal Dockerfile:

FROM python:3.11
COPY . .

RUN pip install --upgrade pip

RUN pip install outlines==0.0.8

CMD ["python", "main.py"]

where main.py is in the same directory and contains the following:

import outlines.text.generate as generate
import outlines.models as models

model = models.transformers("gpt2")

prompt = """You are a sentiment-labelling assistant.
Is the following review positive or negative?

Review: This restaurant is just awesome!
"""
answer = generate.choice(model, ["Positive", "Negative"])(prompt)

Current behaviour (bug)

Getting the following traceback

    import outlines.text.generate as generate
  File "/usr/local/lib/python3.11/site-packages/outlines/__init__.py", line 4, in <module>
    from outlines.text import prompt
  File "/usr/local/lib/python3.11/site-packages/outlines/text/__init__.py", line 2, in <module>
    from .generate import continuation
  File "/usr/local/lib/python3.11/site-packages/outlines/text/generate/__init__.py", line 1, in <module>
    from .continuation import continuation
  File "/usr/local/lib/python3.11/site-packages/outlines/text/generate/continuation.py", line 3, in <module>
    import torch
  File "/usr/local/lib/python3.11/site-packages/torch/__init__.py", line 1239, in <module>
    from torch import onnx as onnx
  File "/usr/local/lib/python3.11/site-packages/torch/onnx/__init__.py", line 12, in <module>
    from . import (  # usort:skip. Keep the order instead of sorting lexicographically
  File "/usr/local/lib/python3.11/site-packages/torch/onnx/symbolic_caffe2.py", line 4, in <module>
    from torch.onnx import symbolic_helper, symbolic_opset9 as opset9
  File "/usr/local/lib/python3.11/site-packages/torch/onnx/symbolic_helper.py", line 610, in <module>
    @_beartype.beartype
     ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_decor/decorcache.py", line 77, in beartype
    return beartype_object(obj, conf)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_decor/decorcore.py", line 98, in beartype_object
    _beartype_object_fatal(obj, conf=conf, **kwargs)
  File "/usr/local/lib/python3.11/site-packages/beartype/_decor/decorcore.py", line 147, in _beartype_object_fatal
    beartype_nontype(obj, **kwargs)  # type: ignore[return-value]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_decor/_decormore.py", line 178, in beartype_nontype
    return beartype_func(obj, **kwargs)  # type: ignore[return-value]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_decor/_decormore.py", line 242, in beartype_func
    func_wrapper_code = generate_code(bear_call)
                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_decor/wrap/wrapmain.py", line 187, in generate_code
    code_check_return = _code_check_return(bear_call)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_decor/wrap/wrapmain.py", line 669, in _code_check_return
    reraise_exception_placeholder(
  File "/usr/local/lib/python3.11/site-packages/beartype/_util/error/utilerror.py", line 214, in reraise_exception_placeholder
    raise exception.with_traceback(exception.__traceback__)
  File "/usr/local/lib/python3.11/site-packages/beartype/_decor/wrap/wrapmain.py", line 618, in _code_check_return
    ) = make_func_wrapper_code(hint_sane, bear_call.conf, cls_stack)  # type: ignore[assignment]
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_util/cache/utilcachecall.py", line 259, in _callable_cached
    raise exception
  File "/usr/local/lib/python3.11/site-packages/beartype/_util/cache/utilcachecall.py", line 251, in _callable_cached
    return_value = args_flat_to_return_value[args_flat] = func(
                                                          ^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_decor/wrap/_wrapcode.py", line 86, in make_func_wrapper_code
    ) = make_check_expr(hint, conf, cls_stack)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_util/cache/utilcachecall.py", line 259, in _callable_cached
    raise exception
  File "/usr/local/lib/python3.11/site-packages/beartype/_util/cache/utilcachecall.py", line 251, in _callable_cached
    return_value = args_flat_to_return_value[args_flat] = func(
                                                          ^^^^^
  File "/usr/local/lib/python3.11/site-packages/beartype/_check/code/codemake.py", line 706, in make_check_expr
    die_if_hint_pep_unsupported(
  File "/usr/local/lib/python3.11/site-packages/beartype/_util/hint/pep/utilpeptest.py", line 205, in die_if_hint_pep_unsupported
    raise BeartypeDecorHintPep484Exception(
beartype.roar.BeartypeDecorHintPep484Exception: Function torch.onnx.symbolic_helper._onnx_unsupported() return PEP 484 type hint "typing.NoReturn" invalid in this type hint context (i.e., "typing.NoReturn" valid only as non-nested return annotation).

Expected behaviour

main.py should print out Positive

Wehzie commented 1 year ago

I have a similar problem. Installing outlines breaks torch.

Reproduce as follows, starting with a clean project.

python -m venv venv source venv/bin/activate pip install torch torchaudio torchvision >>> import torch # this works

After installing outlines, torch breaks.

pip install outlines >>> import torch raise BeartypeDecorHintPep484Exception( beartype.roar.BeartypeDecorHintPep484Exception: Function torch.onnx.symbolic_helper._onnx_unsupported() return PEP 484 type hint "typing.NoReturn" invalid in this type hint context (i.e., "typing.NoReturn" valid only as non-nested return annotation)

Python 3.10.12

Wehzie commented 1 year ago

This appears to be a problem with the latest version of beartype (beartype==0.16.0). I fixed the issue with pip install beartype==0.15.0 after installing outlines. As a side note, I had the same problem with the guidance library.

See https://git.ecker.tech/mrq/ai-voice-cloning/issues/388

MaartenvanMeeuwen commented 1 year ago

Yeah, I just came to the same conclusion.

The following works:

pip install beartype==0.15.0

brandonwillard commented 1 year ago

Yeah, I just came to the same conclusion.

The following works:

pip install beartype==0.15.0

Yeah, it's a pytorch + beartype issue that's been coming up here and there (e.g. https://github.com/outlines-dev/outlines/issues/284#issuecomment-1722308698).