mirror of
https://github.com/explosion/spaCy.git
synced 2025-11-11 05:19:52 +03:00
In order to support Python 3.13, we had to migrate to Cython 3.0. This caused some tricky interaction with our Pydantic usage, because Cython 3 uses the from __future__ import annotations semantics, which causes type annotations to be saved as strings. The end result is that we can't have Language.factory decorated functions in Cython modules anymore, as the Language.factory decorator expects to inspect the signature of the functions and build a Pydantic model. If the function is implemented in Cython, an error is raised because the type is not resolved. To address this I've moved the factory functions into a new module, spacy.pipeline.factories. I've added __getattr__ importlib hooks to the previous locations, in case anyone was importing these functions directly. The change should have no backwards compatibility implications. Along the way I've also refactored the registration of functions for the config. Previously these ran as import-time side-effects, using the registry decorator. I've created instead a new module spacy.registrations. When the registry is accessed it calls a function ensure_populated(), which cases the registrations to occur. I've made a similar change to the Language.factory registrations in the new spacy.pipeline.factories module. I want to remove these import-time side-effects so that we can speed up the loading time of the library, which can be especially painful on the CLI. I also find that I'm often working to track down the implementations of functions referenced by strings in the config. Having the registrations all happen in one place will make this easier. With these changes I've fortunately avoided the need to migrate to Pydantic v2 properly --- we're still using the v1 compatibility shim. We might not be able to hold out forever though: Pydantic (reasonably) aren't actively supporting the v1 shims. I put a lot of work into v2 migration when investigating the 3.13 support, and it's definitely challenging. In any case, it's a relief that we don't have to do the v2 migration at the same time as the Cython 3.0/Python 3.13 support.
75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
from typing import List, Tuple, cast
|
|
|
|
from thinc.api import (
|
|
Linear,
|
|
Logistic,
|
|
Maxout,
|
|
Model,
|
|
chain,
|
|
concatenate,
|
|
glorot_uniform_init,
|
|
list2ragged,
|
|
reduce_first,
|
|
reduce_last,
|
|
reduce_max,
|
|
reduce_mean,
|
|
with_getitem,
|
|
)
|
|
from thinc.types import Floats2d, Ragged
|
|
|
|
from ...tokens import Doc
|
|
from ...util import registry
|
|
from ..extract_spans import extract_spans
|
|
|
|
|
|
def build_linear_logistic(nO=None, nI=None) -> Model[Floats2d, Floats2d]:
|
|
"""An output layer for multi-label classification. It uses a linear layer
|
|
followed by a logistic activation.
|
|
"""
|
|
return chain(Linear(nO=nO, nI=nI, init_W=glorot_uniform_init), Logistic())
|
|
|
|
|
|
def build_mean_max_reducer(hidden_size: int) -> Model[Ragged, Floats2d]:
|
|
"""Reduce sequences by concatenating their mean and max pooled vectors,
|
|
and then combine the concatenated vectors with a hidden layer.
|
|
"""
|
|
return chain(
|
|
concatenate(
|
|
cast(Model[Ragged, Floats2d], reduce_last()),
|
|
cast(Model[Ragged, Floats2d], reduce_first()),
|
|
reduce_mean(),
|
|
reduce_max(),
|
|
),
|
|
Maxout(nO=hidden_size, normalize=True, dropout=0.0),
|
|
)
|
|
|
|
|
|
def build_spancat_model(
|
|
tok2vec: Model[List[Doc], List[Floats2d]],
|
|
reducer: Model[Ragged, Floats2d],
|
|
scorer: Model[Floats2d, Floats2d],
|
|
) -> Model[Tuple[List[Doc], Ragged], Floats2d]:
|
|
"""Build a span categorizer model, given a token-to-vector model, a
|
|
reducer model to map the sequence of vectors for each span down to a single
|
|
vector, and a scorer model to map the vectors to probabilities.
|
|
|
|
tok2vec (Model[List[Doc], List[Floats2d]]): The tok2vec model.
|
|
reducer (Model[Ragged, Floats2d]): The reducer model.
|
|
scorer (Model[Floats2d, Floats2d]): The scorer model.
|
|
"""
|
|
model = chain(
|
|
cast(
|
|
Model[Tuple[List[Doc], Ragged], Tuple[Ragged, Ragged]],
|
|
with_getitem(
|
|
0, chain(tok2vec, cast(Model[List[Floats2d], Ragged], list2ragged()))
|
|
),
|
|
),
|
|
extract_spans(),
|
|
reducer,
|
|
scorer,
|
|
)
|
|
model.set_ref("tok2vec", tok2vec)
|
|
model.set_ref("reducer", reducer)
|
|
model.set_ref("scorer", scorer)
|
|
return model
|