Class NativeWasiNnBindings

java.lang.Object
ai.tegmentum.wasmtime4j.panama.wasi.nn.NativeWasiNnBindings

public final class NativeWasiNnBindings extends Object
Panama FFI bindings for WASI-NN native functions.

This class provides method handles for calling the C-exported WASI-NN functions from the native library. It uses the singleton pattern for efficient handle caching.

Since:
1.0.0
  • Method Details

    • getInstance

      public static NativeWasiNnBindings getInstance()
      Gets the singleton instance.
      Returns:
      the bindings instance
    • nnContextCreate

      public MemorySegment nnContextCreate() throws ai.tegmentum.wasmtime4j.wasi.nn.NnException
      Creates a new NnContext.
      Returns:
      the context pointer
      Throws:
      ai.tegmentum.wasmtime4j.wasi.nn.NnException - if creation fails
    • nnContextIsAvailable

      public int nnContextIsAvailable(MemorySegment ctxPtr)
      Checks if any backends are available.
      Parameters:
      ctxPtr - the context pointer
      Returns:
      1 if available, 0 otherwise
    • nnContextSupportedEncodings

      public int[] nnContextSupportedEncodings(Arena arena, MemorySegment ctxPtr)
      Gets supported encoding codes.
      Parameters:
      arena - the arena for allocations
      ctxPtr - the context pointer
      Returns:
      array of encoding codes
    • nnContextLoadGraph

      public MemorySegment nnContextLoadGraph(Arena arena, MemorySegment ctxPtr, byte[][] parts, int encodingCode, int targetCode) throws ai.tegmentum.wasmtime4j.wasi.nn.NnException
      Loads a graph.
      Parameters:
      arena - the arena for allocations
      ctxPtr - the context pointer
      parts - the model data parts
      encodingCode - the encoding code
      targetCode - the target code
      Returns:
      the graph pointer
      Throws:
      ai.tegmentum.wasmtime4j.wasi.nn.NnException - if loading fails
    • nnContextGetBackendInfo

      public String nnContextGetBackendInfo(MemorySegment ctxPtr)
      Gets backend info as JSON string.
      Parameters:
      ctxPtr - the context pointer
      Returns:
      the JSON info string, or null
    • nnContextClose

      public void nnContextClose(MemorySegment ctxPtr)
      Closes an NnContext.
      Parameters:
      ctxPtr - the context pointer
    • nnGraphCreateExecCtx

      public MemorySegment nnGraphCreateExecCtx(Arena arena, MemorySegment graphPtr) throws ai.tegmentum.wasmtime4j.wasi.nn.NnException
      Creates an execution context from a graph.
      Parameters:
      arena - the arena for allocations
      graphPtr - the graph pointer
      Returns:
      the execution context pointer
      Throws:
      ai.tegmentum.wasmtime4j.wasi.nn.NnException - if creation fails
    • nnGraphClose

      public void nnGraphClose(MemorySegment graphPtr)
      Closes an NnGraph.
      Parameters:
      graphPtr - the graph pointer
    • nnExecSetInputByIndex

      public void nnExecSetInputByIndex(Arena arena, MemorySegment execPtr, int index, int[] dims, int tensorType, byte[] data) throws ai.tegmentum.wasmtime4j.wasi.nn.NnException
      Sets an input tensor by index.
      Parameters:
      arena - the arena for allocations
      execPtr - the execution context pointer
      index - the input index
      dims - the tensor dimensions
      tensorType - the tensor type code
      data - the tensor data
      Throws:
      ai.tegmentum.wasmtime4j.wasi.nn.NnException - if setting fails
    • nnExecSetInputByName

      public void nnExecSetInputByName(Arena arena, MemorySegment execPtr, String name, int[] dims, int tensorType, byte[] data) throws ai.tegmentum.wasmtime4j.wasi.nn.NnException
      Sets an input tensor by name.
      Parameters:
      arena - the arena for allocations
      execPtr - the execution context pointer
      name - the input name
      dims - the tensor dimensions
      tensorType - the tensor type code
      data - the tensor data
      Throws:
      ai.tegmentum.wasmtime4j.wasi.nn.NnException - if setting fails
    • nnExecCompute

      public void nnExecCompute(Arena arena, MemorySegment execPtr) throws ai.tegmentum.wasmtime4j.wasi.nn.NnException
      Runs inference.
      Parameters:
      arena - the arena for allocations
      execPtr - the execution context pointer
      Throws:
      ai.tegmentum.wasmtime4j.wasi.nn.NnException - if inference fails
    • nnExecGetOutputByIndex

      public byte[] nnExecGetOutputByIndex(Arena arena, MemorySegment execPtr, int index) throws ai.tegmentum.wasmtime4j.wasi.nn.NnException
      Gets an output tensor by index as serialized bytes.
      Parameters:
      arena - the arena for allocations
      execPtr - the execution context pointer
      index - the output index
      Returns:
      the serialized tensor bytes, or null if not available
      Throws:
      ai.tegmentum.wasmtime4j.wasi.nn.NnException - if getting fails
    • nnExecGetOutputByName

      public byte[] nnExecGetOutputByName(Arena arena, MemorySegment execPtr, String name) throws ai.tegmentum.wasmtime4j.wasi.nn.NnException
      Gets an output tensor by name as serialized bytes.
      Parameters:
      arena - the arena for allocations
      execPtr - the execution context pointer
      name - the output name
      Returns:
      the serialized tensor bytes, or null if not available
      Throws:
      ai.tegmentum.wasmtime4j.wasi.nn.NnException - if getting fails
    • nnExecClose

      public void nnExecClose(MemorySegment execPtr)
      Closes an execution context.
      Parameters:
      execPtr - the execution context pointer