|
10 | 10 |
|
11 | 11 | import com.facebook.soloader.nativeloader.NativeLoader; |
12 | 12 | import com.facebook.soloader.nativeloader.SystemDelegate; |
| 13 | + |
13 | 14 | import org.pytorch.executorch.annotations.Experimental; |
14 | 15 |
|
15 | 16 | /** |
|
20 | 21 | @Experimental |
21 | 22 | public class Module { |
22 | 23 |
|
23 | | - /** Load mode for the module. Load the whole file as a buffer. */ |
24 | | - public static final int LOAD_MODE_FILE = 0; |
| 24 | + /** |
| 25 | + * Load mode for the module. Load the whole file as a buffer. |
| 26 | + */ |
| 27 | + public static final int LOAD_MODE_FILE = 0; |
| 28 | + |
| 29 | + /** |
| 30 | + * Load mode for the module. Use mmap to load pages into memory. |
| 31 | + */ |
| 32 | + public static final int LOAD_MODE_MMAP = 1; |
| 33 | + |
| 34 | + /** |
| 35 | + * Load mode for the module. Use memory locking and handle errors. |
| 36 | + */ |
| 37 | + public static final int LOAD_MODE_MMAP_USE_MLOCK = 2; |
| 38 | + |
| 39 | + /** |
| 40 | + * Load mode for the module. Use memory locking and ignore errors. |
| 41 | + */ |
| 42 | + public static final int LOAD_MODE_MMAP_USE_MLOCK_IGNORE_ERRORS = 3; |
25 | 43 |
|
26 | | - /** Load mode for the module. Use mmap to load pages into memory. */ |
27 | | - public static final int LOAD_MODE_MMAP = 1; |
| 44 | + /** |
| 45 | + * Reference to the NativePeer object of this module. |
| 46 | + */ |
| 47 | + private NativePeer mNativePeer; |
28 | 48 |
|
29 | | - /** Load mode for the module. Use memory locking and handle errors. */ |
30 | | - public static final int LOAD_MODE_MMAP_USE_MLOCK = 2; |
| 49 | + /** |
| 50 | + * Loads a serialized ExecuTorch module from the specified path on the disk. |
| 51 | + * |
| 52 | + * @param modelPath path to file that contains the serialized ExecuTorch module. |
| 53 | + * @param loadMode load mode for the module. See constants in {@link Module}. |
| 54 | + * @return new {@link org.pytorch.executorch.Module} object which owns the model module. |
| 55 | + */ |
| 56 | + public static Module load(final String modelPath, int loadMode) { |
| 57 | + if (!NativeLoader.isInitialized()) { |
| 58 | + NativeLoader.init(new SystemDelegate()); |
| 59 | + } |
| 60 | + return new Module(new NativePeer(modelPath, loadMode)); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Loads a serialized ExecuTorch module from the specified path on the disk to run on CPU. |
| 65 | + * |
| 66 | + * @param modelPath path to file that contains the serialized ExecuTorch module. |
| 67 | + * @return new {@link org.pytorch.executorch.Module} object which owns the model module. |
| 68 | + */ |
| 69 | + public static Module load(final String modelPath) { |
| 70 | + return load(modelPath, LOAD_MODE_FILE); |
| 71 | + } |
31 | 72 |
|
32 | | - /** Load mode for the module. Use memory locking and ignore errors. */ |
33 | | - public static final int LOAD_MODE_MMAP_USE_MLOCK_IGNORE_ERRORS = 3; |
| 73 | + Module(NativePeer nativePeer) { |
| 74 | + this.mNativePeer = nativePeer; |
| 75 | + } |
34 | 76 |
|
35 | | - /** Reference to the NativePeer object of this module. */ |
36 | | - private NativePeer mNativePeer; |
| 77 | + /** |
| 78 | + * Runs the 'forward' method of this module with the specified arguments. |
| 79 | + * |
| 80 | + * @param inputs arguments for the ExecuTorch module's 'forward' method. Note: if method 'forward' |
| 81 | + * requires inputs but no inputs are given, the function will not error out, but run 'forward' |
| 82 | + * with sample inputs. |
| 83 | + * @return return value from the 'forward' method. |
| 84 | + */ |
| 85 | + public EValue[] forward(EValue... inputs) { |
| 86 | + return mNativePeer.forward(inputs); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Runs the specified method of this module with the specified arguments. |
| 91 | + * |
| 92 | + * @param methodName name of the ExecuTorch method to run. |
| 93 | + * @param inputs arguments that will be passed to ExecuTorch method. |
| 94 | + * @return return value from the method. |
| 95 | + */ |
| 96 | + public EValue[] execute(String methodName, EValue... inputs) { |
| 97 | + return mNativePeer.execute(methodName, inputs); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Load a method on this module. This might help with the first time inference performance, |
| 102 | + * because otherwise the method is loaded lazily when it's execute. Note: this function is |
| 103 | + * synchronous, and will block until the method is loaded. Therefore, it is recommended to call |
| 104 | + * this on a background thread. However, users need to make sure that they don't execute before |
| 105 | + * this function returns. |
| 106 | + * |
| 107 | + * @return the Error code if there was an error loading the method |
| 108 | + */ |
| 109 | + public int loadMethod(String methodName) { |
| 110 | + return mNativePeer.loadMethod(methodName); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Retrieve the in-memory log buffer, containing the most recent ExecuTorch log entries. |
| 115 | + */ |
| 116 | + public String[] readLogBuffer() { |
| 117 | + return mNativePeer.readLogBuffer(); |
| 118 | + } |
37 | 119 |
|
38 | | - /** |
39 | | - * Loads a serialized ExecuTorch module from the specified path on the disk. |
40 | | - * |
41 | | - * @param modelPath path to file that contains the serialized ExecuTorch module. |
42 | | - * @param loadMode load mode for the module. See constants in {@link Module}. |
43 | | - * @return new {@link org.pytorch.executorch.Module} object which owns the model module. |
44 | | - */ |
45 | | - public static Module load(final String modelPath, int loadMode) { |
46 | | - if (!NativeLoader.isInitialized()) { |
47 | | - NativeLoader.init(new SystemDelegate()); |
| 120 | + /** |
| 121 | + * Explicitly destroys the native torch::jit::Module. Calling this method is not required, as the |
| 122 | + * native object will be destroyed when this object is garbage-collected. However, the timing of |
| 123 | + * garbage collection is not guaranteed, so proactively calling {@code destroy} can free memory |
| 124 | + * more quickly. See {@link com.facebook.jni.HybridData#resetNative}. |
| 125 | + */ |
| 126 | + public void destroy() { |
| 127 | + mNativePeer.resetNative(); |
48 | 128 | } |
49 | | - return new Module(new NativePeer(modelPath, loadMode)); |
50 | | - } |
51 | | - |
52 | | - /** |
53 | | - * Loads a serialized ExecuTorch module from the specified path on the disk to run on CPU. |
54 | | - * |
55 | | - * @param modelPath path to file that contains the serialized ExecuTorch module. |
56 | | - * @return new {@link org.pytorch.executorch.Module} object which owns the model module. |
57 | | - */ |
58 | | - public static Module load(final String modelPath) { |
59 | | - return load(modelPath, LOAD_MODE_FILE); |
60 | | - } |
61 | | - |
62 | | - Module(NativePeer nativePeer) { |
63 | | - this.mNativePeer = nativePeer; |
64 | | - } |
65 | | - |
66 | | - /** |
67 | | - * Runs the 'forward' method of this module with the specified arguments. |
68 | | - * |
69 | | - * @param inputs arguments for the ExecuTorch module's 'forward' method. Note: if method 'forward' |
70 | | - * requires inputs but no inputs are given, the function will not error out, but run 'forward' |
71 | | - * with sample inputs. |
72 | | - * @return return value from the 'forward' method. |
73 | | - */ |
74 | | - public EValue[] forward(EValue... inputs) { |
75 | | - return mNativePeer.forward(inputs); |
76 | | - } |
77 | | - |
78 | | - /** |
79 | | - * Runs the specified method of this module with the specified arguments. |
80 | | - * |
81 | | - * @param methodName name of the ExecuTorch method to run. |
82 | | - * @param inputs arguments that will be passed to ExecuTorch method. |
83 | | - * @return return value from the method. |
84 | | - */ |
85 | | - public EValue[] execute(String methodName, EValue... inputs) { |
86 | | - return mNativePeer.execute(methodName, inputs); |
87 | | - } |
88 | | - |
89 | | - /** |
90 | | - * Load a method on this module. This might help with the first time inference performance, |
91 | | - * because otherwise the method is loaded lazily when it's execute. Note: this function is |
92 | | - * synchronous, and will block until the method is loaded. Therefore, it is recommended to call |
93 | | - * this on a background thread. However, users need to make sure that they don't execute before |
94 | | - * this function returns. |
95 | | - * |
96 | | - * @return the Error code if there was an error loading the method |
97 | | - */ |
98 | | - public int loadMethod(String methodName) { |
99 | | - return mNativePeer.loadMethod(methodName); |
100 | | - } |
101 | | - |
102 | | - /** |
103 | | - * Explicitly destroys the native torch::jit::Module. Calling this method is not required, as the |
104 | | - * native object will be destroyed when this object is garbage-collected. However, the timing of |
105 | | - * garbage collection is not guaranteed, so proactively calling {@code destroy} can free memory |
106 | | - * more quickly. See {@link com.facebook.jni.HybridData#resetNative}. |
107 | | - */ |
108 | | - public void destroy() { |
109 | | - mNativePeer.resetNative(); |
110 | | - } |
111 | 129 | } |
0 commit comments