Hi @saudet
I am using JavaCPP PyTorch for PyTorch model deployment in Java applications. My core requirement is to perform PyTorch AOT (Ahead-of-Time) compilation directly in Java to generate lightweight, Python-independent inference models (optimized TorchScript modules).
Currently, JavaCPP PyTorch lacks critical JIT compilation and AOT optimization APIs that are natively available in LibTorch C++. This blocks full AOT model conversion workflows in Java.
Missing Core APIs
I need the following torch::jit functions implemented in JavaCPP:
torch::jit::script(...)
torch::jit::trace(...) not like torch.trace is math
torch::jit::load(...)
torch::jit::freeze(Module module) // we have implement
torch::jit::optimize_for_inference(Module module) // we have implement
Full binding for torch::jit::save(Module module, const std::string& path)
Use Case
I want to implement TorchScript → AOT optimized model conversion in Java, identical to this working LibTorch C++ logic:
运行
// C++ Reference (Working)
torch::jit::Module module = torch::jit::load(input_pt_path);
module.eval();
auto frozen = torch::jit::freeze(module);
torch::jit::optimize_for_inference(frozen);
torch::jit::save(frozen, output_aot_path);
Expected Java Implementation (JavaCPP)
This is the Java code I expect to write with the added APIs:
运行
import org.bytedeco.pytorch.*;
import static org.bytedeco.pytorch.global.torch.*;
import static org.bytedeco.pytorch.global.torch_jit.*;
public class TorchAotConverter {
public static void convertToAOT(String inputPtPath, String outputAotPath) {
// 1. Load TorchScript model
Module module = torch.load(inputPtPath);
// 2. Switch to evaluation mode
module.eval();
// 3. Freeze module (AOT required)
Module frozenModule = torch.freeze(module);
// 4. Run AOT inference optimization
torch.optimize_for_inference(frozenModule);
// 5. Save final AOT model
torch_jit.save(frozenModule, outputAotPath); // we lack it
}
public static void main(String[] args) {
convertToAOT("model.pt", "model.aot");
}
}
```
Value & Impact
Enables full AOT model compilation in Java (no Python dependency)
Matches production-grade LibTorch C++ deployment capabilities
Critical for high-performance Java + PyTorch inference deployment
Unlocks model optimization, freezing, and porting workflows for Java developers
I would greatly appreciate adding these essential torch::jit scripting/tracing/AOT APIs to JavaCPP PyTorch. Thank you!
the libtorch Aot compile model
```cpp
#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>
#include <string>
int main(int argc, const char* argv[]) {
// 1. 检查命令行参数
if (argc != 3) {
std::cerr << "用法: " << argv[0] << " <输入.pt模型路径> <输出.aot模型路径>" << std::endl;
std::cerr << "示例: " << argv[0] << " model.pt model.aot" << std::endl;
return -1;
}
std::string input_pt_path = argv[1]; // 输入的.pt模型
std::string output_aot_path = argv[2]; // 输出的.aot模型
try {
// 2. 加载 TorchScript 格式的 .pt 模型
std::cout << "正在加载 .pt 模型: " << input_pt_path << std::endl;
torch::jit::Module module;
module = torch::jit::load(input_pt_path);
// 3. 【核心】AOT 编译(启用优化,生成无Python依赖的模型)
std::cout << "正在执行 AOT 编译优化..." << std::endl;
// 冻结模型(固定权重,禁止修改,AOT必备步骤)
module.eval(); // 切换到推理模式
auto frozen_module = torch::jit::freeze(module);
// 启用 AOT 编译优化(核心API)
torch::jit::optimize_for_inference(frozen_module);
// 4. 保存编译后的 AOT 模型
std::cout << "正在保存 AOT 模型到: " << output_aot_path << std::endl;
torch::jit::save(frozen_module, output_aot_path);
std::cout << "✅ 模型转换完成!.pt → .aot 成功" << std::endl;
return 0;
} catch (const c10::Error& e) {
std::cerr << "❌ 错误: " << e.what() << std::endl;
return -1;
}
}
```
Hi @saudet
I am using JavaCPP PyTorch for PyTorch model deployment in Java applications. My core requirement is to perform PyTorch AOT (Ahead-of-Time) compilation directly in Java to generate lightweight, Python-independent inference models (optimized TorchScript modules).
Missing Core APIs
I need the following torch::jit functions implemented in JavaCPP:
torch::jit::script(...)
torch::jit::trace(...) not like torch.trace is math
torch::jit::load(...)
torch::jit::freeze(Module module) // we have implement
torch::jit::optimize_for_inference(Module module) // we have implement
Full binding for torch::jit::save(Module module, const std::string& path)
Use Case
I want to implement TorchScript → AOT optimized model conversion in Java, identical to this working LibTorch C++ logic:
Expected Java Implementation (JavaCPP)
This is the Java code I expect to write with the added APIs: