Repository: flairNLP/flair

Issue:
[Bug]: Loaded TransformerEmbeddings no longer have context_length set
### Describe the bug

When loading a saved TransformerEmbeddings in master branch, we lose the context_length variable. This leads to much reduced accuracy for loaded models.

### To Reproduce

```python
from flair.data import Dictionary
from flair.embeddings import TransformerDocumentEmbeddings
from flair.models import TextClassifier

model = TextClassifier(label_type='ner', label_dictionary=Dictionary(),
                       embeddings=TransformerDocumentEmbeddings('distilbert-base-uncased', use_context=True))

print(model.embeddings.context_length)

model.save('tmp/single.pt')
loaded_single_task = TextClassifier.load('tmp/single.pt')

print(loaded_single_task.embeddings.context_length)
```


### Expected behaivor

It should twice print 64 as the `context_length`. However, after loading, it prints 0.

### Logs and Stack traces

_No response_

### Screenshots

_No response_

### Additional Context

_No response_

### Environment

Run in current master branch.


Discussion:

Resolving patch:
diff --git a/flair/embeddings/transformer.py b/flair/embeddings/transformer.py
--- a/flair/embeddings/transformer.py
+++ b/flair/embeddings/transformer.py
@@ -37,7 +37,7 @@
     register_embeddings,
 )
 
-SENTENCE_BOUNDARY_TAG: str = "[SATZ]"
+SENTENCE_BOUNDARY_TAG: str = "[FLERT]"
 
 
 @torch.jit.script_if_tracing
@@ -1162,6 +1162,7 @@ def __setstate__(self, state):
 
     @classmethod
     def from_params(cls, params):
+        params["use_context"] = params.pop("context_length", 0)
         return cls.create_from_state(**params)
 
     def to_params(self):


Test patch:
diff --git a/tests/embeddings/test_transformer_document_embeddings.py b/tests/embeddings/test_transformer_document_embeddings.py
--- a/tests/embeddings/test_transformer_document_embeddings.py
+++ b/tests/embeddings/test_transformer_document_embeddings.py
@@ -1,4 +1,7 @@
+from flair.data import Dictionary
 from flair.embeddings import TransformerDocumentEmbeddings
+from flair.models import TextClassifier
+from flair.nn import Classifier
 from tests.embedding_test_utils import BaseEmbeddingsTest
 
 
@@ -15,3 +18,23 @@ class TestTransformerDocumentEmbeddings(BaseEmbeddingsTest):
 
     name_field = "embeddings"
     invalid_names = ["other", "not/existing/path/to/embeddings"]
+
+
+def test_if_loaded_embeddings_have_all_attributes(tasks_base_path):
+
+    # dummy model with embeddings
+    embeddings = TransformerDocumentEmbeddings(
+        "distilbert-base-uncased",
+        use_context=True,
+        use_context_separator=False,
+    )
+
+    model = TextClassifier(label_type="ner", label_dictionary=Dictionary(), embeddings=embeddings)
+
+    # save the dummy and load it again
+    model.save(tasks_base_path / "single.pt")
+    loaded_single_task = Classifier.load(tasks_base_path / "single.pt")
+
+    # check that context_length and use_context_separator is the same for both
+    assert model.embeddings.context_length == loaded_single_task.embeddings.context_length
+    assert model.embeddings.use_context_separator == loaded_single_task.embeddings.use_context_separator
diff --git a/tests/embeddings/test_transformer_word_embeddings.py b/tests/embeddings/test_transformer_word_embeddings.py
--- a/tests/embeddings/test_transformer_word_embeddings.py
+++ b/tests/embeddings/test_transformer_word_embeddings.py
@@ -109,7 +109,7 @@ def test_transformers_context_expansion(self, results_base_path):
         # test expansion for sentence without context
         sentence = Sentence("This is great!")
         expanded, _ = emb._expand_sentence_with_context(sentence=sentence)
-        assert " ".join([token.text for token in expanded]) == "[SATZ] This is great ! [SATZ]"
+        assert " ".join([token.text for token in expanded]) == "[FLERT] This is great ! [FLERT]"
 
         # test expansion for with previous and next as context
         sentence = Sentence("This is great.")
@@ -118,7 +118,7 @@ def test_transformers_context_expansion(self, results_base_path):
         expanded, _ = emb._expand_sentence_with_context(sentence=sentence)
         assert (
             " ".join([token.text for token in expanded])
-            == "How is it ? [SATZ] This is great . [SATZ] Then again , maybe not ..."
+            == "How is it ? [FLERT] This is great . [FLERT] Then again , maybe not ..."
         )
 
         # test expansion if first sentence is document boundary
@@ -128,7 +128,7 @@ def test_transformers_context_expansion(self, results_base_path):
         sentence._next_sentence = sentence_next
         expanded, _ = emb._expand_sentence_with_context(sentence=sentence)
         assert (
-            " ".join([token.text for token in expanded]) == "[SATZ] This is great ? [SATZ] Then again , maybe not ..."
+            " ".join([token.text for token in expanded]) == "[FLERT] This is great ? [FLERT] Then again , maybe not ..."
         )
 
         # test expansion if we don't use context