Skip to content

Commit

Permalink
Update formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kddubey committed Feb 1, 2024
1 parent 85a2f2a commit e80fc36
Show file tree
Hide file tree
Showing 28 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.1
rev: 24.1.1
hooks:
- id: black
language_version: python3.11
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
requirements_huggingface = [
"sentencepiece>=0.1.99", # for Llama tokenizers. cappr should work out-of-the-box
"torch>=1.12.1",
"transformers>=4.31.0", # high version b/c Llama
"transformers>=4.31.0",
]

requirements_huggingface_dev = [
req if not req.startswith("transformers>=") else "transformers>=4.35.0"
# To test Mistral in our testing workflow, we need >=4.34.0. To demo AutoGPTQ on CPU
# and AutoAWQ with caching, need >=4.35.0
# To test Mistral in our testing workflow, we need >=4.34.0.
# To demo AutoGPTQ on CPU and AutoAWQ with caching, need >=4.35.0.
for req in requirements_huggingface
] + ["huggingface-hub>=0.16.4"]

Expand Down
1 change: 1 addition & 0 deletions src/cappr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
https://cappr.readthedocs.io/
"""

__version__ = "0.8.8"

from . import utils
Expand Down
1 change: 1 addition & 0 deletions src/cappr/huggingface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
https://cappr.readthedocs.io/en/latest/select_a_language_model.html#huggingface
"""

from . import _utils, classify, classify_no_cache
22 changes: 16 additions & 6 deletions src/cappr/huggingface/_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
YouTils
"""

from __future__ import annotations
from contextlib import contextmanager, ExitStack, nullcontext
from functools import lru_cache
Expand Down Expand Up @@ -59,14 +60,21 @@ def _no_grad(model: ModelForCausalLM): # model given to keep interface the same
yield


# Some models don't perfectly implement the HF model call interface. In particular,
# they're missing the return_dict and use_cache kwargs. They're instead in the model
# config. I see that as a more extensible design anyway.


@contextmanager
def _return_dict(model: ModelForCausalLM):
"""
In this context, the model returns a dataclass when it's called.
"""
with _setattr(model.config, "return_dict", True) if hasattr(
model, "config"
) else nullcontext(): # null b/c just try model(...).logits when needed
with (
_setattr(model.config, "return_dict", True)
if hasattr(model, "config")
else nullcontext()
): # null b/c just try model(...).logits when needed
yield


Expand All @@ -75,9 +83,11 @@ def _use_cache(model: ModelForCausalLM):
"""
In this context, the model output includes a `past_key_values` attribute.
"""
with _setattr(model.config, "use_cache", True) if hasattr(
model, "config"
) else nullcontext(): # null b/c just try model(...).past_key_values when needed
with (
_setattr(model.config, "use_cache", True)
if hasattr(model, "config")
else nullcontext()
): # null b/c just try model(...).past_key_values when needed
yield


Expand Down
1 change: 1 addition & 0 deletions src/cappr/huggingface/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
In the implementation, attention block keys and values for prompts are automatically
cached and shared across completions.
"""

from __future__ import annotations
from contextlib import contextmanager, nullcontext
from dataclasses import dataclass
Expand Down
1 change: 1 addition & 0 deletions src/cappr/huggingface/classify_no_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
This module is a mirror of :mod:`cappr.huggingface.classify`. The difference is that
this module **does not** cache attention keys and values.
"""

from __future__ import annotations
from typing import cast, Literal, Mapping, Sequence

Expand Down
1 change: 1 addition & 0 deletions src/cappr/llama_cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
https://cappr.readthedocs.io/en/latest/select_a_language_model.html#llama-cpp
"""

from . import _utils, classify, _classify_no_cache
1 change: 1 addition & 0 deletions src/cappr/llama_cpp/_classify_no_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Mirror. For testing purposes only. Is strictly slower
"""

from __future__ import annotations
from functools import partial
from typing import Literal, Sequence
Expand Down
1 change: 1 addition & 0 deletions src/cappr/llama_cpp/_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Utilz
"""

from __future__ import annotations
from contextlib import contextmanager
from functools import lru_cache
Expand Down
1 change: 1 addition & 0 deletions src/cappr/llama_cpp/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
--local-dir . \\
--local-dir-use-symlinks False
"""

from __future__ import annotations
from contextlib import contextmanager
from typing import cast, Literal, Sequence
Expand Down
1 change: 1 addition & 0 deletions src/cappr/openai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
https://cappr.readthedocs.io/en/latest/select_a_language_model.html#openai
"""

from . import api, classify
1 change: 1 addition & 0 deletions src/cappr/openai/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
You probably just want the :func:`predict` or :func:`predict_examples` functions :-)
"""

from __future__ import annotations
from typing import Literal, Sequence

Expand Down
1 change: 1 addition & 0 deletions src/cappr/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""
Utilities shared across model backends
"""

from . import _batch, _check, _no_cache, classify
1 change: 1 addition & 0 deletions src/cappr/utils/_batch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Batch lists into sublists of constant or variable sizes, and batchify functions
"""

from __future__ import annotations
from functools import wraps
import inspect
Expand Down
1 change: 1 addition & 0 deletions src/cappr/utils/_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- inputting an incorrectly structured prior will cause all of the model's compute to
be a waste.
"""

from __future__ import annotations
from typing import Callable, Literal, Sequence

Expand Down
1 change: 1 addition & 0 deletions src/cappr/utils/_no_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Generic implementation for non-cached LM interfaces
"""

from __future__ import annotations
from functools import lru_cache
from typing import Any, Callable, cast, Literal, Sequence
Expand Down
7 changes: 4 additions & 3 deletions src/cappr/utils/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Transform completion token log-probabilites into a probability distribution over
completions
"""

from __future__ import annotations
from contextlib import contextmanager
from functools import wraps
Expand Down Expand Up @@ -555,9 +556,9 @@ def _predict_examples(predict_proba_examples_func):
def wrapper(
examples: Example | Sequence[Example], *args, **kwargs
) -> str | list[str]:
pred_probs: npt.NDArray[np.floating] | list[
npt.NDArray[np.floating]
] = predict_proba_examples_func(examples, *args, **kwargs)
pred_probs: npt.NDArray[np.floating] | list[npt.NDArray[np.floating]] = (
predict_proba_examples_func(examples, *args, **kwargs)
)
if isinstance(examples, Example):
# User convenience: examples is a singleton
assert pred_probs.ndim == 1 # double check
Expand Down
1 change: 1 addition & 0 deletions tests/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Base classes which are parametrized with a set of test cases which every `classify`
module must pass.
"""

from __future__ import annotations
from typing import Sequence

Expand Down
1 change: 1 addition & 0 deletions tests/_test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Helper functions to test that outputs of `classify` modules have the correct content,
assuming a reference/correct implementation exists.
"""

from __future__ import annotations
from typing import Sequence

Expand Down
1 change: 1 addition & 0 deletions tests/_test_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- outputs have the correct shape and form (content is NOT checked)
- inputs are correctly checked.
"""

from __future__ import annotations
import re
from typing import Sequence
Expand Down
1 change: 1 addition & 0 deletions tests/huggingface/test_huggingface_classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
functions' outputs are numerically close to those from
`cappr.huggingface.classify_no_cache`.
"""

from __future__ import annotations
from contextlib import nullcontext
import os
Expand Down
1 change: 1 addition & 0 deletions tests/llama_cpp/test_llama_cpp_classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
functions' outputs are numerically close to those from
`cappr.llama_cpp._classify_no_cache`.
"""

from __future__ import annotations
from dataclasses import dataclass
import os
Expand Down
1 change: 1 addition & 0 deletions tests/openai/test_openai_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Unit tests `cappr.openai.api`. Currently pretty barebones.
"""

from __future__ import annotations
import os

Expand Down
1 change: 1 addition & 0 deletions tests/openai/test_openai_classify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Unit and integration tests for tests `cappr.openai.classify`.
"""

from __future__ import annotations
import os
import sys
Expand Down
1 change: 1 addition & 0 deletions tests/test_Example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Unit tests `cappr.Example` input checks.
"""

from __future__ import annotations
import re
from typing import Any, Sequence
Expand Down
1 change: 1 addition & 0 deletions tests/utils/test_utils__batch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Unit tests `cappr.utils._batch`.
"""

from __future__ import annotations
import re

Expand Down
1 change: 1 addition & 0 deletions tests/utils/test_utils_classify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Unit tests `cappr.utils.classify`.
"""

from __future__ import annotations
from typing import Any

Expand Down

0 comments on commit e80fc36

Please sign in to comment.