deepjavalibrary / djl

An Engine-Agnostic Deep Learning Framework in Java
https://djl.ai
Apache License 2.0
4.14k stars 659 forks source link

inconsistent inference results from python paddle and djl loaded paddle models #1104

Closed hu-guanwei closed 2 years ago

hu-guanwei commented 3 years ago

I was trying to use djl to load paddle and do inference, but got different results from using python paddle and djl.

My codes:

# python
# paddle 2.0.5
ids = paddle.to_tensor([1, 103, 1138, 422, 2], dtype=np.int64).reshape((1, -1))
seg_ids = paddle.to_tensor([0] * 5, dtype=np.int64).reshape((1, -1))

print(model(ids, seg_ids))

net = paddle.jit.to_static(
    model,
    input_spec=[
        paddle.static.InputSpec(
            shape=[None, None], dtype='int64'),  # input_ids
        paddle.static.InputSpec(
            shape=[None, None], dtype='int64')   # token_type_ids
    ])
# Save in static graph model.
paddle.jit.save(net, 'inference')

gives

Tensor(shape=[1, 5, 13], dtype=float32, place=CUDAPlace(0), stop_gradient=False,
       [[[ 0.66997510, -0.69133747, -0.39862645, -1.35966492,  0.41624478,  0.80915153,  0.71472746, -0.50920713, -0.33507735, -0.63856411, -0.77664024, -1.42996275,  3.04401803],
         [ 0.04969274, -0.26678643, -1.29171944, -0.85316378,  4.57579756,  0.72209787, -0.40451398, -0.57951182, -1.01572132, -0.91371608, -0.78995508, -1.24123633, -0.25500619],
         [-1.07657111,  1.28260732, -1.72831178, -0.01282348,  0.38086778,  3.51755285, -0.58793044, -0.61690950, -1.63578820, -1.50595844, -0.76129240, -1.05396211, -1.30221224],
         [-0.61245722,  1.74191892, -1.00270355, -0.73525578, -0.71968979,  3.86341190, -0.79166001,  0.03567113, -1.42238903, -0.61444962, -0.54218626, -0.35149875, -0.86691952],
         [ 0.28681871, -0.75582063, -0.81654090, -1.64531708,  0.61981243,  1.16615844,  0.07041498, -0.27960852, -0.62263411, -0.59176749, -1.04917181, -1.49355745,  2.74151397]]])

Saved model files are archived in a .zip file,

image

import ai.djl.MalformedModelException;
import ai.djl.inference.Predictor;
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.translate.TranslateException;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws MalformedModelException, ModelNotFoundException, IOException, TranslateException {
        // load model
        Criteria<NDList, NDList> criteria = Criteria.builder()
                .setTypes(NDList.class, NDList.class)
                .optEngine("PaddlePaddle")
                .optModelPath(Paths.get("inference.zip"))
                .optModelName("ernie")
                .build();

        ZooModel<NDList, NDList> model = criteria.loadModel();
        Predictor<NDList, NDList> predictor = model.newPredictor();

        NDManager manager = NDManager.newBaseManager();
        long[] inputIdsData = {1, 103, 1138, 422, 2};
        NDArray inputIds = manager.create(inputIdsData).reshape(1, inputIdsData.length);
        NDArray tokenTypeIds = manager.zeros(new Shape(1, inputIdsData.length), DataType.INT64);

        System.out.println(inputIds);
        System.out.println(tokenTypeIds);
        NDList list = new NDList(inputIds, tokenTypeIds);

        NDList res = predictor.predict(list);
        System.out.println(res.get(0));
    }
}

gives

ND: (1, 5) cpu() int64
[[   1,  103, 1138,  422,    2],
]

ND: (1, 5) cpu() int64
[[ 0,  0,  0,  0,  0],
]

ND: (1, 5, 13) cpu() float32
[[[ 2.01860213e+00,  2.52940321e+00, -9.44176853e-01, -5.39562345e-01, -2.05423892e-01,  8.56708214e-02,  3.35503340e-01, -8.17900956e-01, -3.23039263e-01, -1.89538136e-01, -4.33228910e-01, -9.13791835e-01,  2.94984007e+00],
  [ 6.42231655e+00,  1.30396795e+00, -1.33973932e+00, -5.20015299e-01, -4.19481903e-01, -4.86294270e-01,  4.54121195e-02, -1.44616413e+00, -6.81712091e-01, -1.02773201e+00,  2.05819592e-01, -3.99784416e-01, -2.09515259e-01],
  [ 5.13404655e+00,  2.82334709e+00, -1.40380144e+00,  2.09747449e-01, -5.78439236e-01, -5.24792433e-01, -6.20343126e-02, -1.75117683e+00, -9.18633997e-01, -1.19678259e+00, -3.47811207e-02, -3.20471376e-01, -3.08098763e-01],
  [ 1.79054475e+00,  6.47316408e+00, -1.49921894e+00,  2.36101940e-01, -1.72535479e-01, -1.98051304e-01, -2.37498004e-02, -1.71458733e+00, -9.21652555e-01, -1.49176097e+00, -6.10442698e-01, -2.87929803e-01, -5.88506222e-01],
  [ 1.87688303e+00,  6.15878248e+00, -1.59979665e+00,  1.78547278e-01, -1.22413859e-01, -1.83996633e-01,  1.72288506e-03, -1.62422252e+00, -1.19600701e+00, -1.31948709e+00, -5.31787455e-01, -2.30764952e-02, -1.70150667e-01],
 ],
]

build.gradle

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    jcenter()
}

dependencies {
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.14.1'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.14.1'
    implementation platform("ai.djl:bom:0.12.0")
    implementation "ai.djl:api"
    implementation "ai.djl.paddlepaddle:paddlepaddle-model-zoo:0.12.0"
    runtimeOnly "ai.djl.paddlepaddle:paddlepaddle-native-auto:2.0.2"
    runtimeOnly "ai.djl.mxnet:mxnet-engine"
    runtimeOnly "ai.djl.mxnet:mxnet-native-auto"
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

test {
    useJUnitPlatform()
}
C:\Users\142857\IdeaProjects\djl>gradlew debugEnv
Couldn't find C:\Users\142857\IdeaProjects\djl\\gradle\wrapper\gradle-wrapper.jar, downloading it ...
Downloading from: "https://raw.githubusercontent.com/gradle/gradle/master/gradle/wrapper/gradle-wrapper.jar"
Finished downloading C:\Users\142857\IdeaProjects\djl\\gradle\wrapper\gradle-wrapper.jar
Welcome to Gradle 7.0.2!
Here are the highlights of this release:
 - File system watching enabled by default
 - Support for running with and building Java 16 projects
 - Native support for Apple Silicon processors
 - Dependency catalog feature preview
For more details see https://docs.gradle.org/7.0.2/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :pytorch:pytorch-engine:processResources
Downloading https://publish.djl.ai/pytorch-1.9.0/jnilib/0.13.0/linux-x86_64/cpu/libdjl_torch.so
Downloading https://publish.djl.ai/pytorch-1.9.0/jnilib/0.13.0/linux-x86_64/cu102/libdjl_torch.so
Downloading https://publish.djl.ai/pytorch-1.9.0/jnilib/0.13.0/linux-x86_64/cu111/libdjl_torch.so
Downloading https://publish.djl.ai/pytorch-1.9.0/jnilib/precxx11/0.13.0/linux-x86_64/cpu/libdjl_torch.so
Downloading https://publish.djl.ai/pytorch-1.9.0/jnilib/0.13.0/osx-x86_64/cpu/libdjl_torch.dylib
Downloading https://publish.djl.ai/pytorch-1.9.0/jnilib/0.13.0/win-x86_64/cpu/djl_torch.dll
Downloading https://publish.djl.ai/pytorch-1.9.0/jnilib/0.13.0/win-x86_64/cu102/djl_torch.dll
Downloading https://publish.djl.ai/pytorch-1.9.0/jnilib/0.13.0/win-x86_64/cu111/djl_torch.dll

> Task :integration:debugEnv
[DEBUG] - Found EngineProvider: MXNet
[DEBUG] - Found EngineProvider: PyTorch
[DEBUG] - Found EngineProvider: TensorFlow
[DEBUG] - Found default engine: MXNet
----------- System Properties -----------
java.specification.version: 16
sun.cpu.isalist: amd64
sun.jnu.encoding: GBK
java.class.path: C:\Users\142857\IdeaProjects\djl\integration\build\classes\java\main;C:\Users\142857\IdeaProjects\djl\integration\build\resources\main;C:\Users\142857\.gradle\caches\modules-2\files-2.1\commons-cli\commons-cli\1.4\c
51c00206bb913cd8612b24abd9fa98ae89719b1\commons-cli-1.4.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-slf4j-impl\2.14.1\9a40554b8dab7ac9606089c87ae8a5ba914ec932\log4j-slf4j-impl-2.14.1.jar;C:\
Users\142857\IdeaProjects\djl\basicdataset\build\libs\basicdataset-0.13.0-SNAPSHOT.jar;C:\Users\142857\IdeaProjects\djl\model-zoo\build\libs\model-zoo-0.13.0-SNAPSHOT.jar;C:\Users\142857\IdeaProjects\djl\testing\build\libs\testing-0
.13.0-SNAPSHOT.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.testng\testng\7.3.0\a5069c3dfba58d23702f96c3d9f5081f5ce7136f\testng-7.3.0.jar;C:\Users\142857\IdeaProjects\djl\mxnet\mxnet-model-zoo\build\libs\mxnet-model-zo
o-0.13.0-SNAPSHOT.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\ai.djl.mxnet\mxnet-native-auto\1.8.0\e32265c03e27e1fb18c9c0904733b00f9acffaee\mxnet-native-auto-1.8.0.jar;C:\Users\142857\IdeaProjects\djl\pytorch\pytorch-mode
l-zoo\build\libs\pytorch-model-zoo-0.13.0-SNAPSHOT.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\ai.djl.pytorch\pytorch-native-auto\1.9.0-SNAPSHOT\97bca204fc8cd08742e1b73abebd12fd91cead44\pytorch-native-auto-1.9.0-SNAPSHOT.
jar;C:\Users\142857\IdeaProjects\djl\tensorflow\tensorflow-model-zoo\build\libs\tensorflow-model-zoo-0.13.0-SNAPSHOT.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\ai.djl.tensorflow\tensorflow-native-auto\2.4.1\20b8c7a4e6d45
1e782d15dd30cebd4df0ad86c74\tensorflow-native-auto-2.4.1.jar;C:\Users\142857\IdeaProjects\djl\mxnet\mxnet-engine\build\libs\mxnet-engine-0.13.0-SNAPSHOT.jar;C:\Users\142857\IdeaProjects\djl\pytorch\pytorch-engine\build\libs\pytorch-
engine-0.13.0-SNAPSHOT.jar;C:\Users\142857\IdeaProjects\djl\tensorflow\tensorflow-engine\build\libs\tensorflow-engine-0.13.0-SNAPSHOT.jar;C:\Users\142857\IdeaProjects\djl\api\build\libs\api-0.13.0-SNAPSHOT.jar;C:\Users\142857\.gradl
e\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.31\9545c9cb71de4c18d97a91e32ef0be6f3f6661b7\slf4j-api-1.7.31.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-core\2.14.1\9141212b8507ab50a455
25b545b39d224614528b\log4j-core-2.14.1.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-api\2.14.1\cd8858fbbde69f46bce8db1152c18a43328aae78\log4j-api-2.14.1.jar;C:\Users\142857\.gradle\caches\mod
ules-2\files-2.1\org.apache.commons\commons-csv\1.8\37ca9a9aa2d4be2599e55506a6d3170dd7a3df4\commons-csv-1.8.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\com.beust\jcommander\1.78\a3927de9bd6f351429bcf763712c9890629d8f51\jc
ommander-1.78.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.apache.ant\ant\1.10.3\88becdeb77cdd2457757b7268e1a10666c03d382\ant-1.10.3.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\com.google.inject\guice\4.2.2\
fa13659f9128f4c011c8e1d06f137083b4876377\guice-4.2.2-no_aop.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml\1.21\18775fdda48574784f40b47bf478ab0593f92e4d\snakeyaml-1.21.jar;C:\Users\142857\.gradle\caches\mo
dules-2\files-2.1\com.google.code.gson\gson\2.8.7\69d9503ea0a40ee16f0bcdac7e3eaf83d0fa914a\gson-2.8.7.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\net.java.dev.jna\jna\5.8.0\3551d8d827e54858214107541d3aff9c615cb615\jna-5.8
.0.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.apache.commons\commons-compress\1.20\b8df472b31e1f17c232d2ad78ceb1c84e00c641b\commons-compress-1.20.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.apache.ant\
ant-launcher\1.10.3\9dd5189e7f561ca19833b4e3672720b9bc5cb2fe\ant-launcher-1.10.3.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\javax.inject\javax.inject\1\6975da39a7040257bd51d21a231b76c915872d38\javax.inject-1.jar;C:\Users
\142857\.gradle\caches\modules-2\files-2.1\aopalliance\aopalliance\1.0\235ba8b489512805ac13a8f9ea77a1ca5ebe3e8\aopalliance-1.0.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\com.google.guava\guava\25.1-android\bdaab946ca5ad2
0253502d873ba0c3313d141036\guava-25.1-android.jar;C:\Users\142857\IdeaProjects\djl\tensorflow\tensorflow-api\build\libs\tensorflow-api-0.13.0-SNAPSHOT.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\com.google.code.findbugs\j
sr305\3.0.2\25ea2e8b0c338a877313bd4672d3fe056ea78f0d\jsr305-3.0.2.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.checkerframework\checker-compat-qual\2.0.0\fc89b03860d11d6213d0154a62bcd1c2f69b9efa\checker-compat-qual-2.0
.0.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\com.google.errorprone\error_prone_annotations\2.1.3\39b109f2cd352b2d71b52a3b5a1a9850e1dc304b\error_prone_annotations-2.1.3.jar;C:\Users\142857\.gradle\caches\modules-2\files-
2.1\com.google.j2objc\j2objc-annotations\1.1\ed28ded51a8b1c6b112568def5f4b455e6809019\j2objc-annotations-1.1.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.codehaus.mojo\animal-sniffer-annotations\1.14\775b7e22fb10026eed
3f86e8dc556dfafe35f2d5\animal-sniffer-annotations-1.14.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.bytedeco\javacpp\1.5.5\92e1c31aaed15a3dc12008859a37ced45fa0b730\javacpp-1.5.5.jar;C:\Users\142857\.gradle\caches\modul
es-2\files-2.1\org.tensorflow\tensorflow-core-api\0.3.1\954f292e85f4d2a587ede1b2e1a525e74ef96c97\tensorflow-core-api-0.3.1.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\com.google.protobuf\protobuf-java\3.8.0\b5f93103d11354
0bb848fe9ce4e6819b1f39ee49\protobuf-java-3.8.0.jar;C:\Users\142857\.gradle\caches\modules-2\files-2.1\org.tensorflow\ndarray\0.3.1\3cdb825411a9de908cc3dac740f18628d6512260\ndarray-0.3.1.jar
java.vm.vendor: Oracle Corporation
sun.arch.data.model: 64
user.variant:
java.vendor.url: https://java.oracle.com/
user.timezone: Asia/Shanghai
java.vm.specification.version: 16
os.name: Windows 10
sun.java.launcher: SUN_STANDARD
user.country: CN
sun.boot.library.path: C:\Program Files\Java\jdk-16.0.1\bin;C:\Program Files\Java\jdk-16.0.1\bin
sun.java.command: ai.djl.integration.util.DebugEnvironment
jdk.debug: release
sun.cpu.endian: little
user.home: C:\Users\142857
org.gradle.appname: gradlew
user.language: zh
java.specification.vendor: Oracle Corporation
java.version.date: 2021-04-20
java.home: C:\Program Files\Java\jdk-16.0.1
ai.djl.logging.level: debug
org.gradle.internal.http.connectionTimeout: 60000
file.separator: \
java.vm.compressedOopsMode: Zero based
line.separator:

java.vm.specification.vendor: Oracle Corporation
java.specification.name: Java Platform API Specification
user.script:
sun.management.compiler: HotSpot 64-Bit Tiered Compilers
java.runtime.version: 16.0.1+9-24
user.name: 142857
path.separator: ;
os.version: 10.0
java.runtime.name: Java(TM) SE Runtime Environment
file.encoding: GBK
java.vm.name: Java HotSpot(TM) 64-Bit Server VM
java.vendor.url.bug: https://bugreport.java.com/bugreport/
java.io.tmpdir: C:\Users\142857\AppData\Local\Temp\
org.gradle.internal.http.socketTimeout: 120000
java.version: 16.0.1
user.dir: C:\Users\142857\IdeaProjects\djl\integration
os.arch: amd64
java.vm.specification.name: Java Virtual Machine Specification
sun.os.patch.level:
kotlin.daemon.jvm.options: --illegal-access=permit
java.library.path: C:\Program Files\Java\jdk-16.0.1\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\sy
stem32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;D:\Program
 Files (x86)\Microsoft VS Code\bin;D:\HashiCorp\Vagrant\bin;C:\Program Files\PuTTY\;C:\Program Files\nodejs\;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\IDM Computer Solutio
ns\UltraEdit;C:\Program Files\Go\bin;C:\Program Files\Git\cmd;C:\Users\142857\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Bandizip\;C:\Users\142857\AppData\Roaming\npm;C:\Users\142857\go\bin;D:\haskell\ghcup\bin;.
java.vm.info: mixed mode, sharing
java.vendor: Oracle Corporation
java.vm.version: 16.0.1+9-24
sun.io.unicode.encoding: UnicodeLittle
library.jansi.path: C:\Users\142857\.gradle\native\jansi\1.18\windows64
java.class.version: 60.0
org.gradle.internal.publish.checksums.insecure: true

--------- Environment Variables ---------
USERDOMAIN_ROAMINGPROFILE: LAPTOP-S7T3B3MK
PROCESSOR_LEVEL: 6
SESSIONNAME: Console
ALLUSERSPROFILE: C:\ProgramData
PROCESSOR_ARCHITECTURE: AMD64
PSModulePath: C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules
__INTELLIJ_COMMAND_HISTFILE__: C:\Users\142857\AppData\Roaming\JetBrains\IntelliJIdea2020.3\terminal\history\history-1
SystemDrive: C:
=ExitCode: 00000000
DIRNAME: C:\Users\142857\IdeaProjects\djl\
USERNAME: 142857
CMD_LINE_ARGS: debugEnv
ProgramFiles(x86): C:\Program Files (x86)
DokanLibrary1_LibraryPath_x64: C:\Program Files\Dokan\Dokan Library-1.4.0\lib\
APP_HOME: C:\Users\142857\IdeaProjects\djl\
PATHEXT: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
DriverData: C:\Windows\System32\Drivers\DriverData
OneDriveConsumer: C:\Users\142857\OneDrive
GOPATH: C:\Users\142857\go
ProgramData: C:\ProgramData
ProgramW6432: C:\Program Files
HOMEPATH: \Users\142857
PROCESSOR_IDENTIFIER: Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
ProgramFiles: C:\Program Files
PUBLIC: C:\Users\Public
windir: C:\Windows
=::: ::\
ZES_ENABLE_SYSMAN: 1
_SKIP: 2
LOCALAPPDATA: C:\Users\142857\AppData\Local
USERDOMAIN: LAPTOP-S7T3B3MK
LOGONSERVER: \\LAPTOP-S7T3B3MK
PROMPT: $P$G
CABAL_DIR: D:\haskell\cabal
OneDrive: C:\Users\142857\OneDrive
=C:: C:\Users\142857\IdeaProjects\djl
APPDATA: C:\Users\142857\AppData\Roaming
DOWNLOAD_URL: "https://raw.githubusercontent.com/gradle/gradle/master/gradle/wrapper/gradle-wrapper.jar"
JAVA_EXE: java.exe
GHCUP_INSTALL_BASE_PREFIX: D:\haskell\
DokanLibrary1: C:\Program Files\Dokan\Dokan Library-1.4.0\
DokanLibrary1_LibraryPath_x86: C:\Program Files\Dokan\Dokan Library-1.4.0\x86\lib\
VBOX_MSI_INSTALL_PATH: D:\Program Files\Oracle\VirtualBox\
CommonProgramFiles: C:\Program Files\Common Files
Path: C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System3
2\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;D:\Program Files (x86)\Microsoft VS Code\bin;D:\HashiCorp\Vagrant\bin;C:\Program Files\PuTTY\;C:\Program Files\node
js\;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\IDM Computer Solutions\UltraEdit;C:\Program Files\Go\bin;C:\Program Files\Git\cmd;C:\Users\142857\AppData\Local\Microsoft\Win
dowsApps;C:\Program Files\Bandizip\;C:\Users\142857\AppData\Roaming\npm;C:\Users\142857\go\bin;D:\haskell\ghcup\bin
OS: Windows_NT
COMPUTERNAME: LAPTOP-S7T3B3MK
PROCESSOR_REVISION: 8c01
CLASSPATH: C:\Users\142857\IdeaProjects\djl\\gradle\wrapper\gradle-wrapper.jar
CommonProgramW6432: C:\Program Files\Common Files
ComSpec: C:\Windows\system32\cmd.exe
APP_BASE_NAME: gradlew
TERMINAL_EMULATOR: JetBrains-JediTerm
SystemRoot: C:\Windows
TEMP: C:\Users\142857\AppData\Local\Temp
HOMEDRIVE: C:
USERPROFILE: C:\Users\142857
TMP: C:\Users\142857\AppData\Local\Temp
CommonProgramFiles(x86): C:\Program Files (x86)\Common Files
NUMBER_OF_PROCESSORS: 8
IDEA_INITIAL_DIRECTORY: C:\Users\142857\Desktop

-------------- Directories --------------
temp directory: C:\Users\142857\AppData\Local\Temp
DJL cache directory: C:\Users\142857\.djl.ai
Engine cache directory: C:\Users\142857\.djl.ai

------------------ CUDA -----------------
[DEBUG] - Found cudart: C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common\cudart64_65.dll
[DEBUG] - Using cache dir: C:\Users\142857\.djl.ai\mxnet
[WARN ] - No matching cuda flavor for win found: cu65mkl/sm_75.
[DEBUG] - Loading mxnet library from: C:\Users\142857\.djl.ai\mxnet\1.8.0-mkl-win-x86_64\mxnet.dll
GPU Count: 0
Default Device: cpu()

----------------- Engines ---------------
Default Engine: MXNet
PyTorch: 2
[DEBUG] - Using cache dir: C:\Users\142857\.djl.ai\pytorch
[WARN ] - No matching cuda flavor for win found: cu65.
[INFO ] - Downloading https://publish.djl.ai/pytorch-1.9.0/cpu/win/native/lib/torch.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/pytorch-1.9.0/cpu/win/native/lib/torch_cpu.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/pytorch-1.9.0/cpu/win/native/lib/c10.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/pytorch-1.9.0/cpu/win/native/lib/fbgemm.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/pytorch-1.9.0/cpu/win/native/lib/libiomp5md.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/pytorch-1.9.0/cpu/win/native/lib/asmjit.dll.gz ...
[DEBUG] - Loading pytorch library from: C:\Users\142857\.djl.ai\pytorch\1.9.0-SNAPSHOT-20210709-cpu-win-x86_64\0.13.0-SNAPSHOT-cpu-djl_torch.dll
[INFO ] - Number of inter-op threads is 4
[INFO ] - Number of intra-op threads is 8
MXNet: 0
TensorFlow: 3
[DEBUG] - Using cache dir: C:\Users\142857\.djl.ai\tensorflow
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-errorhandling-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-libraryloader-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-filesystem-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-locale-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/vcomp140.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-synch-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/jnitensorflow.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-string-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-string-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-utility-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-debug-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-console-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-rtlsupport-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/tensorflow_cc.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-datetime-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-environment-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-stdio-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-synch-l1-2-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-file-l1-2-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-processthreads-l1-1-1.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-memory-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/ucrtbase.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/THIRD_PARTY_TF_JNI_LICENSES.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-processthreads-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-handle-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-namedpipe-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-interlocked-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-multibyte-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-convert-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-sysinfo-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/msvcp140.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-math-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-localization-l1-2-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-time-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/vcruntime140.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-file-l2-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/LICENSE.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-runtime-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-heap-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-timezone-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-profile-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-processenvironment-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-util-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/vcruntime140_1.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-crt-heap-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/api-ms-win-core-file-l1-1-0.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/concrt140.dll.gz ...
[INFO ] - Downloading https://publish.djl.ai/tensorflow-2.4.1/win/cpu/libiomp5md.dll.gz ...
[DEBUG] - Loading TensorFlow library from: C:\Users\142857\.djl.ai\tensorflow\2.4.1-cpu-win-x86_64\jnitensorflow.dll
Warning: Could not load Loader: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path: C:\Program Files\Java\jdk-16.0.1\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common Files\Oracle\Java\
Warning: Could not load Loader: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path: C:\Program Files\Java\jdk-16.0.1\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common Files\Oracle\Jav
a\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corpo
ration\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;D:\Program Files (x86)\Microsoft VS Code\bin;D:\HashiCorp\Vagrant\bin;C:\Program Files\PuTTY\;C:\ProgramWarning: Could not load Loader: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path: C:\Program Files\Java\jdk-16.0.1\bin;C:\Windows\Sun\Java\bin;C:\Windows\sys
tem32;C:\Windows;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32
\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDI
A NvDLISR;D:\Program Files (x86)\Microsoft VS Code\bin;D:\HashiCorp\Vagrant\bin;C:\Program Files\PuTTY\;C:\Program Files\nodejs\;C:\Program Files\Docker\Docker\resources\bin;C
:\ProgramData\DockerDesktop\version-bin;C:\Program Files\IDM Computer Solutions\UltraEdit;C:\Program Files\Go\bin;C:\Program Files\Git\cmd;C:\Users\142857\AppData\Local\Micros
oft\WindowsApps;C:\Program Files\Bandizip\;C:\Users\142857\AppData\Roaming\npm;C:\Users\142857\go\bin;D:\haskell\ghcup\bin;.
2021-07-15 13:52:25.995301: I external/org_tensorflow/tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Li
brary (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

--------------- Hardware --------------
Available processors (cores): 8
Byte Order: LITTLE_ENDIAN
Free memory (bytes): 244481248
Maximum memory (bytes): 4238344192
Total memory available to JVM (bytes): 266338304
Heap committed: 266338304
Heap nonCommitted: 34078720

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 5m 36s
frankfliu commented 3 years ago

Is that possible you can share your model?

hu-guanwei commented 3 years ago

Is that possible you can share your model?

will do shortly

hu-guanwei commented 3 years ago

Hi. My newest Python model loading and inference can be found at: https://aistudio.baidu.com/aistudio/projectdetail/2191399?shared=1 which gives inference result as image

DJL loading and inference

import ai.djl.Device;
import ai.djl.MalformedModelException;
import ai.djl.inference.Predictor;
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.TranslateException;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {

    public static void main(String[] args) throws MalformedModelException, ModelNotFoundException, IOException, TranslateException {
        // load model
        Criteria<NDList, NDList> criteria = Criteria.builder()
                .setTypes(NDList.class, NDList.class)
                .optEngine("PaddlePaddle")
                .optModelPath(Paths.get("inference.zip"))
                .optModelName("ernie")
                .optDevice(Device.cpu())
                .optProgress(new ProgressBar())
                .build();

        ZooModel<NDList, NDList> model = criteria.loadModel();
        Predictor<NDList, NDList> predictor = model.newPredictor();

        NDManager manager = NDManager.newBaseManager();
        long[] inputIdsData = {1, 103, 1138, 422, 2};
        NDArray inputIds = manager.create(inputIdsData).reshape(1, inputIdsData.length);
        NDArray tokenTypeIds = manager.zeros(new Shape(1, inputIdsData.length), DataType.INT64);

        System.out.println(inputIds);
        System.out.println(tokenTypeIds);
        NDList list = new NDList(inputIds, tokenTypeIds);

        NDList res = predictor.predict(list);
        System.out.println(res.get(0));
    }
}

which gives: image

model .zip file can be found at: https://www.filemail.com/d/gkzkqngzapjsqcd

lanking520 commented 3 years ago

Will take a look

lanking520 commented 3 years ago

@hu-guanwei I am facing the following error during testing of your model. Could you try to remove ~/.djl.ai/cache and try to run inference again? Looks like we are not using the same model for testing

--------------------------------------
C++ Traceback (most recent call last):
--------------------------------------

----------------------
Error Message Summary:
----------------------
InvalidArgumentError: The DataType of equal Op's duplicable Variable Y must be consistent. The current variable type is (int64_t), but the previous variable type is (int). (at /Users/paddle/wangye19/test/Paddle/paddle/fluid/framework/operator.cc:1520)
  [operator < equal > error]

My code

  public static void main(String[] args) throws IOException, MalformedModelException, TranslateException, ModelNotFoundException {
    Criteria<NDList, NDList> criteria = Criteria.builder()
      .setTypes(NDList.class, NDList.class)
      .optEngine("PaddlePaddle")
      .optModelPath(Paths.get("inference.zip"))
      .optModelName("ernie")
      .optDevice(Device.cpu())
      .optProgress(new ProgressBar())
      .build();

    ZooModel<NDList, NDList> model = ModelZoo.loadModel(criteria);
    Predictor<NDList, NDList> predictor = model.newPredictor();

    NDManager manager = NDManager.newBaseManager();
    long[] inputIdsData = {1, 103, 1138, 422, 2};
    NDArray inputIds = manager.create(inputIdsData, new Shape(1, inputIdsData.length));
    NDArray tokenTypeIds = manager.zeros(new Shape(1, inputIdsData.length), DataType.INT64);

    System.out.println(inputIds);
    System.out.println(tokenTypeIds);
    NDList list = new NDList(inputIds, tokenTypeIds);

    NDList res = predictor.predict(list);
    System.out.println(res.get(0));

  }

Gradle setup

dependencies {
  implementation "ai.djl:api:0.12.0"
  implementation "ai.djl.paddlepaddle:paddlepaddle-engine:0.12.0"
  implementation "ai.djl.paddlepaddle:paddlepaddle-native-auto:2.0.2"
  implementation "org.apache.logging.log4j:log4j-slf4j-impl:2.13.3"
}
lanking520 commented 3 years ago

I found the root cause, checkout this fix:

https://github.com/deepjavalibrary/djl/pull/1111

After this PR merged, the result is consistent.

lanking520 commented 3 years ago

The change has been applied to 0.13.0-SNAPSHOT nightly build and will be official with 0.13.0. @hu-guanwei Please try again and see if you can have consistent result

hu-guanwei commented 3 years ago

The change has been applied to 0.13.0-SNAPSHOT nightly build and will be official with 0.13.0. @hu-guanwei Please try again and see if you can have consistent result

will do shortly. thanks in advance.

frankfliu commented 2 years ago

Closing old issues. Feel free to re-open it if you still have questions.