Chapter 4: PyTorch TorchScript

Production Deployment for PyTorch Models

弘益人間 · Benefit All Humanity

4.1 Introduction to TorchScript

TorchScript is PyTorch's way to create serializable and optimizable models from PyTorch code. It bridges the gap between research (Python) and production (C++), enabling deployment in environments without Python dependencies. TorchScript uses JIT (Just-In-Time) compilation to optimize models for inference.

The core philosophy of TorchScript is to provide a path from dynamic, eager execution in Python to static, optimized execution in production environments. This transition is crucial for deploying models at scale, where performance, reliability, and minimal dependencies are paramount.

TorchScript Benefits

4.2 Tracing vs Scripting

TorchScript offers two methods to convert PyTorch models: tracing and scripting. Understanding when to use each method is critical for successful model deployment.

Tracing

Tracing records operations executed during a forward pass with example inputs. It's ideal for models with static computation graphs.

import torch

# Define model
class SimpleModel(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = torch.nn.Linear(10, 5)
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        return self.relu(self.fc(x))

model = SimpleModel()
example_input = torch.randn(1, 10)

# Trace the model
traced_model = torch.jit.trace(model, example_input)

# Save
traced_model.save('traced_model.pt')

# Load and use
loaded = torch.jit.load('traced_model.pt')
output = loaded(torch.randn(1, 10))

# Inspect the traced graph
print(traced_model.graph)
print(traced_model.code)

Scripting

Scripting analyzes the Python source code directly, capturing control flow and dynamic behavior. Use this when your model contains if statements, loops, or other control structures.

class ConditionalModel(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = torch.nn.Linear(10, 5)

    def forward(self, x):
        # Control flow that must be preserved
        if x.sum() > 0:
            return self.fc(x)
        else:
            return x[:, :5]

model = ConditionalModel()

# Script the model (captures control flow)
scripted_model = torch.jit.script(model)

# Save
scripted_model.save('scripted_model.pt')

# Verify control flow is preserved
print(scripted_model.code)

4.3 When to Use Tracing vs Scripting

Choose the right method based on your model's characteristics:

Use Tracing When:

Use Scripting When:

Hybrid Approach:

4.4 Advanced TorchScript Features

Type Annotations

Type annotations are essential for scripting, helping TorchScript understand your code's intent and catch errors early.

import torch
from typing import List, Tuple, Dict, Optional

class AnnotatedModel(torch.nn.Module):
    def forward(
        self,
        x: torch.Tensor,
        hidden: Tuple[torch.Tensor, torch.Tensor]
    ) -> Dict[str, torch.Tensor]:
        h, c = hidden
        output = self.process(x, h)
        return {"output": output, "hidden": c}

    def process(self, x: torch.Tensor, h: torch.Tensor) -> torch.Tensor:
        return x + h

    def batch_process(
        self,
        inputs: List[torch.Tensor],
        mask: Optional[torch.Tensor] = None
    ) -> torch.Tensor:
        """Process a batch with optional masking"""
        result = torch.stack(inputs)
        if mask is not None:
            result = result * mask
        return result

Custom Operators

import torch

@torch.jit.script
def custom_activation(x: torch.Tensor, threshold: float) -> torch.Tensor:
    """Custom gated activation function"""
    return torch.where(x > threshold, x, torch.zeros_like(x))

@torch.jit.script
def swish(x: torch.Tensor, beta: float = 1.0) -> torch.Tensor:
    """Swish activation: x * sigmoid(beta * x)"""
    return x * torch.sigmoid(beta * x)

class ModelWithCustomOp(torch.nn.Module):
    def forward(self, x):
        x = custom_activation(x, 0.5)
        x = swish(x, beta=1.5)
        return x

model = ModelWithCustomOp()
scripted = torch.jit.script(model)

4.5 Loading and Using TorchScript Models

In Python

import torch
import numpy as np

# Load model
model = torch.jit.load('model.pt')
model.eval()

# Run inference
with torch.no_grad():
    output = model(input_tensor)

# Inspect the graph
print("Graph structure:")
print(model.graph)

print("\nGenerated code:")
print(model.code)

# Check model parameters
for name, param in model.named_parameters():
    print(f"{name}: {param.shape}")

# Benchmark inference
import time
start = time.time()
for _ in range(100):
    with torch.no_grad():
        _ = model(input_tensor)
elapsed = (time.time() - start) / 100
print(f"Average inference time: {elapsed*1000:.2f}ms")

In C++

#include <torch/script.h>
#include <iostream>
#include <memory>
#include <chrono>

int main() {
    // Load model
    torch::jit::script::Module module;
    try {
        module = torch::jit::load("model.pt");
        module.eval();
    } catch (const c10::Error& e) {
        std::cerr << "Error loading model: " << e.what() << "\n";
        return -1;
    }

    // Create input
    std::vector<torch::jit::IValue> inputs;
    inputs.push_back(torch::randn({1, 3, 224, 224}));

    // Execute model with timing
    auto start = std::chrono::high_resolution_clock::now();
    at::Tensor output = module.forward(inputs).toTensor();
    auto end = std::chrono::high_resolution_clock::now();

    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    std::cout << "Inference time: " << duration.count() << "ms\n";
    std::cout << "Output shape: " << output.sizes() << '\n';
    std::cout << "Output (first 5): " << output.slice(1, 0, 5) << '\n';

    return 0;
}

// Compile with:
// g++ -std=c++17 inference.cpp -o inference \
//     -I${TORCH_PATH}/include \
//     -L${TORCH_PATH}/lib \
//     -ltorch -ltorch_cpu -lc10

4.6 Optimization Techniques

Graph Optimization

import torch

model = torch.jit.load('model.pt')

# Optimize for inference
optimized_model = torch.jit.optimize_for_inference(model)

# Freeze model (inline constants, remove training-only ops)
frozen_model = torch.jit.freeze(optimized_model)

# Additional optimizations
# 1. Remove dropout layers
frozen_model.eval()

# 2. Fuse operations
# Conv + BatchNorm + ReLU are automatically fused

# Save optimized version
frozen_model.save('optimized_model.pt')

# Compare model sizes
import os
original_size = os.path.getsize('model.pt') / (1024 * 1024)
optimized_size = os.path.getsize('optimized_model.pt') / (1024 * 1024)
print(f"Original: {original_size:.2f}MB")
print(f"Optimized: {optimized_size:.2f}MB")
print(f"Reduction: {(1 - optimized_size/original_size)*100:.1f}%")

Operator Fusion

PyTorch automatically fuses common operator patterns to reduce memory bandwidth and improve performance.

# PyTorch automatically fuses operations
# Conv + BatchNorm + ReLU → Fused operation

model = torch.nn.Sequential(
    torch.nn.Conv2d(3, 64, 3),
    torch.nn.BatchNorm2d(64),
    torch.nn.ReLU()
)

traced = torch.jit.trace(model, torch.randn(1, 3, 224, 224))
# Fusion happens automatically during tracing

# Common fusion patterns:
# 1. Conv + BN + ReLU
# 2. Linear + ReLU
# 3. Add + ReLU
# 4. Mul + Add (fused multiply-add)

# View fused graph
print(traced.graph)

4.7 Mobile Deployment

PyTorch Mobile

import torch
from torch.utils.mobile_optimizer import optimize_for_mobile

model = torch.jit.load('model.pt')
model.eval()

# Optimize for mobile
mobile_model = optimize_for_mobile(model)

# Save for lite interpreter (smaller runtime)
mobile_model._save_for_lite_interpreter('mobile_model.ptl')

# Verify mobile model
loaded_mobile = torch.jit.load('mobile_model.ptl')
test_input = torch.randn(1, 3, 224, 224)
output = loaded_mobile(test_input)
print(f"Mobile model output shape: {output.shape}")

# In Android (Java):
"""
import org.pytorch.LiteModuleLoader;
import org.pytorch.Module;
import org.pytorch.Tensor;

Module module = LiteModuleLoader.load("mobile_model.ptl");
Tensor input = Tensor.fromBlob(inputArray, shape);
Tensor output = module.forward(IValue.from(input)).toTensor();
float[] scores = output.getDataAsFloatArray();
"""

# In iOS (Swift):
"""
import LibTorch

guard let module = try? LiteModuleLoader.load(modelPath: "mobile_model.ptl") else {
    fatalError("Failed to load model")
}
let input = try! Tensor(shape: [1, 3, 224, 224])
let output = try! module.forward([input])
"""

4.8 Quantization

Quantization reduces model size and improves inference speed by converting weights and activations from floating point to integers.

import torch

# Post-training static quantization
model.eval()

# Step 1: Fuse modules
model_fused = torch.ao.quantization.fuse_modules(
    model,
    [['conv', 'bn', 'relu']]
)

# Step 2: Specify quantization config
model_fused.qconfig = torch.ao.quantization.get_default_qconfig('x86')

# For mobile deployment:
# model_fused.qconfig = torch.ao.quantization.get_default_qconfig('qnnpack')

# Step 3: Prepare for quantization
model_prepared = torch.ao.quantization.prepare(model_fused)

# Step 4: Calibrate with sample data
with torch.no_grad():
    for data, _ in calibration_loader:
        model_prepared(data)

# Step 5: Convert to quantized model
quantized_model = torch.ao.quantization.convert(model_prepared)

# Save quantized model
torch.jit.save(torch.jit.script(quantized_model), 'quantized.pt')

# Compare sizes and performance
original_size = sum(p.numel() * p.element_size() for p in model.parameters())
quantized_size = sum(p.numel() * p.element_size() for p in quantized_model.parameters())
print(f"Size reduction: {original_size / quantized_size:.1f}x")

# Benchmark
import time
inputs = torch.randn(10, 3, 224, 224)

start = time.time()
with torch.no_grad():
    _ = model(inputs)
fp32_time = time.time() - start

start = time.time()
with torch.no_grad():
    _ = quantized_model(inputs)
int8_time = time.time() - start

print(f"Speed improvement: {fp32_time / int8_time:.1f}x")

4.9 Best Practices

Handle Dynamic Shapes

class DynamicModel(torch.nn.Module):
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        batch_size = x.size(0)
        seq_len = x.size(1)
        # Use dynamic dimensions appropriately
        return self.process(x, batch_size, seq_len)

    def process(self, x: torch.Tensor, bs: int, seq: int) -> torch.Tensor:
        # Avoid hard-coded shapes
        output = torch.zeros(bs, seq, 512, device=x.device)
        return output + x

# Trace with different input sizes to verify
traced = torch.jit.trace(model, torch.randn(1, 10, 512))

# Test with various batch sizes
assert traced(torch.randn(4, 20, 512)).shape[0] == 4
assert traced(torch.randn(8, 15, 512)).shape[0] == 8

# Use torch.jit.script for models with truly dynamic behavior
scripted = torch.jit.script(model)

Error Handling

try:
    model = torch.jit.script(MyModel())
    print("Scripting successful!")
except RuntimeError as e:
    print(f"Scripting failed: {e}")
    print("Attempting tracing instead...")
    try:
        example_input = torch.randn(1, 3, 224, 224)
        model = torch.jit.trace(MyModel(), example_input)
        print("Tracing successful!")
    except Exception as e:
        print(f"Both scripting and tracing failed: {e}")
        # Consider fixing model code or using eager mode

# Validate converted model
def validate_model(original, converted, test_inputs):
    """Validate that converted model matches original"""
    original.eval()
    converted.eval()

    with torch.no_grad():
        for inp in test_inputs:
            orig_out = original(inp)
            conv_out = converted(inp)
            diff = torch.abs(orig_out - conv_out)
            max_diff = torch.max(diff).item()
            print(f"Max difference: {max_diff:.2e}")
            assert max_diff < 1e-5, f"Outputs differ too much: {max_diff}"

test_inputs = [torch.randn(1, 3, 224, 224) for _ in range(5)]
validate_model(original_model, traced_model, test_inputs)

4.10 Debugging TorchScript Models

Debugging TorchScript models requires different techniques than standard Python debugging.

Inspect Generated Code

import torch

# Load or create scripted model
model = torch.jit.script(MyModel())

# View the generated TorchScript code
print(model.code)

# View the computation graph
print(model.graph)

# Get detailed graph representation
for node in model.graph.nodes():
    print(f"Node: {node.kind()}")
    print(f"  Inputs: {[i.debugName() for i in node.inputs()]}")
    print(f"  Outputs: {[o.debugName() for o in node.outputs()]}")

# Check for specific operations
def find_operations(graph, op_name):
    """Find all nodes of a specific operation type"""
    return [node for node in graph.nodes() if node.kind() == op_name]

conv_ops = find_operations(model.graph, 'aten::conv2d')
print(f"Found {len(conv_ops)} convolution operations")

Performance Profiling

import torch
from torch.profiler import profile, ProfilerActivity

model = torch.jit.load('model.pt')
input_data = torch.randn(1, 3, 224, 224)

# Profile with PyTorch profiler
with profile(
    activities=[ProfilerActivity.CPU],
    record_shapes=True,
    profile_memory=True
) as prof:
    with torch.no_grad():
        model(input_data)

# Print profiling results
print(prof.key_averages().table(sort_by="cpu_time_total", row_limit=10))

# Export Chrome trace for visualization
prof.export_chrome_trace("trace.json")
# Open trace.json in chrome://tracing

Chapter Summary

Review Questions

  1. What is the difference between torch.jit.trace and torch.jit.script? When would you choose one over the other?
  2. Explain the concept of operator fusion. Provide at least three examples of operations that are commonly fused together.
  3. Write code to convert a PyTorch model to TorchScript and deploy it in a C++ application. Include error handling.
  4. What optimizations does torch.jit.optimize_for_inference provide? How does torch.jit.freeze differ?
  5. Describe the complete quantization process for TorchScript models. What are the trade-offs between accuracy and performance?
  6. How do you handle models with dynamic input shapes in TorchScript? What are the limitations?
  7. Implement a validation function that compares outputs between original and TorchScript models across multiple input sizes.
  8. What are the benefits of using type annotations in TorchScript? Provide examples of complex type signatures.
  9. Explain the mobile deployment workflow using optimize_for_mobile. What optimizations are applied?
  10. How would you debug a TorchScript model that produces different outputs than the original PyTorch model?
  11. Compare the performance characteristics of traced vs scripted models. When might scripted models be slower?
  12. Write code to profile a TorchScript model and identify performance bottlenecks using torch.profiler.
弘益人間 (Hongik Ingan) · Benefit All Humanity

Korea Digital Transformation Detailed Mapping

Korea operates digital transformation through a comprehensive governance system. Digital Government: Digital Platform Government Committee (established September 2022, under the President)·Ministry of the Interior and Safety Digital Government Bureau·e-Government Support Center·Gov.kr·National Citizen Service·KDIS (Korea Digital Information Society)·NIA (National Information Society Agency)·MOIS (Ministry of the Interior and Safety). K-DNS Infrastructure: Korea Internet & Security Agency (KISA) Korea Internet Center·KISA DNS Root Server·KRNIC (Korea Network Information Center)·BGP Korea·National Cyber Security Center (NCSC)·KCC (Korea Communications Commission)·MSIT (Ministry of Science and ICT)·NIA·NIPA. Korean Cloud Infrastructure: KT Cloud·NAVER Cloud (NCloud)·Samsung SDS Cloud·LG U+ Cloud·NHN Cloud·Kakao Enterprise Cloud·SK Telecom Cloud·KISA Cloud Security Assurance Program (CSAP)·KCMVP-validated cloud·ISMS-P (Information Security & Personal Information Management System). Korean Security Certifications: KISA ISMS-P certification·KCMVP (Korean Cryptographic Module Validation Program)·NIS (National Intelligence Service) "National Cryptographic Technology Operation Standards"·NCSC "National Cyber Security Strategy 2024-2028"·CC (Common Criteria) Korean evaluation bodies·EAL4·EAL5·KS X ISO/IEC 15408·19790·24759 Korean Profile. Korean Data Standards: NIA AI Hub·National Data Standardization Committee·Statistics Korea (KOSTAT)·MyData 4 Designated Combination Specialists (Samsung SDS, KICI, KOSTAT, KFTC)·National Institute of Korean Language·National Law Information Center·National Spatial Information Platform·National Spatial Data Center·Korean Spatial Information Standards. Finance and Fintech Standards: FSC (Financial Services Commission)·FSS (Financial Supervisory Service)·FIU (Financial Intelligence Unit)·BOK (Bank of Korea)·FSEC (Financial Security Institute)·KFTC (Korea Financial Telecommunications)·KSD (Korea Securities Depository)·KRX (Korea Exchange) 8-agency cooperation. 5G/6G Communications Infrastructure: 5G subscribers 35 million (2024)·5G base stations 350,000·6G commercialization target 2028·5G dedicated networks 16 operators·6G Acceleration Council (MSIT, 2024). K-Content: KOCCA (Korea Creative Content Agency)·MCST (Ministry of Culture, Sports and Tourism)·KCA (Korea Communications Agency)·Korea Culture Information Service Agency·Korean Film Archive·Korea Publishing Industry Promotion Agency. Data 3 Acts (Personal Information Protection Act·Credit Information Act·Telecommunications Network Act, 2020 enforcement)·Data Industry Act (2021)·Public Data Act (2013)·AI Framework Act (2026)·Digital Platform Government Framework Act (2024 proposed) — Korea digital transformation core legislation.

Korea Industrial, Research, Education Infrastructure Mapping

Korea operates its industrial ecosystem and standardization system through the following core infrastructure. Korea Top 5 Groups: Samsung, Hyundai Motor, LG, SK, Lotte. Each group operates standardization committees and ISO/IEC TC Korean secretariats. Samsung Electronics (semiconductors, displays, home appliances, telecom)·Hyundai Motor (automobiles, mobility)·LG Electronics (home appliances, displays, OLED)·SK hynix (memory)·LG Energy Solution·Samsung SDI (batteries)·POSCO Future M (materials)·Hyundai Mobis (parts). Korean IT Big Tech: NAVER (search, cloud, AI HyperCLOVA)·Kakao (messenger, payment, mobility, banking)·Coupang (e-commerce, logistics)·Karrot Market·Toss·Woowa Brothers. Korea Telcos: SK Telecom·KT·LG U+. 5G·5G dedicated networks·B2B cloud·AI businesses operating. Korea Top 7 Research Universities: Seoul National University·KAIST·POSTECH·Yonsei University·Korea University·UNIST·DGIST·GIST. All serve as standardization R&D bases and ISO/IEC/IEEE Korean chairs. Korea Government-affiliated National Research Institutes (26): KIST, KAERI, KIMM, KIER, KFRI, KRICT, KRIBB, KARI, KASI, KIGAM, KICT, KISTI, KETI, ETRI, NIMS, KIMS, KISDI, KOTRA, STEPI, KOEN, KICCE, KIET, KIPF, KIHASA, KICJ, KLRI. Korea Industrial Complexes / Tech Valleys: Pangyo Techno Valley·Dongtan·Gwanggyo·Songdo IBD·Yeouido·Gangnam·Sihwa·Banwol·Gumi·Ulsan·Changwon·Geoje·Yeosu·Onsan·Cheongju·Iksan·Gwangyang·POSCO Gwangyang Steel Mill·Asan Bay·Seosan·Songdo·Incheon Airport·Sejong·Cheongna·Geomdan. Korea Trade and Finance Infrastructure: Korea International Trade Association (KITA)·Korea Trade-Investment Promotion Agency (KOTRA)·Export-Import Bank of Korea (KEXIM)·Bank of Korea·Kookmin Bank·Shinhan·Hana·Woori·NH Nonghyup·IBK Industrial Bank·SC First Bank·Citi Bank Korea·HSBC Korea·DBS Korea — 14 Korean major banks and foreign banks. Korea K-POP / K-Content: HYBE·SM·YG·JYP 4 major entertainment companies·CJ ENM·tvN·MBC·KBS·SBS·EBS·YTN·Yonhap News TV·JTBC Korean broadcasting·NETFLIX Korea·Disney Plus·TVING·Wavve·Watcha·Coupang Play. Korea Gaming Industry: Nexon·NCsoft·Krafton·Netmarble·Kakao Games·Pearl Abyss·Com2uS·Gamevil·NHN·Smilegate·Webzen. Korea Automotive / Battery: Hyundai Motor·Kia·Genesis·LG Energy Solution·Samsung SDI·SK On·POSCO Future M·EcoPro·L&F battery cathode material suppliers. Korea Semiconductor: Samsung Electronics (HBM3E·HBM4)·SK hynix (HBM3E 12-Hi)·DB HiTek·SK siltron·SK Enpulse·Dongjin Semichem·Seoul Semiconductor·Simmtech·Samsung Display·LG Display.

Korea Standardization Infrastructure Mapping

Korea operates a comprehensive standards governance system through inter-ministerial cooperation. National Standards Council (under Prime Minister's Office, per Framework Act on National Standards Article 5) coordinates KATS (Korean Agency for Technology and Standards), MFDS (Ministry of Food and Drug Safety), MOTIE (Ministry of Trade, Industry and Energy), MSIT (Ministry of Science and ICT), MOIS (Ministry of the Interior and Safety), MOE (Ministry of Environment), MOHW (Ministry of Health and Welfare), MND (Ministry of National Defense), MCST (Ministry of Culture, Sports and Tourism), MOFA (Ministry of Foreign Affairs), MOJ (Ministry of Justice), and FSC (Financial Services Commission). Accreditation and Testing: KOLAS (Korea Laboratory Accreditation Scheme) accredits 800+ testing laboratories. KAS (Korea Accreditation System) accredits 50+ certification bodies. KTC (Korea Testing Certification), KTR (Korea Testing & Research Institute), KTL (Korea Testing Laboratory), and KCL (Korea Conformity Laboratories) provide conformance testing. Telecom and Cyber: KCC (Korea Communications Commission), KCA (Korea Communications Agency), TTA (Telecommunications Technology Association), IITP (Institute for Information & Communications Technology Planning & Evaluation), NIPA (National IT Industry Promotion Agency), KISA (Korea Internet & Security Agency), KCMVP (Korea Cryptographic Module Validation Program), NIS (National Intelligence Service), NSR (National Security Research Institute), and NCSC (National Cyber Security Center). National R&D Centers: KIST, ETRI, KAIST, Seoul National University, Yonsei University, Korea University, POSTECH, UNIST, GIST, DGIST, KISTI, KIER, KIMM, KRICT, KFRI, KRIBB. International Standards Cooperation: ISO TC/SC Korean secretariats, IEC TC/SC Korean secretariats, ITU-T Study Group Korean chairs, 3GPP RAN/SA Korean chairs, IEEE 802 Korean chairs, W3C Korea office, OASIS Korea office, IETF Korea cooperation, OECD CSTP, UN ESCAP, APEC SCSC Korean cooperation. Korean Industrial Standards (KS) Catalog: KS X (Information) 25,000+, KS A (Basic) 15,000+, KS B (Machinery) 25,000+, KS C (Electrical) 18,000+, KS D (Metallurgy) 12,000+, KS E (Mining) 5,000+, KS F (Construction) 18,000+, KS H (Food) 8,000+, KS I (Environment) 5,000+, KS J (Biology) 3,000+, KS K (Textile) 15,000+, KS L (Ceramics) 7,000+, KS M (Chemistry) 12,000+, KS P (Medical) 5,000+, KS Q (Quality Mgmt) 4,000+, KS R (Transport) 12,000+, KS S (Service) 3,000+, KS T (Packaging) 4,000+, KS V (Shipbuilding) 5,000+, KS W (Aerospace) 3,000+ — totaling 220,000+ Korean Industrial Standards. Key Acts: Personal Information Protection Act (Act 19234, effective Sept 15, 2024), Electronic Government Act, Electronic Signature Act, Act on Promotion of Information and Communications Network Utilization and Information Protection, Information and Communications Infrastructure Protection Act, Data Industry Act, Public Data Act, AI Framework Act (Act 20212, effective July 2026), Industrial Technology Innovation Promotion Act, Framework Act on Science and Technology — 70+ Korean standardization-related laws.