docstr: Check that the parameters the bert model DO change when using stage1_bert_finetune, but only for the first couple steps
docstr_tok: ["Check","that","the","parameters","the","bert","model","DO","change","when","using","stage1_bert_finetune",",","but","only","for","the","first","couple","steps"]
code: def test_stage1_bert_finetune(self, wordvec_pretrain_file):
      """
      Check that the parameters the bert model DO change when using stage1_bert_finetune, but only for the first couple steps
      """
      with tempfile.TemporaryDirectory(
          dir=TEST_WORKING_DIR
      ) as tmpdirname:
          bert_model_name = "hf-internal-testing/tiny-bert"
          args = [
              "--bert_model",
              bert_model_name,
              "--stage1_bert_finetune",
              "--optim",
              "adamw",
          ]
          # need to use num_epochs==6 so that epochs 1 and 2 are saved to be different
          # a test of 5 or less means that sometimes it will reload the params
          # at step 2 to get ready for the following iterations with adamw
          args, trained_model = self.run_train_test(
              wordvec_pretrain_file,
              tmpdirname,
              num_epochs=6,
              extra_args=args,
          )
  
          # check that the weights are different
          foundation_cache = FoundationCache()
          bert_model, bert_tokenizer = foundation_cache.load_bert(
              bert_model_name
          )
          assert not self.bert_weights_allclose(
              bert_model, trained_model
          )
  
          # double check that a new bert is created instead of using the FoundationCache when the bert has been trained
          no_finetune_args = self.training_args(
              wordvec_pretrain_file,
              tmpdirname,
              None,
              None,
              "--no_bert_finetune",
              "--no_stage1_bert_finetune",
              "--bert_model",
              bert_model_name,
              "--optim",
              "adamw",
          )
          num_epochs = trained_model.model.args["epochs"]
          each_name = os.path.join(tmpdirname, "each_%02d.pt")
          for i in range(1, num_epochs + 1):
              model_name = each_name % i
              assert os.path.exists(model_name)
              tr = trainer.Trainer.load(
                  model_name,
                  args=no_finetune_args,
                  foundation_cache=foundation_cache,
              )
              assert tr.model.bert_model is not bert_model
              assert not self.bert_weights_allclose(bert_model, tr)
              if i >= num_epochs // 2:
                  assert self.bert_weights_allclose(
                      trained_model.model.bert_model, tr
                  )
  
          # verify that models 1 and 2 are saved to be different
          model_name_1 = each_name % 1
          model_name_2 = each_name % 2
          tr_1 = trainer.Trainer.load(
              model_name_1,
              args=no_finetune_args,
              foundation_cache=foundation_cache,
          )
          tr_2 = trainer.Trainer.load(
              model_name_2,
              args=no_finetune_args,
              foundation_cache=foundation_cache,
          )
          assert not self.bert_weights_allclose(
              tr_1.model.bert_model, tr_2
          )
  
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 28 for calling the TestTrainer obj's underlying member method code with example usage: obj.test_stage1_bert_finetune(wordvec_pretrain_file) without return types