[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-24 (世界標準時間)。"],[],[],null,["# Convert PyTorch models to LiteRT\n\nAI Edge Torch is a library that lets you convert PyTorch models into a `.tflite`\nformat, enabling you to run those models with LiteRT and MediaPipe.\nThis is especially helpful for developers creating mobile apps that run models\ncompletely on-device. AI Edge Torch offers broad CPU coverage, with initial GPU\nand NPU support.\n\nTo get started converting PyTorch models to LiteRT, use the [Pytorch converter\nquickstart](./pytorch_to_tflite). For more information, see the AI Edge Torch\n[GitHub repo](https://github.com/google-ai-edge/ai-edge-torch/).\n\nIf you are specifically converting Large Language Models (LLMs) or\ntransformer-based models, use the [Generative Torch API](./edge_generative),\nwhich handles transformer-specific conversion details like model authoring and\nquantization.\n\nConversion workflow\n-------------------\n\nThe following steps demonstrate a simple end-to-end conversion of a PyTorch\nmodel to LiteRT.\n\n### Import AI Edge Torch\n\nStart by importing the AI Edge Torch (`ai-edge-torch`) pip package, along with\nPyTorch. \n\n import ai_edge_torch\n import torch\n\nFor this example, we also require the following packages: \n\n import numpy\n import torchvision\n\n### Initialize and convert the model\n\nWe will convert\n[ResNet18](https://pytorch.org/vision/main/models/generated/torchvision.models.resnet18.html),\na popular image recognition model. \n\n resnet18 = torchvision.models.resnet18(torchvision.models.ResNet18_Weights.IMAGENET1K_V1).eval()\n\nUse the `convert` method from the AI Edge Torch library to convert the PyTorch\nmodel. \n\n sample_input = (torch.randn(1, 3, 224, 224),)\n edge_model = ai_edge_torch.convert(resnet18.eval(), sample_input)\n\n### Use the model\n\nAfter converting the Pytorch model, you can run inferences with the new\nconverted LiteRT model. \n\n output = edge_model(*sample_inputs)\n\nYou can export and save the converted model in the `.tflite` format for future\nuse. \n\n edge_model.export('resnet.tflite')"]]