Open BowenBao opened 1 year ago
Hi Bowen, for now, you can do this by explicitly using the save_as_external_data
option when you call onnx.save
. This converts the model to use external data. Calling proto2python on this model will do the right thing. Eg., use a save
call as below:
def save(model, folder):
datafile = os.path.join(folder, "external_data.onnx")
modelfile = os.path.join(folder, "model.onnx")
onnx.save(model, modelfile, save_as_external_data=True, all_tensors_to_one_file=True, size_threshold=64, convert_attribute=True, location=datafile)
pymodel = proto2python(model, use_operators=True, inline_const=True)
with open(os.path.join(folder, "model.py"), "w") as f:
f.write(pymodel)
The inline_const
option in proto2python will try to produce more readable code by inlining small constants (typically shapes).
Otherwise generated python code is too large with model parameters defined explicitly.
Example with gpt2 model.
Produces