Skip to content

XOR model export in CI #6756

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

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions extension/training/examples/XOR/export_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
from torch.export.experimental import _export_forward_backward


def _export_model():
net = TrainingNet(Net())
x = torch.randn(1, 2)

# Captures the forward graph. The graph will look similar to the model definition now.
# Will move to export_for_training soon which is the api planned to be supported in the long term.
ep = export(net, (x, torch.ones(1, dtype=torch.int64)))
# Captures the backward graph. The exported_program now contains the joint forward and backward graph.
ep = _export_forward_backward(ep)
# Lower the graph to edge dialect.
ep = to_edge(ep)
# Lower the graph to executorch.
ep = ep.to_executorch()


def main() -> None:
torch.manual_seed(0)
parser = argparse.ArgumentParser(
Expand All @@ -32,18 +47,7 @@ def main() -> None:
)
args = parser.parse_args()

net = TrainingNet(Net())
x = torch.randn(1, 2)

# Captures the forward graph. The graph will look similar to the model definition now.
# Will move to export_for_training soon which is the api planned to be supported in the long term.
ep = export(net, (x, torch.ones(1, dtype=torch.int64)))
# Captures the backward graph. The exported_program now contains the joint forward and backward graph.
ep = _export_forward_backward(ep)
# Lower the graph to edge dialect.
ep = to_edge(ep)
# Lower the graph to executorch.
ep = ep.to_executorch()
ep = _export_model()

# Write out the .pte file.
os.makedirs(args.outdir, exist_ok=True)
Expand Down
2 changes: 1 addition & 1 deletion extension/training/examples/XOR/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def define_common_targets():
runtime.python_library(
name = "export_model_lib",
srcs = ["export_model.py"],
visibility = [],
visibility = ["//executorch/extension/training/examples/XOR/..."],
deps = [
":model",
"//caffe2:torch",
Expand Down
15 changes: 15 additions & 0 deletions extension/training/examples/XOR/test/TARGETS
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

oncall("executorch")

runtime.python_test(
name = "test",
srcs = ["test_export.py"],
visibility = ["//executorch/extension/training/examples/XOR/test/..."],
deps = [
"//caffe2:torch",
"//executorch/exir:lib",
"//executorch/extension/training:lib",
"//executorch/extension/training/examples/XOR:export_model_lib",
],
)
18 changes: 18 additions & 0 deletions extension/training/examples/XOR/test/test_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-unsafe

import unittest

from executorch.extension.training.examples.XOR.export_model import _export_model


class TestXORExport(unittest.TestCase):
def test(self):
_ = _export_model()
# Expect that we reach this far without an exception being thrown.
self.assertTrue(True)
Loading