Class ONNXUtils

java.lang.Object
org.tribuo.util.onnx.ONNXUtils

public abstract class ONNXUtils extends Object
Helper functions for building ONNX protos.
  • Method Details

    • buildTensorTypeNode

      public static OnnxMl.TypeProto buildTensorTypeNode(org.tribuo.util.onnx.ONNXShape shape, OnnxMl.TensorProto.DataType type)
      Builds a type proto for the specified shape and tensor type.
      Parameters:
      shape - The shape.
      type - The tensor type.
      Returns:
      The type proto.
    • scalarBuilder

      public static OnnxMl.TensorProto scalarBuilder(ONNXContext context, String name, int value)
      Builds a TensorProto containing the scalar value.
      Parameters:
      context - The naming context.
      name - The base name for the proto.
      value - The value to store.
      Returns:
      A TensorProto containing the value as an int.
    • scalarBuilder

      public static OnnxMl.TensorProto scalarBuilder(ONNXContext context, String name, long value)
      Builds a TensorProto containing the scalar value.
      Parameters:
      context - The naming context.
      name - The base name for the proto.
      value - The value to store.
      Returns:
      A TensorProto containing the value as a long.
    • scalarBuilder

      public static OnnxMl.TensorProto scalarBuilder(ONNXContext context, String name, float value)
      Builds a TensorProto containing the scalar value.
      Parameters:
      context - The naming context.
      name - The base name for the proto.
      value - The value to store.
      Returns:
      A TensorProto containing the value as a float.
    • scalarBuilder

      public static OnnxMl.TensorProto scalarBuilder(ONNXContext context, String name, double value)
      Builds a TensorProto containing the scalar value.
      Parameters:
      context - The naming context.
      name - The base name for the proto.
      value - The value to store.
      Returns:
      A TensorProto containing the value as a double.
    • floatTensorBuilder

      public static OnnxMl.TensorProto floatTensorBuilder(ONNXContext context, String name, List<Integer> dims, Consumer<FloatBuffer> dataPopulator)
      Generic method to create float OnnxMl.TensorProto instances.
      Parameters:
      context - the naming context.
      name - the base name for the proto.
      dims - the dimensions of the input data.
      dataPopulator - a method to populate a FloatBuffer that will be written into the TensorProto's rawData field.
      Returns:
      a float-typed TensorProto representation of the data.
    • doubleTensorBuilder

      public static OnnxMl.TensorProto doubleTensorBuilder(ONNXContext context, String name, List<Integer> dims, Consumer<DoubleBuffer> dataPopulator)
      Generic method to create double OnnxMl.TensorProto instances.

      Note that ONNX fp64 support is poor compared to fp32.

      Parameters:
      context - the naming context.
      name - the base name for the proto.
      dims - the dimensions of the input data.
      dataPopulator - a method to populate a DoubleBuffer that will be written into the TensorProto's rawData field.
      Returns:
      a double-typed TensorProto representation of the data.
    • arrayBuilder

      public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, float[] parameters)
      Builds a TensorProto containing the array.
      Parameters:
      context - The naming context.
      name - The base name for the proto.
      parameters - The array to store in the proto.
      Returns:
      A TensorProto containing the array as floats.
    • arrayBuilder

      public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, double[] parameters)
      Builds a TensorProto containing the array.

      Downcasts the doubles into floats as ONNX's fp64 support is poor compared to fp32.

      Parameters:
      context - The naming context.
      name - The base name for the proto.
      parameters - The array to store in the proto.
      Returns:
      A TensorProto containing the array as floats.
    • arrayBuilder

      public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, double[] parameters, boolean downcast)
      Builds a TensorProto containing the array.

      Optionally downcasts the doubles into floats.

      Parameters:
      context - The naming context.
      name - The base name for the proto.
      parameters - The array to store in the proto.
      downcast - Downcasts the doubles into floats.
      Returns:
      A TensorProto containing the array as either floats or doubles.
    • arrayBuilder

      public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, int[] parameters)
      Builds a TensorProto containing the array.
      Parameters:
      context - The naming context.
      name - The base name for the proto.
      parameters - The array to store in the proto.
      Returns:
      A TensorProto containing the array as ints.
    • arrayBuilder

      public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, long[] parameters)
      Builds a TensorProto containing the array.
      Parameters:
      context - The naming context.
      name - The base name for the proto.
      parameters - The array to store in the proto.
      Returns:
      A TensorProto containing the array as longs.