Skip to content

Update bp export example #633

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

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 15 additions & 10 deletions examples/export/export_bundled_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


def save_bundled_program(
method_names,
inputs,
exec_prog,
graph_module,
Expand All @@ -30,20 +31,20 @@ def save_bundled_program(
# set for the model. If we wish to test the model with multiple inputs then they can be
# appended to this list. len(inputs) == number of test sets we want to run.
#
# If we have multiple execution plans in this program then we add another list of tuples
# to test that corresponding execution plan. Index of list of tuples will match the index
# of the execution plan against which it will be tested.
# If we have multiple methods in this program then we add another list of tuples to test
# that corresponding method. Index of list of tuples will match the index of the method's name
# in the method_names list forwarded to BundledConfig against which it will be tested.
bundled_inputs = [inputs for _ in range(len(exec_prog.program.execution_plan))]

# For each input tuple we run the graph module and put the resulting output in a list. This
# is repeated over all the tuples present in the input list and then repeated for each execution
# plan we want to test against.
# is repeated over all the tuples present in the input list and then repeated for each method
# name we want to test against.
expected_outputs = [
[[graph_module(*x)] for x in inputs]
for i in range(len(exec_prog.program.execution_plan))
]

bundled_config = BundledConfig(bundled_inputs, expected_outputs)
bundled_config = BundledConfig(method_names, bundled_inputs, expected_outputs)

bundled_program = create_bundled_program(exec_prog.program, bundled_config)
bundled_program_buffer = serialize_from_bundled_program_to_flatbuffer(
Expand All @@ -54,16 +55,18 @@ def save_bundled_program(
file.write(bundled_program_buffer)


def export_to_pte(model_name, model, example_inputs):
def export_to_pte(model_name, model, method_names, example_inputs):
exec_prog = export_to_exec_prog(model, example_inputs)
save_pte_program(exec_prog.buffer, model_name)

# Just as an example to show how multiple input sets can be bundled along, here we
# create a list with the example_inputs tuple used twice. Each instance of example_inputs
# is a Tuple[Union[torch.tenor, int, bool]] which represents one test set for the model.
bundled_inputs = [example_inputs, example_inputs]
print(f"Saving exported program to {model_name}_bundled.pte")
save_bundled_program(bundled_inputs, exec_prog, model, f"{model_name}_bundled.pte")
print(f"Saving exported program to {model_name}_bundled.bp")
save_bundled_program(
method_names, bundled_inputs, exec_prog, model, f"{model_name}_bundled.bp"
)


if __name__ == "__main__":
Expand All @@ -87,4 +90,6 @@ def export_to_pte(model_name, model, example_inputs):
*MODEL_NAME_TO_MODEL[args.model_name]
)

export_to_pte(args.model_name, model, example_inputs)
method_names = ["forward"]

export_to_pte(args.model_name, model, method_names, example_inputs)