onednn.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. var onednn = onednn || {};
  2. var json = json || require('./json');
  3. var base = base || require('./base');
  4. onednn.ModelFactory = class {
  5. match(context) {
  6. const obj = context.open('json');
  7. if (obj && obj.version && obj.engine_kind && obj.fpmath_mode && obj.graph) {
  8. return obj;
  9. }
  10. return null;
  11. }
  12. open(context, match) {
  13. return context.metadata('onednn-metadata.json').then((metadata) => {
  14. if (match) {
  15. return new onednn.Model(metadata, match);
  16. }
  17. throw new onednn.Error("Unsupported oneDNN Graph format '" + match + "'.");
  18. });
  19. }
  20. };
  21. onednn.Model = class {
  22. constructor(metadata, symbol) {
  23. const version = symbol.version;
  24. this._format = 'oneDNN Graph' + (version ? ' v' + version : '');
  25. this._runtime = symbol.engine_kind + ' ' + symbol.fpmath_mode;
  26. this._graphs = [ new onednn.Graph(metadata, symbol) ];
  27. }
  28. get format() {
  29. return this._format;
  30. }
  31. get version() {
  32. return this._version;
  33. }
  34. get runtime() {
  35. return this._runtime;
  36. }
  37. get graphs() {
  38. return this._graphs;
  39. }
  40. };
  41. onednn.Graph = class {
  42. constructor(metadata, symbol) {
  43. this._metadata = metadata;
  44. this._nodes = [];
  45. this._inputs = [];
  46. this._outputs = [];
  47. const initializers = [];
  48. for (const node of symbol.graph) {
  49. if (node.kind == 'Wildcard' && node.inputs.length == 0) {
  50. for (const output of node.outputs) {
  51. initializers.push(output.id);
  52. }
  53. }
  54. }
  55. for (const node of symbol.graph) {
  56. if (!(node.kind == 'Wildcard' && node.inputs.length == 0)) {
  57. this._nodes.push(new onednn.Node(this._metadata, node, symbol.engine_kind, initializers));
  58. }
  59. }
  60. }
  61. get name() {
  62. return '';
  63. }
  64. get type() {
  65. return this._type;
  66. }
  67. get inputs() {
  68. return this._inputs;
  69. }
  70. get outputs() {
  71. return this._outputs;
  72. }
  73. get nodes() {
  74. return this._nodes;
  75. }
  76. };
  77. onednn.Node = class {
  78. constructor(metadata, node, device, initializers) {
  79. this._name = node.name;
  80. this._attributes = [];
  81. this._inputs = [];
  82. this._outputs = [];
  83. this._type = metadata.type(node.kind) || { name: node.kind };
  84. this._device = device;
  85. this._location = node.id;
  86. const attrs = node.attrs;
  87. if (attrs) {
  88. for (const entry of Object.entries(attrs)) {
  89. const name = entry[0];
  90. const value = entry[1];
  91. this._attributes.push(new onednn.Attribute(name, value.type, value.value));
  92. }
  93. }
  94. const inputs = node.inputs || [];
  95. let inputIndex = 0;
  96. for (const input of inputs) {
  97. const shape = !input.shape || (input.shape.length === 1 && input.shape[0] === -1) ? null : new onednn.TensorShape(input.shape);
  98. const type = new onednn.TensorType(input.dtype, shape);
  99. let inputName = (inputs.length == 1) ? 'input' : ('input' + (inputIndex)).toString();
  100. if (this._type && this._type.inputs && this._type.inputs.length > 0) {
  101. inputName = this._type.inputs[inputIndex].name;
  102. }
  103. this._inputs.push(new onednn.Parameter(inputName, [
  104. new onednn.Argument(input.id.toString(), type, initializers.includes(input.id) ? new onednn.Tensor(type, input.property_type) : null)
  105. ]));
  106. inputIndex += 1;
  107. }
  108. const outputs = node.outputs || [];
  109. let outputIndex = 0;
  110. for (const output of outputs) {
  111. const shape = !output.shape || (output.shape.length === 1 && output.shape[0] === -1) ? null : new onednn.TensorShape(output.shape);
  112. const type = new onednn.TensorType(output.dtype, shape);
  113. let outputName = (outputs.length == 1) ? 'output' : ('output' + (outputIndex)).toString();
  114. if (this._type && this._type.outputs && this._type.outputs.length > 0) {
  115. outputName = this._type.outputs[outputIndex].name;
  116. }
  117. this._outputs.push(new onednn.Parameter(outputName, [new onednn.Argument(output.id.toString(), type)]));
  118. outputIndex += 1;
  119. }
  120. }
  121. get type() {
  122. return this._type;
  123. }
  124. get name() {
  125. return this._name;
  126. }
  127. get inputs() {
  128. return this._inputs;
  129. }
  130. get outputs() {
  131. return this._outputs;
  132. }
  133. get attributes() {
  134. return this._attributes;
  135. }
  136. get location() {
  137. return this._location;
  138. }
  139. get device() {
  140. return this._device;
  141. }
  142. };
  143. onednn.Attribute = class {
  144. constructor(name, type, value) {
  145. this._name = name;
  146. this._value = value;
  147. let number;
  148. switch (type) {
  149. case 'bool':
  150. this._type = 'boolean';
  151. switch (value) {
  152. case 1: this._value = true; break;
  153. case 0: this._value = false; break;
  154. default: throw new onednn.Error("Unsupported attribute boolean value '" + value + "'.");
  155. }
  156. break;
  157. case 's64':
  158. this._type = 'int64';
  159. number = Number.parseInt(this._value, 10);
  160. this._value = Number.isNaN(this._value - number) ? value : number;
  161. break;
  162. case 's64[]':
  163. this._type = 'int64[]';
  164. if (this._value.length > 2 && this._value.toString().startsWith('[') && this._value.toString().endsWith(']')) {
  165. let array = [];
  166. const items = this._value.substring(1, this._value.length - 1).split(',')
  167. .map((item) => item.trim())
  168. .map((item) => item.endsWith('L') ? item.substring(0, item.length - 1) : item);
  169. for (const item of items) {
  170. number = Number.parseInt(item, 10);
  171. if (Number.isNaN(item - number)) {
  172. array = null;
  173. }
  174. else if (array != null) {
  175. array.push(number);
  176. }
  177. }
  178. if (array != null) {
  179. this._value = array;
  180. }
  181. }
  182. break;
  183. case 'f32':
  184. this._type = 'float32';
  185. number = Number.parseFloat(this._value);
  186. this._value = Number.isNaN(this._value - number) ? value : number;
  187. break;
  188. case 'f32[]':
  189. this._type = 'float32[]';
  190. if (this._value.length > 2 && this._value.toString().startsWith('[') && this._value.toString().endsWith(']')) {
  191. let array = [];
  192. const items = this._value.substring(1, this._value.length - 1).split(',')
  193. .map((item) => item.trim())
  194. .map((item) => item.endsWith('L') ? item.substring(0, item.length - 1) : item);
  195. for (const item of items) {
  196. number = Number.parseFloat(item);
  197. if (Number.isNaN(item - number)) {
  198. array = null;
  199. }
  200. else if (array != null) {
  201. array.push(number);
  202. }
  203. }
  204. if (array != null) {
  205. this._value = array;
  206. }
  207. }
  208. break;
  209. case 'string':
  210. this._type = 'string';
  211. break;
  212. default: {
  213. throw new onednn.Error("Unsupported attribute array data type '" + type + "'.");
  214. }
  215. }
  216. }
  217. get name() {
  218. return this._name;
  219. }
  220. get type() {
  221. return this._type;
  222. }
  223. get value() {
  224. return this._value;
  225. }
  226. get visible() {
  227. return this._visible == false ? false : true;
  228. }
  229. };
  230. onednn.Parameter = class {
  231. constructor(name, args) {
  232. this._name = name;
  233. this._arguments = args;
  234. }
  235. get name() {
  236. return this._name;
  237. }
  238. get visible() {
  239. return true;
  240. }
  241. get arguments() {
  242. return this._arguments;
  243. }
  244. };
  245. onednn.Argument = class {
  246. constructor(name, type, initializer) {
  247. if (typeof name !== 'string') {
  248. throw new onednn.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  249. }
  250. this._name = name;
  251. this._type = type || null;
  252. this._initializer = initializer || null;
  253. }
  254. get name() {
  255. return this._name;
  256. }
  257. get type() {
  258. return this._type;
  259. }
  260. get initializer() {
  261. return this._initializer;
  262. }
  263. };
  264. onednn.TensorType = class {
  265. constructor(dataType, shape) {
  266. switch (dataType) {
  267. case 'f16': this._dataType = 'float16'; break;
  268. case 'f32': this._dataType = 'float32'; break;
  269. case 's8': this._dataType = 'int8'; break;
  270. case 's32': this._dataType = 'int32'; break;
  271. case 'u8': this._dataType = 'uint8'; break;
  272. case 'bf16': this._dataType = 'bfloat16'; break;
  273. case 'undef': this._dataType = '?'; break;
  274. default: throw new onednn.Error("Unsupported tensor data type '" + dataType.toString() + "'.");
  275. }
  276. this._shape = shape;
  277. }
  278. get dataType() {
  279. return this._dataType;
  280. }
  281. get shape() {
  282. return this._shape;
  283. }
  284. toString() {
  285. return this._dataType + (this._shape ? this._shape.toString() : '[?]');
  286. }
  287. };
  288. onednn.TensorShape = class {
  289. constructor(dimensions) {
  290. this._dimensions = dimensions;
  291. }
  292. get dimensions() {
  293. return this._dimensions;
  294. }
  295. toString() {
  296. return this._dimensions ? ('[' + this._dimensions.map((dimension) => dimension ? dimension.toString() : '?').join(',') + ']') : '';
  297. }
  298. };
  299. onednn.Tensor = class {
  300. constructor(type, property_type) {
  301. this._type = type;
  302. this._category = property_type;
  303. }
  304. get type() {
  305. return this._type;
  306. }
  307. get category() {
  308. return this._category;
  309. }
  310. };
  311. onednn.Error = class extends Error {
  312. constructor(message) {
  313. super(message);
  314. this.name = 'Error loading oneDNN Graph model.';
  315. }
  316. };
  317. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  318. module.exports.ModelFactory = onednn.ModelFactory;
  319. }