-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On-Device Training - Enable loading from buffer (#16417)
- Loading branch information
Showing
23 changed files
with
524 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+2.24 KB
onnxruntime/test/testdata/training_api/ort_format/optimizer_model.ort
Binary file not shown.
70 changes: 70 additions & 0 deletions
70
onnxruntime/test/testdata/training_api/ort_format/prepare_artifacts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
"""This file is used to generate test data for ort format model tests in | ||
orttraining/orttraining/test/training_api/core/training_capi_tests.cc.""" | ||
|
||
import onnx | ||
import torch | ||
import torch.nn as nn | ||
|
||
from onnxruntime.training import artifacts | ||
|
||
|
||
class SimpleNet(nn.Module): | ||
def __init__(self, input_size, hidden_size, output_size): | ||
super().__init__() | ||
self.fc1 = nn.Linear(input_size, hidden_size) | ||
self.relu = nn.ReLU() | ||
self.fc2 = nn.Linear(hidden_size, output_size) | ||
|
||
def forward(self, x): | ||
out = self.fc1(x) | ||
out = self.relu(out) | ||
out = self.fc2(out) | ||
return out | ||
|
||
|
||
def model_export(pt_model, model_path, input_size): | ||
# Generate random input data | ||
input_data = torch.randn(32, input_size) | ||
torch.onnx.export( | ||
pt_model, | ||
input_data, | ||
model_path, | ||
input_names=["input"], | ||
output_names=["output"], | ||
dynamic_axes={"input": {0: "batch_size"}, "output": {0: "batch_size"}}, | ||
) | ||
|
||
|
||
def main(): | ||
# Set the dimensions for input, hidden, and output layers | ||
input_size = 10 | ||
hidden_size = 20 | ||
output_size = 5 | ||
|
||
# Create an instance of the neural network | ||
pt_model = SimpleNet(input_size, hidden_size, output_size) | ||
|
||
train_model_path = "simplenet_training.onnx" | ||
model_export(pt_model, train_model_path, input_size) | ||
|
||
onnx_model = onnx.load(train_model_path) | ||
|
||
requires_grad = ["fc2.weight", "fc2.bias"] | ||
frozen_params = [param.name for param in onnx_model.graph.initializer if param.name not in requires_grad] | ||
|
||
# Generate the training artifacts. | ||
artifacts.generate_artifacts( | ||
onnx_model, | ||
requires_grad=requires_grad, | ||
frozen_params=frozen_params, | ||
loss=artifacts.LossType.CrossEntropyLoss, | ||
optimizer=artifacts.OptimType.AdamW, | ||
ort_format=True, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.