Scalable AI Models with PyTorch Lightning
Sergiy Tkachuk
Director, GenAI Productivity
不依賴 Python
適合生產環境,高效
範例:

不依賴 Python
生產環境高效
範例:

兩種轉換方式:
torch.jit.trace:用範例輸入追蹤執行torch.jit.script:分析 Python 程式碼進行編譯何時使用:
tracescriptimport torch import torch.nn as nnclass SimpleModel(nn.Module): def forward(self, x): return x * 2model = SimpleModel() scripted_model = torch.jit.script(model)
$$
torch.jit.save:將 scripted 模型存成檔案torch.jit.load:載回模型以進行推論$$
# Save the model
torch.jit.save(scripted_mod,"model.pt")
# Load the model
loaded_model=torch.jit.load("model.pt")
範例輸入:
[1.0, 2.0, 3.0]範例輸出:
[2.0, 4.0, 6.0]# Perform inference input_arr = [1.0, 2.0, 3.0] input_tensor = torch.tensor(input_arr)output = loaded_model(input_tensor) print(output)
$$
torch.jit.trace:適用靜態模型torch.jit.script:可處理動態控制流程torch.jit.save:儲存 scripted 模型torch.jit.load:載入以進行推論Scalable AI Models with PyTorch Lightning