tensorflow / java

Java bindings for TensorFlow
Apache License 2.0
785 stars 193 forks source link

Modular Java app can't create tensor object #509

Open nvaranki opened 6 months ago

nvaranki commented 6 months ago

System information

Describe the current behavior Java Gradle project, as built from IntelliJ IDEA pattern, repeatedly reports a fatal warning on attempt to create a tensor object. See the code and log below. The error raises exception that is effectively intercepted by JavaFX runtime. The project was found having Java module support. After this support has been removed from the project, code runs just fine. However, modularity is highly desired for the application.

Describe the expected behavior

After module access permissions (exports, open's) get fixed in tensorflow-core-platform, etc., all Java objects should be created just fine, as they are in non-modular edition of the project.

Code to reproduce the issue

excerpt from file src/main/java/module-info.java:

module com.varankin.ocrc.jfx
{
    requires org.tensorflow.ndarray;
    requires org.tensorflow;
}

excerpt from file build.gradle (remove shown lines to let code run with no error):

plugins 
{
    id 'org.javamodularity.moduleplugin' version '1.8.12'
}
dependencies 
{
    implementation 'org.tensorflow:tensorflow-core-platform:0.5.0'
}

excerpt from file src/main/java/com/varankin/ocrc:

package com.varankin.ocrc;
import org.tensorflow.*;
import org.tensorflow.ndarray.*;
import org.tensorflow.types.TFloat32;
import org.tensorflow.types.TInt32;

public class Inferencer
{
    public void image( float[][] data )
    {
            FloatNdArray data_nda = NdArrays.ofFloats( Shape.of( data.length, data[0].length ) );
            data_nda.elements( /* 0, */ 1 ).forEachIndexed( (ix,nda) -> nda.setFloat( data[(int)ix[0]][(int)ix[1]] ) );
            // OK before this line; any next line fails; 
            TInt32 tdata_is = TInt32.scalarOf( 24 );
            TInt32 tdata_i = TInt32.tensorOf( Shape.of( 24, 24 ) );
            TFloat32 tdata_0 = TFloat32.tensorOf( Shape.of( 24, 24 ) );
            tdata_0.set( data_nda );
            TFloat32 tdata = TFloat32.tensorOf( data_nda );
    }
}

Other info / logs

Warning: Could not create an instance of class org.tensorflow.internal.c_api.presets.tensorflow: java.lang.IllegalAccessException: class org.bytedeco.javacpp.ClassProperties (in module org.bytedeco.javacpp) cannot access class org.tensorflow.internal.c_api.presets.tensorflow (in module org.tensorflow) because module org.tensorflow does not export org.tensorflow.internal.c_api.presets to module org.bytedeco.javacpp

Call stack has been wiped off by JavaFX, below is manual reproduction

Please do not hesitate to ask for runnable example if needed. Current project cannot be presented as-is because of IP protection requirements, and because it's bulky.

Craigacp commented 6 months ago

Can you build the head of the bazelcism branch and see if that works? I recall changing stuff in the module-info on that branch but I'm not sure if it's sufficient. If it still doesn't work try adding exports org.tensorflow.internal.c_api.presets to org.bytedeco.javacpp to the module info of tensorflow-core-native.

nvaranki commented 6 months ago

Oops, things start to go beyond my experience...

I have created sample project, based on official example. Please check with public GitHub link for branch "modular".

It reproduces the problem at commit 918567b97809afac605d6de57de5b5ffe2a1aebc. Failure points out to another issue, on a different API call, namely "TensorFlow.version()". That hints my problem was masked by another one. See attached call stack. run202401071640.txt

As a work-around, I've added dependence on TensorFlow API for Windows, my current platform. That fixes the problem, as you can see at revision b66d7456fad9100dadeef4a44aa19bded2879767. There's hint in the doc for that topic, possibly I didn't understand it.

excerpt from file build.gradle

dependencies 
{
    implementation 'org.tensorflow:tensorflow-core-platform:0.5.0'
    implementation("org.tensorflow:tensorflow-core-api:0.5.0:windows-x86_64")
}

If anybody thinks that the final fix was my extra action and selection of platform must happen automatically, please submit another ticket or repurpose this one. Anyway, all warnings mentioned in ticket description, still exist and are numerous.

saudet commented 6 months ago

/cc @hguillemet