mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
jni-java wrapper for pytorchScript api (#25084)
Summary: TLDR; initial commit of android java-jni wrapper of pytorchscript c++ api The main idea is to provide java interface for android developers to use pytorchscript modules. java API tries to repeat semantic of c++ and python pytorchscript API org.pytorch.Module (wrapper of torch::jit::script::Module) - static Module load(String path) - IValue forward(IValue... inputs) - IValue runMethod(String methodName, IValue... inputs) org.pytorch.Tensor (semantic of at::Tensor) - newFloatTensor(long[] dims, float[] data) - newFloatTensor(long[] dims, FloatBuffer data) - newIntTensor(long[] dims, int[] data) - newIntTensor(long[] dims, IntBuffer data) - newByteTensor(long[] dims, byte[] data) - newByteTensor(long[] dims, ByteBuffer data) org.pytorch.IValue (semantic of at::IValue) - static factory methods to create pytorchscript supported types Examples of usage api could be found in PytorchInstrumentedTests.java: Module module = Module.load(path); IValue input = IValue.tensor(Tensor.newByteTensor(new long[]{1}, Tensor.allocateByteBuffer(1))); IValue output = module.forward(input); Tensor outputTensor = output.getTensor(); ThreadSafety: Api is not thread safe, all synchronization must be done on caller side. Mutability: org.pytorch.Tensor buffer is DirectBuffer with native byte order, can be created with static factory methods specifing DirectBuffer. At the moment org.pytorch.Tensor does not hold at::Tensor on jni side, it has: long[] dimensions, type, DirectByteBuffer blobData Input tensors are mutable (can be modified and used for the next inference), Uses values from buffer on the momment of Module#forward or Module#runMethod calls. Buffers of input tensors is used directly by input at::Tensor Output is copied from output at::Tensor and is immutable. Dependencies: Jni level is implemented with usage of fbjni library, that was developed in Facebook, and was already used and opensourced in several opensource projects, added to the repo as submodule from personal account to be able to switch submodule when fbjni will be opensourced separately. ghstack-source-id: b39c848359a70d717f2830a15265e4aa122279c0 Pull Request resolved: https://github.com/pytorch/pytorch/pull/25084 Pull Request resolved: https://github.com/pytorch/pytorch/pull/25105 Reviewed By: dreiss Differential Revision: D16988107 Pulled By: IvanKobzarev fbshipit-source-id: 41ca7c9869f8370b8504c2ef8a96047cc16516d4
This commit is contained in:
committed by
Facebook Github Bot
parent
3a59a9b36c
commit
d62bca9792
1
android/libs/fbjni
Submodule
1
android/libs/fbjni
Submodule
Submodule android/libs/fbjni added at dc916917e1
Reference in New Issue
Block a user