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

Pytorch preset missing torch::jit related functions #1068

Closed
napps2017 opened this issue Aug 8, 2021 · 51 comments
Closed

Pytorch preset missing torch::jit related functions #1068

napps2017 opened this issue Aug 8, 2021 · 51 comments

Comments

@napps2017
Copy link

Hi there - I'm trying to use the pytorch preset to load a model saved to TorchScript. Having looked at the 'Loading a TorchScript Model in C++' Tutorial (https://pytorch.org/tutorials/advanced/cpp_export.html), I thought this would be done by calling org.bytedeco.pytorch.jit.load("torch_script_model_name.pt") but it looks like the classes/functions related to torch::jit are missing.

@saudet
Copy link
Member

saudet commented Aug 12, 2021

BTW, if your use case is about exporting an existing PyTorch model, you may want to try to export it to ONNX instead since that's well supported by ONNX Runtime: https://github.com/bytedeco/javacpp-presets/tree/master/onnxruntime

@napps2017
Copy link
Author

I managed to get my model into Java using (https://github.com/pytorch/java-demo). My use case is solely inference using my model but I needed to be able to call other helper methods attached to the model in addition to the forward method. I will take a look at whether the model exported in ONNX can provide the same functionality.

@jxtps
Copy link
Contributor

jxtps commented Aug 19, 2021

It seems like the pytorch_java_only library underpinning the java-demo only supports CPU and VULKAN (= mobile android GPUs) inference, not NVIDIA GPU inference: Device.java has only CPU and VULKAN enum values.

ONNX looks workable and like it supports GPU, but a lot more complicated than module.jit.load("my_model.pt").

@saudet
Copy link
Member

saudet commented Aug 27, 2021

Ok, it's done! We can now load TorchScript models like this:
https://github.com/bytedeco/javacpp-presets/blob/master/pytorch/samples/ExampleApp.java

@jxtps
Copy link
Contributor

jxtps commented Sep 10, 2021

Thank you!

I've taken the code for a spin and got it to produce an output. There's a couple of question marks:

  • It seems to have run on the CPU? The output tensor is a CPU tensor (output.device().type() == CPU), and torch.cuda_is_available() is false, and torch.cuda_device_count() == 0. This is despite:
val javacpp = "1.5.7-SNAPSHOT"
libraryDependencies += "org.bytedeco" % "pytorch-platform" % s"1.9.0-$javacpp"
libraryDependencies += "org.bytedeco" % "pytorch-platform-gpu" % s"1.9.0-$javacpp"
libraryDependencies += "org.bytedeco" % "cuda-platform-redist" % s"11.4-8.2-$javacpp"
libraryDependencies += "org.bytedeco" % "mkl-platform-redist" % s"2021.3-$javacpp"

in my build.sbt and a GPU in my win10 machine. I do have CUDA drivers of most likely the wrong version installed, so depending on the load order priority of various DLLs, it may have tried the wrong ones?

  • There's a bit of an impedance mismatch when trying to create Tensors:
    • from_blob has a strange signature (there's tons of them, they all start with a Pointer), compared to https://pytorch.org/cppdocs/notes/tensor_basics.html where it's basically torch::from_blob({1,2,3,4,5,6}, {2,3})? I guess I'd need to do something like val in1 = from_blob(new Pointer(FloatBuffer.wrap(Array[Float](1,2,3,4,5,6))), 2,3), but that coredumps when I do torch.print(in1).
      • Retrying with new FloatPointer(1,2,3,4,5,6) seems to work, as does new FloatPointer(FloatBuffer.wrap(Array[Float](1,2,3,4,5,6))).
    • I can instead do e.g. val in0 = reshape(arange(new Scalar(1), new Scalar(7)), 2, 3), but it's a bit weird to create new Scalars left & right? The c++ API seems to not require that wrapping: https://pytorch.org/docs/stable/generated/torch.arange.html#torch.arange torch.arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
    • These print quite differently with torch.print (which doesn't seem to impact module evaluation, but seems weird?):
from_blob: 
[ CPULongType{2,3} ] 1  2  3
 4  5  6
arange: 
 1  2  3
 4  5  6
  • torch.cuda is a Symbol, which seems to omit some of the useful debugging info mentioned in https://stackoverflow.com/questions/48152674/how-to-check-if-pytorch-is-using-the-gpu like torch.cuda.current_device(), torch.cuda.device(Int), torch.cuda.device_count() and torch.cuda.get_device_name(Int).
  • Getting the shape of a tensor is a bit awkward. tensor.sizes() gives a LongArrayRef, and I guess I can do tensor.sizes().vec().get() to get a long[]? Python & the pytorch_java_only (tensor.shape() -> long[]) libraries seem to have done some customized mapping here?
  • Introspecting the module is a bit confusing. If I load a val module = torch.load("demo-model.pt1"), then:
    • There are zero parameters/named_parameters, and a single attribute/named_attribute: training=false.
    • module.find_method("forward").get().function().pretty_print_schema().getString() prints forward(__torch__.DemoModule self, Tensor data, float incr) -> (Tensor), so clearly some of the information is in there somewhere.
      • Would be great to be able to also get the sizes of the input tensors.
    • But ...function().getSchema.arguments() returns a single Argument, not a list or array of them? And that argument does not appear to contain a list of them (N() is empty).
  • There doesn't seem to be a way to introspect if a MethodOptional or IntOptional actually contain values?
    • When accessing an apparently empty one I get Exception in thread "main" java.lang.RuntimeException: bad optional access.

@saudet
Copy link
Member

saudet commented Sep 11, 2021

in my build.sbt and a GPU in my win10 machine. I do have CUDA drivers of most likely the wrong version installed, so depending on the load order priority of various DLLs, it may have tried the wrong ones?

You'll need the latest version of the drivers for CUDA 11.4 to be able to load, yes.

Mapping a C++ API to Java isn't an easy task, and JavaCPP doesn't try to do anything too fancy, so there are going to be rough edges. Are there any issues that you were not able to work around? I would like to prioritize working on issues that prevent users from getting anything done at all, first. Once those issues are fixed, we can revisit usability issues. Thanks for testing!

@jxtps
Copy link
Contributor

jxtps commented Sep 13, 2021

Reading https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html it says that for CUDA 11.4 you need >=450.80.02 for linux and >=456.38 for windows. I'm on 456.71, so that should ok.

I did however have multiple versions of CUDA installed & in the path. I have removed these from the path, but I still get torch.cuda_is_available() false and torch.cuda_device_count() 0.

The code does seem to work in the sense that it can run on CPU and produce a plausible output, but I have been unable to get it to run on GPU.

Here's a dump running it with System.setProperty("org.bytedeco.javacpp.logger.debug", "true") - there seems to be quite a few load issues? They don't seem to be fatal (= no exception is thrown), but perhaps that's why it won't use the GPU?

Debug: Loading class org.bytedeco.javacpp.presets.javacpp
Debug: Loading class org.bytedeco.javacpp.Loader
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-locale-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-string-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-stdio-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-math-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-heap-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-runtime-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-convert-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-environment-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-time-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-filesystem-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-utility-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-multibyte-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-conio-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-private-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-crt-process-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-console-l1-2-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-string-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-errorhandling-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-timezone-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-file-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-namedpipe-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-handle-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-file-l2-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-heap-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-libraryloader-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-synch-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-processthreads-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-processenvironment-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-datetime-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-localization-l1-2-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-sysinfo-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-synch-l1-2-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-console-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-debug-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-rtlsupport-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-processthreads-l1-1-1.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-file-l1-2-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-profile-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-memory-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-util-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\api-ms-win-core-interlocked-l1-1-0.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\ucrtbase.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\vcruntime140.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\vcruntime140_1.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\msvcp140.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\msvcp140_1.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\concrt140.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\vcomp140.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\javacpp-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\javacpp\windows-x86_64\jnijavacpp.dll
Debug: Loading class org.bytedeco.javacpp.presets.javacpp
Debug: Loading class org.bytedeco.javacpp.Pointer
Debug: Loading class org.bytedeco.javacpp.presets.javacpp
Debug: Loading class org.bytedeco.openblas.global.openblas_nolapack
Debug: Loading class org.bytedeco.javacpp.presets.javacpp
Debug: Loading class org.bytedeco.openblas.global.openblas_nolapack
Debug: Loading library iomp5
Debug: Failed to load for iomp5#iomp5: java.lang.UnsatisfiedLinkError: no iomp5 in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\libiomp5md.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_core.1.dll
Debug: Creating symbolic link C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_core.dll
Debug: Failed to create symbolic link C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_core.dll: java.nio.file.FileSystemException: C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_core.dll: A required privilege is not held by the client.

Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_avx.1.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_avx2.1.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_avx512.1.dll
Debug: Loading library mkl_avx512_mic
Debug: Failed to load for mkl_avx512_mic@.1#mkl_avx512_mic@.1: java.lang.UnsatisfiedLinkError: no mkl_avx512_mic in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_def.1.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_mc.1.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_mc3.1.dll
Debug: Loading library mkl_intel_lp64
Debug: Failed to load for mkl_intel_lp64@.1#mkl_intel_lp64@.1: java.lang.UnsatisfiedLinkError: no mkl_intel_lp64 in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_intel_thread.1.dll
Debug: Loading library mkl_gnu_thread
Debug: Failed to load for mkl_gnu_thread@.1#mkl_gnu_thread@.1: java.lang.UnsatisfiedLinkError: no mkl_gnu_thread in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\mkl_rt.1.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\libopenblas_nolapack.dll
Debug: Loading library openblas_nolapack
Debug: Failed to load for openblas_nolapack@.0: java.lang.UnsatisfiedLinkError: no openblas_nolapack in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\openblas-0.3.17-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\openblas\windows-x86_64\jniopenblas_nolapack.dll
Debug: Loading class org.bytedeco.openblas.global.openblas
Debug: Loading class org.bytedeco.javacpp.presets.javacpp
Debug: Loading class org.bytedeco.openblas.global.openblas_nolapack
Debug: Loading class org.bytedeco.openblas.global.openblas
Debug: Loading C:\Users\jxtps\.javacpp\cache\mkl-2021.3-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\mkl\windows-x86_64\libopenblas.dll
Debug: Loading library openblas
Debug: Failed to load for openblas@.0: java.lang.UnsatisfiedLinkError: no openblas in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\openblas-0.3.17-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\openblas\windows-x86_64\jniopenblas.dll
Debug: Loading class org.bytedeco.pytorch.global.torch
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cudart64_110.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cublasLt64_11.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cublas64_11.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cufft64_10.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\curand64_10.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cusolver64_11.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cusparse64_11.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cudnn64_8.dll
Debug: Loading library nccl64_2
Debug: Failed to load for nccl64_2: java.lang.UnsatisfiedLinkError: no nccl64_2 in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\nvrtc64_112_0.dll
Debug: Loading library myelin64_1
Debug: Failed to load for myelin64_1: java.lang.UnsatisfiedLinkError: no myelin64_1 in java.library.path
Debug: Loading library nvinfer64_7
Debug: Failed to load for nvinfer64_7: java.lang.UnsatisfiedLinkError: no nvinfer64_7 in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cudnn_ops_infer64_8.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cudnn_ops_train64_8.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cudnn_adv_infer64_8.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cudnn_adv_train64_8.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cudnn_cnn_infer64_8.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar\org\bytedeco\cuda\windows-x86_64\cudnn_cnn_train64_8.dll
Debug: Loading library gomp
Debug: Failed to load for gomp@.1: java.lang.UnsatisfiedLinkError: no gomp in java.library.path
Debug: Loading library iomp5
Debug: Failed to load for iomp5: java.lang.UnsatisfiedLinkError: no iomp5 in java.library.path
Debug: Loading library omp
Debug: Failed to load for omp: java.lang.UnsatisfiedLinkError: no omp in java.library.path
Debug: Loading library tbb
Debug: Failed to load for tbb@.2: java.lang.UnsatisfiedLinkError: no tbb in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\asmjit.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\fbgemm.dll
Debug: Loading library nvToolsExt
Debug: Failed to load for nvToolsExt@.1: java.lang.UnsatisfiedLinkError: no nvToolsExt in java.library.path
Debug: Loading library nvToolsExt64_1
Debug: Failed to load for nvToolsExt64_1: java.lang.UnsatisfiedLinkError: no nvToolsExt64_1 in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\c10.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\c10_cuda.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cpu.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll
Debug: Failed to load C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll: java.lang.UnsatisfiedLinkError: C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll: Can't find dependent libraries
Debug: Loading library torch_cuda
Debug: Failed to load for torch_cuda: java.lang.UnsatisfiedLinkError: no torch_cuda in java.library.path
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch.dll
Debug: Failed to load C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch.dll: java.lang.UnsatisfiedLinkError: C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch.dll: Can't find dependent libraries
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\pytorch\windows-x86_64\torch.dll
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\jnitorch.dll
Debug: Loading class org.bytedeco.javacpp.presets.javacpp
Debug: Loading class org.bytedeco.openblas.global.openblas_nolapack
Debug: Loading class org.bytedeco.openblas.global.openblas
Debug: Loading class org.bytedeco.pytorch.global.torch
Debug: Loading class org.bytedeco.pytorch.JitObject
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll
Debug: Failed to load C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll: java.lang.UnsatisfiedLinkError: C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll: Can't find dependent libraries
Debug: Loading library torch_cuda
Debug: Failed to load for torch_cuda: java.lang.UnsatisfiedLinkError: no torch_cuda in java.library.path
Debug: Loading class org.bytedeco.javacpp.presets.javacpp
Debug: Loading class org.bytedeco.openblas.global.openblas_nolapack
Debug: Loading class org.bytedeco.openblas.global.openblas
Debug: Loading class org.bytedeco.pytorch.global.torch
Debug: Loading class org.bytedeco.pytorch.JitModule
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll
Debug: Failed to load C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll: java.lang.UnsatisfiedLinkError: C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll: Can't find dependent libraries
Debug: Loading library torch_cuda
Debug: Failed to load for torch_cuda: java.lang.UnsatisfiedLinkError: no torch_cuda in java.library.path
Debug: Registering org.bytedeco.pytorch.JitModule[address=0x47ce0fb0,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0x47ce0fb0,deallocatorAddress=0x7ff98d313b20]]
Debug: Loading class org.bytedeco.javacpp.presets.javacpp
Debug: Loading class org.bytedeco.openblas.global.openblas_nolapack
Debug: Loading class org.bytedeco.openblas.global.openblas
Debug: Loading class org.bytedeco.pytorch.global.torch
Debug: Loading class org.bytedeco.pytorch.IValueVector
Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll
Debug: Failed to load C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll: java.lang.UnsatisfiedLinkError: C:\Users\jxtps\.javacpp\cache\pytorch-1.9.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar\org\bytedeco\pytorch\windows-x86_64-gpu\torch_cuda.dll: Can't find dependent libraries
Debug: Loading library torch_cuda
Debug: Failed to load for torch_cuda: java.lang.UnsatisfiedLinkError: no torch_cuda in java.library.path
Debug: Registering []

@jxtps
Copy link
Contributor

jxtps commented Sep 13, 2021

There's more in the log as the code runs - it seems like JavaCPP retries loading torch_cuda as it's accessed again.

I can add more if you like, the dump above was quite long as it was ;)

Here's my simple test script:

object PytorchJavacppSpike {

  def main(args: Array[String]): Unit = {
    System.setProperty("org.bytedeco.javacpp.logger.debug", "true")

    /* try to use MKL when available *//* try to use MKL when available */
    System.setProperty("org.bytedeco.openblas.load", "mkl")

    System.out.println("loading module...")

    var module: JitModule = null
    try // Deserialize the ScriptModule from a file using torch::jit::load().
      module = load("demo-model.pt1")
    catch {
      case e: Exception =>
        System.err.println("error loading the model")
        throw e
    }

    System.out.println("ok")

    // Create a vector of inputs.
    val inputs = new IValueVector()
    //inputs.push_back(new IValue(ones(1, 3, 224, 224)))
    val in0: Tensor = reshape(arange(new Scalar(1), new Scalar(7)), 2, 3)
    println("in0: ")
    print(in0)
    // val p = new Pointer(FloatBuffer.wrap(Array[Float](1,2,3,4,5,6)))
    //val p = new FloatPointer(1,2,3,4,5,6)
    val p = new FloatPointer(FloatBuffer.wrap(Array[Float](1, 2, 3, 4, 5, 6)))
    val in1: Tensor = from_blob(p, 2, 3)
    println("in1: ")
    print(in1)
    inputs.push_back(new IValue(in1))
    inputs.push_back(new IValue(new Scalar(3.0)))


    // Execute the model and turn its output into a tensor.
    val output: Tensor = module.forward(inputs).toTensor
    System.out.println("sizes: " + output.sizes().vec())
    System.out.println("output: " + output)
    System.out.println("pytorch output ---- ")
    print(output)
    System.out.println("pytorch output ---- ")

    val device = output.device()
    System.out.println("device type: " + device.`type`())
    System.out.println("device is_cpu: " + device.is_cpu())
    System.out.println("device is_cuda: " + device.is_cuda())
    System.out.println("device is_hip: " + device.is_hip())
    System.out.println("device is_xpu: " + device.is_xpu())

    System.out.println("torch.cuda_is_available() " + torch.cuda_is_available())
    System.out.println("torch.cuda_device_count() " + torch.cuda_device_count())
    System.out.println("torch.cuda() " + torch.cuda())

    System.out.println("module.parameters(): " + module.parameters().size())
    System.out.println("module.named_parameters(): " + module.named_parameters().size())
    System.out.println("module.attributes(): " + module.attributes().size())
    System.out.println("module.named_attributes(): " + module.named_attributes().size())

    val methodOpt: MethodOptional = module.find_method("forward")
    System.out.println("methodOpt: " + methodOpt)
    val method = methodOpt.get()
    System.out.println("method: " + method)
    System.out.println("method.num_inputs: " + method.num_inputs())
    System.out.println("method.name: " + method.name().getString)
    System.out.println("method.function().pretty_print_schema: " + method.function().pretty_print_schema().getString())
    val schema = method.function().getSchema
    System.out.println("method.function().getSchema.arguments: " + schema.arguments())

    def printArguments(label: String, root: Argument): Unit = {

      System.out.println(s" ------ ${label} ------")
      System.out.println("root.type(): " + root.`type`())
      System.out.println("root.type(): " + root.`type`().str().getString)
      System.out.println("root.`type`().kind(): " + root.`type`().kind())
      if (root.name() != null && !root.name().isNull) System.out.println("root.name().getString: " + root.name().getString)
      System.out.println("root.N(): " + root.N())
      System.out.println("root.N().isNull: " + root.N().isNull)
      //      System.out.println("root.N().get(): " + root.N().get())
      //      for (i <- 0 to root.N().get()) {
      //        val arg = root.position(i)
      //
      //        System.out.println(s"arg_${i}.type(): " + arg.`type`())
      //        System.out.println(s"arg_${i}.name().getString: " + arg.name().getString)
      //      }
    }

    printArguments("args", schema.arguments())
    printArguments("returns", schema.returns())
  }

with build.sbt:

name := "pytorch_spike"

version := "1.0"

scalaVersion := "2.12.13"

resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"

val javacpp = "1.5.7-SNAPSHOT"
libraryDependencies += "org.bytedeco" % "pytorch-platform" % s"1.9.0-$javacpp"
libraryDependencies += "org.bytedeco" % "pytorch-platform-gpu" % s"1.9.0-$javacpp"
libraryDependencies += "org.bytedeco" % "cuda-platform-redist" % s"11.4-8.2-$javacpp"
libraryDependencies += "org.bytedeco" % "mkl-platform-redist" % s"2021.3-$javacpp"

The demo-model.pt1 is from https://github.com/pytorch/java-demo

@saudet
Copy link
Member

saudet commented Sep 14, 2021

Debug: Failed to load for nvToolsExt64_1: java.lang.UnsatisfiedLinkError: no nvToolsExt64_1 in java.library.path

You're going to need nvToolsExt64_1.dll on Windows. That typically gets installed with the driver under C:/Program Files/NVIDIA Corporation/NvToolsExt/bin/x64/ but if you have it in a different directory, please add it to your PATH.

@jxtps
Copy link
Contributor

jxtps commented Sep 14, 2021

Installed CUDA 11.4 and got the missing dll you referenced, and now torch.cuda_is_available() true and torch.cuda_device_count() 1, thanks!

For future reference: https://developer.download.nvidia.com/NsightVisualStudio/2.2/Documentation/UserGuide/HTML/Content/NVIDIA_Tools_Extension_Library_NVTX.htm

@jxtps
Copy link
Contributor

jxtps commented Sep 14, 2021

Should that DLL be included in the CUDA presets for windows?

I reinstalled the NVIDIA driver first and didn't get the DLL, I had to install CUDA itself.

@saudet
Copy link
Member

saudet commented Sep 15, 2021

Should that DLL be included in the CUDA presets for windows?

I reinstalled the NVIDIA driver first and didn't get the DLL, I had to install CUDA itself.

Hum, ok, so it gets installed with CUDA. It looks like a deprecated API, but since PyTorch needs it, sure, let's bundle it! I've added it in commit 02c27e4, and the snapshots have been updated, so please give it a try with mvn -U .... Thanks!

@jxtps
Copy link
Contributor

jxtps commented Sep 16, 2021

Success!

I removed the recently installed CUDA libs from my PATH, and re-fetched cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64.jar and cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64-redist.jar, and now torch.cuda_is_available() true without relying on CUDA being pre-installed.

Looked like you added jninvToolsExt.dll to cuda-11.4-8.2-1.5.7-SNAPSHOT-windows-x86_64.jar?

@saudet
Copy link
Member

saudet commented Sep 24, 2021

@jxtps BTW, it's also possible to use the libraries that come with LibTorch, see issue #1083.

@jbaron
Copy link

jbaron commented Oct 24, 2021

The import of torch visions and other torch-script models works like a charm (using mostly CPU+MKL). I noticed there is also the torch.trace function available, but based on the signature that seems not to be the same as torch::jit::trace

Is there a way to create a torch-script JitModule from a regular Module defined in Java? So something like:

   jitModel = torch.trace(myModule, exampleData)

@saudet
Copy link
Member

saudet commented Oct 24, 2021

Is there a way to create a torch-script JitModule from a regular Module defined in Java? So something like:

   jitModel = torch.trace(myModule, exampleData)

@jbaron According to issue pytorch/pytorch#17614 (comment) it doesn't appear possible, no. But it's still possible to train a torch::jit::script::Module, see discussion #1075.

@jbaron
Copy link

jbaron commented Oct 24, 2021

Thanks. I am a bit surprised to see that tracing is not done by recording the native C++ operations since I would expect it to be almost the same as a forward pass in training. But seems a lot of the tracing logic resides in Python, which is a shame for re-usability.

@saudet
Copy link
Member

saudet commented Oct 24, 2021

Again, see issue pytorch/pytorch#26086 about that:

We have no current plans to support script export from the C++ frontend. Doing so is a very high-complexity project (multiple full time engineers working on it basically indefinitely), and we don't think it's worth it due to the number of alternatives available. cc @yf225 who is the maintainer for this area.

@jxtps
Copy link
Contributor

jxtps commented Dec 16, 2021

Did something happen to the snapshots? I'm re-importing my project:

resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
// https://github.com/bytedeco/javacpp-presets/tree/master/pytorch
val javacpp = "1.5.7-SNAPSHOT"
libraryDependencies += "org.bytedeco" % "pytorch-platform" % s"1.10.0-$javacpp"
libraryDependencies += "org.bytedeco" % "pytorch-platform-gpu" % s"1.10.0-$javacpp"
libraryDependencies += "org.bytedeco" % "cuda-platform-redist" % s"11.4-8.2-$javacpp"
libraryDependencies += "org.bytedeco" % "mkl-platform-redist" % s"2021.4-$javacpp"

And get a bunch of errors:

[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#pytorch-platform;1.10.0-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#javacpp-platform;1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#javacpp;1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#openblas-platform;0.3.18-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#openblas;0.3.18-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#pytorch;1.10.0-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#pytorch-platform-gpu;1.10.0-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#cuda-platform-redist;11.4-8.2-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#cuda-platform;11.4-8.2-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#cuda;11.4-8.2-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#mkl-platform-redist;2021.4-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#mkl-platform;2021.4-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#mkl;2021.4-1.5.7-SNAPSHOT
[info] downloading https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/javacpp-platform/1.5.7-SNAPSHOT/javacpp-platform-1.5.7-20211215.035353-371.jar ...
[info] 	[SUCCESSFUL ] org.bytedeco#javacpp-platform;1.5.7-SNAPSHOT!javacpp-platform.jar (404ms)
[info] downloading https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/javacpp/1.5.7-SNAPSHOT/javacpp-1.5.7-20211215.035453-368.jar ...
[warn] 	Detected merged artifact: [FAILED     ] org.bytedeco#pytorch;1.10.0-1.5.7-SNAPSHOT!pytorch.jar:  (0ms).
[warn] ==== local: tried
[warn]   C:\Users\jxtps\.ivy2\local\org.bytedeco\pytorch\1.10.0-1.5.7-SNAPSHOT\jars\pytorch-windows-x86_64.jar
[warn] ==== public: tried
[warn]   https://repo1.maven.org/maven2/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64.jar
[warn] ==== local-preloaded-ivy: tried
[warn]   C:\Users\jxtps\.sbt\preloaded\org.bytedeco\pytorch\1.10.0-1.5.7-SNAPSHOT\jars\pytorch-windows-x86_64.jar
[warn] ==== local-preloaded: tried
[warn]   file:/C:/Users/jxtps/.sbt/preloaded/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64.jar
[warn] ==== Sonatype OSS Snapshots: tried
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-20211115.130833-181-windows-x86_64.jar
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64.jar
[warn] 	Detected merged artifact: [FAILED     ] org.bytedeco#pytorch;1.10.0-1.5.7-SNAPSHOT!pytorch.jar:  (0ms).
[warn] ==== local: tried
[warn]   C:\Users\jxtps\.ivy2\local\org.bytedeco\pytorch\1.10.0-1.5.7-SNAPSHOT\jars\pytorch-macosx-x86_64.jar
[warn] ==== public: tried
[warn]   https://repo1.maven.org/maven2/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-macosx-x86_64.jar
[warn] ==== local-preloaded-ivy: tried
[warn]   C:\Users\jxtps\.sbt\preloaded\org.bytedeco\pytorch\1.10.0-1.5.7-SNAPSHOT\jars\pytorch-macosx-x86_64.jar
[warn] ==== local-preloaded: tried
[warn]   file:/C:/Users/jxtps/.sbt/preloaded/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-macosx-x86_64.jar
[warn] ==== Sonatype OSS Snapshots: tried
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-20211115.130833-181-macosx-x86_64.jar
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-macosx-x86_64.jar
[warn] 	Detected merged artifact: [FAILED     ] org.bytedeco#pytorch;1.10.0-1.5.7-SNAPSHOT!pytorch.jar:  (0ms).
[warn] ==== local: tried
[warn]   C:\Users\jxtps\.ivy2\local\org.bytedeco\pytorch\1.10.0-1.5.7-SNAPSHOT\jars\pytorch-windows-x86_64-gpu.jar
[warn] ==== public: tried
[warn]   https://repo1.maven.org/maven2/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar
[warn] ==== local-preloaded-ivy: tried
[warn]   C:\Users\jxtps\.sbt\preloaded\org.bytedeco\pytorch\1.10.0-1.5.7-SNAPSHOT\jars\pytorch-windows-x86_64-gpu.jar
[warn] ==== local-preloaded: tried
[warn]   file:/C:/Users/jxtps/.sbt/preloaded/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar
[warn] ==== Sonatype OSS Snapshots: tried
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-20211115.130833-181-windows-x86_64-gpu.jar
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64-gpu.jar
[warn] 	Detected merged artifact: [FAILED     ] org.bytedeco#pytorch;1.10.0-1.5.7-SNAPSHOT!pytorch.jar:  (0ms).
[warn] ==== local: tried
[warn]   C:\Users\jxtps\.ivy2\local\org.bytedeco\pytorch\1.10.0-1.5.7-SNAPSHOT\jars\pytorch-linux-x86_64.jar
[warn] ==== public: tried
[warn]   https://repo1.maven.org/maven2/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-linux-x86_64.jar
[warn] ==== local-preloaded-ivy: tried
[warn]   C:\Users\jxtps\.sbt\preloaded\org.bytedeco\pytorch\1.10.0-1.5.7-SNAPSHOT\jars\pytorch-linux-x86_64.jar
[warn] ==== local-preloaded: tried
[warn]   file:/C:/Users/jxtps/.sbt/preloaded/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-linux-x86_64.jar
[warn] ==== Sonatype OSS Snapshots: tried
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-20211115.130833-181-linux-x86_64.jar
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/pytorch-1.10.0-1.5.7-SNAPSHOT-linux-x86_64.jar
[info] 	[SUCCESSFUL ] org.bytedeco#javacpp;1.5.7-SNAPSHOT!javacpp.jar (2869ms)
[warn] 	::::::::::::::::::::::::::::::::::::::::::::::
[warn] 	::              FAILED DOWNLOADS            ::
[warn] 	:: ^ see resolution messages for details  ^ ::
[warn] 	::::::::::::::::::::::::::::::::::::::::::::::
[warn] 	:: org.bytedeco#pytorch;1.10.0-1.5.7-SNAPSHOT!pytorch.jar
[warn] 	:: org.bytedeco#pytorch;1.10.0-1.5.7-SNAPSHOT!pytorch.jar
[warn] 	:: org.bytedeco#pytorch;1.10.0-1.5.7-SNAPSHOT!pytorch.jar
[warn] 	:: org.bytedeco#pytorch;1.10.0-1.5.7-SNAPSHOT!pytorch.jar
[warn] 	::::::::::::::::::::::::::::::::::::::::::::::

@jxtps
Copy link
Contributor

jxtps commented Dec 16, 2021

Can't find the bytedeco pytorch libraries on https://oss.sonatype.org/#nexus-search;quick~pytorch ?

@saudet
Copy link
Member

saudet commented Dec 16, 2021

@jxtps
Copy link
Contributor

jxtps commented Dec 16, 2021

Ah, yes, there's a long list of them there, but the specific ones that I get error messages for aren't for some reason.

So there are many windows snapshots, but no pytorch-1.10.0-1.5.7-20211115.130833-181-windows-x86_64.jar, only a pytorch-1.10.0-1.5.7-20211115.130833-181-linux-x86_64-gpu.jar for that specific timestamp.

There is a pytorch-1.10.0-1.5.7-20211115.084250-182-windows-x86_64.jar (slightly different timestamp) which was built shortly after the linux one, so maybe there's a glitch in how "snapshot" maps to a specific file for the non-linux jars?

@saudet
Copy link
Member

saudet commented Dec 19, 2021

@jxtps I've fixed the snapshots, please give it a try again! As for GPU support, please use LibTorch for now:
https://github.com/bytedeco/javacpp-presets/tree/master/pytorch#documentation

@jxtps
Copy link
Contributor

jxtps commented Dec 22, 2021

@jxtps
Copy link
Contributor

jxtps commented Dec 22, 2021

They're present for 1.10.0 ( https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.0-1.5.7-SNAPSHOT/ ) but then I get a weird linking error for jnitorch:

Debug: Loading C:\Users\jxtps\.javacpp\cache\pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\pytorch\windows-x86_64\jnitorch.dll
Debug: Failed to load C:\Users\jxtps\.javacpp\cache\pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\pytorch\windows-x86_64\jnitorch.dll: java.lang.UnsatisfiedLinkError: C:\Users\jxtps\.javacpp\cache\pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\pytorch\windows-x86_64\jnitorch.dll: The specified procedure could not be found
Debug: Loading library jnitorch
Debug: Failed to load for jnitorch: java.lang.UnsatisfiedLinkError: no jnitorch in java.library.path
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnitorch in java.library.path
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
	at java.lang.Runtime.loadLibrary0(Runtime.java:870)
	at java.lang.System.loadLibrary(System.java:1122)
	at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1762)
	at org.bytedeco.javacpp.Loader.load(Loader.java:1369)
	at org.bytedeco.javacpp.Loader.load(Loader.java:1181)
	at org.bytedeco.javacpp.Loader.load(Loader.java:1157)
	at org.bytedeco.pytorch.global.torch.<clinit>(torch.java:19)
	at spike.PytorchJavacppSpike$.main(PytorchJavacppSpike.scala:27)
	at spike.PytorchJavacppSpike.main(PytorchJavacppSpike.scala)
Caused by: java.lang.UnsatisfiedLinkError: C:\Users\jxtps\.javacpp\cache\pytorch-1.10.0-1.5.7-SNAPSHOT-windows-x86_64.jar\org\bytedeco\pytorch\windows-x86_64\jnitorch.dll: The specified procedure could not be found
	at java.lang.ClassLoader$NativeLibrary.load(Native Method)
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1934)
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1817)
	at java.lang.Runtime.load0(Runtime.java:809)
	at java.lang.System.load(System.java:1086)
	at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1709)
	... 6 more

The file is present on disk, but The specified procedure could not be found error message seems to point to some versioning issue?

@saudet
Copy link
Member

saudet commented Dec 22, 2021

Yes, that means there's some function definitions missing from the DLLs you are giving it. It's probably just because the versions of LibTorch you're trying to use isn't the right one. If you're sure your versions of PyTorch match, please try to use something like the Dependencies tool to find out what exactly is missing:
https://github.com/bytedeco/javacpp-presets/wiki/Debugging-UnsatisfiedLinkError-on-Windows

@jxtps
Copy link
Contributor

jxtps commented Dec 23, 2021

The specific SBT errors I'm getting with 1.10.1:

[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#pytorch-platform;1.10.1-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#javacpp-platform;1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#javacpp;1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#openblas-platform;0.3.18-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#openblas;0.3.18-1.5.7-SNAPSHOT
[warn] Choosing Sonatype OSS Snapshots for org.bytedeco#pytorch;1.10.1-1.5.7-SNAPSHOT
[warn] 	Detected merged artifact: [FAILED     ] org.bytedeco#pytorch;1.10.1-1.5.7-SNAPSHOT!pytorch.jar:  (0ms).
[warn] ==== local: tried
[warn]   C:\Users\jxtps\.ivy2\local\org.bytedeco\pytorch\1.10.1-1.5.7-SNAPSHOT\jars\pytorch-linux-x86_64${javacpp.platform.extension}.jar
[warn] ==== public: tried
[warn]   https://repo1.maven.org/maven2/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-SNAPSHOT-linux-x86_64${javacpp.platform.extension}.jar
[warn] ==== local-preloaded-ivy: tried
[warn]   C:\Users\jxtps\.sbt\preloaded\org.bytedeco\pytorch\1.10.1-1.5.7-SNAPSHOT\jars\pytorch-linux-x86_64${javacpp.platform.extension}.jar
[warn] ==== local-preloaded: tried
[warn]   file:/C:/Users/jxtps/.sbt/preloaded/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-SNAPSHOT-linux-x86_64${javacpp.platform.extension}.jar
[warn] ==== Sonatype OSS Snapshots: tried
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-20211219.163942-4-linux-x86_64${javacpp.platform.extension}.jar
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-SNAPSHOT-linux-x86_64${javacpp.platform.extension}.jar
[warn] 	Detected merged artifact: [FAILED     ] org.bytedeco#pytorch;1.10.1-1.5.7-SNAPSHOT!pytorch.jar:  (0ms).
[warn] ==== local: tried
[warn]   C:\Users\jxtps\.ivy2\local\org.bytedeco\pytorch\1.10.1-1.5.7-SNAPSHOT\jars\pytorch-windows-x86_64${javacpp.platform.extension}.jar
[warn] ==== public: tried
[warn]   https://repo1.maven.org/maven2/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-SNAPSHOT-windows-x86_64${javacpp.platform.extension}.jar
[warn] ==== local-preloaded-ivy: tried
[warn]   C:\Users\jxtps\.sbt\preloaded\org.bytedeco\pytorch\1.10.1-1.5.7-SNAPSHOT\jars\pytorch-windows-x86_64${javacpp.platform.extension}.jar
[warn] ==== local-preloaded: tried
[warn]   file:/C:/Users/jxtps/.sbt/preloaded/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-SNAPSHOT-windows-x86_64${javacpp.platform.extension}.jar
[warn] ==== Sonatype OSS Snapshots: tried
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-20211219.163942-4-windows-x86_64${javacpp.platform.extension}.jar
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-SNAPSHOT-windows-x86_64${javacpp.platform.extension}.jar
[warn] 	Detected merged artifact: [FAILED     ] org.bytedeco#pytorch;1.10.1-1.5.7-SNAPSHOT!pytorch.jar:  (0ms).
[warn] ==== local: tried
[warn]   C:\Users\jxtps\.ivy2\local\org.bytedeco\pytorch\1.10.1-1.5.7-SNAPSHOT\jars\pytorch-macosx-x86_64${javacpp.platform.extension}.jar
[warn] ==== public: tried
[warn]   https://repo1.maven.org/maven2/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-SNAPSHOT-macosx-x86_64${javacpp.platform.extension}.jar
[warn] ==== local-preloaded-ivy: tried
[warn]   C:\Users\jxtps\.sbt\preloaded\org.bytedeco\pytorch\1.10.1-1.5.7-SNAPSHOT\jars\pytorch-macosx-x86_64${javacpp.platform.extension}.jar
[warn] ==== local-preloaded: tried
[warn]   file:/C:/Users/jxtps/.sbt/preloaded/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-SNAPSHOT-macosx-x86_64${javacpp.platform.extension}.jar
[warn] ==== Sonatype OSS Snapshots: tried
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-20211219.163942-4-macosx-x86_64${javacpp.platform.extension}.jar
[warn]   https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/pytorch-1.10.1-1.5.7-SNAPSHOT-macosx-x86_64${javacpp.platform.extension}.jar
[warn] 	::::::::::::::::::::::::::::::::::::::::::::::
[warn] 	::              FAILED DOWNLOADS            ::
[warn] 	:: ^ see resolution messages for details  ^ ::
[warn] 	::::::::::::::::::::::::::::::::::::::::::::::
[warn] 	:: org.bytedeco#pytorch;1.10.1-1.5.7-SNAPSHOT!pytorch.jar
[warn] 	:: org.bytedeco#pytorch;1.10.1-1.5.7-SNAPSHOT!pytorch.jar
[warn] 	:: org.bytedeco#pytorch;1.10.1-1.5.7-SNAPSHOT!pytorch.jar
[warn] 	::::::::::::::::::::::::::::::::::::::::::::::
[error] sbt.librarymanagement.ResolveException: download failed: org.bytedeco#pytorch;1.10.1-1.5.7-SNAPSHOT!pytorch.jar

I can see the relevant files on https://oss.sonatype.org/content/repositories/snapshots/org/bytedeco/pytorch/1.10.1-1.5.7-SNAPSHOT/ (sorry I got the paths mixed up), but ${javacpp.platform.extension} seems to be messing them up?

I tried adding javaOptions += "-Djavacpp.platform.extension=" to my build.sbt, but no dice.

@saudet
Copy link
Member

saudet commented Dec 23, 2021

It sounds like you're trying to use a very old version of sbt. IIRC, all these bugs have been fixed a few years ago.

@jxtps
Copy link
Contributor

jxtps commented Dec 23, 2021

Ah, right, I had reused an old project without looking at the SBT version, that resolved it, thanks!

The linking issue remains unfortunately. I am using 1.10.1 for both libraryDependencies += "org.bytedeco" % "pytorch-platform" % s"1.10.1-1.5.7-SNAPSHOT" and for LibTorch (I've triple-checked it). When I use the Dependency tool it reports an issue:

Pasted-20211222-164107-1047x568-(1,83)-redact-109x13-(48,1)_

When I hover over the red icon it says torch_cpu.dll module has missing imports but I don't know how to proceed from there?

@saudet
Copy link
Member

saudet commented Dec 23, 2021

Click on it to see the missing imports...

@jxtps
Copy link
Contributor

jxtps commented Dec 23, 2021

I've clicked on it plenty, but I don't see any missing imports? The window has four sections:

image

These seem to contain:
Top left: tree of dependencies.
Top right: list of module imports.
Middle right: list of module exports.
Bottom: list of modules with some additional information.

So presumably the missing imports would be shown in the top right window.

When I scroll through every single import for jnitorch.dll there's nothing that stands out about them to me (they all have Ordinal N/A, an int Hint, a super-long function name, are in one of the listed modules, Delayed=False, Demangler=Microsoft), and none are colored red or similar.

When I scroll through every single import for torch_cpu.dll, they all seem to be self-referential (i.e. their Module columns has torch_cpu.dll in it).

Cross-matching a handful of the imports in jnitorch.dll with the exports in torch_cpu.dll, they seem to be there. It uses (tens of?) thousands of functions, so cross-checking each and every one by hand is not quite reasonable.

???

@saudet
Copy link
Member

saudet commented Dec 23, 2021

Well, something's missing. Someone will need to debug this, might as well be you. :) I find that Dependency Walker is usually still better at spotting missing things even though it hasn't been updated for many years. Please give that one a try as well:
https://www.dependencywalker.com/

@jxtps
Copy link
Contributor

jxtps commented Dec 23, 2021

Found the bastard. Apparently I missed it when scrolling through the list in the original dependencies tool. Anyway, here it is in all its glory:

N/A, 5074 (0x000013d2), public: __cdecl at::indexing::TensorIndex::TensorIndex(void) __ptr64, c:\work\jni\torch_cpu.dll, False, Microsoft

@saudet
Copy link
Member

saudet commented Dec 23, 2021

Ok, so Visual Studio doesn't like that definition it seems. I've tweaked this in commit 4b18d83. Let's see if it likes it better that way.

@saudet
Copy link
Member

saudet commented Dec 23, 2021

@jxtps The snapshots have been updated! Please let me know if this is still happening. Thanks for the help!

@jxtps
Copy link
Contributor

jxtps commented Dec 23, 2021

Ok, so even after updating to the new snapshot I'm still getting the same error:

N/A, 5074 (0x000013d2), public: __cdecl at::indexing::TensorIndex::TensorIndex(void) __ptr64, c:\work\jni\torch_cpu.dll, False, Microsoft

That's from .javacpp\cache\pytorch-1.10.1-1.5.7-20211223.084148-8-windows-x86_64.jar\org\bytedeco\pytorch\windows-x86_64\jnitorch.dll I also diffed the two dlls, they're ever so slightly different.

Separately it turns out you can sort the imports in Dependencies by clicking the little headers, and that also goes for the first header that marks the missing functions in magenta/red (fuchsia?), so a single click on that header shows you the missing methods quickly & easily.

@saudet
Copy link
Member

saudet commented Dec 23, 2021

Yeah, ok, it looks like Visual Studio wants to import inline functions no matter what:
https://docs.microsoft.com/en-us/cpp/cpp/defining-inline-cpp-functions-with-dllexport-and-dllimport
So, let's see, I think just setting a default argument on an existing function should do the trick.
I've done that in 20f9e68. Please give it a try when the snapshots are done in a couple of hours.

@jxtps
Copy link
Contributor

jxtps commented Dec 24, 2021

It linked & torch.cuda_is_available() == true, thanks!

@jxtps
Copy link
Contributor

jxtps commented Dec 31, 2021

I've now actually run an exported model, and it is successfully running on GPU, thanks!

I'm a little fuzzy on the precise memory ownership semantics - do I need to explicitly call .close() on both the FloatPointer, and on the Tensor in the code below? Or is closing the Tensor sufficient?

float[] floats = /* image loading code */
FloatPointer p = new FloatPointer(FloatBuffer.wrap(floats));
Tensor t = from_blob(p, 1, 3, h, w);

(yes, this should presumably be in a try-with-resources block)

Getting the data out of the resulting tensor is actually a little tricky. Below is the crude recipe I've got so far. Note that TensorAccessor is unmapped - I don't know what idiomatic usage should be, saw some references to it.

Tensor tensor = module.forward(inputs).toTensor(); // Model internally transfers tensor to CPU, otherwise need .to(...)
FloatBuffer fbuf = tensor.data_ptr_float().limit(tensor.numel()).asBuffer(); // Requires that tensor.has_storage()==true & probably that tensor.is_contiguous()==true
// Calling fbuf.array() barfs, so get() the contents instead
fbuf.rewind(); // Probably unnecessary
float[] floats = new float[fbuf.remaining()];
fbuf.get(floats);

The tensor presumably needs to be closed - do the intermediate FloatPointers (that I eventually call .asBuffer() on) also need to be closed explicitly?

@jxtps
Copy link
Contributor

jxtps commented Dec 31, 2021

Also, any chance for a non-snapshot 1.5.7? The snapshots don't really work with out deploy system - it wants stable / immutable builds. Thanks!

@saudet
Copy link
Member

saudet commented Jan 1, 2022

I'm a little fuzzy on the precise memory ownership semantics - do I need to explicitly call .close() on both the FloatPointer, and on the Tensor in the code below? Or is closing the Tensor sufficient?

I don't think Tensor takes ownership of a pointer like that, let's check the docs:

If you want to be sure you're doing it right, just call close() on everything, that's fine, but that's what PointerScope is for:

Getting the data out of the resulting tensor is actually a little tricky. Below is the crude recipe I've got so far. Note that TensorAccessor is unmapped - I don't know what idiomatic usage should be, saw some references to it.

TensorAccessor is only useful because it's more efficient than Tensor itself, but it's not going to be fast when used with JNI anyway, so if you're looking for something fast, what you want to use is an Indexer:

If you have already tried the Indexer interface and found it to be lacking though, please do mention it. I can't guess that you've already tried it just from what you're saying here! Please be more specific in your questions.

The tensor presumably needs to be closed - do the intermediate FloatPointers (that I eventually call .asBuffer() on) also need to be closed explicitly?

We can't force a Buffer to take ownership of a random pointer, that's part of the docs too:

Also, any chance for a non-snapshot 1.5.7? The snapshots don't really work with out deploy system - it wants stable / immutable builds. Thanks!

In 2 or 3 months, probably. You're still requesting fixes every other week, so I don't see the point of clogging the central repository with binaries no one is ever going to use! But if you mean that you would like to do it yourself, please let me know your account name on Sonatype OSSRH, and I'll give you deploy access, that's fine.

@saudet
Copy link
Member

saudet commented Jan 4, 2022

@jxtps I've added to Tensor a few factory methods that you might find useful in commit 392c34c.
Please let me know if you're having any issues with them though. Thanks for your time testing all this!

@jxtps
Copy link
Contributor

jxtps commented Jan 4, 2022

Getting data out

I was unaware of the Indexer interface, will take a look at that when I implement the actual production code.

PointerScope looks interesting, neat using a ThreadLocal for the scopeStack, giving Java a pythonic with PointerScope(): feel while still allowing multi-threaded operation ;) Since everything is a pointer it looks like that'll be a great catch-all.

In 2 or 3 months

Sounds good, fair point on the rate of change, hopefully things will stabilize soon - I think it's close now that I have an end-to-end proof of concept.

Factory methods

Those look great!

Thanks for your time

We both know who needs to thank whom here, so thanks! ;)

@saudet
Copy link
Member

saudet commented Feb 10, 2022

These new features and fixes have all been released with version 1.5.7 alongside full builds for PyTorch 1.10.2. Enjoy!

@saudet saudet closed this as completed Feb 10, 2022
@saudet
Copy link
Member

saudet commented Mar 15, 2022

@jxtps I've updated a couple of more things, so let me reply to all your initial questions to see if there's anything still missing for your application.

  • There's a bit of an impedance mismatch when trying to create Tensors:

With the factory methods added in commit 392c34c, we can easily create tensors from Java arrays, for example in this case, something like Tensor.create(new float[]{1,2,3,4,5,6}, 2,3).

Those functions seem to be available only from the Python API, in which case we can't map them directly. If there are any functions available from C++ that you need to access from Java though, please let me know.

  • Getting the shape of a tensor is a bit awkward. tensor.sizes() gives a LongArrayRef, and I guess I can do tensor.sizes().vec().get() to get a long[]? Python & the pytorch_java_only (tensor.shape() -> long[]) libraries seem to have done some customized mapping here?

The C++ API relies on a lot of wrapper objects, but the user doesn't need to deal with them explicitly since the C++ language itself can do implicit conversions. The Java language cannot do that, so we need to wrap and unwrap a lot of things like that manually. I don't see an easy way to work around this, in general. In this case, yes, tensor.sizes().vec().get() looks like the easiest way to get a long[] out of that. We can also add some custom mappings if you'd like. In the case of Tensor, please open a pull request with whatever you want included in here:
https://github.com/bytedeco/javacpp-presets/blob/master/pytorch/src/main/java/org/bytedeco/pytorch/AbstractTensor.java

  • Introspecting the module is a bit confusing. If I load a val module = torch.load("demo-model.pt1"), then:

    • There are zero parameters/named_parameters, and a single attribute/named_attribute: training=false.

Those functions have been available, anything in particular missing?
https://github.com/bytedeco/javacpp-presets/blob/master/pytorch/src/gen/java/org/bytedeco/pytorch/Module.java
https://github.com/bytedeco/javacpp-presets/blob/master/pytorch/src/gen/java/org/bytedeco/pytorch/JitModule.java

  • But ...function().getSchema.arguments() returns a single Argument, not a list or array of them? And that argument does not appear to contain a list of them (N() is empty).

I've added an ArgumentVector instance for this purpose in commit e6140a9.

  • There doesn't seem to be a way to introspect if a MethodOptional or IntOptional actually contain values?

I've added has_value() methods to those classes in commit bytedeco/javacpp@cd47d1d.

@jxtps
Copy link
Contributor

jxtps commented Mar 24, 2022

That all sounds great! Maybe we could add a shape() method to AbstractTensor.java to mimic what's conveniently available in python. Here's a stab:

public long[] shape() {
    long[] out = new long[(int) ndimension()];
    for (int i = 0; i < out.length; i++) out[i] = size(i);
    return out;
}

(Note that LongArrayRef sizes() is defined in TensorBase and hence not available in AbstractTensor, so can't just do sizes().vec().get())

In scala I'd make that a lazy val - I guess we could add a private long[] _shape and memoize it, but then we have the otherwise unnecessary extra member. I guess Tensors are often quite large, so maybe that's not that big of a concern? Here's that version:

private long[] _shape;
public long[] shape() {
    if (_shape == null) {
        // Fill-then-assign to guarantee that concurrent calls always return a filled array. 
        // (Assign-then-fill would risk returning an unfilled array in a parallel call).
        long[] out = new long[(int) ndimension()];
        for (int i = 0; i < out.length; i++) out[i] = size(i);
        _shape = out;
    }
    return _shape;
}

(I don't think it'd be worth it to add synchronization to ensure only a single long[] ever gets created - concurrent tensor calls should be exceptionally rare)

Happy to do a pull request with whichever version you prefer.

@saudet
Copy link
Member

saudet commented Mar 24, 2022

We can add a sizes() abstract method to AbstractTensor, but it's probably better to just use the simpler interface like you did. That looks fine, please do open a pull request!

If Tensor can change shape, we can't cache it into a Java array. Are you sure that never happens?

jxtps added a commit to jxtps/javacpp-presets that referenced this issue Mar 25, 2022
Add `shape()` convenience method, similar to `sizes().vec().get()`. See bytedeco#1068 for discussion.
@jxtps
Copy link
Contributor

jxtps commented Mar 25, 2022

That can indeed happen, with a call to an in-place transpose_() or squeeze_(), good catch. Also, if we internally memoize, then the caller could modify the contents, which on some level isn't that big of a deal, but definitely goes against typical Java behavior.

Created a pull request with the simpler version: #1161

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

No branches or pull requests

4 participants