class_docstr: This class mirrors deepspeed functionality. Not all changes are implemented in this version.
  
  See TorchEngineFull for more modifications.
class_docstr_tok: ["This","class","mirrors","deepspeed","functionality",".","Not","all","changes","are","implemented","in","this","version",".","See","TorchEngineFull","for","more","modifications","."]
docstr: Analogous to save_final_model, but save model to hugginface hub.
docstr_tok: ["Analogous","to","save_final_model",",","but","save","model","to","hugginface","hub","."]
code: def push_to_hub(self, tokenizer, cfg, dryrun=False):
      """Analogous to save_final_model, but save model to hugginface hub."""
      from huggingface_hub import HfApi
      from io import BytesIO
  
      api = HfApi()
  
      if not dryrun:
          log.info(
              f"Pushing model to hub repository {cfg.impl.hf_directoy_name}."
          )
          final_state_dict = self.retrieve_model_state_dict()
          self.model.load_state_dict(final_state_dict)
  
          # Push model with safetensors:
          # This is a manual modification of model.push_to_hub which doesn't support safetensors yet
          repo_id = cfg.impl.hf_directoy_name
          if os.path.isdir(repo_id):
              working_dir = repo_id
              repo_id = repo_id.split(os.path.sep)[-1]
          else:
              working_dir = repo_id.split("/")[-1]
          repo_id = self.model._create_repo(repo_id)
          use_temp_dir = not os.path.isdir(working_dir)
          with working_or_temp_dir(
              working_dir=working_dir, use_temp_dir=use_temp_dir
          ) as work_dir:
              files_timestamps = self.model._get_files_timestamps(
                  work_dir
              )
              # Save all files.
              self.model.save_pretrained(
                  work_dir,
                  max_shard_size="10GB",
                  safe_serialization=True,
                  state_dict=self.retrieve_model_state_dict(),
              )
              self.model._upload_modified_files(
                  work_dir,
                  repo_id,
                  files_timestamps,
                  commit_message=None,
                  token=None,
                  create_pr=False,
              )
          # Push tokenizer:
          tokenizer.push_to_hub(cfg.impl.hf_directoy_name)
          # Push config files:
          for config_group, config_name in zip(
              [cfg.arch, cfg.data, cfg.train], ["arch", "data", "train"]
          ):
              buffer = BytesIO()
              buffer.write(
                  json.dumps(
                      OmegaConf.to_container(
                          config_group, resolve=True
                      ),
                      indent=4,
                  ).encode()
              )
              api.upload_file(
                  path_or_fileobj=buffer,
                  path_in_repo=f"{config_name}_budget_hours_{cfg.budget}.json",
                  repo_id=f"{api.whoami()['name']}/{cfg.impl.hf_directoy_name}",  # there has to be a better way to do this, but ...
                  repo_type="model",
              )
      else:
          log.info(
              f"Skipping huggingface upload in dryrun state. Would upload to {cfg.impl.hf_directoy_name}."
          )
  
imports: import torch
  import torch._inductor.utils
  import os
  import json
  from omegaconf import OmegaConf
  from functools import partial
  from contextlib import nullcontext
  import time
  import logging
  import transformers
  from safetensors.torch import load_file, save_file
  from transformers.utils.generic import working_or_temp_dir
  from torch.distributed.optim import ZeroRedundancyOptimizer
  from .utils import group_parameters, prepare_pretraining_dataloader, update_ema, updated_latest_weight_average
  from .optimizers.schedulers import get_schedule_fn
  from .optimizers import Adahessian, AdamWScale, Shampoo, LARS, SAM, ProgressiveBatching, AGD, Sophia
  import warnings
label_desc: Use image node_id 18 for calling the TorchEngineMinimal obj's underlying member method code with example usage: obj.push_to_hub(tokenizer, cfg, dryrun) without return types