code: def test_replace_listeners():
      orig_config = Config().from_str(cfg_string)
      nlp = util.load_model_from_config(
          orig_config, auto_fill=True, validate=True
      )
      text = "This is awesome"
      examples = [
          Example.from_dict(
              nlp.make_doc(text), {"tags": ["A", "B", "C"]}
          )
      ]
      optimizer = nlp.initialize(lambda: examples)
      # verify correct configuration with transformer listener
      transformer = nlp.get_pipe("transformer")
      tagger = nlp.get_pipe("tagger")
      tagger_tok2vec = tagger.model.get_ref("tok2vec")
      tagger_listener = tagger_tok2vec.get_ref("listener")
      assert isinstance(tagger_listener, TransformerListener)
      assert transformer.listener_map["tagger"][0] == tagger_listener
      assert isinstance(transformer.model, TransformerModel)
      assert (
          nlp.config["components"]["transformer"]["model"][
              "@architectures"
          ]
          == "spacy-transformers.TransformerModel.v3"
      )
      assert (
          nlp.config["components"]["tagger"]["model"]["tok2vec"][
              "@architectures"
          ]
          == "spacy-transformers.TransformerListener.v1"
      )
      # train pipe before replacing listeners
      for i in range(2):
          losses = {}
          nlp.update(examples, sgd=optimizer, losses=losses)
          doc = nlp(text)
  
      preds = [t.tag_ for t in doc]
      doc_tensor = tagger_tok2vec.predict([doc])
  
      # replace listener and verify predictions are still the same
      nlp.replace_listeners("transformer", "tagger", ["model.tok2vec"])
      tagger = nlp.get_pipe("tagger")
      tagger_tok2vec = tagger.model.get_ref("tok2vec")
      assert isinstance(tagger_tok2vec, Model)
      assert tagger_tok2vec.layers[0].layers[0].name == "transformer"
      assert (
          nlp.config["components"]["tagger"]["model"]["tok2vec"][
              "@architectures"
          ]
          == "spacy-transformers.Tok2VecTransformer.v3"
      )
      doc2 = nlp(text)
      assert preds == [t.tag_ for t in doc2]
      pred_tensor = tagger_tok2vec.predict([doc2])
      _assert_equal_tensors(doc_tensor, pred_tensor)
  
      # attempt training with the new pipeline
      optimizer = nlp.resume_training()
      for i in range(2):
          losses = {}
          nlp.update(examples, sgd=optimizer, losses=losses)
          assert losses["tagger"] > 0.0
  
      # check for presence of additional fields in model_output
      assert doc2._.trf_data.model_output.pooler_output is not None
      assert doc2._.trf_data.model_output.attentions is not None
  
      # ensure IO goes OK
      doc_tensor_trained = tagger_tok2vec.predict([doc])
      with make_tempdir() as d:
          file_path = d / "trained_nlp"
          nlp.to_disk(file_path)
          nlp2 = util.load_model_from_path(file_path)
          doc3 = nlp2(text)
          tagger2 = nlp2.get_pipe("tagger")
          tagger_tok2vec2 = tagger2.model.get_ref("tok2vec")
          pred_tensor = tagger_tok2vec2.predict([doc3])
          _assert_equal_tensors(doc_tensor_trained, pred_tensor)
  
imports: import pytest
  from packaging.version import Version
  import torch
  import spacy
  from spacy.language import Language
  from spacy.training.example import Example
  from spacy.util import make_tempdir
  from spacy.vocab import Vocab
  from spacy.tokens import Doc
  from spacy import util
  from thinc.api import Model, Config, get_current_ops, NumpyOps
  from spacy.tests.util import assert_docs_equal
  from .util import DummyTransformer, _assert_equal_tensors
  from ..None import TransformerModel
  from ..pipeline_component import Transformer
  from ..layers import TransformerListener
  from ..data_classes import TransformerData, FullTransformerBatch
label_desc: Use image node_id 16 for calling a global function with example usage: test_replace_listeners() without return types