1 /* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 package org.pytorch.executorch; 10 11 import com.facebook.jni.HybridData; 12 import com.facebook.jni.annotations.DoNotStrip; 13 import com.facebook.soloader.nativeloader.NativeLoader; 14 import org.pytorch.executorch.annotations.Experimental; 15 16 /** 17 * Interface for the native peer object for entry points to the Module 18 * 19 * <p>Warning: These APIs are experimental and subject to change without notice 20 */ 21 @Experimental 22 class NativePeer { 23 static { 24 // Loads libexecutorch.so from jniLibs 25 NativeLoader.loadLibrary("executorch"); 26 } 27 28 private final HybridData mHybridData; 29 30 @DoNotStrip initHybrid(String moduleAbsolutePath, int loadMode)31 private static native HybridData initHybrid(String moduleAbsolutePath, int loadMode); 32 NativePeer(String moduleAbsolutePath, int loadMode)33 NativePeer(String moduleAbsolutePath, int loadMode) { 34 mHybridData = initHybrid(moduleAbsolutePath, loadMode); 35 } 36 37 /** Clean up the native resources associated with this instance */ resetNative()38 public void resetNative() { 39 mHybridData.resetNative(); 40 } 41 42 /** Run a "forward" call with the given inputs */ 43 @DoNotStrip forward(EValue... inputs)44 public native EValue[] forward(EValue... inputs); 45 46 /** Run an arbitrary method on the module */ 47 @DoNotStrip execute(String methodName, EValue... inputs)48 public native EValue[] execute(String methodName, EValue... inputs); 49 50 /** 51 * Load a method on this module. 52 * 53 * @return the Error code if there was an error loading the method 54 */ 55 @DoNotStrip loadMethod(String methodName)56 public native int loadMethod(String methodName); 57 58 /** Retrieve the in-memory log buffer, containing the most recent ExecuTorch log entries. */ 59 @DoNotStrip readLogBuffer()60 public native String[] readLogBuffer(); 61 } 62