From b87c547db9ec31172b0f4fca4bd65cd634873d07 Mon Sep 17 00:00:00 2001 From: anush-933 Date: Sun, 4 Oct 2026 20:56:27 +0530 Subject: [PATCH 1/3] fix: vLLM token passing, tokenizer trust_remote_code, LoRA dedup, CLI LoRA args, doc fixes --- llmsql/_cli/inference.py | 26 +++++++++++++++++++++- llmsql/inference/README.md | 2 +- llmsql/inference/inference_transformers.py | 7 +++--- llmsql/inference/inference_vllm.py | 18 +++++++-------- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/llmsql/_cli/inference.py b/llmsql/_cli/inference.py index 919ced4..1137ddc 100644 --- a/llmsql/_cli/inference.py +++ b/llmsql/_cli/inference.py @@ -283,6 +283,16 @@ def add_common_generation_args( type=json.loads, help="JSON string for SamplingParams kwargs", ) + # LoRA args + self._parser_vllm.add_argument("--enable-lora", action="store_true", help="Enable LoRA adapters") + self._parser_vllm.add_argument("--lora-path", help="Path to LoRA adapter") + self._parser_vllm.add_argument("--lora-name", help="Name of LoRA adapter") + self._parser_vllm.add_argument("--lora-scale", type=float, default=1.0, help="LoRA scaling factor") + self._parser_vllm.add_argument( + "--lora-config", + type=json.loads, + help="JSON string for LoRA config dict", + ) add_common_benchmark_args(self._parser_vllm) @@ -356,17 +366,31 @@ def _execute_transformers(args: argparse.Namespace) -> None: def _execute_vllm(args: argparse.Namespace) -> None: from llmsql import inference_vllm + llm_kwargs = merge_model_args(args.model_args, args.llm_kwargs) + lora_config = args.lora_config + if lora_config is None and args.lora_path: + lora_config = { + "lora_path": args.lora_path, + "lora_name": args.lora_name if args.lora_name else "default", + "lora_scale": args.lora_scale, + } + if args.enable_lora: + if llm_kwargs is None: + llm_kwargs = {} + llm_kwargs["enable_lora"] = True + inference_vllm( model_name=args.model_name, trust_remote_code=args.trust_remote_code, tensor_parallel_size=args.tensor_parallel_size, hf_token=args.hf_token, - llm_kwargs=merge_model_args(args.model_args, args.llm_kwargs), + llm_kwargs=llm_kwargs, use_chat_template=args.use_chat_template, max_new_tokens=args.max_new_tokens, temperature=args.temperature, do_sample=args.do_sample, sampling_kwargs=args.sampling_kwargs, + lora_config=lora_config, version=args.version, output_file=args.output_file, workdir_path=args.workdir_path, diff --git a/llmsql/inference/README.md b/llmsql/inference/README.md index b47dafc..547eaba 100644 --- a/llmsql/inference/README.md +++ b/llmsql/inference/README.md @@ -235,7 +235,7 @@ Runs inference using the [vLLM](https://github.com/vllm-project/vllm) backend fo | `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()`. | -| `llm_kwargs` | `bool` | `True` | Whether to use chat template of the tokenizer | +| `use_chat_template` | `bool` | `True` | Whether to use chat template of the tokenizer | #### Generation diff --git a/llmsql/inference/inference_transformers.py b/llmsql/inference/inference_transformers.py index 3bcb381..2bec76f 100644 --- a/llmsql/inference/inference_transformers.py +++ b/llmsql/inference/inference_transformers.py @@ -142,7 +142,8 @@ def inference_transformers( # Tokenizer Loading: tokenizer_kwargs: Additional arguments for AutoTokenizer.from_pretrained(). 'padding_side' defaults to "left". - Note: 'trust_remote_code', 'token' are handled separately and will override values here. + Note: 'trust_remote_code', 'token' are handled separately; + values in tokenizer_kwargs take precedence if explicitly passed. # Prompt & Chat: @@ -231,11 +232,11 @@ 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, } + load_tok_args.update(tokenizer_kwargs) tokenizer = AutoTokenizer.from_pretrained(tok_name, **load_tok_args) if tokenizer.pad_token is None: diff --git a/llmsql/inference/inference_vllm.py b/llmsql/inference/inference_vllm.py index 53b4ebf..c4ba20a 100644 --- a/llmsql/inference/inference_vllm.py +++ b/llmsql/inference/inference_vllm.py @@ -21,11 +21,11 @@ max_new_tokens=256, temperature=0.7, tensor_parallel_size=1, - lora_path="path/to/lora" + lora_path="path/to/lora", ) Notes -~~~~~ +----- This function uses the vLLM backend. Outputs may differ from the Transformers backend due to differences in implementation, batching, and numerical precision. @@ -105,8 +105,8 @@ def inference_vllm( hf_token: Hugging Face authentication token. llm_kwargs: Additional arguments for vllm.LLM(). Note: 'model', 'tokenizer', 'tensor_parallel_size', - 'trust_remote_code' are handled separately and will - override values here. + 'trust_remote_code', 'token'/'hf_token' are handled separately; + values in llm_kwargs take precedence on conflicts. lora_config: Optional dict with LoRA parameters: - lora_path: Path to the pretrained LoRA adapter (required if enable_lora) @@ -124,7 +124,7 @@ def inference_vllm( do_sample: Whether to use sampling vs greedy decoding. sampling_kwargs: Additional arguments for vllm.SamplingParams(). Note: 'temperature', 'max_tokens' are handled - separately and will override values here. + separately; values in sampling_kwargs take precedence. # Benchmark: version: LLMSQL version @@ -188,10 +188,6 @@ def inference_vllm( ) if enable_lora and lora_config is None: raise ValueError("`enable_lora` is True but no `lora_config` was provided.") - if lora_config is not None and not enable_lora: - raise ValueError( - "`lora_config` provided but `enable_lora` is not True in llm_kwargs." - ) # --- init model --- llm_init_args = { @@ -199,8 +195,10 @@ def inference_vllm( "tokenizer": model_name, "tensor_parallel_size": tensor_parallel_size, "trust_remote_code": trust_remote_code, - **llm_kwargs, # user overrides } + if hf_token: + llm_init_args["token"] = hf_token + llm_init_args.update(llm_kwargs) log.info(f"Loading vLLM model '{model_name}' (tp={tensor_parallel_size})...") llm = LLM(**llm_init_args) From 4b877906e78da10e105790ec3245212281f9d42e Mon Sep 17 00:00:00 2001 From: anush-933 Date: Tue, 6 Oct 2026 09:46:38 +0530 Subject: [PATCH 2/3] fix(#110): pass hf_token as hf_token to vLLM; keep LoRA validation checks --- llmsql/inference/inference_vllm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llmsql/inference/inference_vllm.py b/llmsql/inference/inference_vllm.py index c4ba20a..485bede 100644 --- a/llmsql/inference/inference_vllm.py +++ b/llmsql/inference/inference_vllm.py @@ -197,7 +197,7 @@ def inference_vllm( "trust_remote_code": trust_remote_code, } if hf_token: - llm_init_args["token"] = hf_token + llm_init_args["hf_token"] = hf_token llm_init_args.update(llm_kwargs) log.info(f"Loading vLLM model '{model_name}' (tp={tensor_parallel_size})...") From dc4ff941928764fa91b8e2b6eadc6bccf17d7325 Mon Sep 17 00:00:00 2001 From: anush-933 Date: Sat, 10 Oct 2026 20:49:35 +0530 Subject: [PATCH 3/3] test(#110): add mocked tests for hf_token, tokenizer trust_remote_code, kwargs precedence, and CLI LoRA; make --lora-path imply enable_lora; ruff format --- llmsql/_cli/inference.py | 15 ++- llmsql/inference/inference_vllm.py | 2 +- tests/cli/test_model_args.py | 96 +++++++++++++++++++ .../test_inference_transformers_tokenizer.py | 64 +++++++++++++ tests/inference/test_inference_vllm_args.py | 91 ++++++++++++++++++ 5 files changed, 263 insertions(+), 5 deletions(-) create mode 100644 tests/inference/test_inference_transformers_tokenizer.py create mode 100644 tests/inference/test_inference_vllm_args.py diff --git a/llmsql/_cli/inference.py b/llmsql/_cli/inference.py index 1137ddc..7d64489 100644 --- a/llmsql/_cli/inference.py +++ b/llmsql/_cli/inference.py @@ -176,7 +176,9 @@ def _add_args(self) -> None: # COMMON BENCHMARK ARGS # ========================= def add_common_benchmark_args(parser: argparse.ArgumentParser) -> None: - parser.add_argument("--version", default="2.0", choices=get_available_versions()) + parser.add_argument( + "--version", default="2.0", choices=get_available_versions() + ) parser.add_argument("--output-file", default="llm_sql_predictions.jsonl") parser.add_argument( "--workdir-path", @@ -284,10 +286,14 @@ def add_common_generation_args( help="JSON string for SamplingParams kwargs", ) # LoRA args - self._parser_vllm.add_argument("--enable-lora", action="store_true", help="Enable LoRA adapters") + self._parser_vllm.add_argument( + "--enable-lora", action="store_true", help="Enable LoRA adapters" + ) self._parser_vllm.add_argument("--lora-path", help="Path to LoRA adapter") self._parser_vllm.add_argument("--lora-name", help="Name of LoRA adapter") - self._parser_vllm.add_argument("--lora-scale", type=float, default=1.0, help="LoRA scaling factor") + self._parser_vllm.add_argument( + "--lora-scale", type=float, default=1.0, help="LoRA scaling factor" + ) self._parser_vllm.add_argument( "--lora-config", type=json.loads, @@ -374,7 +380,8 @@ def _execute_vllm(args: argparse.Namespace) -> None: "lora_name": args.lora_name if args.lora_name else "default", "lora_scale": args.lora_scale, } - if args.enable_lora: + # Providing a LoRA config implies `enable_lora=True`. + if args.enable_lora or lora_config is not None: if llm_kwargs is None: llm_kwargs = {} llm_kwargs["enable_lora"] = True diff --git a/llmsql/inference/inference_vllm.py b/llmsql/inference/inference_vllm.py index 485bede..71269be 100644 --- a/llmsql/inference/inference_vllm.py +++ b/llmsql/inference/inference_vllm.py @@ -105,7 +105,7 @@ def inference_vllm( hf_token: Hugging Face authentication token. llm_kwargs: Additional arguments for vllm.LLM(). Note: 'model', 'tokenizer', 'tensor_parallel_size', - 'trust_remote_code', 'token'/'hf_token' are handled separately; + 'trust_remote_code', 'hf_token' are handled separately; values in llm_kwargs take precedence on conflicts. lora_config: Optional dict with LoRA parameters: diff --git a/tests/cli/test_model_args.py b/tests/cli/test_model_args.py index 45a789e..f4fe734 100644 --- a/tests/cli/test_model_args.py +++ b/tests/cli/test_model_args.py @@ -202,6 +202,102 @@ def test_vllm_model_args_reach_llm_kwargs(monkeypatch, flag): } +def test_vllm_lora_path_produces_lora_config(monkeypatch): + mock_inference = MagicMock(return_value=[]) + import llmsql + + monkeypatch.setitem(llmsql.__dict__, "inference_vllm", mock_inference) + + _run_cli( + monkeypatch, + [ + "inference", + "vllm", + "--model-name", + "m", + "--lora-path", + "/path/to/adapter", + ], + ) + + mock_inference.assert_called_once() + kwargs = mock_inference.call_args.kwargs + assert kwargs["lora_config"] == { + "lora_path": "/path/to/adapter", + "lora_name": "default", + "lora_scale": 1.0, + } + # `--lora-path` implies `enable_lora=True` + assert kwargs["llm_kwargs"]["enable_lora"] is True + + +def test_vllm_lora_name_and_scale_are_forwarded(monkeypatch): + mock_inference = MagicMock(return_value=[]) + import llmsql + + monkeypatch.setitem(llmsql.__dict__, "inference_vllm", mock_inference) + + _run_cli( + monkeypatch, + [ + "inference", + "vllm", + "--model-name", + "m", + "--lora-path", + "/path/to/adapter", + "--lora-name", + "my-adapter", + "--lora-scale", + "0.5", + ], + ) + + kwargs = mock_inference.call_args.kwargs + assert kwargs["lora_config"] == { + "lora_path": "/path/to/adapter", + "lora_name": "my-adapter", + "lora_scale": 0.5, + } + + +def test_vllm_lora_config_json_implies_enable_lora(monkeypatch): + mock_inference = MagicMock(return_value=[]) + import llmsql + + monkeypatch.setitem(llmsql.__dict__, "inference_vllm", mock_inference) + + lora_config = {"lora_path": "/path/to/adapter", "lora_name": "n"} + _run_cli( + monkeypatch, + [ + "inference", + "vllm", + "--model-name", + "m", + "--lora-config", + json.dumps(lora_config), + ], + ) + + kwargs = mock_inference.call_args.kwargs + assert kwargs["lora_config"] == lora_config + assert kwargs["llm_kwargs"]["enable_lora"] is True + + +def test_vllm_without_lora_keeps_none(monkeypatch): + mock_inference = MagicMock(return_value=[]) + import llmsql + + monkeypatch.setitem(llmsql.__dict__, "inference_vllm", mock_inference) + + _run_cli(monkeypatch, ["inference", "vllm", "--model-name", "m"]) + + kwargs = mock_inference.call_args.kwargs + assert kwargs["lora_config"] is None + assert kwargs["llm_kwargs"] is None + + @pytest.mark.parametrize( "argv", [ diff --git a/tests/inference/test_inference_transformers_tokenizer.py b/tests/inference/test_inference_transformers_tokenizer.py new file mode 100644 index 0000000..d67972d --- /dev/null +++ b/tests/inference/test_inference_transformers_tokenizer.py @@ -0,0 +1,64 @@ +"""Tokenizer loading in ``inference_transformers`` (issue #110). + +Covers the ``trust_remote_code`` argument reaching ``AutoTokenizer.from_pretrained`` +and the documented precedence where ``tokenizer_kwargs`` override explicit params. +""" + +from unittest.mock import MagicMock + +import pytest + +import llmsql.inference.inference_transformers as it + + +class _StopLoading(Exception): + pass + + +def _captured_tokenizer_kwargs(monkeypatch, **kwargs): + captured = {} + + def fake_tok_from_pretrained(name, **load_kwargs): + captured["name"] = name + captured.update(load_kwargs) + raise _StopLoading + + monkeypatch.setattr( + it.AutoModelForCausalLM, "from_pretrained", lambda *a, **k: MagicMock() + ) + monkeypatch.setattr(it.AutoTokenizer, "from_pretrained", fake_tok_from_pretrained) + with pytest.raises(_StopLoading): + it.inference_transformers("some/model", **kwargs) + return captured + + +def test_tokenizer_respects_trust_remote_code_false(monkeypatch): + captured = _captured_tokenizer_kwargs(monkeypatch, trust_remote_code=False) + assert captured["name"] == "some/model" + assert captured["trust_remote_code"] is False + + +def test_tokenizer_respects_trust_remote_code_true(monkeypatch): + captured = _captured_tokenizer_kwargs(monkeypatch, trust_remote_code=True) + assert captured["trust_remote_code"] is True + + +def test_tokenizer_kwargs_override_explicit_trust_remote_code(monkeypatch): + captured = _captured_tokenizer_kwargs( + monkeypatch, + trust_remote_code=True, + tokenizer_kwargs={"trust_remote_code": False}, + ) + assert captured["trust_remote_code"] is False + + +def test_tokenizer_kwargs_override_padding_side_default(monkeypatch): + captured = _captured_tokenizer_kwargs( + monkeypatch, tokenizer_kwargs={"padding_side": "right"} + ) + assert captured["padding_side"] == "right" + + +def test_tokenizer_padding_side_defaults_to_left(monkeypatch): + captured = _captured_tokenizer_kwargs(monkeypatch) + assert captured["padding_side"] == "left" diff --git a/tests/inference/test_inference_vllm_args.py b/tests/inference/test_inference_vllm_args.py new file mode 100644 index 0000000..3603747 --- /dev/null +++ b/tests/inference/test_inference_vllm_args.py @@ -0,0 +1,91 @@ +"""Mocked tests for the args ``inference_vllm`` forwards to ``vllm.LLM`` (issue #110). + +These don't need a GPU: ``vllm.LLM`` and the benchmark data loaders are mocked. +They're skipped entirely when the optional ``vllm`` extra isn't installed. +""" + +import json +from pathlib import Path +from unittest.mock import MagicMock + +import pytest + +pytest.importorskip("vllm") + +import llmsql.inference.inference_vllm as mod + +questions = [ + {"question_id": "q1", "table_id": "t1", "question": "Select name from students;"}, +] +tables = [ + { + "table_id": "t1", + "header": ["id", "name"], + "types": ["int", "str"], + "rows": [[1, "Alice"]], + }, +] + + +def _captured_llm_kwargs(monkeypatch, tmp_path, **kwargs): + q_file = tmp_path / "questions.jsonl" + t_file = tmp_path / "tables.jsonl" + out_file = tmp_path / "out.jsonl" + q_file.write_text("\n".join(json.dumps(q) for q in questions)) + t_file.write_text("\n".join(json.dumps(t) for t in tables)) + + monkeypatch.setattr( + mod, + "load_jsonl", + lambda path: [json.loads(line) for line in Path(path).read_text().splitlines()], + ) + monkeypatch.setattr(mod, "overwrite_jsonl", lambda path: Path(path).touch()) + monkeypatch.setattr(mod, "save_jsonl_lines", lambda path, lines: Path(path).touch()) + monkeypatch.setattr(mod, "choose_prompt_builder", lambda shots: lambda *a: "PROMPT") + + captured = {} + fake_llm = MagicMock() + fake_llm.generate.return_value = [MagicMock(outputs=[MagicMock(text="SELECT 1")])] + + def fake_LLM(**llm_kwargs): + captured.update(llm_kwargs) + return fake_llm + + monkeypatch.setattr(mod, "LLM", fake_LLM) + + call_kwargs = { + "model_name": "dummy-model", + "output_file": str(out_file), + "workdir_path": str(tmp_path), + "num_fewshots": 1, + "batch_size": 1, + "max_new_tokens": 8, + "temperature": 0.0, + } + call_kwargs.update(kwargs) + + mod.inference_vllm(**call_kwargs) + return captured + + +def test_hf_token_reaches_llm_as_hf_token(monkeypatch, tmp_path): + captured = _captured_llm_kwargs(monkeypatch, tmp_path, hf_token="secret-token") + assert captured["hf_token"] == "secret-token" + assert "token" not in captured + + +def test_hf_token_defaults_to_none_when_unset(monkeypatch, tmp_path): + monkeypatch.delenv("HF_TOKEN", raising=False) + captured = _captured_llm_kwargs(monkeypatch, tmp_path) + assert "hf_token" not in captured + + +def test_llm_kwargs_override_explicit_params(monkeypatch, tmp_path): + captured = _captured_llm_kwargs( + monkeypatch, + tmp_path, + trust_remote_code=True, + llm_kwargs={"trust_remote_code": False, "max_model_len": 2048}, + ) + assert captured["trust_remote_code"] is False + assert captured["max_model_len"] == 2048