Skip to content

Commit 8c432de

Browse files
Add a kw-based function to create a builder (#315)
A new function using kwargs has been added, called `from_config`. The old function has been renamed `from_configpath` instead. This approach has been favored over creating yet another data structure in python. The validation stays in Rust, as checked in the pytests. Closes #314
1 parent 8d99cd0 commit 8c432de

16 files changed

Lines changed: 799 additions & 65 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ setup:
1111

1212
.PHONY: build-py
1313
build-py:
14-
maturin develop -m encoderfile-py/Cargo.toml
14+
uv run maturin develop -m encoderfile-py/Cargo.toml
1515

1616
.PHONY: stubtest
1717
stubtest:

allowlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# ignore missing __all__ in encoderfile
22
encoderfile.encoderfile.__all__
3+
# encoderfile.encoderfile.EncoderfileBuilder.from_config

encoderfile-py/python/encoderfile/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# type: ignore
22
from .encoderfile import * # noqa # type: ignore
3-
3+
from .enums import * # noqa # type: ignore
44

55
__doc__ = encoderfile.__doc__ # noqa # type: ignore
66
if hasattr(encoderfile, "__all__"): # noqa # type: ignore

encoderfile-py/python/encoderfile/encoderfile.pyi

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,62 @@
11
from typing import Optional, final
2+
from .enums import ModelType
3+
4+
@final
5+
class TargetSpec:
6+
arch: str
7+
os: str
8+
abi: str
9+
10+
@staticmethod
11+
def parse(spec: str) -> "TargetSpec": ...
212

313
@final
414
class EncoderfileBuilder:
5-
@classmethod
15+
@staticmethod
616
def from_config(
7-
cls,
817
config: str,
918
) -> "EncoderfileBuilder": ...
19+
@staticmethod
20+
def from_dict(
21+
*,
22+
name: str,
23+
version: Optional[str] = "0.1.0",
24+
model_type: ModelType,
25+
path: str,
26+
output_path: Optional[str] = None,
27+
cache_dir: Optional[str] = None,
28+
base_binary_path: Optional[str] = None,
29+
transform: Optional[str] = None,
30+
lua_libs: Optional[list[str]] = None,
31+
tokenizer: Optional[TokenizerBuildConfig] = None,
32+
validate_transform: bool = True,
33+
target: Optional[str] = None,
34+
) -> "EncoderfileBuilder": ...
1035
def build(
1136
self,
12-
working_dir: Optional[str] = None,
37+
workdir: Optional[str] = None,
1338
version: Optional[str] = None,
1439
no_download: bool = False,
1540
): ...
1641

42+
@final
43+
class TokenizerBuildConfig:
44+
pad_strategy: Optional[str]
45+
truncation_side: Optional[str]
46+
truncation_strategy: Optional[str]
47+
max_length: Optional[int]
48+
stride: Optional[int]
49+
50+
@staticmethod
51+
def new(
52+
*,
53+
pad_strategy: Optional[str] = None,
54+
truncation_side: Optional[str] = None,
55+
truncation_strategy: Optional[str] = None,
56+
max_length: Optional[int] = None,
57+
stride: Optional[int] = None,
58+
) -> "TokenizerBuildConfig": ...
59+
1760
@final
1861
class ModelConfig:
1962
model_type: str
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from enum import StrEnum
2+
3+
4+
class ModelType(StrEnum):
5+
Embedding = "embedding"
6+
SequenceClassification = "sequence_classification"
7+
TokenClassification = "token_classification"
8+
SentenceEmbedding = "sentence_embedding"
9+
10+
11+
class TokenizerTruncationSide(StrEnum):
12+
Left = "left"
13+
Right = "right"
14+
15+
16+
class TokenizerTruncationStrategy(StrEnum):
17+
LongestFirst = "longest_first"
18+
OnlyFirst = "only_first"
19+
OnlySecond = "only_second"
20+
21+
22+
class TokenizerPadStrategy(StrEnum):
23+
BatchLongest = "batch_longest"
24+
Fixed = "fixed"

encoderfile-py/python/tests/assets/test_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ encoderfile:
22
name: my-model-2
33
path: models/token_classification
44
model_type: token_classification
5-
output_path: ./test-model.encoderfile
5+
output_path: /tmp/test-model.encoderfile
66
transform: |
77
--- Applies a softmax across token classification logits.
88
--- Each token classification is normalized independently.

encoderfile-py/python/tests/assets/test_config_lua.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ encoderfile:
22
name: my-model-2-lua
33
path: models/token_classification
44
model_type: token_classification
5-
output_path: ./test-model.encoderfile
5+
output_path: /tmp/test-model-lua.encoderfile
66
lua_libs:
77
- table
88
- math

encoderfile-py/python/tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
2+
from pathlib import Path
23

34
import yaml
5+
import json
46

57

68
def asset_path(filename: str) -> str:
@@ -22,3 +24,13 @@ def load_yaml_asset(filename):
2224
return yaml.safe_load(f)
2325
else:
2426
raise ValueError("Only yaml files are supported for this fixture.")
27+
28+
29+
def load_json(path: Path):
30+
"""
31+
Loads a json asset file from the assets directory.
32+
"""
33+
if path.suffix != ".json":
34+
raise ValueError("Only json files are supported for this fixture.")
35+
with open(path, "r", encoding="utf-8") as f:
36+
return json.load(f)

0 commit comments

Comments
 (0)