HI, @saudet
in C++ LibTorch, the TORCH_ARG macro is used extensively in Options classes (like EmbeddingOptions, Conv2dOptions, EmbeddingBagOptions, etc.). This macro generates:
A member variable.
A setter method that returns a reference to the Options object itself (T& name(const T& v)).
Currently, the JavaCPP generated bindings for these Options classes usually return a pointer/reference to the field itself (e.g., LongPointer, DoubleOptional), which prevents the Chained Assignment pattern that is standard in LibTorch.
Example Comparison
Current Java Usage:
Java
EmbeddingOptions options = new EmbeddingOptions(10, 2);
options.padding_idx().put(3);
options.max_norm().put(2.0);
options.sparse().put(true);
// This is verbose and differs significantly from the C++ experience.
Desired Java Usage (Matching C++):
Java
EmbeddingOptions options = new EmbeddingOptions(10, 2)
.padding_idx(3)
.max_norm(2.0)
.sparse(true);
Proposed Solutions
- Mapping via @name to C++ Setters
Since TORCH_ARG actually creates a method with the same name as the field, we can explicitly map the setter in the preset.
For example, in EmbeddingOptions:
Java
@name("padding_idx")
public native @ByRef EmbeddingOptions padding_idx(@ByRef LongOptional v);
2. Manual Wrapper Methods in Presets
We can add manual helper methods in the torch preset configuration to wrap the pointer-based access into a fluent API.
Java
public EmbeddingOptions padding_idx(long value) {
padding_idx().put(value);
return this;
}
Affected Classes
This pattern affects almost all torch::nn Options classes, including but not limited to:
EmbeddingOptions / EmbeddingBagOptions
Conv2dOptions / LinearOptions
LSTMOptions / RNNOptions
Additional Context
Providing a Fluent API would make the Java development experience much closer to the original LibTorch C++ API, reducing the cognitive load for developers porting Python/C++ code to Java.
The current implementation of AOTIModelContainerRunner torch::inductor::AOTIModelPackageLoader and other high-level APIs would also benefit from easier configuration of input options.
thanks
HI, @saudet
in C++ LibTorch, the TORCH_ARG macro is used extensively in Options classes (like EmbeddingOptions, Conv2dOptions, EmbeddingBagOptions, etc.). This macro generates:
A member variable.
A setter method that returns a reference to the Options object itself (T& name(const T& v)).
Currently, the JavaCPP generated bindings for these Options classes usually return a pointer/reference to the field itself (e.g., LongPointer, DoubleOptional), which prevents the Chained Assignment pattern that is standard in LibTorch.
Example Comparison
Current Java Usage:
Java
EmbeddingOptions options = new EmbeddingOptions(10, 2);
options.padding_idx().put(3);
options.max_norm().put(2.0);
options.sparse().put(true);
// This is verbose and differs significantly from the C++ experience.
Desired Java Usage (Matching C++):
Java
EmbeddingOptions options = new EmbeddingOptions(10, 2)
.padding_idx(3)
.max_norm(2.0)
.sparse(true);
Proposed Solutions
Since TORCH_ARG actually creates a method with the same name as the field, we can explicitly map the setter in the preset.
For example, in EmbeddingOptions:
Java
@name("padding_idx")
public native @ByRef EmbeddingOptions padding_idx(@ByRef LongOptional v);
2. Manual Wrapper Methods in Presets
We can add manual helper methods in the torch preset configuration to wrap the pointer-based access into a fluent API.
Java
public EmbeddingOptions padding_idx(long value) {
padding_idx().put(value);
return this;
}
Affected Classes
This pattern affects almost all torch::nn Options classes, including but not limited to:
EmbeddingOptions / EmbeddingBagOptions
Conv2dOptions / LinearOptions
LSTMOptions / RNNOptions
Additional Context
Providing a Fluent API would make the Java development experience much closer to the original LibTorch C++ API, reducing the cognitive load for developers porting Python/C++ code to Java.
The current implementation of AOTIModelContainerRunner torch::inductor::AOTIModelPackageLoader and other high-level APIs would also benefit from easier configuration of input options.
thanks