def _save_model(self, output_dir=None, optimizer=None, scheduler=None, model=None, results=None):
    if (not output_dir):
        output_dir = self.args.output_dir
    os.makedirs(output_dir, exist_ok=True)
    logger.info(f'Saving model into {output_dir}')
    if (model and (not self.args.no_save)):
        model_to_save = (model.module if hasattr(model, 'module') else model)
        self._save_model_args(output_dir)
        if (self.args.model_type in ['bart', 'marian']):
            os.makedirs(os.path.join(output_dir), exist_ok=True)
            model_to_save.save_pretrained(output_dir)
            self.config.save_pretrained(output_dir)
            if (self.args.model_type == 'bart'):
                self.encoder_tokenizer.save_pretrained(output_dir)
        else:
            os.makedirs(os.path.join(output_dir, 'encoder'), exist_ok=True)
            os.makedirs(os.path.join(output_dir, 'decoder'), exist_ok=True)
            self.encoder_config.save_pretrained(os.path.join(output_dir, 'encoder'))
            self.decoder_config.save_pretrained(os.path.join(output_dir, 'decoder'))
            model_to_save = (self.model.encoder.module if hasattr(self.model.encoder, 'module') else self.model.encoder)
            model_to_save.save_pretrained(os.path.join(output_dir, 'encoder'))
            model_to_save = (self.model.decoder.module if hasattr(self.model.decoder, 'module') else self.model.decoder)
            model_to_save.save_pretrained(os.path.join(output_dir, 'decoder'))
            self.encoder_tokenizer.save_pretrained(os.path.join(output_dir, 'encoder'))
            self.decoder_tokenizer.save_pretrained(os.path.join(output_dir, 'decoder'))
        torch.save(self.args, os.path.join(output_dir, 'training_args.bin'))
        if (optimizer and scheduler and self.args.save_optimizer_and_scheduler):
            torch.save(optimizer.state_dict(), os.path.join(output_dir, 'optimizer.pt'))
            torch.save(scheduler.state_dict(), os.path.join(output_dir, 'scheduler.pt'))
    if results:
        output_eval_file = os.path.join(output_dir, 'eval_results.txt')
        with open(output_eval_file, 'w') as writer:
            for key in sorted(results.keys()):
                writer.write('{} = {}\n'.format(key, str(results[key])))
def _save_model(self, output_dir=None, optimizer=None, scheduler=None, model=None, results=None):
    if (not output_dir):
        output_dir = self.args.output_dir
    os.makedirs(output_dir, exist_ok=True)
    logger.info(f'Saving model into {output_dir}')
    if (model and (not self.args.no_save)):
        model_to_save = (model.module if hasattr(model, 'module') else model)
        self._save_model_args(output_dir)
        if (self.args.model_type in ['bart', 'marian']):
            os.makedirs(os.path.join(output_dir), exist_ok=True)
            model_to_save.save_pretrained(output_dir)
            self.config.save_pretrained(output_dir)
            if (self.args.model_type == 'bart'):
                self.encoder_tokenizer.save_pretrained(output_dir)
        else:
            os.makedirs(os.path.join(output_dir, 'encoder'), exist_ok=True)
            os.makedirs(os.path.join(output_dir, 'decoder'), exist_ok=True)
            self.encoder_config.save_pretrained(os.path.join(output_dir, 'encoder'))
            self.decoder_config.save_pretrained(os.path.join(output_dir, 'decoder'))
            model_to_save = (self.model.encoder.module if hasattr(self.model.encoder, 'module') else self.model.encoder)
            model_to_save.save_pretrained(os.path.join(output_dir, 'encoder'))
            model_to_save = (self.model.decoder.module if hasattr(self.model.decoder, 'module') else self.model.decoder)
            model_to_save.save_pretrained(os.path.join(output_dir, 'decoder'))
            self.encoder_tokenizer.save_pretrained(os.path.join(output_dir, 'encoder'))
            self.decoder_tokenizer.save_pretrained(os.path.join(output_dir, 'decoder'))
        torch.save(self.args, os.path.join(output_dir, 'training_args.bin'))
        if (optimizer and scheduler and self.args.save_optimizer_and_scheduler):
            torch.save(optimizer.state_dict(), os.path.join(output_dir, 'optimizer.pt'))
            torch.save(scheduler.state_dict(), os.path.join(output_dir, 'scheduler.pt'))
    if results:
        output_eval_file = os.path.join(output_dir, 'eval_results.txt')
        with open(output_eval_file, 'w') as writer:
            for key in sorted(results.keys()):
                writer.write('{} = {}\n'.format(key, str(results[key])))