Skip to content

Make this work with LibTorch 2.12#2

Open
slburson wants to merge 3 commits into
borodust:masterfrom
slburson:scott-changes-part-1
Open

Make this work with LibTorch 2.12#2
slburson wants to merge 3 commits into
borodust:masterfrom
slburson:scott-changes-part-1

Conversation

@slburson

Copy link
Copy Markdown

This now successfully runs on LibTorch 2.12, producing a bindings file that compiles. I haven't built the adapter yet. Besides fixing some Claw bugs, I did the following:

  • arranged to use a binary LibTorch distribution, so you don't have to build PyTorch from source
  • created a patch for one LibTorch header file that was tickling a couple of Claw bugs, to work around them
  • tweaked the defwrapper form to include names in torch::nn::
  • added directions to the README

I'm opening this PR so you can see what I've been doing, but it isn't ready to merge yet. In fact — what's your feeling about this project? Do you want to keep control of it, or are you happy to let me take it over? If the latter, we can just consider my fork to be the official repo. Do you mind if I rename it to CL-Torch?

Scott L. Burson added 2 commits May 25, 2026 01:32
This now successfully runs on LibTorch 2.12, producing a bindings file
that compiles.  I haven't built the adapter yet.  Besides fixing some
Claw bugs, I did the following:

- arranged to use a binary LibTorch distribution, so you don't have to
build PyTorch from source
- created a patch for one LibTorch header file that was tickling a
couple of Claw bugs, to work around them
- tweaked the 'defwrapper' form to include names in 'torch::nn::'
- added directions to the README
@slburson

Copy link
Copy Markdown
Author

Ha ha, the joke was on me: getting the bindings file to compile was the easy part; the adapter was much harder. For starters, I had to make some changes to its CMakeLists.txt, to force it to build with Clang and the Clang headers only.

The big problem was with inherited constructors. Example:

struct A {
    A(int);
    A(char *);
};

struct B : A {
    using A::A;
};

Here B inherits all the declared constructors of A; also, neither class has a default constructor. Unfortunately, libclang does not put the correct constructor set on the CXType (I have filed a bug). It is possible to find the using declaration, with the set of constructors to be inherited, by walking the decl for B; I spent quite some time working out how to do this, but eventually I decided to table that work and just edit out the construct from the LibTorch headers. This wasn't completely trivial either, but I managed to do it. (See libtorch-patch-01-inherited-constructors in this repo.)

Once that was done, there were just a few minor bugs in the adapter generation that I had to fix, and it built! 🚀

Then I had to get the shared library to load. That wasn't quite trivial either. The first problem was that Claw uses the Clang headers if available (see claw.util::list-system-paths), but LibTorch was compiled against the GCC headers. This showed up as different mangled names for functions with a parameter of type ostream&. (There could well have been other problems; dlopen gives up after the first error.) I temporarily hacked list-system-paths to ignore Clang, and tried a couple of different GCC header tree versions; 15 didn't work, but 10 did.

After that:

  • The two static methods in torch::nn::functions::CrossMapLRN2d (declared in torch/csrc/api/include/torch/nn/modules/_functions.h) were evidently compiled with visibility=hidden, because they were private to the LibTorch library.
  • Function torch::getAllCustomClassesNames (declared in torch/custom_classes.h) was also hidden. In this case, I think the hiding may be inadvertent.
  • at::TensorIteratorBase::apply_perm_and_mul (declared in ATen/TensorIterator.h) is simply unimplemented.

For the moment, I have hand-edited these out of the adapter and bindings files. I guess I'll patch the headers again to get rid of them.

And with that — it loads!!! 🚀 🚀 🚀

Whew! Now let me try calling some functions...

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