|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +from executorch.exir import to_edge |
| 8 | +from torch import int64, long, no_grad, randint, Tensor, zeros |
| 9 | +from torch.export import export, ExportedProgram |
| 10 | +from torch.nn.attention import sdpa_kernel, SDPBackend |
| 11 | +from torchtune.models.phi3._model_builders import lora_phi3_mini |
| 12 | + |
| 13 | + |
| 14 | +@no_grad() |
| 15 | +def export_mini_phi3_lora(model) -> None: |
| 16 | + """ |
| 17 | + Export the example mini-phi3 with LoRA model to executorch. |
| 18 | +
|
| 19 | + Note: need to use the SDPBackend's custom kernel for sdpa (scalable |
| 20 | + dot product attention) because the default sdpa kernel used in the |
| 21 | + model results in a internally mutating graph. |
| 22 | + """ |
| 23 | + model.eval() |
| 24 | + # 1. torch.export: Defines the program with the ATen operator set. |
| 25 | + print("Exporting to aten dialect") |
| 26 | + example_args = (randint(0, 100, (1, 100), dtype=long),) |
| 27 | + with sdpa_kernel([SDPBackend.MATH]): |
| 28 | + aten_dialect: ExportedProgram = export(model, example_args) |
| 29 | + |
| 30 | + # 2. to_edge: Make optimizations for Edge devices. |
| 31 | + print("Lowering to edge dialect") |
| 32 | + edge_program = to_edge(aten_dialect) |
| 33 | + |
| 34 | + # 3. to_executorch: Convert the graph to an ExecuTorch program. |
| 35 | + print("Exporting to executorch") |
| 36 | + executorch_program = edge_program.to_executorch() |
| 37 | + |
| 38 | + # 4. Save the compiled .pte program. |
| 39 | + print("Saving to mini_phi3_lora.pte") |
| 40 | + with open("mini_phi3_lora.pte", "wb") as file: |
| 41 | + file.write(executorch_program.buffer) |
| 42 | + |
| 43 | + print("Done.") |
| 44 | + |
| 45 | + |
| 46 | +def run_mini_phi3_lora(model) -> Tensor: |
| 47 | + """Run the model and return the result.""" |
| 48 | + args = zeros([3072, 1], dtype=int64) |
| 49 | + model.eval() |
| 50 | + res = model(args) |
| 51 | + return res |
| 52 | + |
| 53 | + |
| 54 | +def main() -> None: |
| 55 | + mini_lora_model = lora_phi3_mini( |
| 56 | + lora_attn_modules=[ |
| 57 | + "q_proj", |
| 58 | + ] |
| 59 | + ) |
| 60 | + export_mini_phi3_lora(mini_lora_model) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
0 commit comments