in pytext/task/tasks.py [0:0]
def torchscript_export(self, model, export_path=None, export_config=None): # noqa
# unpack export config
# unpack export config
if export_config is None:
export_config = ExportConfig()
quantize = export_config.torchscript_quantize
accelerate = export_config.accelerate
seq_padding_control = export_config.seq_padding_control
batch_padding_control = export_config.batch_padding_control
if (accelerate is not None) and (accelerate != []):
raise RuntimeError(
"old-style task.py does not support export for NNPI accelerators"
)
cuda.CUDA_ENABLED = False
model.cpu()
optimizer = self.trainer.optimizer
optimizer.pre_export(model)
model.eval()
model.prepare_for_onnx_export_()
unused_raw_batch, batch = next(
iter(self.data.batches(Stage.TRAIN, load_early=True))
)
inputs = model.onnx_trace_input(batch)
model(*inputs)
if quantize:
model.quantize()
if self.trace_both_encoders:
trace = jit.trace(model, inputs)
else:
trace = jit.trace(model.encoder1, (inputs[0],))
if hasattr(model, "torchscriptify"):
trace = model.torchscriptify(
self.data.tensorizers, trace, self.trace_both_encoders
)
if seq_padding_control is not None:
if hasattr(trace, "set_padding_control"):
trace.set_padding_control("sequence_length", seq_padding_control)
else:
print(
"Padding_control not supported by model. Ignoring padding_control"
)
if batch_padding_control is not None:
if hasattr(trace, "set_padding_control"):
trace.set_padding_control("batch_length", batch_padding_control)
else:
print(
"Padding_control not supported by model. Ignoring padding_control"
)
trace.apply(lambda s: s._pack() if s._c._has_method("_pack") else None)
if export_path is not None:
print(f"Saving torchscript model to: {export_path}")
with PathManager.open(export_path, "wb") as f:
torch.jit.save(trace, f)
return trace