Class Fp16Conversions


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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static float bf16ToFloat​(short input)
      Converts a bf16 value stored in a short into a float value.
      static java.nio.FloatBuffer convertBf16BufferToFloatBuffer​(java.nio.ShortBuffer buf)
      Casts a buffer of bf16 values stored as shorts into a buffer of floats.
      static java.nio.ShortBuffer convertFloatBufferToBf16Buffer​(java.nio.FloatBuffer buf)
      Rounds a buffer of floats into a buffer containing bf16 values (stored as shorts in Java).
      static java.nio.ShortBuffer convertFloatBufferToFp16Buffer​(java.nio.FloatBuffer buf)
      Rounds a buffer of floats into a buffer containing fp16 values (stored as shorts in Java).
      static java.nio.FloatBuffer convertFp16BufferToFloatBuffer​(java.nio.ShortBuffer buf)
      Casts a buffer of fp16 values stored as shorts into a buffer of floats.
      static short floatToBf16​(float input)
      Converts a float into bf16.
      static short floatToFp16​(float input)
      Converts a float value into a fp16 value stored in a short.
      static float fp16ToFloat​(short input)
      Converts a fp16 value stored in a short into a float value.
      static short mlasFloatToFp16​(float input)
      Rounds a float value to fp16.
      static float mlasFp16ToFloat​(short input)
      Upcasts a fp16 value to a float.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • convertFloatBufferToFp16Buffer

        public static java.nio.ShortBuffer convertFloatBufferToFp16Buffer​(java.nio.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 java.nio.FloatBuffer convertFp16BufferToFloatBuffer​(java.nio.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 java.nio.ShortBuffer convertFloatBufferToBf16Buffer​(java.nio.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 java.nio.FloatBuffer convertBf16BufferToFloatBuffer​(java.nio.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.