Skip to content

Add TorchScript/JIT module support for model inference - #87

Open
tphakala wants to merge 2 commits into
lwch:masterfrom
tphakala:feature/jit-support
Open

Add TorchScript/JIT module support for model inference#87
tphakala wants to merge 2 commits into
lwch:masterfrom
tphakala:feature/jit-support

Conversation

@tphakala

@tphakala tphakala commented Nov 26, 2025

Copy link
Copy Markdown

Summary

  • Add new jit package with Module type for loading and running TorchScript models
  • Support single and multi-output forward passes (Forward, ForwardMulti) for models like BirdNET v3.0
  • Implement device targeting (CPU/CUDA), eval/train mode switching, and thread-safe cleanup
  • Leverage Go 1.25 features: runtime.AddCleanup, iter.Seq2, testing/synctest, CGO escape hints

中文摘要 (AI翻译,如有错误请见谅)

新增 TorchScript/JIT 模块支持,实现模型推理功能:

  • 新增 jit 包,包含用于加载和运行 TorchScript 模型的 Module 类型
  • 支持单输出和多输出的前向传播(ForwardForwardMulti),适用于 BirdNET v3.0 等返回多个输出的模型
  • 实现设备选择(CPU/CUDA)、评估/训练模式切换,以及线程安全的资源清理
  • 使用 Go 1.25 新特性:runtime.AddCleanupiter.Seq2testing/synctest、CGO 逃逸提示

新增文件:

  • jit/jit.go - 高级 JIT 模块 API
  • jit/jit_test.go - 完整的测试套件和基准测试
  • internal/torch/jit.go - 底层 CGO 绑定
  • internal/torch/jit.h - C 函数声明
  • lib/jit.cpp - 使用 libtorch 的 C++ 实现

修改文件:

  • go.mod - 更新为 Go 1.25
  • internal/torch/api.h - 添加 jit_module 类型
  • internal/torch/exception.hpp - 添加 auto_catch_jit_module 模板
  • lib/CMakeLists.txt - 添加 jit.cpp 到构建

Changes

New files:

  • jit/jit.go - High-level JIT module API
  • jit/jit_test.go - Comprehensive test suite with benchmarks
  • internal/torch/jit.go - Low-level CGO bindings
  • internal/torch/jit.h - C function declarations
  • lib/jit.cpp - C++ implementation using libtorch
  • JIT_IMPLEMENTATION_PLAN.md - Implementation documentation

Modified files:

  • go.mod - Updated to Go 1.25
  • internal/torch/api.h - Added jit_module type
  • internal/torch/exception.hpp - Added auto_catch_jit_module template
  • lib/CMakeLists.txt - Added jit.cpp to build

API Example

model, err := jit.Load("model.pt")
if err != nil {
    return err
}
defer model.Close()

model.Eval()

// Single output
output, err := model.Forward(input)

// Multi-output (e.g., BirdNET returns embeddings + predictions)
outputs, err := model.ForwardMulti(input, 2)
for idx, out := range jit.Outputs(outputs) {
    fmt.Printf("Output %d shape: %v\n", idx, out.Shapes())
}

Test plan

  • Unit tests pass with BirdNET v3.0 model
  • Tests cover: Load, LoadToDevice, Forward, ForwardMulti, iterator, eval/train modes, Close idempotency
  • Concurrent inference test using testing/synctest
  • Batch inference test
  • Benchmarks: ~143-145ms/inference on CPU

🤖 Generated with Claude Code

tphakala and others added 2 commits November 26, 2025 20:46
Add comprehensive JIT support enabling TorchScript model loading and inference:

- New jit package with Module type for loading and running TorchScript models
- Support for single and multi-output forward passes (ForwardMulti)
- Device targeting (CPU/CUDA) for model loading and moving
- Eval/Train mode switching
- Go 1.25 features: runtime.AddCleanup, iter.Seq2, testing/synctest
- CGO optimization hints (#cgo noescape, #cgo nocallback)
- Thread-safe cleanup with sync.Once pattern

C++ implementation:
- New lib/jit.cpp with libtorch torch::jit integration
- Error handling via exception.hpp templates
- Support for tuple outputs from models

This enables loading and running TorchScript models like BirdNET v3.0
which return multiple outputs (embeddings + predictions).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Remove redundant cleaned flag, use atomic.Bool for thread-safe closed state
- Add ErrModuleClosed error and closed-state checks to Forward, ForwardMulti, ToDevice
- Add nil guards to Eval and Train methods
- Add stddef.h include for portable size_t definition
- Add null pointer validation in C++ jit_forward, jit_forward_multi, jit_to_device

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant