Add TorchScript/JIT module support for model inference - #87
Open
tphakala wants to merge 2 commits into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
jitpackage withModuletype for loading and running TorchScript modelsForward,ForwardMulti) for models like BirdNET v3.0runtime.AddCleanup,iter.Seq2,testing/synctest, CGO escape hints中文摘要 (AI翻译,如有错误请见谅)
新增 TorchScript/JIT 模块支持,实现模型推理功能:
jit包,包含用于加载和运行 TorchScript 模型的Module类型Forward、ForwardMulti),适用于 BirdNET v3.0 等返回多个输出的模型runtime.AddCleanup、iter.Seq2、testing/synctest、CGO 逃逸提示新增文件:
jit/jit.go- 高级 JIT 模块 APIjit/jit_test.go- 完整的测试套件和基准测试internal/torch/jit.go- 底层 CGO 绑定internal/torch/jit.h- C 函数声明lib/jit.cpp- 使用 libtorch 的 C++ 实现修改文件:
go.mod- 更新为 Go 1.25internal/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 APIjit/jit_test.go- Comprehensive test suite with benchmarksinternal/torch/jit.go- Low-level CGO bindingsinternal/torch/jit.h- C function declarationslib/jit.cpp- C++ implementation using libtorchJIT_IMPLEMENTATION_PLAN.md- Implementation documentationModified files:
go.mod- Updated to Go 1.25internal/torch/api.h- Addedjit_moduletypeinternal/torch/exception.hpp- Addedauto_catch_jit_moduletemplatelib/CMakeLists.txt- Added jit.cpp to buildAPI Example
Test plan
testing/synctest🤖 Generated with Claude Code