AI Edge Torch هي مكتبة تتيح لك تحويل نماذج PyTorch إلى تنسيق .tflite
، ما يتيح لك تشغيل هذه النماذج باستخدام LiteRT وMediaPipe.
ويُعدّ ذلك مفيدًا بشكل خاص للمطوّرين الذين ينشئون تطبيقات متوافق مع الأجهزة الجوّالة تعمل على تشغيل النماذج
تمامًا على الجهاز. يوفّر AI Edge Torch تغطية واسعة النطاق لوحدة المعالجة المركزية (CPU)، مع دعم GPU
ووحدة المعالجة العصبية (NPU) الأوّلي.
إذا كنت تحوّل نماذج لغوية كبيرة (LLM) أو نماذج مستندة إلى تقنية "المحوِّل" على وجه التحديد، استخدِم Generative Torch API، التي تعالج تفاصيل التحويل الخاصة بتقنية "المحوِّل"، مثل إنشاء النماذج ومقدار الترميز.
سير عمل الإحالات الناجحة
توضّح الخطوات التالية عملية تحويل بسيطة من البداية إلى النهاية لنموذج PyTorch
إلى LiteRT.
استيراد ميزة "مصباح الذكاء الاصطناعي"
ابدأ باستيراد حزمة pip الخاصة بـ AI Edge Torch (ai-edge-torch) مع
PyTorch.
importai_edge_torchimporttorch
في هذا المثال، نحتاج أيضًا إلى الحِزم التالية:
importnumpyimporttorchvision
إعداد النموذج وتحويله
سنحوّل
ResNet18، وهو
نموذج شائع للتعرّف على الصور.
تاريخ التعديل الأخير: 2025-07-24 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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')"]]