[PyTorch] How to clone an instance of Module? #1230
-
How do you subclass Cloneable when extending Module defined in Java? (Or more generally how to properly clone a class defined in Java that extends Module?) For example, in the SimpleMNIST example I am trying to clone The docs here http://bytedeco.org/javacpp-presets/pytorch/apidocs/org/bytedeco/pytorch/Module.html says
which is what I'm guessing I have to extend instead of just the base Module. I'm guessing this is torch::nn::Cloneable, and not the java.lang's Cloneable interface (I tried that and it did not work). I saw Javacpp has code like https://github.com/bytedeco/javacpp-presets/blob/master/pytorch/src/gen/java/org/bytedeco/pytorch/Conv1dImplCloneable.java#L18 which then produces specific cloneable components. Can we do something like that in Java directly on an object defined using JavaCPP (like |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 12 replies
-
That's a class template, so we can't create new instances of that without a C++ compiler. The easiest way to go about something like this is to fork the presets and add what you need as new files in @HGuillemet might have other ideas. |
Beta Was this translation helpful? Give feedback.
-
I see. Are there any other ways to clone Module then? We would want to be able to define networks in Java without having to write C++ code every time.
… On Sep 15, 2022, at 5:47 PM, Samuel Audet ***@***.***> wrote:
That's a class template, so we can't create new instances of that without a C++ compiler. The easiest way to go about something like this is to fork the presets and add what you need as new files in @platform(include=..., along with an updated InfoMap, in a similar fashion to what @yukoba did in the case of LLVM here:
https://github.com/bytedeco/javacpp-presets/blob/master/llvm/src/main/resources/org/bytedeco/llvm/include/FullOptimization.h
@HGuillemet might have other ideas.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.
|
Beta Was this translation helpful? Give feedback.
-
If you'd like to clone an instance of a Java class subclassing Module, you need a Java implementation of For saving/loading models, I currently use |
Beta Was this translation helpful? Give feedback.
-
It's probably just getting garbage collected prematurely. Try to keep a
reference in a field.
|
Beta Was this translation helpful? Give feedback.
If you'd like to clone an instance of a Java class subclassing Module, you need a Java implementation of
clone()
, since the C++ library knows nothing about the Java subclass. We could define someCloneableModule
mappingtorch::nn::Cloneable<torch::nn::Module>
and Java modules could inherit from it instead ofModule
. So the Java implementation ofclone()
could call the C++clone()
fromCloneable
, but it won't help since you'll get an object that is not an instance of the Java class.For saving/loading models, I currently use
OutputArchive
andModule.save
/Module.load
.Maybe could you use it for cloning too ?