delete the save_torchscript export file, but copy its content to the new export.py for the future maybe

This commit is contained in:
Andrej Karpathy
2023-08-21 05:09:06 +00:00
parent ea44f53568
commit dd61b13e57
2 changed files with 31 additions and 66 deletions
+31
View File
@@ -14,6 +14,9 @@ Among the "output" versions of .bin files:
This script aspires to provide all of these conversions.
"""
import os
import gzip
import shutil
import struct
import argparse
import numpy as np
@@ -343,6 +346,34 @@ def model_export(model, filepath, version):
else:
raise ValueError(f"unknown version {version}")
def torchscript_export(model, filepath, zero_params=False, gzip_output=False):
"""
(This was submitted via a PR earlier. Leaving it here, but "orphaned" for now)
Saves the model as a TorchScript.
The resulting file can be loaded in C++ code and then used for training or
inference with:
#include <torch/script.h>
torch::jit::Module module = torch::jit::load("model.pt")
Note that the serialized model includes the initial parameters and with the default
ModelArgs the file is 59M and gzips down to 55M. If you want to serialize/distribute
the model parameters separately you can zero out the parameters before saving it and
it will gzip down to 780K.
"""
# If requested zero params before saving the model. This is useful in
# conjunction with gzip_output.
if zero_params:
for p in model.parameters():
p.detach().zero_()
torch.jit.save(torch.jit.script(model), filepath)
if gzip_output:
with open(filepath, "rb") as f_in:
with gzip.open(f"{filepath}.gz", "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
os.unlink(filepath)
# -----------------------------------------------------------------------------
# CLI entrypoint