class_docstr: Base class for model saving operations
  
  Inherited classes must implement private methods:
  * `_save`
  * `_rm_checkpoint
class_docstr_tok: ["Base","class","for","model","saving","operations","Inherited","classes","must","implement","private","methods",":","*","`","_save","`","*","`","_rm_checkpoint"]
docstr: Main entry point for model saver
  
  It wraps the `_save` method with checks and apply `keep_checkpoint`
  related logic
docstr_tok: ["Main","entry","point","for","model","saver","It","wraps","the","`","_save","`","method","with","checks","and","apply","`","keep_checkpoint","`","related","logic"]
code: def save(self, step, moving_average=None):
      """Main entry point for model saver
  
      It wraps the `_save` method with checks and apply `keep_checkpoint`
      related logic
      """
  
      if self.keep_checkpoint == 0 or step == self.last_saved_step:
          return
  
      save_model = self.model
      if moving_average:
          model_params_data = []
          for avg, param in zip(
              moving_average, save_model.parameters()
          ):
              model_params_data.append(param.data)
              param.data = avg.data
  
      if self.save_format == "pytorch":
          ckpt_path, _ = self._save(step, save_model)
      elif self.save_format == "safetensors":
          ckpt_path, model_path = self._st_save(step, save_model)
  
      self.last_saved_step = step
  
      if moving_average:
          for param_data, param in zip(
              model_params_data, save_model.parameters()
          ):
              param.data = param_data
  
      if ckpt_path is not None:  # not None when process id 0
          if self.keep_checkpoint > 0:
              if (
                  len(self.checkpoint_queue)
                  == self.checkpoint_queue.maxlen
              ):
                  todel = self.checkpoint_queue.popleft()
                  self._rm_checkpoint(todel)
                  if self.save_format == "safetensors":
                      todel = self.model_queue.popleft()
                      self._rm_checkpoint(todel)
              self.checkpoint_queue.append(ckpt_path)
              if self.save_format == "safetensors":
                  self.model_queue.append(model_path)
  
file_path: OpenNMT-py/onmt/models/model_saver.py
imports: import os
  import torch
  import re
  from collections import deque
  from onmt.utils.logging import logger
  from onmt.inputters.inputter import vocabs_to_dict
  from onmt.modules.lora import lora_state_dict
label_desc: Use image node_id 2 for calling the ModelSaverBase obj's underlying member method code with example usage: obj.save(step, moving_average) without return types