Skip to content

Implement libtorch-based policy inference in ctrader#71

Closed
lee101 wants to merge 1 commit intomainfrom
libtorch-policy-infer
Closed

Implement libtorch-based policy inference in ctrader#71
lee101 wants to merge 1 commit intomainfrom
libtorch-policy-infer

Conversation

@lee101
Copy link
Owner

@lee101 lee101 commented Mar 22, 2026

Summary

  • Adds policy_infer.cpp with real TorchScript model loading and inference via libtorch C++ API, exposed through C-compatible extern "C" functions
  • Stub policy_infer.c remains as fallback when TORCH_DIR is not set -- existing build is unchanged
  • Makefile updated with download_libtorch target (fetches CPU libtorch), conditional C++/C build paths, and test_policy target
  • 30 unit tests covering load/unload, bad path handling, forward pass correctness, determinism, multi-symbol output, output-fewer-than-requested edge case

Test plan

  • cd ctrader && make test -- existing tests pass (38 + 11 = 49 passed)
  • cd ctrader && make download_libtorch && TORCH_DIR=libtorch make test_policy -- 30 passed
  • cd ctrader && make test_policy without TORCH_DIR -- gracefully skips

Generated with Claude Code

Adds policy_infer.cpp with real TorchScript model loading and inference
via libtorch C++ API, exposed through C-compatible extern "C" functions.
The stub policy_infer.c remains as fallback when TORCH_DIR is not set.

Makefile updated with download_libtorch target, conditional C++/C build,
and test_policy target. 30 unit tests covering load/unload, forward pass,
determinism, multi-symbol output, and edge cases.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codex-infinite
Copy link

Codex Infinity
Hi! I'm Codex Infinity, your coding agent for this repo.

Start a task on this PR's branch by commenting:

Tasks and logs: https://codex-infinity.com

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b60ffd34f3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

int policy_load(Policy *policy, const char *model_path) {
memset(policy, 0, sizeof(*policy));
try {
auto *mod = new torch::jit::Module(torch::jit::load(model_path));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Load TorchScript modules onto CPU explicitly

Checked ctrader/Makefile: download_libtorch fetches the CPU-only libtorch build, but policy_load() uses bare torch::jit::load(model_path) here. When a checkpoint was exported from CUDA (which is a common training path in this repo), TorchScript will try to restore tensors back to that saved device, so loading the model on the CPU trading bot fails before inference ever starts. Pass an explicit CPU device/map_location during load so GPU-trained artifacts remain deployable.

Useful? React with 👍 / 👎.

#include <cstdio>

int policy_load(Policy *policy, const char *model_path) {
memset(policy, 0, sizeof(*policy));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Unload the previous module before clearing Policy

If policy_load() is called on an already-loaded Policy (for example during an in-process model refresh), the memset here wipes out policy->model before policy_unload() can free it. That leaks the original torch::jit::Module, and if the second load fails we also lose the only handle to the previously working model.

Useful? React with 👍 / 👎.

@lee101
Copy link
Owner Author

lee101 commented Mar 22, 2026

Merged manually into main (cfe9478) with Makefile conflict resolution combining PR #70 MKTD reader + mktd_reader.c sources.

lee101 added a commit that referenced this pull request Mar 22, 2026
- C++ policy_infer.cpp with TorchScript model loading via libtorch
- Conditional build: uses policy_infer.cpp when TORCH_DIR set, fallback
  to policy_infer.c (C stub) otherwise
- download_libtorch Make target for CPU libtorch
- Combined with PR #70: vendor/cJSON.c + mktd_reader.c in C_SRCS

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@lee101 lee101 closed this Mar 22, 2026
lee101 added a commit that referenced this pull request Mar 24, 2026
- C++ policy_infer.cpp with TorchScript model loading via libtorch
- Conditional build: uses policy_infer.cpp when TORCH_DIR set, fallback
  to policy_infer.c (C stub) otherwise
- download_libtorch Make target for CPU libtorch
- Combined with PR #70: vendor/cJSON.c + mktd_reader.c in C_SRCS

Co-Authored-By: Claude Sonnet 4.6 <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