Backends

This page is for the people who write or change a model: what a backend is, how the spaCy one runs a pipeline, and how to change the pipeline of grc_grecy, add another spaCy pipeline as a model, or add a task. To tag texts, read Tagger usage (command line) and API usage (Python).

The backend is the part that turns tokens into predictions. Everything else (the data iterator, the processors, SpaceAfter, --tasks, the formatter) is the same for all of them. There are two kinds, and engines of the second one can be mixed:

PaPie 1.0 (the default)

spaCy

In the package of the model

Backend is not set (a composite tagger, with one engine)

Backend = "spacy"

Tagger

CompositeTagger

SpacyTagger

Models

<papie:repository#variant,task,task>: a model of Hugging Face

<pipeline,task,task>: the name of one spaCy pipeline, that gives all the tasks

Tasks

what the model was trained for

lemma, pos, tag, morph

What the model needs

pie-extended download (optional: it is also downloaded when first used)

pie-extended install-addons (the pipeline, with pip)

get_tagger() reads Backend in the package of the model and builds the matching tagger. The composite tagger takes each task from the engine that is best at it (PaPie 1.0, Sôtêr, a spaCy pipeline): see Combine engines.

How the spaCy backend runs a pipeline

  • Words, not text. The words come from the data iterator, and are given to spaCy as already tokenized documents: the tokenizer of spaCy is never used. The tokenizer of the model has to give what the pipeline was trained on (for GreCy, the apostrophe of an elision is written U+02BC for the model, and your spelling is kept in token: see pie_extended.processes.greek).

  • Tasks are attributes of the tokens. lemma, pos, tag and morph are the attributes lemma_, pos_, tag_ and morph of a spaCy token (morph is _ when it is empty). They are listed in SpacyTagger.EXTRACTORS.

  • Only what is useless is skipped. The components that never give a task (the parser, the named entities…, SpacyTagger.UNUSED_COMPONENTS) are not run, the others are: pipelines differ (the transformer ones of GreCy have a morpheus_lemmatizer that the small ones lack, and skipping it changes lemmas). The gain is small, because the encoder costs far more than the parser.

  • One pipeline per tagger. A pipeline that is not installed raises PipelineNotFound, a FileNotFoundError whose message tells how to install it: the command shows it.

Why words, and not the text?

spaCy could be given the raw text, and tokenize it itself. We send words instead, and it was measured why (GreCy pipelines against the Universal Dependencies treebanks they are trained on, dev sets; the treebanks are non-commercial, so they are not part of the repository):

spaCy’s tokenizer, on the text

Our tokenizer

Sentences with exactly the tokens of the treebank (Perseus, 1137 sentences)

99.6%

99.8%

The tokens are the same for practical purposes, and when they are, the predictions are the same: with grc_perseus_trf on 400 sentences, the lemma (93.52%) and the part of speech (89.58%) are identical whether the model receives the text, our words, or our words with the real spacing (the small pipelines too). So the raw text brings nothing. What it costs is the normalization done before the model:

Elided words of PROIEL (90), grc_proiel_trf

Lemma right

Apostrophe written U+02BC (what we send; your spelling stays in token)

100%

As the treebank writes it (’)

57%

The raw text, tokenized by spaCy

54%

GreCy only understands the elision when its apostrophe is U+02BC, and real texts hardly ever write it so. Sending the text would also give up the tokens that are excluded from tagging, treated, SpaceAfter and our sentences. (Take the absolute numbers with care: the treebanks are the data of the pipelines, and only the differences between the lines are the point.)

Change the pipeline of grc_grecy

The name of the pipeline is written once, in the table of the models (pie_extended/models.py):

Model(name="grc_grecy", ..., models="<grc_perseus_trf,lemma,morph,pos,tag>", process="greek:grecy", backend="spacy")

It is the pipeline that pie-extended tag grc_grecy runs, and the one that pie-extended install-addons grc_grecy installs: grecy_addons (in pie_extended/processes/greek.py) reads it too (Model.spacy_model). To run another pipeline:

  1. Change the name in models to a GreCy pipeline: grc_perseus_{sm,lg,trf} or grc_proiel_{sm,lg,trf} (the Perseus one with a transformer, grc_perseus_trf, is the default).

  2. Install it: pie-extended install-addons grc_grecy. It takes the wheel of the pipeline in the releases of GreCy, of the version GRECY_VERSION in processes/greek.py (change it when GreCy publishes a new one).

  3. Check what it needs. The transformer pipelines need spacy-transformers, which is in the extra grecy of pyproject.toml. If you choose a pipeline without a transformer, you can remove it from there.

  4. Check the tasks of Models: they must be given by the pipeline. The sm and trf pipelines of both treebanks give the four of the default (the tests check it); the lg ones were not tried.

  5. Run the tests: pytest tests/test_spacy_tagger.py. They use the pipeline of the model and compare the result with plain spaCy for every GreCy pipeline that is installed (they are skipped without spaCy or without the pipeline).

  6. Tell your users what changes. The treebanks follow different conventions: the values of tag and morph are not the same from a pipeline of Perseus to one of PROIEL (see Tagger usage).

Choose the spaCy pipeline: get_tagger, or the tagger itself

There are two levels, and they do the same thing with a different amount of help.

With get_tagger, the usual way: the name of the model gives its package, and model_path replaces what it runs.

tagger = get_tagger("grc_grecy", model_path="grc_proiel_sm", tasks=["lemma", "pos"])
iterator, processor = get_pipeline("grc_grecy", tagger, space_after=False)

get_tagger does five things: it finds the package of the model, and its Backend; it turns the name of the spaCy pipeline into the full <pipeline,tasks> of the model; it works out, from the columns you ask for with tasks, the tasks that have to be computed (and drops the models that are not needed); it makes the disambiguator of the model, if it has one; and it keeps the columns in tagger.selected_outputs, that get_pipeline() reads afterwards.

With the tagger itself: a SpacyTagger takes the spaCy pipeline and the tasks in its constructor, and a composite tagger takes its engines with CompositeTagger().add_model("papie:org/repo#variant", "lemma", "pos").

from pie_extended.cli.utils import get_pipeline
from pie_extended.spacy_tagger import SpacyTagger

tagger = SpacyTagger(pipeline="grc_proiel_sm", tasks=["lemma", "pos"])
tagger.selected_outputs = ["lemma", "pos"]          # the columns: see below
iterator, processor = get_pipeline("grc_grecy", tagger, space_after=False)

The processing pipeline (the data iterator and the processor, see the vocabulary) does not depend on the spaCy pipeline: the same one works with any GreCy pipeline.

Tasks are computed, columns are written

The tasks of the constructor are what the spaCy pipeline computes. The columns that appear are tagger.selected_outputs (None: all of them). Without it, the output also has the columns that are not tasks, such as treated, which is what you want for a tagger made by hand if you did not ask for columns. get_tagger(tasks=...) sets it for you, so treated is left out when you ask for lemma and pos: setting it by hand, as above, gives exactly the same output (a test checks it).

For a Pie model, get_tagger is the way: only it knows how to find the tasks that the post-processing needs. (The Latin model needs lemma to split its enclitics, even when you do not ask for it: the column is hidden, but computed.)

Nothing has to be changed for one run: --model_path grc_proiel_sm on the command line (or model_path="grc_proiel_sm" in get_tagger()) runs another pipeline without touching the code, with the tasks of the model (see Tagger usage).

Warning

Two different GreCy pipelines cannot be loaded in the same Python process: spaCy refuses to register their components twice. A tagger loads one, but a script that compares pipelines has to use one process for each (see tests/spacy_compare.py).

Add another spaCy pipeline as a model

A spaCy model is a line of the table like the others (see Add a model), with three differences: backend="spacy", the name of the pipeline in models, and an addons in its process that installs the pipeline. Here, with foo_spacy:

# pie_extended/models.py
Model(
    name="foo_spacy", lang="foo", title="Foo (spaCy)", authors=["Author 1", "Author 2"],
    description="A readable description", link="https://example.org/foo",
    models="<foo_core_news_sm,lemma,pos,morph>",    # the pipeline, then the tasks it gives
    process="foo:spacy", backend="spacy",           # get_tagger builds a SpacyTagger
),
# pie_extended/processes/foo.py
import subprocess
import sys

from pie_extended.pipeline.iterators.proto import DataIterator
from pie_extended.pipeline.postprocessor.proto import ProcessorPrototype
from pie_extended.pipeline.tokenizers.simple_tokenizer import SimpleTokenizer
from pie_extended.processes import Process


def pipeline(max_tokens=256):
    # spaCy receives words: tokenize like the data the pipeline was trained on
    return DataIterator(tokenizer=SimpleTokenizer(), max_tokens=max_tokens), ProcessorPrototype()


def addons(model) -> bool:
    """ What `pie-extended install-addons foo_spacy` runs: install the pipeline """
    return subprocess.call([sys.executable, "-m", "pip", "install", model.spacy_model]) == 0


PROCESSES = {"spacy": Process(pipeline, addons=addons)}

A spaCy model is not downloaded: files stays empty. Add spacy (and what the pipeline needs) to an extra of pyproject.toml: spaCy is an optional dependency, imported only when the pipeline is loaded. The model is used as any other, and gives what its pipeline predicts:

tagger = get_tagger("foo_spacy", tasks=["lemma", "pos"])
iterator, processor = get_pipeline("foo_spacy", tagger, space_after=False)

for token in tagger.tag_str("A text.", iterator=iterator, processor=processor):
    print(token)           # {'form': 'A', 'lemma': ..., 'pos': ...}

Add a task to the spaCy backend

A task is an attribute of the token: add it to EXTRACTORS, and stop skipping the component that gives it. For the dependency labels:

from pie_extended.spacy_tagger import SpacyTagger


class DependencyTagger(SpacyTagger):
    EXTRACTORS = {**SpacyTagger.EXTRACTORS, "dep": lambda token: token.dep_}
    UNUSED_COMPONENTS = SpacyTagger.UNUSED_COMPONENTS - {"parser"}   # the parser gives a task now


tagger = DependencyTagger()
tagger.add_model("grc_perseus_sm", "lemma", "dep")
tagged, tasks = tagger.tag([["ἐν", "ἀρχῇ", "ἦν", "ὁ", "λόγος"]])
print(tasks)
print(tagged[0])
['dep', 'lemma']
[('ἐν', ('case', 'ἐν')), ('ἀρχῇ', ('ROOT', 'ἀρχή')), ('ἦν', ('cop', 'εἰμί')), ('ὁ', ('det', 'ὁ')), ('λόγος', ('nsubj', 'λόγος'))]

get_tagger builds a SpacyTagger for Backend = "spacy": use your class directly, as here, with the iterator and the processor of the model (get_pipeline accepts any tagger). The tasks are sorted, as Pie does.

For the reference of the class: SpacyTagger.

PaPie 1.0: the models

PaPie 1.0 (imported as papie) is Pie rewritten under PyTorch-Lightning, and runs all the models of the package. They are on Hugging Face, in biblissima-textes: papie-latin (lasla), papie-french (fro and frm) and papie-legacy, where each other model is a pack (the networks of a language, one for each task). The files are plain (safetensors, no pickle: nothing to run when they are loaded), they are downloaded from the Hub when the model is first used, by variant, and cached; pie-extended download MODEL does it in advance.

Models of a package tells where, with papie_models(repository, variant, tasks):

from pie_extended.papie import papie_models

Models = papie_models("biblissima-textes/papie-latin", "morph", ["lemma", "pos", "Case"])
# '<papie:biblissima-textes/papie-latin#morph,lemma,pos,Case>'
Models = papie_models("biblissima-textes/papie-legacy", "hy_xcl/*", ["lemma", "pos"])   # a pack

papie:REPOSITORY#VARIANT@REVISION names a model, and #PACK/* a pack. In Python, or for one run, any model of PaPie 1.0 can be used, with no change of the code, and mixed with another engine:

tagger = get_tagger("lasla", tasks=["lemma", "pos"])          # only these heads are run
tagger = get_tagger("lasla", model_path="<papie:biblissima-textes/papie-latin#morph@COMMIT,lemma,pos>")

A model of the package is a wrapper around its Hugging Face model: the same model can be named by its short name (lasla) or by the Hub (biblissima-textes/papie-latin#morph, with @commit to pin it), and pie-extended list shows which wraps which. What the wrapper adds is the processes that go with the model (tokenizer, tagset, disambiguation). A model of Hugging Face that nothing wraps still runs, with the simple tokenizer and the columns it gives.

Pin the revision (a commit) for reproducibility. Only the heads asked for with --tasks are run. The models of the package that PaPie 0.x ran (lasla, fro…) are the same ones, converted: the columns are the same. Two models were not converted (a custom cell of their character network): dum only gives lemma, and osp was converted from another file.

Combine engines: a lemmatizer and a spaCy pipeline

Not every task has to come from the same tool. grc_soter takes the lemma from Sôtêr mini, a lemmatizer of Ancient Greek of 1.9 million parameters (Apache-2.0), and the part of speech from a light GreCy pipeline:

pip install "pie-extended[soter]"
pie-extended install-addons grc_soter     # the spaCy pipeline, and Sôtêr (7.5 MB, kept in the cache of Hugging Face)
pie-extended tag grc_soter demo.txt
token  lemma  pos    treated  SpaceAfter
ἐν     ἐν     ADP    ἐν       _
ἀρχῇ   ἀρχή   NOUN   ἀρχῇ     _
ἦν     εἰμί   AUX    ἦν       _
ὁ      ὁ      DET    ὁ        _
λόγος  λόγος  NOUN   λόγος    No
,      ,      PUNCT  ,        _

How it is written

A CompositeTagger has several engines, and each task is given by one of them. They are written as the other models (<name,tasks>), in Models or in --model_path:

Name

Engine

Gives

soter:Zual/soter-mini

the lemmatizer Sôtêr (SoterLemmatizer)

lemma

soter:Zual/soter-mini#proiel

the same, with the lemmas of the convention of PROIEL (#ptnk: PTNK)

lemma

grc_perseus_sm, any other name

a spaCy pipeline (SpacyTagger)

lemma, pos, tag, morph

So grc_soter is only this line of the table:

Model(name="grc_soter", ..., models="<soter:Zual/soter-mini,lemma><grc_perseus_sm,pos>", process="greek:soter", backend="composite")

and any other combination is a --model_path away, with no code to change, for grc_soter as for grc_grecy. For example the lemmas of PROIEL, and the morphology of the pipeline as well:

pie-extended tag grc_grecy demo.txt --model_path "<soter:Zual/soter-mini#proiel,lemma><grc_proiel_sm,pos,morph>"

The conventions of lemmatization differ between the treebanks: Sôtêr gives the one of Perseus (the entries of the LSJ), and restores the other ones from a table of its repository. The verb of εἶπεν is εἶπον for Perseus and λέγω for PROIEL: with #proiel you get λέγω.

get_tagger makes a CompositeTagger when the model has Backend = "composite", or when the tasks of a spaCy model come from several entries. A task given by two engines is an error, and is found before anything is loaded.

Only what is asked for is loaded. The columns that you ask for decide which engines are needed: --tasks pos never loads Sôtêr, and --tasks lemma never loads spaCy.

What the combination gives

Against the treebank of Perseus (400 sentences of the dev set, the tokens are the gold ones for every engine, CPU):

Lemma

UPOS

Sôtêr mini

94.6%

grc_perseus_sm (light)

92.8%

81.6%

grc_perseus_trf (default of grc_grecy)

93.5%

89.6%

grc_soter: lemma of Sôtêr, part of speech of grc_perseus_sm

94.6%

81.6%

The lemma is better with Sôtêr, and the part of speech is 8 points worse with the light pipeline than with the transformer, and less steady on rare forms: the light pipeline calls the elided διʼ an adjective, with a right lemma. Choose the pipeline of the part of speech accordingly (--model_path "<soter:Zual/soter-mini,lemma><grc_perseus_trf,pos>" puts the transformer back). The lemma is also the slow part: about 4 ms for a token (12 threads), ten times the whole of the light pipeline. Take the numbers with care: these treebanks are data of the pipelines, and only the differences matter.

Each engine reads its own apostrophe

The engines do not agree on the apostrophe of an elision (δι᾽), and this was measured on the 90 elided words of PROIEL:

Apostrophe given to the engine

Sôtêr: lemma right

GreCy: lemma right

U+2019 ’ (as in the treebanks)

100%

57%

ASCII '

89%

U+02BC ʼ

46%

100%

The tokenizer gives U+02BC (GreCy needs it), so Sôtêr converts every apostrophe to U+2019, and the text to NFC (it works on bytes, and gives gibberish for decomposed accents), inside the engine: whichever tokenizer a model uses, each engine gets what it was trained on. Your spelling stays in token.

What it needs

  • transformers (pip install "pie-extended[soter]", with spaCy and GreCy for the part of speech);

  • Sôtêr is downloaded from Hugging Face the first time (7.5 MB, in ~/.cache/huggingface, or HF_HOME). With HF_HUB_OFFLINE=1 nothing is downloaded: run pie-extended install-addons grc_soter first;

  • Sôtêr only gives lemma. Two GreCy pipelines cannot be combined (see above): the spaCy engine of a combination is one;

  • the options of Sôtêr (the context of 8 words, the batch, the device) are those of SoterLemmatizer. --device cuda was not tried.