bytedeco / javacpp-presets

The missing Java distribution of native C++ libraries
Other
2.68k stars 744 forks source link

NegativeArraySizeException when building native image with cuda-redist using (flandmark-demo) #1496

Closed alawasoft closed 7 months ago

alawasoft commented 7 months ago

I'm trying to build a native image using the GraalVM native-image tool for the flandmark-demo example from the javacv-examples repository. However, I'm encountering a NegativeArraySizeException during the native image generation process. Here's the relevant part of the modified build.sbt file from flandmark demo repo:

import sbt._
import sbt.Keys._
import sbtassembly.AssemblyPlugin.autoImport._
import scala.language.postfixOps
import scala.sys.process.Process

name := "flandmark-demo"
version := "1.5.8"
scalaVersion := "3.3.1"

scalacOptions ++= Seq("-unchecked", "-deprecation", "-optimize", "-Xlint")

lazy val platform = org.bytedeco.javacpp.Loader.Detector.getPlatform

val gpuDeps = Seq("opencv" -> "4.6.0", "opencv-gpu" -> "4.6.0", "openblas" -> "0.3.21","cuda-redist" -> "11.8-8.6")

libraryDependencies ++= Seq(
  "org.bytedeco" % "javacpp" % "1.5.8" classifier platform,
  "org.bytedeco" % "javacv" % "1.5.8",
  "org.bytedeco" % "flandmark" % "1.07-1.5.8" classifier "",
  "org.bytedeco" % "flandmark" % "1.07-1.5.8" classifier platform,
)

javaCppVersion := "1.5.8"
javaCppPlatform := Seq(org.bytedeco.javacpp.Loader.Detector.getPlatform)
javaCppPresetLibs := gpuDeps

Compile / mainClass := Some("flandmark.Example1")
Compile / run / mainClass := Some("flandmark.Example1")

fork := true

enablePlugins(NativeImagePlugin, AssemblyPlugin)

nativeImageOutput := target.value / "native-image" / s"test"
nativeImageGraalHome := Path(System.getProperty("java.home")).asPath //graalvm java home

assembly / assemblyMergeStrategy := {
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case x => MergeStrategy.first
}

nativeImageOptions := Seq(
  "-J-Xmx24g",
  "--allow-incomplete-classpath",
  s"--class-path=${(assembly / assemblyOutputPath).value.toString}",
  "--verbose",
  "-Ob",
  "--diagnostics-mode"
)

nativeImage := (nativeImage dependsOn assembly).value

Thrown error

Failed generating after 3m 22s.

The build process encountered an unexpected error:

java.lang.NegativeArraySizeException: -278388736

Here are some additional details:

Operating System: ubuntu-amd64 GraalVM version: 22.0.1 (Latest) javacpp-presets version: 1.5.8 flandmark-demo example: repo link

this only occur when cuda-redist is included in the build, opencv and the other libs works just fine,

Tested with javacpp 1.5.11-SNAPSHOT , produces the same error. Tested in windows, produces the same error.

Please let me know if you need any further information to investigate this issue. Thank you for your help!

saudet commented 7 months ago

It just looks like the size of the image is limited to 2 GB

alawasoft commented 7 months ago

That totally make sense. it seems like I should separate out the libs from the executable. how would a typical native-image project ideal ways of loading and excluding cuda-redist (since cuda redist is quite big), a short brief advice would really helps! thanks saudet!

saudet commented 7 months ago

Instead of including the JAR in the image, you could extract it somewhere on the system and set the system PATH to where you extracted it