TorchScript وtorch.compile
جمّد النموذج وسرّعه لخدمة التنبؤات
TorchScript وtorch.compile درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Production Needs a Frozen Model
Your training code is flexible Python, but servers want something fast and stable. TorchScript freezes your model into a portable, optimized form. 🚀
What TorchScript Actually Is
TorchScript is a serializable, optimizable subset of Python that runs without a Python interpreter, so your model can ship to C++ servers and mobile.
Tracing: Record One Real Run
Tracing runs your model on an example input and records every operation it performs, capturing the path the data actually took.
import torch
traced = torch.jit.trace(model, example_input)Tracing Misses Control Flow
Because tracing only follows one run, it silently ignores if-statements and loops that depend on the data. Those branches just vanish.
Scripting: Read the Logic
Scripting compiles your code directly, preserving every loop and condition. Use it when control flow depends on the input values.
scripted = torch.jit.script(model)Save and Load Anywhere
A scripted or traced model saves to a single self-contained file you can load on any machine, no original class definition required.
scripted.save('model.pt')
loaded = torch.jit.load('model.pt')Meet torch.compile
The modern alternative is torch.compile, which speeds up your model with one line while keeping your code plain Python.
model = torch.compile(model)How torch.compile Speeds Things Up
Under the hood torch.compile fuses many small operations into fewer optimized kernels, cutting overhead and boosting throughput on GPU.
The First Call Is Slow
The first forward pass after torch.compile is slow because it compiles in the background. Every call after that runs much faster. ⏳
Always Set Eval Mode First
Before you trace, script, or compile for serving, call model.eval() so dropout and batch norm behave correctly for inference.
model.eval()
traced = torch.jit.trace(model, x)Which One Should You Reach For?
Choose TorchScript when you need a Python-free deployment file, and torch.compile when you want a quick speedup inside Python.
Quick Check
One method captures only the path one example takes. Which is it?
Recap: Freeze and Accelerate
You learned to freeze a model with TorchScript via tracing or scripting, save it anywhere, and accelerate inference in Python with torch.compile. 🎉
الأسئلة الشائعة
هل درس «TorchScript وtorch.compile» مجاني؟
نعم — نص درس «TorchScript وtorch.compile» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «TorchScript وtorch.compile»؟
جمّد النموذج وسرّعه لخدمة التنبؤات تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «TorchScript وtorch.compile»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- TorchScript وtorch.compile
- التصدير إلى ONNX
- التكميم لنماذج أصغر وأسرع
- تقديم النموذج باستخدام FastAPI