code: def test_encoder_decoder_from_pretrained(self):
      load_weight_prefix = (
          TFVisionEncoderDecoderModel.load_weight_prefix
      )
  
      config = self.get_encoder_decoder_config()
      feature_extractor = AutoFeatureExtractor.from_pretrained(
          "google/vit-base-patch16-224-in21k"
      )
      decoder_tokenizer = AutoTokenizer.from_pretrained("gpt2")
  
      img = prepare_img()
      pixel_values = feature_extractor(
          images=img, return_tensors="tf"
      ).pixel_values
      decoder_input_ids = decoder_tokenizer(
          "Linda Davis", return_tensors="tf"
      ).input_ids
  
      with tempfile.TemporaryDirectory() as tmp_dirname:
          # Since most of HF's models don't have pretrained cross-attention layers, they are randomly
          # initialized even if we create models using `from_pretrained` method.
          # For the tests, the decoder need to be a model with pretrained cross-attention layers.
          # So we create pretrained models (without `load_weight_prefix`), save them, and later,
          # we load them using `from_pretrained`.
          # (we don't need to do this for encoder, but let's make the code more similar between encoder/decoder)
          encoder = TFAutoModel.from_pretrained(
              "google/vit-base-patch16-224-in21k", name="encoder"
          )
          # It's necessary to specify `add_cross_attention=True` here.
          decoder = TFAutoModelForCausalLM.from_pretrained(
              "gpt2",
              is_decoder=True,
              add_cross_attention=True,
              name="decoder",
          )
          pretrained_encoder_dir = os.path.join(
              tmp_dirname, "pretrained_encoder"
          )
          pretrained_decoder_dir = os.path.join(
              tmp_dirname, "pretrained_decoder"
          )
          encoder.save_pretrained(pretrained_encoder_dir)
          decoder.save_pretrained(pretrained_decoder_dir)
          del encoder
          del decoder
  
          enc_dec_model = TFVisionEncoderDecoderModel.from_encoder_decoder_pretrained(
              pretrained_encoder_dir,
              pretrained_decoder_dir,
          )
          # check that the from pretrained methods work
          enc_dec_model.save_pretrained(tmp_dirname)
          enc_dec_model = TFVisionEncoderDecoderModel.from_pretrained(
              tmp_dirname
          )
  
          output = enc_dec_model(
              pixel_values,
              decoder_input_ids=decoder_input_ids,
              labels=decoder_input_ids,
          )
  
          loss_pretrained = output.loss
          del enc_dec_model
  
          # Create the model using `__init__` with loaded ``pretrained`` encoder / decoder
          encoder = TFAutoModel.from_pretrained(
              pretrained_encoder_dir,
              load_weight_prefix=load_weight_prefix,
              name="encoder",
          )
          decoder = TFAutoModelForCausalLM.from_pretrained(
              pretrained_decoder_dir,
              load_weight_prefix=load_weight_prefix,
              name="decoder",
          )
          enc_dec_model = TFVisionEncoderDecoderModel(
              config=config, encoder=encoder, decoder=decoder
          )
  
      output = enc_dec_model(
          pixel_values,
          decoder_input_ids=decoder_input_ids,
          labels=decoder_input_ids,
      )
  
      loss_init = output.loss
  
      max_diff = np.max(np.abs(loss_pretrained - loss_init))
      expected_diff = 0.0
  
      self.assertAlmostEqual(max_diff, expected_diff, places=4)
  
imports: import copy
  import os
  import tempfile
  import unittest
  import numpy
  from transformers import is_tf_available, is_torch_available, is_vision_available
  from transformers.testing_utils import is_pt_tf_cross_test, require_tf, require_torch, require_vision, slow, torch_device
  from transformers.utils.generic import ModelOutput
  from ...test_modeling_tf_common import floats_tensor, ids_tensor
  from ..gpt2.test_modeling_tf_gpt2 import TFGPT2ModelTester
  from ..vit.test_modeling_tf_vit import TFViTModelTester
label_desc: Use image node_id 5 for calling the TFVisionEncoderDecoderModelSaveLoadTests obj's underlying member method code with example usage: obj.test_encoder_decoder_from_pretrained() without return types