Repository navigation
Closed
Labels
ready-for-agentScoped and ready for a contributor or agent to pick up
Milestone
Description
Parent: #148 · Epic: #126 · Blocked by: #128, #143
Work on branch 148-load-an-encoderdetector-explicitly-and-call-detect; commit the changes and close the ticket.
Problem
TransformerDetector.__init__ downloads the tokenizer and model inside the constructor, so a service cannot build a detector from objects it already holds, and every option is a keyword whose conflicts are checked at runtime.
Current behavior
lettucedetect/detectors/transformer.py:28-83constructor:batch_size < 1raisesValueError(:58),AutoTokenizer.from_pretrained(:62),AutoModelForTokenClassification.from_pretrained(:63).- Scoring:
_predict_single(:241),_predict_chunked(:341,max()over passage groups),_group_passages_into_chunks(:84),_build_spans_from_tokens(:383). - Spans carry
confidence; noscore,usageorwarnings.
Steps
- Add
EncoderConfig(frozen pydantic,extra="forbid"):max_length=4096,batch_size=16,lang="en". Unknown option →ConfigurationError(invalid_config); unknown language →invalid_language;batch_size < 1→invalid_batch_size; each withparam. - Add
EncoderDetector(model, tokenizer, typer=None, *, config=None, model_id=None, model_version=None). It downloads nothing, reads no environment variable, writes no file. - Implement
_detectforSourcesInputandPromptInputby moving the scoring code (private method names stay) behind it.scoreis the maximum token probability of the span;textequals the answer between offsets. - Plug into the Add the reusable detector contract suite #143 contract suite with a stub model and the
local_wordpiece_tokenizerfixture. - Guard the scoring path with one private
threading.RLock:_detecttakes it around tokenizing, the forward pass and aggregation, so concurrentdetectandadetectcalls on one instance run one at a time. Reason:transformersrewrites the fast tokenizer's truncation and padding settings on every call, so concurrent calls on one tokenizer can return wrong tokens or raiseValueError(reproduced locally by forcing thread switches; rare at default interpreter settings), and threads sharing one model gain little on a CPU or GPU. It is anRLockso thatdetect_batch(Score EncoderDetector batches in padded groups #177) can fall back to the single-request path while it holds the lock. The lock is created on first use by a private helper, not in__init__, because a detector built with__new__and its attributes set by hand never runs__init__(story 56); a module-level guard lock makes two racing first calls get the same lock. No config field and no public argument. The private scoring methods that existing tests patch don't take the lock themselves. - Add the two concurrency tests below.
Acceptance criteria
- Constructing with a stub model offline in an empty cwd succeeds and leaves it empty.
-
detect/adetectspans equal the Pin today's TransformerDetector output with golden fixtures #128 golden"spans"output for the same inputs, withscore= oldconfidence; multi-group sources are aggregated withmax()as today. -
await adetect(...)equalsdetectfor both request kinds (the model call runs inadetect's worker thread; no override). -
asyncio.gatherover at least 16adetectcalls with different inputs on oneEncoderDetector(stub model, the reallocal_wordpiece_tokenizerfixture, inputs long enough to truncate and pad) returns each call's own result, equal to the same inputs run one by one withdetect, and a stub model that records entries and exits shows no two scoring calls overlapping. - Racing first calls from several threads (released together by a barrier) on a detector built with
__new__and its attributes set by hand create exactly one lock. - Config errors carry the listed codes and
param. -
EncoderDetectorpasses the contract suite. - Existing tests pass; follows CONTRIBUTING "Errors and logging".
Start here
uv run --extra dev pytest tests/test_inference_pytest.py -q
Metadata
Metadata
Assignees
Labels
ready-for-agentScoped and ready for a contributor or agent to pick up
Activity
feat: add EncoderDetector and EncoderConfig with detect spans (#149)