001/* 002 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.tribuo.util.onnx; 018 019import ai.onnx.proto.OnnxMl; 020import com.google.protobuf.ByteString; 021 022import java.nio.ByteBuffer; 023import java.nio.ByteOrder; 024import java.nio.DoubleBuffer; 025import java.nio.FloatBuffer; 026import java.nio.IntBuffer; 027import java.nio.LongBuffer; 028import java.util.Arrays; 029import java.util.Collections; 030import java.util.List; 031import java.util.function.Consumer; 032import java.util.stream.Collectors; 033 034/** 035 * Helper functions for building ONNX protos. 036 */ 037public abstract class ONNXUtils { 038 039 /** 040 * Private constructor for abstract util class. 041 */ 042 private ONNXUtils() {} 043 044 /** 045 * Builds a type proto for the specified shape and tensor type. 046 * @param shape The shape. 047 * @param type The tensor type. 048 * @return The type proto. 049 */ 050 public static OnnxMl.TypeProto buildTensorTypeNode(ONNXShape shape, OnnxMl.TensorProto.DataType type) { 051 OnnxMl.TypeProto.Builder builder = OnnxMl.TypeProto.newBuilder(); 052 053 OnnxMl.TypeProto.Tensor.Builder tensorBuilder = OnnxMl.TypeProto.Tensor.newBuilder(); 054 tensorBuilder.setElemType(type.getNumber()); 055 tensorBuilder.setShape(shape.getProto()); 056 builder.setTensorType(tensorBuilder.build()); 057 058 return builder.build(); 059 } 060 061 /** 062 * Builds a TensorProto containing the scalar value. 063 * @param context The naming context. 064 * @param name The base name for the proto. 065 * @param value The value to store. 066 * @return A TensorProto containing the value as an int. 067 */ 068 public static OnnxMl.TensorProto scalarBuilder(ONNXContext context, String name, int value) { 069 OnnxMl.TensorProto.Builder scalarBuilder = OnnxMl.TensorProto.newBuilder(); 070 scalarBuilder.setName(context.generateUniqueName(name)); 071 scalarBuilder.setDataType(OnnxMl.TensorProto.DataType.INT32.getNumber()); 072 scalarBuilder.addInt32Data(value); 073 return scalarBuilder.build(); 074 } 075 076 /** 077 * Builds a TensorProto containing the scalar value. 078 * @param context The naming context. 079 * @param name The base name for the proto. 080 * @param value The value to store. 081 * @return A TensorProto containing the value as a long. 082 */ 083 public static OnnxMl.TensorProto scalarBuilder(ONNXContext context, String name, long value) { 084 OnnxMl.TensorProto.Builder scalarBuilder = OnnxMl.TensorProto.newBuilder(); 085 scalarBuilder.setName(context.generateUniqueName(name)); 086 scalarBuilder.setDataType(OnnxMl.TensorProto.DataType.INT64.getNumber()); 087 scalarBuilder.addInt64Data(value); 088 return scalarBuilder.build(); 089 } 090 091 /** 092 * Builds a TensorProto containing the scalar value. 093 * @param context The naming context. 094 * @param name The base name for the proto. 095 * @param value The value to store. 096 * @return A TensorProto containing the value as a float. 097 */ 098 public static OnnxMl.TensorProto scalarBuilder(ONNXContext context, String name, float value) { 099 OnnxMl.TensorProto.Builder scalarBuilder = OnnxMl.TensorProto.newBuilder(); 100 scalarBuilder.setName(context.generateUniqueName(name)); 101 scalarBuilder.setDataType(OnnxMl.TensorProto.DataType.FLOAT.getNumber()); 102 scalarBuilder.addFloatData(value); 103 return scalarBuilder.build(); 104 } 105 106 /** 107 * Builds a TensorProto containing the scalar value. 108 * @param context The naming context. 109 * @param name The base name for the proto. 110 * @param value The value to store. 111 * @return A TensorProto containing the value as a double. 112 */ 113 public static OnnxMl.TensorProto scalarBuilder(ONNXContext context, String name, double value) { 114 OnnxMl.TensorProto.Builder scalarBuilder = OnnxMl.TensorProto.newBuilder(); 115 scalarBuilder.setName(context.generateUniqueName(name)); 116 scalarBuilder.setDataType(OnnxMl.TensorProto.DataType.DOUBLE.getNumber()); 117 scalarBuilder.addDoubleData(value); 118 return scalarBuilder.build(); 119 } 120 121 /** 122 * Generic method to create float {@link OnnxMl.TensorProto} instances. 123 * 124 * @param context the naming context. 125 * @param name the base name for the proto. 126 * @param dims the dimensions of the input data. 127 * @param dataPopulator a method to populate a {@link FloatBuffer} that will be written into the TensorProto's rawData field. 128 * @return a float-typed TensorProto representation of the data. 129 */ 130 public static OnnxMl.TensorProto floatTensorBuilder(ONNXContext context, String name, List<Integer> dims, Consumer<FloatBuffer> dataPopulator) { 131 int size = dims.stream().reduce((a, b) -> a * b).orElse(0); 132 ByteBuffer buffer = ByteBuffer.allocate(size * 4).order(ByteOrder.LITTLE_ENDIAN); 133 FloatBuffer floatBuffer = buffer.asFloatBuffer(); 134 dataPopulator.accept(floatBuffer); 135 floatBuffer.rewind(); 136 return OnnxMl.TensorProto.newBuilder() 137 .setName(context.generateUniqueName(name)) 138 .setDataType(OnnxMl.TensorProto.DataType.FLOAT.getNumber()) 139 .addAllDims(dims.stream().map(Integer::longValue).collect(Collectors.toList())) 140 .setRawData(ByteString.copyFrom(buffer)) 141 .build(); 142 } 143 144 /** 145 * Generic method to create double {@link OnnxMl.TensorProto} instances. 146 * <p> 147 * Note that ONNX fp64 support is poor compared to fp32. 148 * @param context the naming context. 149 * @param name the base name for the proto. 150 * @param dims the dimensions of the input data. 151 * @param dataPopulator a method to populate a {@link DoubleBuffer} that will be written into the TensorProto's rawData field. 152 * @return a double-typed TensorProto representation of the data. 153 */ 154 public static OnnxMl.TensorProto doubleTensorBuilder(ONNXContext context, String name, List<Integer> dims, Consumer<DoubleBuffer> dataPopulator) { 155 int size = dims.stream().reduce((a, b) -> a * b).orElse(0); 156 ByteBuffer buffer = ByteBuffer.allocate(size * 8).order(ByteOrder.LITTLE_ENDIAN); 157 DoubleBuffer doubleBuffer = buffer.asDoubleBuffer(); 158 dataPopulator.accept(doubleBuffer); 159 doubleBuffer.rewind(); 160 return OnnxMl.TensorProto.newBuilder() 161 .setName(context.generateUniqueName(name)) 162 .setDataType(OnnxMl.TensorProto.DataType.DOUBLE.getNumber()) 163 .addAllDims(() -> dims.stream().map(Integer::longValue).iterator()) 164 .setRawData(ByteString.copyFrom(buffer)) 165 .build(); 166 } 167 168 /** 169 * Builds a TensorProto containing the array. 170 * @param context The naming context. 171 * @param name The base name for the proto. 172 * @param parameters The array to store in the proto. 173 * @return A TensorProto containing the array as floats. 174 */ 175 public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, float[] parameters) { 176 return floatTensorBuilder(context, name, Collections.singletonList(parameters.length), 177 (FloatBuffer fb) -> fb.put(parameters)); 178 } 179 180 /** 181 * Builds a TensorProto containing the array. 182 * <p> 183 * Downcasts the doubles into floats as ONNX's fp64 support is poor compared to fp32. 184 * @param context The naming context. 185 * @param name The base name for the proto. 186 * @param parameters The array to store in the proto. 187 * @return A TensorProto containing the array as floats. 188 */ 189 public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, double[] parameters) { 190 return arrayBuilder(context,name,parameters,true); 191 } 192 193 /** 194 * Builds a TensorProto containing the array. 195 * <p> 196 * Optionally downcasts the doubles into floats. 197 * @param context The naming context. 198 * @param name The base name for the proto. 199 * @param parameters The array to store in the proto. 200 * @param downcast Downcasts the doubles into floats. 201 * @return A TensorProto containing the array as either floats or doubles. 202 */ 203 public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, double[] parameters, boolean downcast) { 204 if(downcast) { 205 return floatTensorBuilder(context, name, Collections.singletonList(parameters.length), 206 (FloatBuffer fb) -> Arrays.stream(parameters).forEachOrdered(d -> fb.put((float)d))); 207 } else { 208 return doubleTensorBuilder(context, name, Collections.singletonList(parameters.length), 209 (DoubleBuffer db) -> Arrays.stream(parameters).forEachOrdered(db::put)); 210 } 211 } 212 213 /** 214 * Builds a TensorProto containing the array. 215 * @param context The naming context. 216 * @param name The base name for the proto. 217 * @param parameters The array to store in the proto. 218 * @return A TensorProto containing the array as ints. 219 */ 220 public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, int[] parameters) { 221 OnnxMl.TensorProto.Builder arrBuilder = OnnxMl.TensorProto.newBuilder(); 222 arrBuilder.setName(context.generateUniqueName(name)); 223 arrBuilder.addDims(parameters.length); 224 int capacity = parameters.length * 4; 225 ByteBuffer buffer = ByteBuffer.allocate(capacity).order(ByteOrder.LITTLE_ENDIAN); 226 arrBuilder.setDataType(OnnxMl.TensorProto.DataType.INT32.getNumber()); 227 IntBuffer intBuffer = buffer.asIntBuffer(); 228 intBuffer.put(parameters); 229 intBuffer.rewind(); 230 arrBuilder.setRawData(ByteString.copyFrom(buffer)); 231 return arrBuilder.build(); 232 } 233 234 /** 235 * Builds a TensorProto containing the array. 236 * @param context The naming context. 237 * @param name The base name for the proto. 238 * @param parameters The array to store in the proto. 239 * @return A TensorProto containing the array as longs. 240 */ 241 public static OnnxMl.TensorProto arrayBuilder(ONNXContext context, String name, long[] parameters) { 242 OnnxMl.TensorProto.Builder arrBuilder = OnnxMl.TensorProto.newBuilder(); 243 arrBuilder.setName(context.generateUniqueName(name)); 244 arrBuilder.addDims(parameters.length); 245 int capacity = parameters.length * 8; 246 ByteBuffer buffer = ByteBuffer.allocate(capacity).order(ByteOrder.LITTLE_ENDIAN); 247 arrBuilder.setDataType(OnnxMl.TensorProto.DataType.INT64.getNumber()); 248 LongBuffer longBuffer = buffer.asLongBuffer(); 249 longBuffer.put(parameters); 250 longBuffer.rewind(); 251 arrBuilder.setRawData(ByteString.copyFrom(buffer)); 252 return arrBuilder.build(); 253 } 254}