Skip to content
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

Java bindings #2100

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open

Java bindings #2100

wants to merge 32 commits into from

Conversation

Vasniktel
Copy link

@Vasniktel Vasniktel commented Nov 29, 2019

Implementation of automatic bindings generator for Java.
We are using Nd4j on the Java side to work with matrices.
Types are being mapped as follows:

  • arma::Mat<T> is mapped to INDArray class of Nd4j library.
  • std::vector<T> is mapped to List<T>
  • int -> Integer
  • double -> Double
  • std::string -> String

If you want to try it yourself:

  1. Clone the repo.
  2. mkdir build && cd build
  3. cmake -D BUILD_JAVA_BINDINGS=1 ..
  4. make java
  5. cd src/mlpack/bindings/java/maven/target

There must be a *.jar file in the directory. You can use it to run generated bindings. Don't forget to put it and its dependencies (https://mvnrepository.com/artifact/org.nd4j/nd4j-api/1.0.0-beta5) in your classpath.

TODO:

  • Make better docs
  • Generate .jar file with native libraries
  • Make it run in CI

@mlpack-bot
Copy link

mlpack-bot bot commented Nov 29, 2019

Thanks for opening your first pull request in this repository! Someone will review it when they have a chance. In the mean time, please be sure that you've handled the following things, to make the review process quicker and easier:

  • All code should follow the style guide
  • Documentation added for any new functionality
  • Tests added for any new functionality
  • Tests that are added follow the testing guide
  • Headers and license information added to the top of any new code files
  • HISTORY.md updated if the changes are big or user-facing
  • All CI checks should be passing

Thank you again for your contributions! 👍

Copy link
Member

@rcurtin rcurtin left a comment

Choose a reason for hiding this comment

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

@Vasniktel, this is a truly impressive effort. Adding a binding for another language is a lot of work. I'm really excited to add these. I think that the review process will take a lot of time, though; since we're basically adding an entire new interface to mlpack, lots of testing and review have to happen in order to make it all fit. I have to apologize that I've been slow about this initial review---the PR itself is pretty intimidating. :)

My review here is pretty initial---it's likely as we dig deeper, we'll find more things to change or fix or what.

So far, I've checked out the bindings and built them locally and run the tests, which all pass fine. From my end I need to do the following before I think everything is ready for release:

  • high-level structural review of the code (it would probably be good to take a second pass though)
  • fine-grained review of all the code
  • test build documentation for the website and deploy
  • try to do some data science with mlpack from Java
  • understand how the Java bindings would fit in with Scala and how we can cover that also
  • figure out what packaging the Java bindings look like (plus Scala too?)
  • make sure tests run on different OSes (Linux/OS X/Windows)

Many of the comments I make during this process might be outside the scope of this PR, so we can always open issues for them that can be handled later. I'm sure we won't get everything perfect right after the first merge, but I think we can get most of the way there. :)

src/mlpack/bindings/java/get_java_type.hpp Outdated Show resolved Hide resolved
src/mlpack/bindings/java/CMakeLists.txt Outdated Show resolved Hide resolved
src/mlpack/bindings/java/CMakeLists.txt Outdated Show resolved Hide resolved
src/mlpack/bindings/java/CMakeLists.txt Show resolved Hide resolved
src/mlpack/bindings/java/CMakeLists.txt Outdated Show resolved Hide resolved
src/mlpack/bindings/java/print_output_param_impl.hpp Outdated Show resolved Hide resolved
@@ -14,6 +14,7 @@

#include <mlpack/prereqs.hpp>
#include <mlpack/core/math/lin_alg.hpp>
#include <mlpack/core/math/ccov.hpp>
Copy link
Member

Choose a reason for hiding this comment

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

I think we removed this some time ago, my guess is this is just a merge artifact. But we shouldn't re-add ccov.hpp because it doesn't exist anymore. :)

Copy link
Author

Choose a reason for hiding this comment

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

Partly fixed in 6574dc8. I was unable to get it compiled without adding these and some other includes. Particularly, it says that mlpack::math::ColumnCovariance is not defined (not sure what the problem is)

#elif(BINDING_TYPE == BINDING_TYPE_JAVA) // This is a Java binding.

// TODO: not sure where we use this
#define BINDING_MATRIX_TRANSPOSED true
Copy link
Member

Choose a reason for hiding this comment

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

This is only used for the NMF binding, because it makes a difference which matrix is W and which is H when the binding operates on transposed or non-transposed data.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the insight :) I'm not sure though what value I should set it to for Java

@@ -15,6 +15,8 @@
#define MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_FUNCTION_HPP

#include <mlpack/prereqs.hpp>
#include <mlpack/core/math/make_alias.hpp>
#include <mlpack/core/math/shuffle_data.hpp>
Copy link
Member

Choose a reason for hiding this comment

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

I'm also not sure that this change is needed.

Copy link
Author

Choose a reason for hiding this comment

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

Partly fixed in 6574dc8.

@zoq
Copy link
Member

zoq commented Jan 24, 2020

Hello @Vasniktel do you think you could update the linux build steps: https://github.com/mlpack/mlpack/blob/master/.ci/linux-steps.yaml to enable the java bindings?

@Vasniktel
Copy link
Author

Time for a small status report ;)

I've been working out the way bindings are being generated. We are compiling bindings for every method separately right now (every *.so file is being compiled separately by cmake script after the appropriate JNI wrapper file was generated by JavaCPP library). The whole process looks like this:

  • compile a program to generate *.java binding file (this stage is the same in all bindings generators)
  • generate that *.java file
  • run Java compiler to compile *.java file to bytecode
  • run JavaCPP on the compiled bytecode to get JNI *.cpp wrapper file
  • compile wrapper file to a shared library

Disadvantages of this approach are:

  • it looks too complicated to me
  • the compilation is slow
  • generation of *.jar files is probably gonna be tricky as well

An alternative approach could be to make JavaCPP do all the hard work. In that way, the process reduces to the following:

  • compile generator program for every method in the library
  • generate *.java classes for all these methods
  • run maven with JavaCPP plugin to compile all shared libraries and package them in a *.jar file

The only downside is that we have to pass all compiler flags to JavaCPP maven plugin to make sure bindings are compiled correctly. I'm working on this new process right now.

Meanwhile, I've noticed that usage of move assignment operator for arma::mat(data, n_rows, n_cols, false, true); actually copies memory. A solution would be to pass false as the last value to the constructor. This issue arises in both julia and go bindings

@mlpack-bot
Copy link

mlpack-bot bot commented Mar 23, 2020

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions! 👍

@mlpack-bot mlpack-bot bot added the s: stale label Mar 23, 2020
@Vasniktel
Copy link
Author

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions!

Hey, @rcurtin. It would be good to keep this issue open ;)

@rcurtin
Copy link
Member

rcurtin commented Mar 24, 2020

@Vasniktel agreed, I'm sorry I haven't managed to review this. Things have been crazy. :(

@Vasniktel
Copy link
Author

@Vasniktel agreed, I'm sorry I haven't managed to review this. Things have been crazy. :(

That's ok ;). I don't have much time to work on this myself.

@Yashwants19
Copy link
Member

@Vasniktel if you want I can help you in this, or else if you don't enough time I can open a new PR for completing this. :)

@Vasniktel
Copy link
Author

@Yashwants19 Thanks. It would be great if you could take this over. I don't think I will be able to finish this any time soon :( Sorry for the long delay.

@caballeto
Copy link

Is this coming any time soon?

@mlpack-bot mlpack-bot bot closed this Jul 28, 2021
@rcurtin rcurtin reopened this Jul 28, 2021
@rcurtin
Copy link
Member

rcurtin commented Jul 28, 2021

I'd like to keep this one open---it is really nice code, it just needs to be reviewed and merged. I hope to have the time sometime...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants