[[["เข้าใจง่าย","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 UTC"],[],[],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')"]]