Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llmsql/_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ llmsql inference transformers \
```

This command calls [`inference_transformers()`](../inference/inference_transformers.py).
The model dtype defaults to `auto` (from the model config). Remote code is disabled
by default; add `--trust-remote-code` only for models that require custom code.

### 2) vLLM backend

Expand All @@ -39,6 +41,8 @@ llmsql inference vllm \
```

This command calls [`inference_vllm()`](../inference/inference_vllm.py).
Remote code is disabled by default; add `--trust-remote-code` only for models that
require custom code.

### 3) OpenAI-compatible API backend

Expand Down
6 changes: 3 additions & 3 deletions llmsql/_cli/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ def add_common_generation_args(
self._parser_transformers.add_argument(
"--trust-remote-code",
action=argparse.BooleanOptionalAction,
default=True,
default=False,
)
self._parser_transformers.add_argument("--dtype", default="float16")
self._parser_transformers.add_argument("--dtype", default="auto")
self._parser_transformers.add_argument("--device-map", default="auto")
self._parser_transformers.add_argument("--hf-token")
self._parser_transformers.add_argument(
Expand Down Expand Up @@ -256,7 +256,7 @@ def add_common_generation_args(
self._parser_vllm.add_argument(
"--trust-remote-code",
action=argparse.BooleanOptionalAction,
default=True,
default=False,
)
self._parser_vllm.add_argument(
"--tensor-parallel-size",
Expand Down
8 changes: 5 additions & 3 deletions llmsql/inference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ Runs inference using the Hugging Face `transformers` backend.
| ------------------------------- | --------------------- | ------------- | -------------------------------------------------------------- |
| `model_or_model_name_or_path` | `str \| AutoModelForCausalLM` | *required* | Model object, HuggingFace model name, or local path. |
| `tokenizer_or_name` | `str \| Any \| None` | `None` | Tokenizer object, name, or None (infers from model). |
| `trust_remote_code` | `bool` | `True` | Whether to trust remote code when loading models. |
| `dtype` | `torch.dtype \| str` | `torch.float16` | Model precision (e.g., `torch.float16`, `"bfloat16"`, `"auto"`). |
| `trust_remote_code` | `bool` | `False` | Whether to trust remote code when loading models. |
| `dtype` | `torch.dtype \| str` | `"auto"` | Model precision (uses the model config; e.g., `torch.float16`, `"bfloat16"`). |
| `device_map` | `str \| dict \| None` | `"auto"` | Device placement strategy for multi-GPU. |
| `hf_token` | `str \| None` | `None` | Hugging Face authentication token. |
| `model_kwargs` | `dict \| None` | `None` | Additional kwargs for `AutoModelForCausalLM.from_pretrained()`. |
Expand Down Expand Up @@ -218,6 +218,8 @@ Runs inference using the Hugging Face `transformers` backend.

**Note:** Values in `model_kwargs` / `tokenizer_kwargs` take precedence over the explicit parameters (e.g., `dtype`, `trust_remote_code`). A `dtype` or `torch_dtype` entry in `model_kwargs` replaces `dtype` and may be given as a string.

Models that require custom repository code must be loaded with `trust_remote_code=True` in Python or `--trust-remote-code` on the CLI. Remote code execution is disabled by default for both model and tokenizer loading.

---

### `inference_vllm(...)`
Expand All @@ -231,7 +233,7 @@ Runs inference using the [vLLM](https://github.com/vllm-project/vllm) backend fo
| Argument | Type | Default | Description |
| ------------------------------- | -------------- | ------- | ------------------------------------------------ |
| `model_name` | `str` | *required* | Hugging Face model name or local path. |
| `trust_remote_code` | `bool` | `True` | Whether to trust remote code when loading. |
| `trust_remote_code` | `bool` | `False` | Whether to trust remote code when loading. |
| `tensor_parallel_size` | `int` | `1` | Number of GPUs for tensor parallelism. |
| `hf_token` | `str \| None` | `None` | Hugging Face authentication token. |
| `llm_kwargs` | `dict \| None` | `None` | Additional kwargs for `vllm.LLM()`. |
Expand Down
11 changes: 6 additions & 5 deletions llmsql/inference/inference_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def inference_transformers(
tokenizer_or_name: str | Any | None = None,
*,
# --- Model Loading Parameters ---
trust_remote_code: bool = True,
dtype: torch.dtype | str = torch.float16,
trust_remote_code: bool = False,
dtype: torch.dtype | str = "auto",
device_map: str | dict[str, int] | None = "auto",
hf_token: str | None = None,
model_kwargs: dict[str, Any] | None = None,
Expand Down Expand Up @@ -129,8 +129,9 @@ def inference_transformers(
tokenizer_or_name: Tokenizer object or HF tokenizer name/path.

# Model Loading:
trust_remote_code: Whether to trust remote code (default: True).
dtype: Torch dtype for model (default: float16). Strings such as
trust_remote_code: Whether to trust remote code (default: False).
dtype: Torch dtype for model (default: "auto", using the model config).
Strings such as
"bfloat16" or "auto" are accepted as well.
device_map: Device placement strategy (default: "auto").
hf_token: Hugging Face authentication token.
Expand Down Expand Up @@ -231,7 +232,7 @@ def inference_transformers(

if tok_name:
load_tok_args = {
"trust_remote_code": True,
"trust_remote_code": trust_remote_code,
"token": hf_token,
"padding_side": tokenizer_kwargs.get("padding_side", "left"),
**tokenizer_kwargs,
Expand Down
4 changes: 2 additions & 2 deletions llmsql/inference/inference_vllm.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def inference_vllm(
model_name: str,
*,
# === Model Loading Parameters ===
trust_remote_code: bool = True,
trust_remote_code: bool = False,
tensor_parallel_size: int = 1,
hf_token: str | None = None,
llm_kwargs: dict[str, Any] | None = None,
Expand Down Expand Up @@ -100,7 +100,7 @@ def inference_vllm(
model_name: Hugging Face model name or path.

# Model Loading:
trust_remote_code: Whether to trust remote code (default: True).
trust_remote_code: Whether to trust remote code (default: False).
tensor_parallel_size: Number of GPUs for tensor parallelism (default: 1).
hf_token: Hugging Face authentication token.
llm_kwargs: Additional arguments for vllm.LLM().
Expand Down
13 changes: 7 additions & 6 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

import llmsql
from llmsql._cli.llmsql_cli import ParserCLI


Expand Down Expand Up @@ -45,6 +46,8 @@ async def test_transformers_backend_called(monkeypatch):
call_kwargs = mock_inference.call_args.kwargs
assert call_kwargs["model_or_model_name_or_path"] == "Qwen/Qwen2.5-1.5B-Instruct"
assert call_kwargs["temperature"] == 0.9
assert call_kwargs["dtype"] == "auto"
assert call_kwargs["trust_remote_code"] is False
assert call_kwargs["generation_kwargs"]["top_p"] == 0.9


Expand All @@ -56,10 +59,7 @@ async def test_vllm_backend_called(monkeypatch):
pytest.importorskip("vllm")
mock_inference = MagicMock(return_value=[])

monkeypatch.setattr(
"llmsql.inference_vllm",
mock_inference,
)
monkeypatch.setitem(llmsql.__dict__, "inference_vllm", mock_inference)

test_args = [
"llmsql",
Expand All @@ -83,6 +83,7 @@ async def test_vllm_backend_called(monkeypatch):
call_kwargs = mock_inference.call_args.kwargs
assert call_kwargs["model_name"] == "mistralai/Mixtral-8x7B-Instruct-v0.1"
assert call_kwargs["tensor_parallel_size"] == 2
assert call_kwargs["trust_remote_code"] is False


def test_boolean_options():
Expand All @@ -91,7 +92,7 @@ def test_boolean_options():
(
["inference", "transformers", "--model-or-model-name-or-path", "m"],
"trust_remote_code",
True,
False,
"--trust-remote-code",
),
(
Expand All @@ -103,7 +104,7 @@ def test_boolean_options():
(
["inference", "vllm", "--model-name", "m"],
"trust_remote_code",
True,
False,
"--trust-remote-code",
),
(
Expand Down
51 changes: 51 additions & 0 deletions tests/inference/test_inference_transformers_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,57 @@ def test_string_dtype_argument_is_converted(monkeypatch):
assert "dtype" not in captured


def test_default_dtype_is_auto(monkeypatch):
captured = _captured_load_kwargs(monkeypatch)
assert captured["torch_dtype"] == "auto"
assert captured["trust_remote_code"] is False


@pytest.mark.parametrize("trust_remote_code", [False, True])
def test_remote_code_setting_is_passed_to_model_and_tokenizer(
monkeypatch, tmp_path, trust_remote_code
):
captured = {"model": {}, "tokenizer": {}}

class FakeModel:
def eval(self):
pass

class FakeTokenizer:
pad_token = "pad"
eos_token = "eos"
pad_token_id = 0

def fake_model_from_pretrained(name, **kwargs):
captured["model"].update(kwargs)
return FakeModel()

monkeypatch.setattr(
it.AutoModelForCausalLM, "from_pretrained", fake_model_from_pretrained
)

def fake_tokenizer_from_pretrained(name, **kwargs):
captured["tokenizer"].update(kwargs)
return FakeTokenizer()

monkeypatch.setattr(
it.AutoTokenizer, "from_pretrained", fake_tokenizer_from_pretrained
)
monkeypatch.setattr(it, "_setup_seed", lambda **kwargs: None)
monkeypatch.setattr(it, "_maybe_download", lambda *args: "unused")
monkeypatch.setattr(it, "load_jsonl", lambda path: [])

it.inference_transformers(
"some/model",
trust_remote_code=trust_remote_code,
output_file=str(tmp_path / "predictions.jsonl"),
workdir_path=str(tmp_path),
)

assert captured["model"]["trust_remote_code"] is trust_remote_code
assert captured["tokenizer"]["trust_remote_code"] is trust_remote_code


@pytest.mark.parametrize("key", ["dtype", "torch_dtype"])
def test_model_kwargs_dtype_overrides_argument(monkeypatch, key):
model_kwargs = {key: "float32", "revision": "main"}
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_inference_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_resolve_workdir_path_creates_temp(monkeypatch):
path = mod.resolve_workdir_path(None)

assert isinstance(path, Path)
assert str(path) == fake_dir
assert path == Path(fake_dir)


def test_resolve_workdir_logs(monkeypatch):
Expand Down
Loading