@modular-prompt/driver 0.16.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -10
- package/dist/cache-controller.d.ts +4 -0
- package/dist/cache-controller.d.ts.map +1 -1
- package/dist/driver-registry/config-based-factory.d.ts.map +1 -1
- package/dist/driver-registry/config-based-factory.js +6 -0
- package/dist/driver-registry/config-based-factory.js.map +1 -1
- package/dist/driver-registry/factory-helper.d.ts.map +1 -1
- package/dist/driver-registry/factory-helper.js +9 -2
- package/dist/driver-registry/factory-helper.js.map +1 -1
- package/dist/driver-registry/index.d.ts +1 -1
- package/dist/driver-registry/index.d.ts.map +1 -1
- package/dist/driver-registry/types.d.ts +15 -1
- package/dist/driver-registry/types.d.ts.map +1 -1
- package/dist/formatter/converter.d.ts.map +1 -1
- package/dist/formatter/converter.js +31 -2
- package/dist/formatter/converter.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/local-inference/adapters.d.ts +6 -0
- package/dist/local-inference/adapters.d.ts.map +1 -1
- package/dist/local-inference/driver.d.ts.map +1 -1
- package/dist/local-inference/driver.js +45 -24
- package/dist/local-inference/driver.js.map +1 -1
- package/dist/local-inference/process-client.d.ts +4 -2
- package/dist/local-inference/process-client.d.ts.map +1 -1
- package/dist/local-inference/process-client.js +24 -8
- package/dist/local-inference/process-client.js.map +1 -1
- package/dist/local-inference/process-communication.d.ts +9 -2
- package/dist/local-inference/process-communication.d.ts.map +1 -1
- package/dist/local-inference/process-communication.js +37 -5
- package/dist/local-inference/process-communication.js.map +1 -1
- package/dist/local-inference/protocol.d.ts +4 -0
- package/dist/local-inference/protocol.d.ts.map +1 -1
- package/dist/local-inference/request-queue.d.ts +1 -1
- package/dist/local-inference/request-queue.d.ts.map +1 -1
- package/dist/local-inference/request-queue.js +26 -7
- package/dist/local-inference/request-queue.js.map +1 -1
- package/dist/local-inference/stream-utils.d.ts +6 -0
- package/dist/local-inference/stream-utils.d.ts.map +1 -1
- package/dist/local-inference/stream-utils.js.map +1 -1
- package/dist/mlx-ml/mlx-cache-controller.d.ts +9 -0
- package/dist/mlx-ml/mlx-cache-controller.d.ts.map +1 -1
- package/dist/mlx-ml/mlx-cache-controller.js +158 -33
- package/dist/mlx-ml/mlx-cache-controller.js.map +1 -1
- package/dist/mlx-ml/mlx-cache-support.d.ts +2 -2
- package/dist/mlx-ml/mlx-cache-support.d.ts.map +1 -1
- package/dist/mlx-ml/mlx-cache-support.js +8 -3
- package/dist/mlx-ml/mlx-cache-support.js.map +1 -1
- package/dist/mlx-ml/mlx-driver.d.ts +0 -1
- package/dist/mlx-ml/mlx-driver.d.ts.map +1 -1
- package/dist/mlx-ml/mlx-driver.js +1 -8
- package/dist/mlx-ml/mlx-driver.js.map +1 -1
- package/dist/mlx-ml/process/index.d.ts +1 -1
- package/dist/mlx-ml/process/index.d.ts.map +1 -1
- package/dist/mlx-ml/process/index.js +2 -2
- package/dist/mlx-ml/process/index.js.map +1 -1
- package/dist/models-config/index.d.ts +1 -1
- package/dist/models-config/index.d.ts.map +1 -1
- package/dist/models-config/index.js +1 -1
- package/dist/models-config/index.js.map +1 -1
- package/dist/models-config/resolve.d.ts +9 -1
- package/dist/models-config/resolve.d.ts.map +1 -1
- package/dist/models-config/resolve.js +94 -2
- package/dist/models-config/resolve.js.map +1 -1
- package/dist/models-config/types.d.ts +3 -1
- package/dist/models-config/types.d.ts.map +1 -1
- package/dist/pytorch/process/index.d.ts +4 -2
- package/dist/pytorch/process/index.d.ts.map +1 -1
- package/dist/pytorch/process/index.js +24 -7
- package/dist/pytorch/process/index.js.map +1 -1
- package/dist/pytorch/pytorch-cache-controller.d.ts +84 -0
- package/dist/pytorch/pytorch-cache-controller.d.ts.map +1 -0
- package/dist/pytorch/pytorch-cache-controller.js +742 -0
- package/dist/pytorch/pytorch-cache-controller.js.map +1 -0
- package/dist/pytorch/pytorch-cache-support.d.ts +23 -0
- package/dist/pytorch/pytorch-cache-support.d.ts.map +1 -0
- package/dist/pytorch/pytorch-cache-support.js +47 -0
- package/dist/pytorch/pytorch-cache-support.js.map +1 -0
- package/dist/pytorch/pytorch-driver.d.ts +8 -1
- package/dist/pytorch/pytorch-driver.d.ts.map +1 -1
- package/dist/pytorch/pytorch-driver.js +40 -0
- package/dist/pytorch/pytorch-driver.js.map +1 -1
- package/dist/runtime/check.d.ts.map +1 -1
- package/dist/runtime/check.js +8 -6
- package/dist/runtime/check.js.map +1 -1
- package/dist/runtime/index.d.ts +2 -2
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +2 -2
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/manifest-core.d.mts +1 -0
- package/dist/runtime/manifest-core.mjs +1 -0
- package/dist/runtime/manifest-core.mjs.map +1 -1
- package/dist/runtime/manifest.d.ts +2 -0
- package/dist/runtime/manifest.d.ts.map +1 -1
- package/dist/runtime/manifest.js.map +1 -1
- package/dist/runtime/paths-core.d.mts +15 -1
- package/dist/runtime/paths-core.d.mts.map +1 -1
- package/dist/runtime/paths-core.mjs +50 -5
- package/dist/runtime/paths-core.mjs.map +1 -1
- package/dist/runtime/paths.d.ts +2 -2
- package/dist/runtime/paths.d.ts.map +1 -1
- package/dist/runtime/paths.js +2 -2
- package/dist/runtime/paths.js.map +1 -1
- package/dist/runtime/pytorch-template-core.d.mts +11 -0
- package/dist/runtime/pytorch-template-core.d.mts.map +1 -0
- package/dist/runtime/pytorch-template-core.mjs +54 -0
- package/dist/runtime/pytorch-template-core.mjs.map +1 -0
- package/dist/runtime/setup-commands-core.d.mts +3 -0
- package/dist/runtime/setup-commands-core.d.mts.map +1 -1
- package/dist/runtime/setup-commands-core.mjs +4 -0
- package/dist/runtime/setup-commands-core.mjs.map +1 -1
- package/dist/runtime/setup-commands.d.ts +1 -1
- package/dist/runtime/setup-commands.d.ts.map +1 -1
- package/dist/runtime/setup-commands.js +1 -1
- package/dist/runtime/setup-commands.js.map +1 -1
- package/docs/DRIVER_API.md +455 -0
- package/docs/LOCAL_MODEL_SETUP.md +765 -0
- package/docs/mlx-api-selection.md +301 -0
- package/package.json +9 -5
- package/scripts/runtime-cli.bin.test.ts +142 -0
- package/scripts/runtime-cli.js +305 -35
- package/scripts/runtime-cli.test.ts +163 -0
- package/src/mlx-ml/python/__main__.py +1 -1
- package/src/mlx-ml/python/backends/base.py +88 -18
- package/src/mlx-ml/python/backends/mlx_lm.py +28 -3
- package/src/mlx-ml/python/backends/mlx_vlm.py +679 -2
- package/src/mlx-ml/python/handlers/cache.py +4 -0
- package/src/mlx-ml/python/handlers/generate.py +33 -10
- package/src/mlx-ml/python/handlers/tokenize.py +1 -4
- package/src/mlx-ml/python/pyproject.toml +1 -1
- package/src/mlx-ml/python/server.py +2 -0
- package/src/mlx-ml/python/uv.lock +8 -8
- package/src/pytorch/templates/cpu-minimal/backends/base.py +139 -0
- package/src/pytorch/templates/cpu-minimal/backends/transformers_lm.py +1167 -0
- package/src/pytorch/{python → templates/cpu-minimal}/handlers/__init__.py +1 -0
- package/src/pytorch/templates/cpu-minimal/handlers/cache.py +88 -0
- package/src/pytorch/templates/cpu-minimal/handlers/generate.py +157 -0
- package/src/pytorch/{python → templates/cpu-minimal}/pyproject.toml +2 -2
- package/src/pytorch/{python → templates/cpu-minimal}/server.py +20 -2
- package/src/pytorch/templates/cpu-minimal/tests/test_cache_handler.py +284 -0
- package/src/pytorch/templates/cpu-minimal/tests/test_capabilities.py +14 -0
- package/src/pytorch/templates/cpu-minimal/tests/test_server.py +141 -0
- package/src/pytorch/templates/cpu-minimal/tests/test_transformers_errors.py +89 -0
- package/src/pytorch/templates/cpu-minimal/tests/test_transformers_lm_cache.py +554 -0
- package/src/pytorch/templates/cpu-minimal/utils/__init__.py +0 -0
- package/src/pytorch/{python → templates/cpu-minimal}/utils/token_utils.py +2 -2
- package/src/pytorch/templates/cpu-minimal/utils/transformers_errors.py +54 -0
- package/src/pytorch/{python → templates/cpu-minimal}/uv.lock +149 -109
- package/src/pytorch/templates/cuda/__main__.py +19 -0
- package/src/pytorch/templates/cuda/backends/__init__.py +3 -0
- package/src/pytorch/{python → templates/cuda}/backends/base.py +54 -6
- package/src/pytorch/templates/cuda/backends/transformers_lm.py +379 -0
- package/src/pytorch/templates/cuda/handlers/__init__.py +7 -0
- package/src/pytorch/templates/cuda/handlers/cache.py +93 -0
- package/src/pytorch/templates/cuda/handlers/cancel.py +53 -0
- package/src/pytorch/templates/cuda/handlers/capabilities.py +6 -0
- package/src/pytorch/templates/cuda/handlers/completion.py +15 -0
- package/src/pytorch/templates/cuda/handlers/format_test.py +70 -0
- package/src/pytorch/templates/cuda/handlers/generate.py +152 -0
- package/src/pytorch/templates/cuda/handlers/render.py +40 -0
- package/src/pytorch/templates/cuda/handlers/tokenize.py +63 -0
- package/src/pytorch/templates/cuda/pyproject.toml +37 -0
- package/src/pytorch/templates/cuda/server.py +158 -0
- package/src/pytorch/templates/cuda/tests/__init__.py +0 -0
- package/src/pytorch/templates/cuda/tests/test_cache_handler.py +207 -0
- package/src/pytorch/templates/cuda/tests/test_capabilities.py +14 -0
- package/src/pytorch/templates/cuda/tests/test_server.py +145 -0
- package/src/pytorch/templates/cuda/tests/test_transformers_errors.py +89 -0
- package/src/pytorch/templates/cuda/tests/test_transformers_lm_cache.py +288 -0
- package/src/pytorch/templates/cuda/utils/__init__.py +0 -0
- package/src/pytorch/templates/cuda/utils/chat_template_constraints.py +164 -0
- package/src/pytorch/templates/cuda/utils/prompt_builder.py +54 -0
- package/src/pytorch/templates/cuda/utils/template_render.py +80 -0
- package/src/pytorch/templates/cuda/utils/token_utils.py +376 -0
- package/src/pytorch/templates/cuda/utils/transformers_errors.py +54 -0
- package/src/pytorch/templates/cuda/uv.lock +734 -0
- package/src/pytorch/python/backends/transformers_lm.py +0 -127
- package/src/pytorch/python/handlers/generate.py +0 -68
- /package/src/pytorch/{python → templates/cpu-minimal}/__main__.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/backends/__init__.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/handlers/cancel.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/handlers/capabilities.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/handlers/completion.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/handlers/format_test.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/handlers/render.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/handlers/tokenize.py +0 -0
- /package/src/pytorch/{python/utils → templates/cpu-minimal/tests}/__init__.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/utils/chat_template_constraints.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/utils/prompt_builder.py +0 -0
- /package/src/pytorch/{python → templates/cpu-minimal}/utils/template_render.py +0 -0
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import os
|
|
4
|
-
from dataclasses import dataclass
|
|
5
|
-
from threading import Thread
|
|
6
|
-
from typing import Any, Iterator
|
|
7
|
-
|
|
8
|
-
import torch
|
|
9
|
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
10
|
-
|
|
11
|
-
from backends.base import ModelBackend
|
|
12
|
-
from utils.token_utils import is_eod_token
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
@dataclass
|
|
16
|
-
class StreamChunk:
|
|
17
|
-
text: str
|
|
18
|
-
prompt_tokens: int | None = None
|
|
19
|
-
generation_tokens: int | None = None
|
|
20
|
-
finish_reason: str | None = None
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
class TransformersLmBackend(ModelBackend):
|
|
24
|
-
"""Transformers causal LM backend (text-only, CPU-first)."""
|
|
25
|
-
|
|
26
|
-
def __init__(self, device: str | None = None) -> None:
|
|
27
|
-
self.model: Any | None = None
|
|
28
|
-
self.tokenizer: Any | None = None
|
|
29
|
-
self._device_name = device or os.environ.get("PYTORCH_DEVICE", "cpu")
|
|
30
|
-
self._device = torch.device(self._device_name)
|
|
31
|
-
|
|
32
|
-
def load(self, model_name: str) -> None:
|
|
33
|
-
trust_remote_code = os.environ.get("PYTORCH_TRUST_REMOTE_CODE", "").lower() in (
|
|
34
|
-
"1",
|
|
35
|
-
"true",
|
|
36
|
-
"yes",
|
|
37
|
-
)
|
|
38
|
-
self.tokenizer = AutoTokenizer.from_pretrained(
|
|
39
|
-
model_name,
|
|
40
|
-
trust_remote_code=trust_remote_code,
|
|
41
|
-
)
|
|
42
|
-
if self.tokenizer.pad_token is None and self.tokenizer.eos_token is not None:
|
|
43
|
-
self.tokenizer.pad_token = self.tokenizer.eos_token
|
|
44
|
-
|
|
45
|
-
dtype = torch.float32 if self._device.type == "cpu" else torch.float16
|
|
46
|
-
self.model = AutoModelForCausalLM.from_pretrained(
|
|
47
|
-
model_name,
|
|
48
|
-
trust_remote_code=trust_remote_code,
|
|
49
|
-
torch_dtype=dtype,
|
|
50
|
-
)
|
|
51
|
-
self.model.to(self._device)
|
|
52
|
-
self.model.eval()
|
|
53
|
-
|
|
54
|
-
def get_tokenizer(self) -> Any:
|
|
55
|
-
return self.tokenizer
|
|
56
|
-
|
|
57
|
-
def stream_generate(
|
|
58
|
-
self,
|
|
59
|
-
prompt: str | list[int],
|
|
60
|
-
options: dict,
|
|
61
|
-
images: list | None = None,
|
|
62
|
-
prompt_cache: list | None = None,
|
|
63
|
-
) -> Iterator[StreamChunk]:
|
|
64
|
-
if images:
|
|
65
|
-
raise ValueError("TransformersLmBackend does not support vision input")
|
|
66
|
-
if self.model is None or self.tokenizer is None:
|
|
67
|
-
raise RuntimeError("Model is not loaded")
|
|
68
|
-
|
|
69
|
-
final_options = {"max_tokens": 256, **options}
|
|
70
|
-
max_new_tokens = int(final_options.pop("max_tokens", 256))
|
|
71
|
-
temperature = float(final_options.pop("temperature", 1.0))
|
|
72
|
-
top_p = final_options.pop("top_p", None)
|
|
73
|
-
top_k = final_options.pop("top_k", None)
|
|
74
|
-
|
|
75
|
-
if isinstance(prompt, list):
|
|
76
|
-
input_ids = torch.tensor([prompt], device=self._device)
|
|
77
|
-
prompt_token_count = len(prompt)
|
|
78
|
-
else:
|
|
79
|
-
encoded = self.tokenizer(prompt, return_tensors="pt")
|
|
80
|
-
input_ids = encoded["input_ids"].to(self._device)
|
|
81
|
-
prompt_token_count = int(input_ids.shape[-1])
|
|
82
|
-
|
|
83
|
-
do_sample = temperature > 0
|
|
84
|
-
gen_kwargs: dict[str, Any] = {
|
|
85
|
-
"input_ids": input_ids,
|
|
86
|
-
"max_new_tokens": max_new_tokens,
|
|
87
|
-
"do_sample": do_sample,
|
|
88
|
-
}
|
|
89
|
-
if do_sample:
|
|
90
|
-
gen_kwargs["temperature"] = temperature
|
|
91
|
-
if top_p is not None:
|
|
92
|
-
gen_kwargs["top_p"] = float(top_p)
|
|
93
|
-
if top_k is not None:
|
|
94
|
-
gen_kwargs["top_k"] = int(top_k)
|
|
95
|
-
|
|
96
|
-
streamer = TextIteratorStreamer(
|
|
97
|
-
self.tokenizer,
|
|
98
|
-
skip_special_tokens=True,
|
|
99
|
-
skip_prompt=True,
|
|
100
|
-
)
|
|
101
|
-
gen_kwargs["streamer"] = streamer
|
|
102
|
-
|
|
103
|
-
thread = Thread(target=self.model.generate, kwargs=gen_kwargs)
|
|
104
|
-
thread.start()
|
|
105
|
-
|
|
106
|
-
generation_tokens = 0
|
|
107
|
-
for text in streamer:
|
|
108
|
-
generation_tokens += 1
|
|
109
|
-
chunk = StreamChunk(
|
|
110
|
-
text=text,
|
|
111
|
-
prompt_tokens=prompt_token_count if generation_tokens == 1 else None,
|
|
112
|
-
generation_tokens=generation_tokens if generation_tokens == 1 else None,
|
|
113
|
-
)
|
|
114
|
-
if is_eod_token(chunk, self.tokenizer):
|
|
115
|
-
chunk.finish_reason = "stop"
|
|
116
|
-
yield chunk
|
|
117
|
-
break
|
|
118
|
-
yield chunk
|
|
119
|
-
|
|
120
|
-
thread.join()
|
|
121
|
-
|
|
122
|
-
def supports_vision(self) -> bool:
|
|
123
|
-
return False
|
|
124
|
-
|
|
125
|
-
@property
|
|
126
|
-
def model_kind(self) -> str:
|
|
127
|
-
return "lm"
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import json
|
|
4
|
-
|
|
5
|
-
from backends.base import ModelBackend
|
|
6
|
-
from handlers.cancel import poll_cancel
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def _stream_to_stdout(
|
|
10
|
-
backend: ModelBackend,
|
|
11
|
-
prompt: str | list[int],
|
|
12
|
-
options: dict,
|
|
13
|
-
images: list | None = None,
|
|
14
|
-
primer: str | None = None,
|
|
15
|
-
) -> None:
|
|
16
|
-
if images:
|
|
17
|
-
raise ValueError("PyTorch LIP backend does not support images in Phase 6")
|
|
18
|
-
|
|
19
|
-
if primer is not None:
|
|
20
|
-
print(primer, end="", flush=True)
|
|
21
|
-
|
|
22
|
-
last_response = None
|
|
23
|
-
for response in backend.stream_generate(prompt, options, images):
|
|
24
|
-
if poll_cancel():
|
|
25
|
-
break
|
|
26
|
-
print(response.text.replace("\0", "").replace("\x1e", ""), end="", flush=True)
|
|
27
|
-
last_response = response
|
|
28
|
-
|
|
29
|
-
meta: dict = {}
|
|
30
|
-
if last_response is not None:
|
|
31
|
-
if last_response.prompt_tokens is not None:
|
|
32
|
-
meta["prompt_tokens"] = last_response.prompt_tokens
|
|
33
|
-
if last_response.generation_tokens is not None:
|
|
34
|
-
meta["generation_tokens"] = last_response.generation_tokens
|
|
35
|
-
|
|
36
|
-
if meta:
|
|
37
|
-
print(f"\x1e__META__:{json.dumps(meta)}", end="\0", flush=True)
|
|
38
|
-
else:
|
|
39
|
-
print("", end="\0", flush=True)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def handle_generate(
|
|
43
|
-
backend: ModelBackend,
|
|
44
|
-
prompt: str | list[int],
|
|
45
|
-
options: dict | None = None,
|
|
46
|
-
images: list | None = None,
|
|
47
|
-
max_image_size: int = 768,
|
|
48
|
-
primer: str | None = None,
|
|
49
|
-
cache_path: str | None = None,
|
|
50
|
-
cache_trim_tokens: int | None = None,
|
|
51
|
-
) -> None:
|
|
52
|
-
"""LIP generate: 整形済み prompt のストリーム推論(KV キャッシュ非対応)"""
|
|
53
|
-
if cache_path or cache_trim_tokens is not None:
|
|
54
|
-
raise ValueError("PyTorch LIP backend does not support prompt caching")
|
|
55
|
-
|
|
56
|
-
if options is None:
|
|
57
|
-
options = {}
|
|
58
|
-
|
|
59
|
-
final_options = dict(options)
|
|
60
|
-
final_options.pop("trust_remote_code", None)
|
|
61
|
-
|
|
62
|
-
_stream_to_stdout(
|
|
63
|
-
backend,
|
|
64
|
-
prompt,
|
|
65
|
-
final_options,
|
|
66
|
-
images=images,
|
|
67
|
-
primer=primer,
|
|
68
|
-
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|