docstr: Check that the parameters the bert model DO change when using bert_finetune
docstr_tok: ["Check","that","the","parameters","the","bert","model","DO","change","when","using","bert_finetune"]
code: def one_layer_finetune_transformer_test(
      self, wordvec_pretrain_file, transformer_name
  ):
      """
      Check that the parameters the bert model DO change when using bert_finetune
      """
      with tempfile.TemporaryDirectory(
          dir=TEST_WORKING_DIR
      ) as tmpdirname:
          args = [
              "--bert_model",
              transformer_name,
              "--bert_finetune",
              "--bert_finetune_layers",
              "1",
              "--optim",
              "adamw",
              "--bert_finetune_layers",
              "1",
          ]
          args, trained_model = self.run_train_test(
              wordvec_pretrain_file, tmpdirname, extra_args=args
          )
  
          # check that the weights of the last layer are different,
          # but the weights of the earlier layers and
          # non-transformer-layers are the same
          foundation_cache = FoundationCache()
          bert_model, bert_tokenizer = foundation_cache.load_bert(
              transformer_name
          )
          assert bert_model.config.num_hidden_layers > 1
          layer_name = "layer.%d." % (
              bert_model.config.num_hidden_layers - 1
          )
          for name, parameter in bert_model.named_parameters():
              other_name = "bert_model." + name
              other_parameter = trained_model.model.get_parameter(
                  other_name
              )
              if layer_name in name:
                  if (
                      "rel_attn.seg_embed" in name
                      or "rel_attn.r_s_bias" in name
                  ):
                      # not sure why this happens for xlnet, just roll with it
                      continue
                  assert not torch.allclose(
                      parameter.cpu(), other_parameter.cpu()
                  )
              else:
                  assert torch.allclose(
                      parameter.cpu(), other_parameter.cpu()
                  )
  
imports: from collections import defaultdict
  import logging
  import pathlib
  import tempfile
  import pytest
  import torch
  from torch import optim
  from stanza import Pipeline
  from stanza.models import constituency_parser
  from stanza.models.common import pretrain
  from stanza.models.common.bert_embedding import load_bert, load_tokenizer
  from stanza.models.common.foundation_cache import FoundationCache
  from stanza.models.common.utils import set_random_seed
  from stanza.models.constituency import lstm_model
  from stanza.models.constituency import trainer
  from stanza.models.constituency import tree_reader
  from stanza.tests import *
label_desc: Use image node_id 29 for calling the TestTrainer obj's underlying member method code with example usage: obj.one_layer_finetune_transformer_test(wordvec_pretrain_file, transformer_name) without return types