Class Native

java.lang.Object
smile.torch.Native

public final class Native extends Object
Internal helpers for the FFM (Foreign Function and Memory API) binding to the smile_torch native library. This class glues the idiomatic smile.deep.tensor API to the raw smile_torch_h downcall stubs generated by jextract.

It centralizes three concerns: converting native error sentinels into Java exceptions, freeing native handles when their Java wrappers become unreachable (via a shared Cleaner), and a handful of downcalls for functions that are not yet covered by the generated bindings.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Frees an ST_TensorIndex handle exactly once.
    static final record 
    Frees an ST_Tensor handle exactly once.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Cleaner
    Shared cleaner that releases native handles owned by Java wrappers (tensors, indices, modules, …) once those wrappers are no longer reachable.
  • Method Summary

    Modifier and Type
    Method
    Description
    Verifies that a native call returned a non-null handle, throwing the native error message otherwise.
    static String
    Returns the message describing the most recent native failure, or an empty string if none.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CLEANER

      public static final Cleaner CLEANER
      Shared cleaner that releases native handles owned by Java wrappers (tensors, indices, modules, …) once those wrappers are no longer reachable. It is a safety net; callers should still close resources deterministically via try-with-resources or an AutoScope where possible.
  • Method Details

    • lastError

      public static String lastError()
      Returns the message describing the most recent native failure, or an empty string if none.
      Returns:
      the last native error message.
    • check

      public static MemorySegment check(MemorySegment handle)
      Verifies that a native call returned a non-null handle, throwing the native error message otherwise.
      Parameters:
      handle - the handle returned by a native call.
      Returns:
      the validated handle.