Class Fp16Conversions

java.lang.Object
ai.onnxruntime.platform.Fp16Conversions

public final class Fp16Conversions extends Object
Conversions between fp16, bfloat16 and fp32.
  • Method Details

    • convertFloatBufferToFp16Buffer

      public static ShortBuffer convertFloatBufferToFp16Buffer(FloatBuffer buf)
      Rounds a buffer of floats into a buffer containing fp16 values (stored as shorts in Java).

      Respects the position and limit of the input buffer.

      Parameters:
      buf - The buffer of floats.
      Returns:
      A buffer of fp16 values stored as shorts.
    • convertFp16BufferToFloatBuffer

      public static FloatBuffer convertFp16BufferToFloatBuffer(ShortBuffer buf)
      Casts a buffer of fp16 values stored as shorts into a buffer of floats.

      Respects the position and limit of the input buffer.

      Parameters:
      buf - The buffer of fp16 values stored as shorts.
      Returns:
      A buffer of float values.
    • convertFloatBufferToBf16Buffer

      public static ShortBuffer convertFloatBufferToBf16Buffer(FloatBuffer buf)
      Rounds a buffer of floats into a buffer containing bf16 values (stored as shorts in Java).

      Respects the position and limit of the input buffer.

      Parameters:
      buf - The buffer of floats.
      Returns:
      A buffer of bf16 values stored as shorts.
    • convertBf16BufferToFloatBuffer

      public static FloatBuffer convertBf16BufferToFloatBuffer(ShortBuffer buf)
      Casts a buffer of bf16 values stored as shorts into a buffer of floats.

      Respects the position and limit of the input buffer.

      Parameters:
      buf - The buffer of bf16 values stored as shorts.
      Returns:
      A buffer of float values.
    • fp16ToFloat

      public static float fp16ToFloat(short input)
      Converts a fp16 value stored in a short into a float value.

      Note on Java 20 or newer this uses Float.float16ToFloat which may use CPU specific instructions for the conversion, otherwise it uses the conversion operation from ORT's native implementation.

      Parameters:
      input - The fp16 value.
      Returns:
      The float value.
    • floatToFp16

      public static short floatToFp16(float input)
      Converts a float value into a fp16 value stored in a short.

      Note on Java 20 or newer this uses Float.floatToFloat16 which may use CPU specific instructions for the conversion, otherwise it uses the conversion operation from ORT's native implementation.

      Parameters:
      input - The float value.
      Returns:
      The fp16 value.
    • mlasFp16ToFloat

      public static float mlasFp16ToFloat(short input)
      Upcasts a fp16 value to a float. Mirrors the conversion in MLAS.
      Parameters:
      input - A uint16_t representing an IEEE half precision float.
      Returns:
      A float.
    • mlasFloatToFp16

      public static short mlasFloatToFp16(float input)
      Rounds a float value to fp16. Mirrors the conversion in MLAS.
      Parameters:
      input - A float value.
      Returns:
      The value rounded to an IEEE half precision value.
    • bf16ToFloat

      public static float bf16ToFloat(short input)
      Converts a bf16 value stored in a short into a float value.
      Parameters:
      input - A uint16_t representing a bfloat16 value.
      Returns:
      A float.
    • floatToBf16

      public static short floatToBf16(float input)
      Converts a float into bf16. May not produce correct values for subnormal floats.

      Rounds to nearest even.

      Parameters:
      input - The float input.
      Returns:
      A bfloat16 value which is closest to the float.