attribute.ts 9.54 KB
Newer Older
gaoqiong's avatar
gaoqiong committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import Long from 'long';
import {onnx} from 'onnx-proto';

import {onnxruntime} from './ort-schema/ort-generated';
import {Tensor} from './tensor';
import {decodeUtf8String, LongUtil} from './util';

import ortFbs = onnxruntime.experimental.fbs;

export declare namespace Attribute {
  export interface DataTypeMap {
    float: number;
    int: number;
    string: string;
    tensor: Tensor;
    floats: number[];
    ints: number[];
    strings: string[];
    tensors: Tensor[];
  }

  export type DataType = keyof DataTypeMap;
}

type ValueTypes = Attribute.DataTypeMap[Attribute.DataType];

type Value = [ValueTypes, Attribute.DataType];

export class Attribute {
  constructor(attributes: onnx.IAttributeProto[]|ortFbs.Attribute[]|null|undefined) {
    this._attributes = new Map();
    if (attributes !== null && attributes !== undefined) {
      for (const attr of attributes) {
        if (attr instanceof onnx.AttributeProto) {
          this._attributes.set(attr.name, [Attribute.getValue(attr), Attribute.getType(attr)]);
        } else if (attr instanceof ortFbs.Attribute) {
          this._attributes.set(attr.name()!, [Attribute.getValue(attr), Attribute.getType(attr)]);
        }
      }
      if (this._attributes.size < attributes.length) {
        throw new Error('duplicated attribute names');
      }
    }
  }

  set(key: string, type: Attribute.DataType, value: ValueTypes): void {
    this._attributes.set(key, [value, type]);
  }
  delete(key: string): void {
    this._attributes.delete(key);
  }
  getFloat(key: string, defaultValue?: Attribute.DataTypeMap['float']) {
    return this.get(key, 'float', defaultValue);
  }

  getInt(key: string, defaultValue?: Attribute.DataTypeMap['int']) {
    return this.get(key, 'int', defaultValue);
  }

  getString(key: string, defaultValue?: Attribute.DataTypeMap['string']) {
    return this.get(key, 'string', defaultValue);
  }

  getTensor(key: string, defaultValue?: Attribute.DataTypeMap['tensor']) {
    return this.get(key, 'tensor', defaultValue);
  }

  getFloats(key: string, defaultValue?: Attribute.DataTypeMap['floats']) {
    return this.get(key, 'floats', defaultValue);
  }

  getInts(key: string, defaultValue?: Attribute.DataTypeMap['ints']) {
    return this.get(key, 'ints', defaultValue);
  }

  getStrings(key: string, defaultValue?: Attribute.DataTypeMap['strings']) {
    return this.get(key, 'strings', defaultValue);
  }

  getTensors(key: string, defaultValue?: Attribute.DataTypeMap['tensors']) {
    return this.get(key, 'tensors', defaultValue);
  }

  private get<V extends Attribute.DataTypeMap[Attribute.DataType]>(
      key: string, type: Attribute.DataType, defaultValue?: V): V {
    const valueAndType = this._attributes.get(key);
    if (valueAndType === undefined) {
      if (defaultValue !== undefined) {
        return defaultValue;
      }
      throw new Error(`required attribute not found: ${key}`);
    }
    if (valueAndType[1] !== type) {
      throw new Error(`type mismatch: expected ${type} but got ${valueAndType[1]}`);
    }
    return valueAndType[0] as V;
  }

  private static getType(attr: onnx.IAttributeProto|ortFbs.Attribute): Attribute.DataType {
    const type = attr instanceof onnx.AttributeProto ? (attr).type : (attr as ortFbs.Attribute).type();
    switch (type) {
      case onnx.AttributeProto.AttributeType.FLOAT:
        return 'float';
      case onnx.AttributeProto.AttributeType.INT:
        return 'int';
      case onnx.AttributeProto.AttributeType.STRING:
        return 'string';
      case onnx.AttributeProto.AttributeType.TENSOR:
        return 'tensor';
      case onnx.AttributeProto.AttributeType.FLOATS:
        return 'floats';
      case onnx.AttributeProto.AttributeType.INTS:
        return 'ints';
      case onnx.AttributeProto.AttributeType.STRINGS:
        return 'strings';
      case onnx.AttributeProto.AttributeType.TENSORS:
        return 'tensors';
      default:
        throw new Error(`attribute type is not supported yet: ${onnx.AttributeProto.AttributeType[type]}`);
    }
  }

  private static getValue(attr: onnx.IAttributeProto|ortFbs.Attribute) {
    const attrType = attr instanceof onnx.AttributeProto ? attr.type : (attr as ortFbs.Attribute).type();
    if (attrType === onnx.AttributeProto.AttributeType.GRAPH || attrType === onnx.AttributeProto.AttributeType.GRAPHS) {
      throw new Error('graph attribute is not supported yet');
    }

    const value = this.getValueNoCheck(attr);

    // cast LONG to number
    if (attrType === onnx.AttributeProto.AttributeType.INT && LongUtil.isLong(value)) {
      return LongUtil.longToNumber(value as Long | flatbuffers.Long);
    }

    // cast LONG[] to number[]
    if (attrType === onnx.AttributeProto.AttributeType.INTS) {
      const arr = (value as Array<number|Long|flatbuffers.Long>);
      const numberValue: number[] = new Array<number>(arr.length);

      for (let i = 0; i < arr.length; i++) {
        const maybeLong = arr[i];
        numberValue[i] = LongUtil.longToNumber(maybeLong);
      }

      return numberValue;
    }

    // cast onnx.TensorProto to onnxjs.Tensor
    if (attrType === onnx.AttributeProto.AttributeType.TENSOR) {
      return attr instanceof onnx.AttributeProto ? Tensor.fromProto(value as onnx.ITensorProto) :
                                                   Tensor.fromOrtTensor(value as ortFbs.Tensor);
    }

    // cast onnx.TensorProto[] to onnxjs.Tensor[]
    if (attrType === onnx.AttributeProto.AttributeType.TENSORS) {
      if (attr instanceof onnx.AttributeProto) {
        const tensorProtos = value as onnx.ITensorProto[];
        return tensorProtos.map(value => Tensor.fromProto(value));
      } else if (attr instanceof ortFbs.Attribute) {
        const tensorProtos = value as ortFbs.Tensor[];
        return tensorProtos.map(value => Tensor.fromOrtTensor(value));
      }
    }

    // cast Uint8Array to string
    if (attrType === onnx.AttributeProto.AttributeType.STRING) {
      // string in onnx attribute is of uint8array type, so we need to convert it to string below. While in ort format,
      // string attributes are returned as string, so no conversion is needed.
      if (attr instanceof onnx.AttributeProto) {
        const utf8String = value as Uint8Array;
        return decodeUtf8String(utf8String);
      }
    }

    // cast Uint8Array[] to string[]
    if (attrType === onnx.AttributeProto.AttributeType.STRINGS) {
      // strings in onnx attribute is returned as uint8array[], so we need to convert it to string[] below. While in ort
      // format strings attributes are returned as string[], so no conversion is needed.
      if (attr instanceof onnx.AttributeProto) {
        const utf8Strings = value as Uint8Array[];
        return utf8Strings.map(decodeUtf8String);
      }
    }

    return value as ValueTypes;
  }

  private static getValueNoCheck(attr: onnx.IAttributeProto|ortFbs.Attribute) {
    return attr instanceof (onnx.AttributeProto) ? this.getValueNoCheckFromOnnxFormat(attr) :
                                                   this.getValueNoCheckFromOrtFormat(attr as ortFbs.Attribute);
  }

  private static getValueNoCheckFromOnnxFormat(attr: onnx.IAttributeProto) {
    switch (attr.type!) {
      case onnx.AttributeProto.AttributeType.FLOAT:
        return attr.f;
      case onnx.AttributeProto.AttributeType.INT:
        return attr.i;
      case onnx.AttributeProto.AttributeType.STRING:
        return attr.s;
      case onnx.AttributeProto.AttributeType.TENSOR:
        return attr.t;
      case onnx.AttributeProto.AttributeType.GRAPH:
        return attr.g;
      case onnx.AttributeProto.AttributeType.FLOATS:
        return attr.floats;
      case onnx.AttributeProto.AttributeType.INTS:
        return attr.ints;
      case onnx.AttributeProto.AttributeType.STRINGS:
        return attr.strings;
      case onnx.AttributeProto.AttributeType.TENSORS:
        return attr.tensors;
      case onnx.AttributeProto.AttributeType.GRAPHS:
        return attr.graphs;
      default:
        throw new Error(`unsupported attribute type: ${onnx.AttributeProto.AttributeType[attr.type!]}`);
    }
  }

  private static getValueNoCheckFromOrtFormat(attr: ortFbs.Attribute) {
    switch (attr.type()) {
      case ortFbs.AttributeType.FLOAT:
        return attr.f();
      case ortFbs.AttributeType.INT:
        return attr.i();
      case ortFbs.AttributeType.STRING:
        return attr.s();
      case ortFbs.AttributeType.TENSOR:
        return attr.t();
      case ortFbs.AttributeType.GRAPH:
        return attr.g();
      case ortFbs.AttributeType.FLOATS:
        return attr.floatsArray();
      case ortFbs.AttributeType.INTS: {
        const ints = [];
        for (let i = 0; i < attr.intsLength(); i++) {
          ints.push(attr.ints(i)!);
        }
        return ints;
      }
      case ortFbs.AttributeType.STRINGS: {
        const strings = [];
        for (let i = 0; i < attr.stringsLength(); i++) {
          strings.push(attr.strings(i));
        }
        return strings;
      }
      case ortFbs.AttributeType.TENSORS: {
        const tensors = [];
        for (let i = 0; i < attr.tensorsLength(); i++) {
          tensors.push(attr.tensors(i)!);
        }
        return tensors;
      }
      // case ortFbs.AttributeType.GRAPHS:
      // TODO: Subgraph not supported yet.
      // const graphs = [];
      // for (let i = 0; i < attr.graphsLength(); i++) {
      //   graphs.push(attr.graphs(i)!);
      // }
      // return graphs;
      default:
        throw new Error(`unsupported attribute type: ${ortFbs.AttributeType[attr.type()]}`);
    }
  }

  protected _attributes: Map<string, Value>;
}