examples/research_projects/quantization-qdqbert/trainer_quant_qa.py/QuestionAnsweringTrainer/save_onnx
class QuestionAnsweringTrainer:    def save_onnx(self, output_dir="./"):
        eval_dataset = self.eval_dataset
        eval_dataloader = self.get_eval_dataloader(eval_dataset)

        batch = next(iter(eval_dataloader))

        # saving device - to make it consistent
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

        # convert to tuple
        input_tuple = tuple(v.to(device) for k, v in batch.items())

        logger.info("Converting model to be onnx compatible")
        from pytorch_quantization.nn import TensorQuantizer

        TensorQuantizer.use_fb_fake_quant = True

        model = self.model.to(device)

        model.eval()
        model.float()

        model_to_save = model.module if hasattr(model, "module") else model
        quant_trainer.configure_model(model_to_save, self.quant_trainer_args)

        output_model_file = os.path.join(output_dir, "model.onnx")
        logger.info(f"exporting model to {output_model_file}")

        axes = {0: "batch_size", 1: "seq_len"}

        torch.onnx.export(
            model_to_save,
            input_tuple,
            output_model_file,
            export_params=True,
            opset_version=13,
            do_constant_folding=True,
            input_names=["input_ids", "attention_mask", "token_type_ids"],
            output_names=["output_start_logits", "output_end_logits"],
            dynamic_axes={
                "input_ids": axes,
                "attention_mask": axes,
                "token_type_ids": axes,
                "output_start_logits": axes,
                "output_end_logits": axes,
            },
            verbose=True,
        )
        logger.info("onnx export finished")