-
Notifications
You must be signed in to change notification settings - Fork 747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding new presets for pytorch c++ frontend? #623
Comments
AFAIK, that's what was known as Caffe2, and sure it makes sense to want to use it from Java.
|
BTW, @Neiko2002, is this something you would be also interested in having? |
/cc @cypof |
@saudet To be honest I have never tried pytorch yet. Their python API looks similar to Java. A javacpp version of it might be easier to use than the official C++ API. Right now we deploy javacpp-tensorflow in production systems but train networks in python. Pytorch on the other hand has a simple C++ API to train and run a network. Looking at the C++ API it does not feel it offers more than the Tensorflow API. |
I think there is great potential in being able to (re)train on a platform that's more efficient than CPython, so I do hope this panes out. In any case, TensorFlow has obviously given up on that, let's see where PyTorch is willing to go! They used to promote Lua for this exact reason, although not Java for whatever political reason, so anyway IMO there is a chance they will redo something similar for C++11: @karllessard If you have any opinions about this, I would love to hear them! |
For the moment no I don't but this is very interesting, thanks for the tip @saudet ! |
I've been looking at the the C++ API over the past couple of days, and it looks like a mess. It mixes types from Caffe2, ATen, and C10 with no clear understanding of how they fit together: pytorch/pytorch#14850 Moreover, it's currently unable to train for MNIST using simple examples on either CPU or GPU: We should probably wait until all this gets cleared up and fixed before starting to map anything to Java... |
Could you give me a hand? I can not find the file, torch/torch.h. |
It looks like that specific instability issue has been resolved as pytorch/pytorch#15522 is now closed. Looking at their release log they seem to have been pretty busy: https://github.com/pytorch/pytorch/releases Though the C++ API is still labeled as being in beta: https://pytorch.org/cppdocs/ Might it make sense to package something up? |
@jxtps It still looks like a mess to me. There's still very little information about what C10 is supposed to be, apart from this: https://github.com/pytorch/pytorch/wiki/Software-Architecture-for-c10 But if you're willing to understand what we need to map and how, that would be a good first step I think. |
Yeah, it seems like they've got a lot of history to deal with in that project. Judging by https://pytorch.org/cppdocs/ it looks like the C++ frontend (= I would hope that wherever they end up with their c10 Tensor-library, it will be backwards compatible in the sense that 1. all relevant operations are preserved and 2. no operation subtly changes behavior but retains the same overall signature. So at some upgrade point there will be a massive number of compile errors as I recognize that the correct approach here may very well be "wait another year". |
Hi,as of 2021 the cpp frontend seems stable enough,I think we can just look files at There are projects like |
There's been some work on this recently. @wmeddie Would you be able to make public what you have? |
My fork is available master...wmeddie:master But need to give it a little bit of a clean up. If other people are interested in helping get it over the finish line I can create a PR over the weekend I think. |
@wmeddie Thanks for your nice job! I would be glad to help since I want to do this 2 years ago.. I have forked from your branch,will start a initial test build. |
Hey everyone, I've finally taken some time to start working on this myself, and although there is still much to be done, the basics are there to get something like a simple training example for MNIST working on Linux, Mac, and Windows, with MKL and everything: Now, some important parts of the C++ API are probably missing and/or not functional, yet, and since it has a pretty large surface it would be a good idea to prioritize needs. Please let me know what you think we should work on next:
In any case, please give it a try with the snapshots (instructions at http://bytedeco.org/builds/) and let me know what you think! /cc @HGuillemet @stu1130 |
I gave a first try. It seems quite promising. Also it would be interesting to have some Java-ish replacement for the ExpandingArray template type. Can we use generics here ? |
Thanks for trying it out!
We can add/generate as many "helper" methods and classes as we want, sure, but there's no way to automate this kind of translation process from C++ to Java in a generic fashion, so that kind of thing is probably not going to be part of JavaCPP itself or of the presets for PyTorch, but of some higher-level project, such as JavaCV in the case of OpenCV, FFmpeg, etc, or TF Java in the case of TensorFlow. |
The configuration of a Convolution module should look like this:
But the stride, padding, bias, etc... setters are not defined yet because declared in |
I see, I'm not sure at the moment how we could make this work well since it uses the convOptions = new Conv2dOptions(numChannelsIn, numChannelsOut, new LongPointer(3, 3));
convOptions.stride().put(new long[] {3, 3});
convOptions.padding().put(new long[] {1, 1});
convOptions.bias().put(false);
conv = new Conv2dImpl(convOptions); Is this the only thing that's not working for you though? Instead of small usability issues like that, I would like to hear in priority about important issues preventing someone from getting anything working at all, and fix those first. However, if everything else is working for you, please let me know, and we can start looking at what we can do to polish that. |
Ok, I didn't realize we could set options this way.
|
There seems to be an issue with It's common in Pytorch to define a module as a sequence of other modules:
which maps with the C++ API to:
A In the preset we cannot rely on the C++ library and its implementation of AnyModule to dynamically call a |
It looks like that needs an actual array of tensors, and not an array of pointers to tensors, so if we don't have that, we need to first create one possibly like this: Tensor array = new Tensor(x.length);
for (int i = 0; i < x.length; i++) {
array.getPointer(i).put(x[i]);
}
new TensorArrayRef(array, x.length);
Yeah, that looks really complicated. I don't see at the moment how we could make this work nicely. It doesn't sound like the
|
New issue: the C++ lib handles modules either as normal struct/object (the The documentation says the use of module holders is needed for some features like the serizalization API. I don't know why and haven't tested it (I haven't found the entrypoint in the preset). Another issue : the |
|
Thanks, will have to look into this in a bit more detail I guess (especially also for own defined Modules and the implications on how to use/register them). Time to brush up on my C/C++ skills ;) |
In my case I haven't face any error when calling
|
Indeed seems that if I play by those rules no issue, thanks. P.S from my understanding the main reason to register a module is to make it easier to retrieve the trainable parameters. I guess some dictionary traversal is performed when you call net.parameters(). But if you could get the parameters in another way, it is not really required. But I could be very wrong here. |
Actually, I think the problem is with the dynamic cast here below: |
@saudet thanks for looking in to this. Right now only doing some simple prototyping to see what could be the best approach for my use-cases. So nothing is really vital at this moment, especially if there is a work around like in this case. |
@jbaron I've updated to PyTorch 1.9.0 in commit f1ea76f, and I also added var lstm = new LSTMImpl(50, 20);
register_module("lstm1", lstm.asModule()); Let me know if there's anything else missing that you would like to have! And thanks for testing |
With the update, the data type for padding has changed from an expanding array of longs to some variant type that can be either an array, kSame or kValid. Code like:
doesn't work anymore. How can we handle this variant type ? |
Would something like this not work (Koltin sample):
|
There must be some header value discriminating the actual type of the value stored in the variant. Also beware of |
Thanks, indeed misread the LongPointer API. The following code runs without a segmentation fault:
|
Right, we probably need to map those variants to set the options correctly, so I've done that in commit 7b9ccf3. Please give it a try! |
It now works as with version 1.8 for my cases with CNN and explicit paddings. Thanks ! |
Short question, when a method expects a pointer to std::ostream, how to provide this? Are these C++ std lib classes also mapped to Java equivalents? |
It's not currently mapped to anything, it's a pretty ugly API, so usually we get overloads using |
The presets for the C++ API of PyTorch 1.9.0 have been released with version 1.5.6! Thanks to everyone for helping out with this effort. I'll close this issue, but please do open new separate threads to continue the discussion about various remaining issues and potential enhancements. /cc @stu1130 |
@jbaron BTW, it looks like we can train TorchScript models using the C++ API without CPython, see pytorch/pytorch#17614. That's one way you could go about it with your models: Define them in Python, but train them using the C++ API. This is unlike SavedModel from TensorFlow, which AFAIK cannot be trained from outside CPython. |
Sounds great and would be very nice since not only we'll have a lot of (proven) models to pick from, I assume it will also be faster since fewer calls between Java and C++ during training. |
A couple of issues with the
|
AFAIK, |
Back to this issue. About iterators: they seem to work as expected using this pattern :
|
It should be possible to parse |
Not at the moment, nothing prioritary. |
@HGuillemet I've included Let me know if there is anything else missing! |
I had to remove: .valueTypes("@Cast({\"\", \"torch::OrderedDict<std::string,torch::nn::AnyModule>&&\"}) @StdMove StringAnyModuleDict") line 1929 of |
Recently
pytorch
release preview version 1.0, which features the experimentalC++ frontend
. The pytorch C++ frontend is easier and more complete thantensorflow
.Python:
C++:
Will you consider adding the pytorch c++ frontend presets?
The text was updated successfully, but these errors were encountered: