2020-07-22 11:42:59 +00:00
|
|
|
import pytest
|
2021-12-04 19:34:48 +00:00
|
|
|
from catalogue import RegistryError
|
2020-09-26 11:13:57 +00:00
|
|
|
from thinc.api import Config, ConfigValidationError
|
2021-12-04 19:34:48 +00:00
|
|
|
|
2020-02-27 17:42:27 +00:00
|
|
|
import spacy
|
2020-08-05 21:35:09 +00:00
|
|
|
from spacy.lang.de import German
|
2021-12-04 19:34:48 +00:00
|
|
|
from spacy.lang.en import English
|
2023-06-14 15:48:41 +00:00
|
|
|
from spacy.language import DEFAULT_CONFIG, DEFAULT_CONFIG_PRETRAIN_PATH, Language
|
|
|
|
from spacy.ml.models import (
|
|
|
|
MaxoutWindowEncoder,
|
|
|
|
MultiHashEmbed,
|
|
|
|
build_tb_parser_model,
|
|
|
|
build_Tok2Vec_model,
|
|
|
|
)
|
2020-12-08 06:41:03 +00:00
|
|
|
from spacy.schemas import ConfigSchema, ConfigSchemaPretrain
|
2023-06-27 15:36:33 +00:00
|
|
|
from spacy.training import Example
|
2023-06-14 15:48:41 +00:00
|
|
|
from spacy.util import (
|
|
|
|
load_config,
|
|
|
|
load_config_from_str,
|
|
|
|
load_model_from_config,
|
|
|
|
registry,
|
|
|
|
)
|
2020-07-22 11:42:59 +00:00
|
|
|
|
2020-12-08 06:41:03 +00:00
|
|
|
from ..util import make_tempdir
|
2020-02-27 17:42:27 +00:00
|
|
|
|
|
|
|
nlp_config_string = """
|
2020-08-04 13:09:37 +00:00
|
|
|
[paths]
|
2020-09-29 20:33:46 +00:00
|
|
|
train = null
|
|
|
|
dev = null
|
2020-08-04 13:09:37 +00:00
|
|
|
|
2020-09-17 09:38:59 +00:00
|
|
|
[corpora]
|
2020-09-15 19:58:04 +00:00
|
|
|
|
2020-09-17 09:38:59 +00:00
|
|
|
[corpora.train]
|
2020-08-04 13:09:37 +00:00
|
|
|
@readers = "spacy.Corpus.v1"
|
2020-08-20 09:20:58 +00:00
|
|
|
path = ${paths.train}
|
2020-08-04 13:09:37 +00:00
|
|
|
|
2020-09-17 09:38:59 +00:00
|
|
|
[corpora.dev]
|
2020-08-04 13:09:37 +00:00
|
|
|
@readers = "spacy.Corpus.v1"
|
2020-08-20 09:20:58 +00:00
|
|
|
path = ${paths.dev}
|
2020-08-04 13:09:37 +00:00
|
|
|
|
2020-09-17 09:38:59 +00:00
|
|
|
[training]
|
|
|
|
|
2020-08-04 13:09:37 +00:00
|
|
|
[training.batcher]
|
2020-09-03 15:30:41 +00:00
|
|
|
@batchers = "spacy.batch_by_words.v1"
|
2020-08-04 13:09:37 +00:00
|
|
|
size = 666
|
2020-07-22 11:42:59 +00:00
|
|
|
|
2020-02-27 17:42:27 +00:00
|
|
|
[nlp]
|
|
|
|
lang = "en"
|
2020-07-22 11:42:59 +00:00
|
|
|
pipeline = ["tok2vec", "tagger"]
|
2020-02-27 17:42:27 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
[components]
|
2020-07-12 12:28:34 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
[components.tok2vec]
|
2020-07-22 15:29:31 +00:00
|
|
|
factory = "tok2vec"
|
2020-02-27 17:42:27 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
[components.tok2vec.model]
|
2020-02-27 17:42:27 +00:00
|
|
|
@architectures = "spacy.HashEmbedCNN.v1"
|
|
|
|
pretrained_vectors = null
|
|
|
|
width = 342
|
|
|
|
depth = 4
|
|
|
|
window_size = 1
|
|
|
|
embed_size = 2000
|
|
|
|
maxout_pieces = 3
|
|
|
|
subword_features = true
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
[components.tagger]
|
2020-07-22 15:29:31 +00:00
|
|
|
factory = "tagger"
|
2020-02-27 17:42:27 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
[components.tagger.model]
|
2022-03-15 13:15:31 +00:00
|
|
|
@architectures = "spacy.Tagger.v2"
|
2020-02-27 17:42:27 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
[components.tagger.model.tok2vec]
|
2020-07-29 11:47:37 +00:00
|
|
|
@architectures = "spacy.Tok2VecListener.v1"
|
2020-08-20 09:20:58 +00:00
|
|
|
width = ${components.tok2vec.model.width}
|
2020-02-27 17:42:27 +00:00
|
|
|
"""
|
|
|
|
|
2020-12-08 06:41:03 +00:00
|
|
|
pretrain_config_string = """
|
|
|
|
[paths]
|
|
|
|
train = null
|
|
|
|
dev = null
|
|
|
|
|
|
|
|
[corpora]
|
|
|
|
|
|
|
|
[corpora.train]
|
|
|
|
@readers = "spacy.Corpus.v1"
|
|
|
|
path = ${paths.train}
|
|
|
|
|
|
|
|
[corpora.dev]
|
|
|
|
@readers = "spacy.Corpus.v1"
|
|
|
|
path = ${paths.dev}
|
|
|
|
|
|
|
|
[training]
|
|
|
|
|
|
|
|
[training.batcher]
|
|
|
|
@batchers = "spacy.batch_by_words.v1"
|
|
|
|
size = 666
|
|
|
|
|
|
|
|
[nlp]
|
|
|
|
lang = "en"
|
|
|
|
pipeline = ["tok2vec", "tagger"]
|
|
|
|
|
|
|
|
[components]
|
|
|
|
|
|
|
|
[components.tok2vec]
|
|
|
|
factory = "tok2vec"
|
|
|
|
|
|
|
|
[components.tok2vec.model]
|
|
|
|
@architectures = "spacy.HashEmbedCNN.v1"
|
|
|
|
pretrained_vectors = null
|
|
|
|
width = 342
|
|
|
|
depth = 4
|
|
|
|
window_size = 1
|
|
|
|
embed_size = 2000
|
|
|
|
maxout_pieces = 3
|
|
|
|
subword_features = true
|
|
|
|
|
|
|
|
[components.tagger]
|
|
|
|
factory = "tagger"
|
|
|
|
|
|
|
|
[components.tagger.model]
|
2022-03-15 13:15:31 +00:00
|
|
|
@architectures = "spacy.Tagger.v2"
|
2020-12-08 06:41:03 +00:00
|
|
|
|
|
|
|
[components.tagger.model.tok2vec]
|
|
|
|
@architectures = "spacy.Tok2VecListener.v1"
|
|
|
|
width = ${components.tok2vec.model.width}
|
|
|
|
|
|
|
|
[pretraining]
|
|
|
|
"""
|
|
|
|
|
2020-02-27 17:42:27 +00:00
|
|
|
|
2020-12-18 10:56:57 +00:00
|
|
|
parser_config_string_upper = """
|
2020-02-27 17:42:27 +00:00
|
|
|
[model]
|
2020-12-18 10:56:57 +00:00
|
|
|
@architectures = "spacy.TransitionBasedParser.v2"
|
2020-09-23 14:53:49 +00:00
|
|
|
state_type = "parser"
|
2020-09-23 11:35:09 +00:00
|
|
|
extra_state_tokens = false
|
2020-02-27 17:42:27 +00:00
|
|
|
hidden_width = 66
|
|
|
|
maxout_pieces = 2
|
2020-12-18 10:56:57 +00:00
|
|
|
use_upper = true
|
|
|
|
|
|
|
|
[model.tok2vec]
|
|
|
|
@architectures = "spacy.HashEmbedCNN.v1"
|
|
|
|
pretrained_vectors = null
|
|
|
|
width = 333
|
|
|
|
depth = 4
|
|
|
|
embed_size = 5555
|
|
|
|
window_size = 1
|
|
|
|
maxout_pieces = 7
|
|
|
|
subword_features = false
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
parser_config_string_no_upper = """
|
|
|
|
[model]
|
|
|
|
@architectures = "spacy.TransitionBasedParser.v2"
|
|
|
|
state_type = "parser"
|
|
|
|
extra_state_tokens = false
|
|
|
|
hidden_width = 66
|
|
|
|
maxout_pieces = 2
|
|
|
|
use_upper = false
|
2020-02-27 17:42:27 +00:00
|
|
|
|
|
|
|
[model.tok2vec]
|
|
|
|
@architectures = "spacy.HashEmbedCNN.v1"
|
|
|
|
pretrained_vectors = null
|
|
|
|
width = 333
|
|
|
|
depth = 4
|
|
|
|
embed_size = 5555
|
|
|
|
window_size = 1
|
|
|
|
maxout_pieces = 7
|
|
|
|
subword_features = false
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2021-03-02 16:56:28 +00:00
|
|
|
@registry.architectures("my_test_parser")
|
2020-02-27 17:42:27 +00:00
|
|
|
def my_parser():
|
2020-02-28 10:57:41 +00:00
|
|
|
tok2vec = build_Tok2Vec_model(
|
2020-07-28 20:43:19 +00:00
|
|
|
MultiHashEmbed(
|
|
|
|
width=321,
|
2020-10-05 19:13:53 +00:00
|
|
|
attrs=["LOWER", "SHAPE"],
|
|
|
|
rows=[5432, 5432],
|
|
|
|
include_static_vectors=False,
|
2020-07-28 20:43:19 +00:00
|
|
|
),
|
2020-08-05 14:00:59 +00:00
|
|
|
MaxoutWindowEncoder(width=321, window_size=3, maxout_pieces=4, depth=2),
|
2020-02-28 10:57:41 +00:00
|
|
|
)
|
|
|
|
parser = build_tb_parser_model(
|
2020-09-23 11:35:09 +00:00
|
|
|
tok2vec=tok2vec,
|
2020-09-23 14:53:49 +00:00
|
|
|
state_type="parser",
|
2020-09-23 11:35:09 +00:00
|
|
|
extra_state_tokens=True,
|
|
|
|
hidden_width=65,
|
|
|
|
maxout_pieces=5,
|
2020-12-18 10:56:57 +00:00
|
|
|
use_upper=True,
|
2020-02-28 10:57:41 +00:00
|
|
|
)
|
2020-02-27 17:42:27 +00:00
|
|
|
return parser
|
|
|
|
|
|
|
|
|
2021-12-04 19:34:48 +00:00
|
|
|
@pytest.mark.issue(8190)
|
|
|
|
def test_issue8190():
|
|
|
|
"""Test that config overrides are not lost after load is complete."""
|
|
|
|
source_cfg = {
|
|
|
|
"nlp": {
|
|
|
|
"lang": "en",
|
|
|
|
},
|
|
|
|
"custom": {"key": "value"},
|
|
|
|
}
|
|
|
|
source_nlp = English.from_config(source_cfg)
|
|
|
|
with make_tempdir() as dir_path:
|
|
|
|
# We need to create a loadable source pipeline
|
|
|
|
source_path = dir_path / "test_model"
|
|
|
|
source_nlp.to_disk(source_path)
|
|
|
|
nlp = spacy.load(source_path, config={"custom": {"key": "updated_value"}})
|
|
|
|
|
|
|
|
assert nlp.config["custom"]["key"] == "updated_value"
|
|
|
|
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def test_create_nlp_from_config():
|
|
|
|
config = Config().from_str(nlp_config_string)
|
|
|
|
with pytest.raises(ConfigValidationError):
|
2020-09-27 20:21:31 +00:00
|
|
|
load_model_from_config(config, auto_fill=False)
|
|
|
|
nlp = load_model_from_config(config, auto_fill=True)
|
2020-08-04 13:09:37 +00:00
|
|
|
assert nlp.config["training"]["batcher"]["size"] == 666
|
2020-07-22 11:42:59 +00:00
|
|
|
assert len(nlp.config["training"]) > 1
|
|
|
|
assert nlp.pipe_names == ["tok2vec", "tagger"]
|
|
|
|
assert len(nlp.config["components"]) == 2
|
|
|
|
assert len(nlp.config["nlp"]["pipeline"]) == 2
|
|
|
|
nlp.remove_pipe("tagger")
|
|
|
|
assert len(nlp.config["components"]) == 1
|
|
|
|
assert len(nlp.config["nlp"]["pipeline"]) == 1
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
bad_cfg = {"yolo": {}}
|
|
|
|
load_model_from_config(Config(bad_cfg), auto_fill=True)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
bad_cfg = {"pipeline": {"foo": "bar"}}
|
|
|
|
load_model_from_config(Config(bad_cfg), auto_fill=True)
|
|
|
|
|
|
|
|
|
2020-12-08 06:41:03 +00:00
|
|
|
def test_create_nlp_from_pretraining_config():
|
|
|
|
"""Test that the default pretraining config validates properly"""
|
|
|
|
config = Config().from_str(pretrain_config_string)
|
|
|
|
pretrain_config = load_config(DEFAULT_CONFIG_PRETRAIN_PATH)
|
|
|
|
filled = config.merge(pretrain_config)
|
2021-01-05 02:41:53 +00:00
|
|
|
registry.resolve(filled["pretraining"], schema=ConfigSchemaPretrain)
|
2020-12-08 06:41:03 +00:00
|
|
|
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def test_create_nlp_from_config_multiple_instances():
|
|
|
|
"""Test that the nlp object is created correctly for a config with multiple
|
|
|
|
instances of the same component."""
|
|
|
|
config = Config().from_str(nlp_config_string)
|
|
|
|
config["components"] = {
|
|
|
|
"t2v": config["components"]["tok2vec"],
|
|
|
|
"tagger1": config["components"]["tagger"],
|
|
|
|
"tagger2": config["components"]["tagger"],
|
|
|
|
}
|
|
|
|
config["nlp"]["pipeline"] = list(config["components"].keys())
|
2020-09-27 20:21:31 +00:00
|
|
|
nlp = load_model_from_config(config, auto_fill=True)
|
2020-07-22 11:42:59 +00:00
|
|
|
assert nlp.pipe_names == ["t2v", "tagger1", "tagger2"]
|
|
|
|
assert nlp.get_pipe_meta("t2v").factory == "tok2vec"
|
|
|
|
assert nlp.get_pipe_meta("tagger1").factory == "tagger"
|
|
|
|
assert nlp.get_pipe_meta("tagger2").factory == "tagger"
|
|
|
|
pipeline_config = nlp.config["components"]
|
|
|
|
assert len(pipeline_config) == 3
|
|
|
|
assert list(pipeline_config.keys()) == ["t2v", "tagger1", "tagger2"]
|
|
|
|
assert nlp.config["nlp"]["pipeline"] == ["t2v", "tagger1", "tagger2"]
|
|
|
|
|
|
|
|
|
2020-02-27 17:42:27 +00:00
|
|
|
def test_serialize_nlp():
|
2021-07-02 07:48:26 +00:00
|
|
|
"""Create a custom nlp pipeline from config and ensure it serializes it correctly"""
|
2020-02-27 17:42:27 +00:00
|
|
|
nlp_config = Config().from_str(nlp_config_string)
|
2020-09-27 20:21:31 +00:00
|
|
|
nlp = load_model_from_config(nlp_config, auto_fill=True)
|
2020-08-31 19:24:33 +00:00
|
|
|
nlp.get_pipe("tagger").add_label("A")
|
2020-09-28 19:35:09 +00:00
|
|
|
nlp.initialize()
|
2020-02-27 17:42:27 +00:00
|
|
|
assert "tok2vec" in nlp.pipe_names
|
|
|
|
assert "tagger" in nlp.pipe_names
|
|
|
|
assert "parser" not in nlp.pipe_names
|
|
|
|
assert nlp.get_pipe("tagger").model.get_ref("tok2vec").get_dim("nO") == 342
|
|
|
|
|
|
|
|
with make_tempdir() as d:
|
|
|
|
nlp.to_disk(d)
|
|
|
|
nlp2 = spacy.load(d)
|
|
|
|
assert "tok2vec" in nlp2.pipe_names
|
|
|
|
assert "tagger" in nlp2.pipe_names
|
|
|
|
assert "parser" not in nlp2.pipe_names
|
|
|
|
assert nlp2.get_pipe("tagger").model.get_ref("tok2vec").get_dim("nO") == 342
|
|
|
|
|
|
|
|
|
|
|
|
def test_serialize_custom_nlp():
|
2021-07-02 07:48:26 +00:00
|
|
|
"""Create a custom nlp pipeline and ensure it serializes it correctly"""
|
2020-02-27 17:42:27 +00:00
|
|
|
nlp = English()
|
|
|
|
parser_cfg = dict()
|
2020-02-28 10:57:41 +00:00
|
|
|
parser_cfg["model"] = {"@architectures": "my_test_parser"}
|
2020-07-22 11:42:59 +00:00
|
|
|
nlp.add_pipe("parser", config=parser_cfg)
|
2020-09-28 19:35:09 +00:00
|
|
|
nlp.initialize()
|
2020-02-27 17:42:27 +00:00
|
|
|
|
|
|
|
with make_tempdir() as d:
|
|
|
|
nlp.to_disk(d)
|
|
|
|
nlp2 = spacy.load(d)
|
|
|
|
model = nlp2.get_pipe("parser").model
|
2020-07-22 11:42:59 +00:00
|
|
|
model.get_ref("tok2vec")
|
2020-02-27 17:42:27 +00:00
|
|
|
# check that we have the correct settings, not the default ones
|
2020-12-18 10:56:57 +00:00
|
|
|
assert model.get_ref("upper").get_dim("nI") == 65
|
|
|
|
assert model.get_ref("lower").get_dim("nI") == 65
|
2020-02-27 17:42:27 +00:00
|
|
|
|
|
|
|
|
2020-12-18 10:56:57 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"parser_config_string", [parser_config_string_upper, parser_config_string_no_upper]
|
|
|
|
)
|
|
|
|
def test_serialize_parser(parser_config_string):
|
2021-07-02 07:48:26 +00:00
|
|
|
"""Create a non-default parser config to check nlp serializes it correctly"""
|
2020-02-27 17:42:27 +00:00
|
|
|
nlp = English()
|
|
|
|
model_config = Config().from_str(parser_config_string)
|
2020-07-22 11:42:59 +00:00
|
|
|
parser = nlp.add_pipe("parser", config=model_config)
|
2020-02-27 17:42:27 +00:00
|
|
|
parser.add_label("nsubj")
|
2020-09-28 19:35:09 +00:00
|
|
|
nlp.initialize()
|
2020-02-27 17:42:27 +00:00
|
|
|
|
|
|
|
with make_tempdir() as d:
|
|
|
|
nlp.to_disk(d)
|
|
|
|
nlp2 = spacy.load(d)
|
|
|
|
model = nlp2.get_pipe("parser").model
|
2020-07-22 11:42:59 +00:00
|
|
|
model.get_ref("tok2vec")
|
2020-02-27 17:42:27 +00:00
|
|
|
# check that we have the correct settings, not the default ones
|
2020-12-18 10:56:57 +00:00
|
|
|
if model.attrs["has_upper"]:
|
|
|
|
assert model.get_ref("upper").get_dim("nI") == 66
|
|
|
|
assert model.get_ref("lower").get_dim("nI") == 66
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
|
|
|
|
def test_config_nlp_roundtrip():
|
2021-03-09 03:01:13 +00:00
|
|
|
"""Test that a config produced by the nlp object passes training config
|
2020-07-22 11:42:59 +00:00
|
|
|
validation."""
|
|
|
|
nlp = English()
|
|
|
|
nlp.add_pipe("entity_ruler")
|
|
|
|
nlp.add_pipe("ner")
|
2020-09-27 20:21:31 +00:00
|
|
|
new_nlp = load_model_from_config(nlp.config, auto_fill=False)
|
2020-07-22 11:42:59 +00:00
|
|
|
assert new_nlp.config == nlp.config
|
|
|
|
assert new_nlp.pipe_names == nlp.pipe_names
|
|
|
|
assert new_nlp._pipe_configs == nlp._pipe_configs
|
|
|
|
assert new_nlp._pipe_meta == nlp._pipe_meta
|
|
|
|
assert new_nlp._factory_meta == nlp._factory_meta
|
|
|
|
|
|
|
|
|
2020-08-27 14:44:36 +00:00
|
|
|
def test_config_nlp_roundtrip_bytes_disk():
|
|
|
|
"""Test that the config is serialized correctly and not interpolated
|
|
|
|
by mistake."""
|
|
|
|
nlp = English()
|
|
|
|
nlp_bytes = nlp.to_bytes()
|
|
|
|
new_nlp = English().from_bytes(nlp_bytes)
|
|
|
|
assert new_nlp.config == nlp.config
|
|
|
|
nlp = English()
|
|
|
|
with make_tempdir() as d:
|
|
|
|
nlp.to_disk(d)
|
|
|
|
new_nlp = spacy.load(d)
|
|
|
|
assert new_nlp.config == nlp.config
|
|
|
|
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def test_serialize_config_language_specific():
|
|
|
|
"""Test that config serialization works as expected with language-specific
|
|
|
|
factories."""
|
|
|
|
name = "test_serialize_config_language_specific"
|
|
|
|
|
|
|
|
@English.factory(name, default_config={"foo": 20})
|
|
|
|
def custom_factory(nlp: Language, name: str, foo: int):
|
|
|
|
return lambda doc: doc
|
|
|
|
|
|
|
|
nlp = Language()
|
|
|
|
assert not nlp.has_factory(name)
|
|
|
|
nlp = English()
|
|
|
|
assert nlp.has_factory(name)
|
|
|
|
nlp.add_pipe(name, config={"foo": 100}, name="bar")
|
|
|
|
pipe_config = nlp.config["components"]["bar"]
|
|
|
|
assert pipe_config["foo"] == 100
|
2020-07-22 15:29:31 +00:00
|
|
|
assert pipe_config["factory"] == name
|
2020-07-22 11:42:59 +00:00
|
|
|
|
|
|
|
with make_tempdir() as d:
|
|
|
|
nlp.to_disk(d)
|
|
|
|
nlp2 = spacy.load(d)
|
|
|
|
assert nlp2.has_factory(name)
|
|
|
|
assert nlp2.pipe_names == ["bar"]
|
|
|
|
assert nlp2.get_pipe_meta("bar").factory == name
|
|
|
|
pipe_config = nlp2.config["components"]["bar"]
|
|
|
|
assert pipe_config["foo"] == 100
|
2020-07-22 15:29:31 +00:00
|
|
|
assert pipe_config["factory"] == name
|
2020-07-22 11:42:59 +00:00
|
|
|
|
|
|
|
config = Config().from_str(nlp2.config.to_str())
|
|
|
|
config["nlp"]["lang"] = "de"
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
# German doesn't have a factory, only English does
|
|
|
|
load_model_from_config(config)
|
|
|
|
|
|
|
|
|
|
|
|
def test_serialize_config_missing_pipes():
|
|
|
|
config = Config().from_str(nlp_config_string)
|
|
|
|
config["components"].pop("tok2vec")
|
|
|
|
assert "tok2vec" in config["nlp"]["pipeline"]
|
|
|
|
assert "tok2vec" not in config["components"]
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
load_model_from_config(config, auto_fill=True)
|
2020-08-05 21:35:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_config_overrides():
|
|
|
|
overrides_nested = {"nlp": {"lang": "de", "pipeline": ["tagger"]}}
|
|
|
|
overrides_dot = {"nlp.lang": "de", "nlp.pipeline": ["tagger"]}
|
|
|
|
# load_model from config with overrides passed directly to Config
|
|
|
|
config = Config().from_str(nlp_config_string, overrides=overrides_dot)
|
2020-09-27 20:21:31 +00:00
|
|
|
nlp = load_model_from_config(config, auto_fill=True)
|
2020-08-05 21:35:09 +00:00
|
|
|
assert isinstance(nlp, German)
|
|
|
|
assert nlp.pipe_names == ["tagger"]
|
|
|
|
# Serialized roundtrip with config passed in
|
|
|
|
base_config = Config().from_str(nlp_config_string)
|
2020-09-27 20:21:31 +00:00
|
|
|
base_nlp = load_model_from_config(base_config, auto_fill=True)
|
2020-08-05 21:35:09 +00:00
|
|
|
assert isinstance(base_nlp, English)
|
|
|
|
assert base_nlp.pipe_names == ["tok2vec", "tagger"]
|
|
|
|
with make_tempdir() as d:
|
|
|
|
base_nlp.to_disk(d)
|
|
|
|
nlp = spacy.load(d, config=overrides_nested)
|
|
|
|
assert isinstance(nlp, German)
|
|
|
|
assert nlp.pipe_names == ["tagger"]
|
|
|
|
with make_tempdir() as d:
|
|
|
|
base_nlp.to_disk(d)
|
|
|
|
nlp = spacy.load(d, config=overrides_dot)
|
|
|
|
assert isinstance(nlp, German)
|
|
|
|
assert nlp.pipe_names == ["tagger"]
|
|
|
|
with make_tempdir() as d:
|
|
|
|
base_nlp.to_disk(d)
|
|
|
|
nlp = spacy.load(d)
|
|
|
|
assert isinstance(nlp, English)
|
|
|
|
assert nlp.pipe_names == ["tok2vec", "tagger"]
|
2020-08-13 15:38:30 +00:00
|
|
|
|
|
|
|
|
2023-06-27 15:36:33 +00:00
|
|
|
@pytest.mark.filterwarnings("ignore:\\[W036")
|
|
|
|
def test_config_overrides_registered_functions():
|
|
|
|
nlp = spacy.blank("en")
|
|
|
|
nlp.add_pipe("attribute_ruler")
|
|
|
|
with make_tempdir() as d:
|
|
|
|
nlp.to_disk(d)
|
|
|
|
nlp_re1 = spacy.load(
|
|
|
|
d,
|
|
|
|
config={
|
|
|
|
"components": {
|
|
|
|
"attribute_ruler": {
|
|
|
|
"scorer": {"@scorers": "spacy.tagger_scorer.v1"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
nlp_re1.config["components"]["attribute_ruler"]["scorer"]["@scorers"]
|
|
|
|
== "spacy.tagger_scorer.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
@registry.misc("test_some_other_key")
|
|
|
|
def misc_some_other_key():
|
|
|
|
return "some_other_key"
|
|
|
|
|
|
|
|
nlp_re2 = spacy.load(
|
|
|
|
d,
|
|
|
|
config={
|
|
|
|
"components": {
|
|
|
|
"attribute_ruler": {
|
|
|
|
"scorer": {
|
|
|
|
"@scorers": "spacy.overlapping_labeled_spans_scorer.v1",
|
|
|
|
"spans_key": {"@misc": "test_some_other_key"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert nlp_re2.config["components"]["attribute_ruler"]["scorer"][
|
|
|
|
"spans_key"
|
|
|
|
] == {"@misc": "test_some_other_key"}
|
|
|
|
# run dummy evaluation (will return None scores) in order to test that
|
|
|
|
# the spans_key value in the nested override is working as intended in
|
|
|
|
# the config
|
|
|
|
example = Example.from_dict(nlp_re2.make_doc("a b c"), {})
|
|
|
|
scores = nlp_re2.evaluate([example])
|
|
|
|
assert "spans_some_other_key_f" in scores
|
|
|
|
|
|
|
|
|
2020-08-13 15:38:30 +00:00
|
|
|
def test_config_interpolation():
|
|
|
|
config = Config().from_str(nlp_config_string, interpolate=False)
|
2020-09-17 09:38:59 +00:00
|
|
|
assert config["corpora"]["train"]["path"] == "${paths.train}"
|
2020-08-13 15:38:30 +00:00
|
|
|
interpolated = config.interpolate()
|
2020-09-29 20:33:46 +00:00
|
|
|
assert interpolated["corpora"]["train"]["path"] is None
|
2020-08-13 15:38:30 +00:00
|
|
|
nlp = English.from_config(config)
|
2020-09-17 09:38:59 +00:00
|
|
|
assert nlp.config["corpora"]["train"]["path"] == "${paths.train}"
|
2020-08-13 15:38:30 +00:00
|
|
|
# Ensure that variables are preserved in nlp config
|
2020-08-20 09:20:58 +00:00
|
|
|
width = "${components.tok2vec.model.width}"
|
2020-08-13 15:38:30 +00:00
|
|
|
assert config["components"]["tagger"]["model"]["tok2vec"]["width"] == width
|
|
|
|
assert nlp.config["components"]["tagger"]["model"]["tok2vec"]["width"] == width
|
|
|
|
interpolated2 = nlp.config.interpolate()
|
2020-09-29 20:33:46 +00:00
|
|
|
assert interpolated2["corpora"]["train"]["path"] is None
|
2020-08-13 15:38:30 +00:00
|
|
|
assert interpolated2["components"]["tagger"]["model"]["tok2vec"]["width"] == 342
|
|
|
|
nlp2 = English.from_config(interpolated)
|
2020-09-29 20:33:46 +00:00
|
|
|
assert nlp2.config["corpora"]["train"]["path"] is None
|
2020-08-13 15:38:30 +00:00
|
|
|
assert nlp2.config["components"]["tagger"]["model"]["tok2vec"]["width"] == 342
|
2020-08-24 13:56:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_config_optional_sections():
|
|
|
|
config = Config().from_str(nlp_config_string)
|
|
|
|
config = DEFAULT_CONFIG.merge(config)
|
|
|
|
assert "pretraining" not in config
|
2020-09-27 20:21:31 +00:00
|
|
|
filled = registry.fill(config, schema=ConfigSchema, validate=False)
|
2020-08-24 13:56:03 +00:00
|
|
|
# Make sure that optional "pretraining" block doesn't default to None,
|
|
|
|
# which would (rightly) cause error because it'd result in a top-level
|
|
|
|
# key that's not a section (dict). Note that the following roundtrip is
|
|
|
|
# also how Config.interpolate works under the hood.
|
|
|
|
new_config = Config().from_str(filled.to_str())
|
|
|
|
assert new_config["pretraining"] == {}
|
2020-08-24 20:53:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_config_auto_fill_extra_fields():
|
|
|
|
config = Config({"nlp": {"lang": "en"}, "training": {}})
|
|
|
|
assert load_model_from_config(config, auto_fill=True)
|
|
|
|
config = Config({"nlp": {"lang": "en"}, "training": {"extra": "hello"}})
|
2020-09-27 20:21:31 +00:00
|
|
|
nlp = load_model_from_config(config, auto_fill=True, validate=False)
|
2020-08-24 20:53:47 +00:00
|
|
|
assert "extra" not in nlp.config["training"]
|
|
|
|
# Make sure the config generated is valid
|
|
|
|
load_model_from_config(nlp.config)
|
2020-09-23 15:32:14 +00:00
|
|
|
|
|
|
|
|
2020-12-18 10:56:57 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"parser_config_string", [parser_config_string_upper, parser_config_string_no_upper]
|
|
|
|
)
|
|
|
|
def test_config_validate_literal(parser_config_string):
|
2020-09-23 15:32:14 +00:00
|
|
|
nlp = English()
|
|
|
|
config = Config().from_str(parser_config_string)
|
|
|
|
config["model"]["state_type"] = "nonsense"
|
|
|
|
with pytest.raises(ConfigValidationError):
|
|
|
|
nlp.add_pipe("parser", config=config)
|
|
|
|
config["model"]["state_type"] = "ner"
|
2020-09-23 15:33:13 +00:00
|
|
|
nlp.add_pipe("parser", config=config)
|
2021-01-13 01:02:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_config_only_resolve_relevant_blocks():
|
|
|
|
"""Test that only the relevant blocks are resolved in the different methods
|
|
|
|
and that invalid blocks are ignored if needed. For instance, the [initialize]
|
|
|
|
shouldn't be resolved at runtime.
|
|
|
|
"""
|
|
|
|
nlp = English()
|
|
|
|
config = nlp.config
|
|
|
|
config["training"]["before_to_disk"] = {"@misc": "nonexistent"}
|
|
|
|
config["initialize"]["lookups"] = {"@misc": "nonexistent"}
|
|
|
|
# This shouldn't resolve [training] or [initialize]
|
|
|
|
nlp = load_model_from_config(config, auto_fill=True)
|
|
|
|
# This will raise for nonexistent value
|
|
|
|
with pytest.raises(RegistryError):
|
|
|
|
nlp.initialize()
|
|
|
|
nlp.config["initialize"]["lookups"] = None
|
|
|
|
nlp.initialize()
|
2021-04-12 12:35:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_hyphen_in_config():
|
|
|
|
hyphen_config_str = """
|
|
|
|
[nlp]
|
|
|
|
lang = "en"
|
|
|
|
pipeline = ["my_punctual_component"]
|
|
|
|
|
|
|
|
[components]
|
|
|
|
|
|
|
|
[components.my_punctual_component]
|
|
|
|
factory = "my_punctual_component"
|
|
|
|
punctuation = ["?","-"]
|
|
|
|
"""
|
|
|
|
|
|
|
|
@spacy.Language.factory("my_punctual_component")
|
|
|
|
class MyPunctualComponent(object):
|
|
|
|
name = "my_punctual_component"
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
nlp,
|
|
|
|
name,
|
|
|
|
punctuation,
|
|
|
|
):
|
|
|
|
self.punctuation = punctuation
|
|
|
|
|
|
|
|
nlp = English.from_config(load_config_from_str(hyphen_config_str))
|
2021-06-28 09:48:00 +00:00
|
|
|
assert nlp.get_pipe("my_punctual_component").punctuation == ["?", "-"]
|