class_docstr: Trainer is training and eval loop for adversarial training.
  
  It is designed to work with PyTorch and Transformers models.
  
  Args:
      model_wrapper (:class:`~textattack.models.wrappers.ModelWrapper`):
          Model wrapper containing both the model and the tokenizer.
      task_type (:obj:`str`, `optional`, defaults to :obj:`"classification"`):
          The task that the model is trained to perform.
          Currently, :class:`~textattack.Trainer` supports two tasks: (1) :obj:`"classification"`, (2) :obj:`"regression"`.
      attack (:class:`~textattack.Attack`):
          :class:`~textattack.Attack` used to generate adversarial examples for training.
      train_dataset (:class:`~textattack.datasets.Dataset`):
          Dataset for training.
      eval_dataset (:class:`~textattack.datasets.Dataset`):
          Dataset for evaluation
      training_args (:class:`~textattack.TrainingArgs`):
          Arguments for training.
  
  Example::
  
      >>> import textattack
      >>> import transformers
  
      >>> model = transformers.AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
      >>> tokenizer = transformers.AutoTokenizer.from_pretrained("bert-base-uncased")
      >>> model_wrapper = textattack.models.wrappers.HuggingFaceModelWrapper(model, tokenizer)
  
      >>> # We only use DeepWordBugGao2018 to demonstration purposes.
      >>> attack = textattack.attack_recipes.DeepWordBugGao2018.build(model_wrapper)
      >>> train_dataset = textattack.datasets.HuggingFaceDataset("imdb", split="train")
      >>> eval_dataset = textattack.datasets.HuggingFaceDataset("imdb", split="test")
  
      >>> # Train for 3 epochs with 1 initial clean epochs, 1000 adversarial examples per epoch, learning rate of 5e-5, and effective batch size of 32 (8x4).
      >>> training_args = textattack.TrainingArgs(
      ...     num_epochs=3,
      ...     num_clean_epochs=1,
      ...     num_train_adv_examples=1000,
      ...     learning_rate=5e-5,
      ...     per_device_train_batch_size=8,
      ...     gradient_accumulation_steps=4,
      ...     log_to_tb=True,
      ... )
  
      >>> trainer = textattack.Trainer(
      ...     model_wrapper,
      ...     "classification",
      ...     attack,
      ...     train_dataset,
      ...     eval_dataset,
      ...     training_args
      ... )
      >>> trainer.train()
  
  .. note::
      When using :class:`~textattack.Trainer` with `parallel=True` in :class:`~textattack.TrainingArgs`,
      make sure to protect the “entry point” of the program by using :obj:`if __name__ == '__main__':`.
      If not, each worker process used for generating adversarial examples will execute the training code again.
class_docstr_tok: ["Trainer","is","training","and","eval","loop","for","adversarial","training",".","It","is","designed","to","work","with","PyTorch","and","Transformers","models",".","Args",":","model_wrapper","(",":","class",":","`","~textattack.models.wrappers.ModelWrapper","`",")",":","Model","wrapper","containing","both","the","model","and","the","tokenizer",".","task_type","(",":","obj",":","`","str","`",",","`","optional","`",",","defaults","to",":","obj",":","`","``","classification","''","`",")",":","The","task","that","the","model","is","trained","to","perform",".","Currently",",",":","class",":","`","~textattack.Trainer","`","supports","two","tasks",":","(","1",")",":","obj",":","`","``","classification","''","`",",","(","2",")",":","obj",":","`","``","regression","''","`",".","attack","(",":","class",":","`","~textattack.Attack","`",")",":",":","class",":","`","~textattack.Attack","`","used","to","generate","adversarial","examples","for","training",".","train_dataset","(",":","class",":","`","~textattack.datasets.Dataset","`",")",":","Dataset","for","training",".","eval_dataset","(",":","class",":","`","~textattack.datasets.Dataset","`",")",":","Dataset","for","evaluation","training_args","(",":","class",":","`","~textattack.TrainingArgs","`",")",":","Arguments","for","training",".","Example",":",":",">",">",">","import","textattack",">",">",">","import","transformers",">",">",">","model","=","transformers.AutoModelForSequenceClassification.from_pretrained","(","``","bert-base-uncased","''",")",">",">",">","tokenizer","=","transformers.AutoTokenizer.from_pretrained","(","``","bert-base-uncased","''",")",">",">",">","model_wrapper","=","textattack.models.wrappers.HuggingFaceModelWrapper","(","model",",","tokenizer",")",">",">",">","#","We","only","use","DeepWordBugGao2018","to","demonstration","purposes",".",">",">",">","attack","=","textattack.attack_recipes.DeepWordBugGao2018.build","(","model_wrapper",")",">",">",">","train_dataset","=","textattack.datasets.HuggingFaceDataset","(","``","imdb","''",",","split=","''","train","''",")",">",">",">","eval_dataset","=","textattack.datasets.HuggingFaceDataset","(","``","imdb","''",",","split=","''","test","''",")",">",">",">","#","Train","for","3","epochs","with","1","initial","clean","epochs",",","1000","adversarial","examples","per","epoch",",","learning","rate","of","5e-5",",","and","effective","batch","size","of","32","(","8x4",")",".",">",">",">","training_args","=","textattack.TrainingArgs","(","...","num_epochs=3",",","...","num_clean_epochs=1",",","...","num_train_adv_examples=1000",",","...","learning_rate=5e-5",",","...","per_device_train_batch_size=8",",","...","gradient_accumulation_steps=4",",","...","log_to_tb=True",",","...",")",">",">",">","trainer","=","textattack.Trainer","(","...","model_wrapper",",","...","``","classification","''",",","...","attack",",","...","train_dataset",",","...","eval_dataset",",","...","training_args","...",")",">",">",">","trainer.train","(",")","..","note",":",":","When","using",":","class",":","`","~textattack.Trainer","`","with","`","parallel=True","`","in",":","class",":","`","~textattack.TrainingArgs","`",",","make","sure","to","protect","the","\u201c","entry","point","\u201d","of","the","program","by","using",":","obj",":","`","if","__name__","==","'__main__","'",":","`",".","If","not",",","each","worker","process","used","for","generating","adversarial","examples","will","execute","the","training","code","again","."]
code: def _save_model_checkpoint(
      self,
      model,
      tokenizer,
      step=None,
      epoch=None,
      best=False,
      last=False,
  ):
      # Save model checkpoint
      if step:
          dir_name = f"checkpoint-step-{step}"
      if epoch:
          dir_name = f"checkpoint-epoch-{epoch}"
      if best:
          dir_name = "best_model"
      if last:
          dir_name = "last_model"
  
      output_dir = os.path.join(self.training_args.output_dir, dir_name)
      if not os.path.exists(output_dir):
          os.makedirs(output_dir)
  
      if isinstance(model, torch.nn.DataParallel):
          model = model.module
  
      if isinstance(
          model, (WordCNNForClassification, LSTMForClassification)
      ):
          model.save_pretrained(output_dir)
      elif isinstance(model, transformers.PreTrainedModel):
          model.save_pretrained(output_dir)
          tokenizer.save_pretrained(output_dir)
      else:
          state_dict = {
              k: v.cpu() for k, v in model.state_dict().items()
          }
          torch.save(
              state_dict,
              os.path.join(output_dir, "pytorch_model.bin"),
          )
  
imports: import collections
  import json
  import logging
  import math
  import os
  import scipy
  import torch
  import tqdm
  import transformers
  import textattack
  from textattack.shared.utils import logger
  from .attack import Attack
  from .attack_args import AttackArgs
  from .attack_results import MaximizedAttackResult, SuccessfulAttackResult
  from .attacker import Attacker
  from .model_args import HUGGINGFACE_MODELS
  from .models.helpers import LSTMForClassification, WordCNNForClassification
  from .models.wrappers import ModelWrapper
  from .training_args import CommandLineTrainingArgs, TrainingArgs
label_desc: Use image node_id 4 for calling the Trainer obj's underlying member method code with example usage: obj._save_model_checkpoint(model, tokenizer, step, epoch, best, last) without return types