Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ONNX] Support constant, buffer, parameters in ExportedProgram serialization to ONNX tensor #126473

Open
titaiwangms opened this issue May 16, 2024 · 2 comments
Assignees
Labels
module: onnx Related to torch.onnx onnx-triaged triaged by ONNX team triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@titaiwangms
Copy link
Collaborator

titaiwangms commented May 16, 2024

torch.onnx.dynamo_export currently supports ExportedProgram with the help of input adapter to prepend the lifted constant, buffer, and parameters:

class PrependParamsBuffersConstantAotAutogradInputStep(InputAdaptStep):

So the inputs need to be adapted before the onnx model is executed:

onnx_input = self.adapt_torch_inputs_to_onnx(
*args, model_with_state_dict=model_with_state_dict, **kwargs
)
options = options or ONNXRuntimeOptions()
providers = (
options.execution_providers or onnxruntime.get_available_providers()
)
ort_session = onnxruntime.InferenceSession(onnx_model, providers=providers)

However, this temporary workaround should be fixed with actual serialization of the weights and buffers. A potential fix can be serializing the weights and buffers when we save the model to ONNX:

def save(
self,
destination: Union[str, io.BufferedIOBase],
*,
include_initializers: bool = True,
model_state: Optional[Union[Dict[str, Any], str]] = None,
serializer: Optional[ONNXProgramSerializer] = None,
) -> None:
"""Saves the in-memory ONNX model to ``destination`` using specified ``serializer``.
Args:
destination: The destination to save the ONNX model. It can be either a string or a file-like object.
When used with ``model_state``, it must be a string with a full path to the destination.
If `destination` is a string, besides saving the ONNX model into a file, model weights are also stored
in separate files in the same directory as the ONNX model. E.g. for `destination="/path/model.onnx"`,
the initializers are saved in "/path/" folder along with "onnx.model".
include_initializers: Whether to include initializers in the ONNX graph as external data.
Cannot be combined with `model_state_dict`.
model_state: The state_dict of the PyTorch model containing all weights on it.
It can be either a string with the path to a checkpoint or a dictionary with the actual model state.
The supported file formats are the same as those supported by `torch.load` and `safetensors.safe_open`.
Required when :func:`enable_fake_mode` is used but real initializers are needed on the ONNX graph.
serializer: The serializer to use. If not specified, the model will be serialized as Protobuf.
"""

Related #126071
Related #126479

@titaiwangms titaiwangms added module: onnx Related to torch.onnx onnx-triaged triaged by ONNX team labels May 16, 2024
@titaiwangms
Copy link
Collaborator Author

cc @justinchuby @shubhambhokare1

@titaiwangms
Copy link
Collaborator Author

Potentially, user might want us to unlift parameters/weights when the model is small.

@mikaylagawarecki mikaylagawarecki added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 20, 2024
@justinchuby justinchuby self-assigned this May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: onnx Related to torch.onnx onnx-triaged triaged by ONNX team triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

3 participants