class_docstr: Wrapper class for Hugging Face Transformers PyTorch T5 model.
class_docstr_tok: ["Wrapper","class","for","Hugging","Face","Transformers","PyTorch","T5","model","."]
docstr: Constructor for HfModel class.
  
  Args:
    model_spec: A str to pass into the `pretrained_model_name_or_path`
      argument of `transformers.T5ForConditionalGeneration.from_pretrained`
      (e.g. `"t5-base"` or a path to a previously trained model) or an
      instance of the `transformers.configuration_t5.T5Config` class to use
      to directly construct the `transformers.T5ForConditionalGeneration`
      object.
    model_dir: str, directory to save and load model checkpoints.
    device: `torch.device` on which the model should be run.
docstr_tok: ["Constructor","for","HfModel","class",".","Args",":","model_spec",":","A","str","to","pass","into","the","`","pretrained_model_name_or_path","`","argument","of","`","transformers.T5ForConditionalGeneration.from_pretrained","`","(","e.g",".","`","``","t5-base","''","`","or","a","path","to","a","previously","trained","model",")","or","an","instance","of","the","`","transformers.configuration_t5.T5Config","`","class","to","use","to","directly","construct","the","`","transformers.T5ForConditionalGeneration","`","object",".","model_dir",":","str",",","directory","to","save","and","load","model","checkpoints",".","device",":","`","torch.device","`","on","which","the","model","should","be","run","."]
code: def __init__(self, model_spec, model_dir, device):
      """Constructor for HfModel class.
  
      Args:
        model_spec: A str to pass into the `pretrained_model_name_or_path`
          argument of `transformers.T5ForConditionalGeneration.from_pretrained`
          (e.g. `"t5-base"` or a path to a previously trained model) or an
          instance of the `transformers.configuration_t5.T5Config` class to use
          to directly construct the `transformers.T5ForConditionalGeneration`
          object.
        model_dir: str, directory to save and load model checkpoints.
        device: `torch.device` on which the model should be run.
      """
      # We have to import transformers here because it has a side effect of
      # creating a TensorFlow graph, which prevents eager execution from being
      # enabled in files that import hf_model.py
      import transformers  # pylint: disable=import-outside-toplevel,g-import-not-at-top
  
      if isinstance(model_spec, str):
          self._model = (
              transformers.T5ForConditionalGeneration.from_pretrained(
                  model_spec
              )
          )
      elif isinstance(model_spec, transformers.T5Config):
          self._model = transformers.T5ForConditionalGeneration(
              model_spec
          )
      else:
          raise ValueError("model_spec should be a string or T5Config.")
  
      tf.io.gfile.makedirs(model_dir)
      self._writer = torch.utils.tensorboard.writer.SummaryWriter(
          model_dir
      )
      self._model_dir = model_dir
      self._device = device
      if self._device.type == "cuda":
          self._model.cuda()
      self._step = 0
      self.load_latest_checkpoint()
      self.to_tensor = functools.partial(
          torch.as_tensor, device=self._device, dtype=torch.long
      )
  
imports: import functools
  import itertools
  import os
  import re
  import time
  from absl import logging
  import mesh_tensorflow.transformer.dataset
  import seqio
  import t5.data
  from t5.models import utils
  from t5.models.t5_model import T5Model
  import tensorflow.compat.v1
  import tensorflow_datasets
  import torch
  import torch.utils.tensorboard
label_desc: Use image node_id 1 to create a new HfPyTorchModel object from inherited base classes: T5Model with example: obj = HfPyTorchModel(model_spec, model_dir, device)