Skip to content

Commit

Permalink
Add AutoAWQ integration
Browse files Browse the repository at this point in the history
  • Loading branch information
rlouf committed Nov 24, 2023
1 parent 8804595 commit a117d9d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
45 changes: 45 additions & 0 deletions outlines/models/awq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import TYPE_CHECKING, Optional

from .transformers import Transformer, TransformerTokenizer

if TYPE_CHECKING:
from transformers import PreTrainedModel, PreTrainedTokenizer


class AWQModel(Transformer):
"""Represents a `transformers` model."""

def __init__(
self,
model: "PreTrainedModel",
tokenizer: "PreTrainedTokenizer",
):
self.device = model.model.device
self.model = model
self.tokenizer = tokenizer


def awq(
model_name: str,
fuse_layers: bool = True,
device: Optional[str] = None,
model_kwargs: dict = {},
tokenizer_kwargs: dict = {},
):
try:
from awq import AutoAWQForCausalLM
except ImportError:
raise ImportError(
"The `autoawq` and `transformers` library needs to be installed in order to use `AutoAWQ` models."
)

model_kwargs["fuse_layers"] = fuse_layers
model_kwargs["safetensors"] = True

if device is not None:
model_kwargs["device_map"] = device

model = AutoAWQForCausalLM.from_quantized(model_name, **model_kwargs)
tokenizer = TransformerTokenizer(model_name, trust_remote_code=True)

return AWQModel(model, tokenizer)
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ exclude=["examples"]

[[tool.mypy.overrides]]
module = [
"awq.*",
"auto_gptq.*",
"jinja2",
"joblib.*",
"jsonschema.*",
Expand Down

0 comments on commit a117d9d

Please sign in to comment.