Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Note: some tests are adjusted in this PR. This was done first, before implementing the code changes, as we were aware that the
initialize
statements shouldn't be there, cf explosion/spaCy#8319 and explosion/spaCy#8566.Description
Before this PR, the HF Transformer model was loaded through
set_pytorch_transformer
(stored inmodel.attrs["set_transformer"]
), but this happened in theinitialize
call ofTransformerModel
. Unfortunately, this meant that saving/loading a transformer-based pipeline was kind of broken, as you needed to callinitialize
on a previously trained pipeline, which isn't the normal spaCy API. This also broke thefrom_bytes
/to_bytes
typical API.Furthermore, the
from_disk
/to_disk
functionality worked with a "listener" transformer, because thetransformer
pipeline component saved out the PyTorch files directly. However, this solution did not work for the "inline" Transformer, because theTransformerModel
would be used directly and not via the pipeline component.I've been looking at various different solutions, but the proposal in this PR is the only one that I got working for all use-cases: basically we need to load/define the transformer model in the constructor as we do for any other spaCy component.
Unfortunately with this proposal, if you'd have the
initialize
calls as before in your old code, it will crash, complaining about the fact that the transformer model can't be set twice. I think this is actually the correct behaviour in the end, but it might break some people's code. The fix is obvious/easy though.But we'll have to discuss which version bump we want to do when releasing this.
Fixes explosion/spaCy#8319
Fixes explosion/spaCy#8566