tflite_metadata.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import * as path from 'path';
  2. import * as fs from 'fs/promises';
  3. import * as url from 'url';
  4. import * as flatc from './flatc.js';
  5. const main = async () => {
  6. const dirname = path.dirname(url.fileURLToPath(import.meta.url));
  7. const schema = path.join(dirname, '..', 'third_party', 'source', 'tensorflow', 'tensorflow', 'lite', 'schema', 'schema.fbs');
  8. const file = path.join(dirname, '..', 'source', 'tflite-metadata.json');
  9. const input = await fs.readFile(file, 'utf-8');
  10. const json = JSON.parse(input);
  11. const operators = new Map();
  12. const attributes = new Map();
  13. for (const operator of json) {
  14. if (operators.has(operator.name)) {
  15. throw new Error(`Duplicate operator '${operator.name}'.`);
  16. }
  17. operators.set(operator.name, operator);
  18. if (operator && operator.attributes) {
  19. for (const attribute of operator.attributes) {
  20. const name = `${operator.name}:${attribute.name}`;
  21. attributes.set(name, attribute);
  22. }
  23. }
  24. }
  25. const root = new flatc.Root('tflite');
  26. await root.load([], [ schema ]);
  27. const namespace = root.find('tflite', flatc.Namespace);
  28. const builtOperator = namespace.find('tflite.BuiltinOperator', flatc.Type);
  29. const upperCase = new Set([ '2D', 'LSH', 'SVDF', 'RNN', 'L2', 'LSTM' ]);
  30. for (const op of builtOperator.values.keys()) {
  31. let op_key = op === 'BATCH_MATMUL' ? 'BATCH_MAT_MUL' : op;
  32. op_key = op_key.split('_').map((s) => (s.length < 1 || upperCase.has(s)) ? s : s[0] + s.substring(1).toLowerCase()).join('');
  33. const table = namespace.find(`tflite.${op_key}Options`, flatc.Type);
  34. if (table && table.fields.size > 0) {
  35. if (!operators.has(op_key)) {
  36. const operator = { name: op_key };
  37. operators.set(op_key, operator);
  38. json.push(operator);
  39. }
  40. const operator = operators.get(op_key);
  41. operator.attributes = operator.attributes || [];
  42. for (const field of table.fields.values()) {
  43. const attr_key = `${op_key}:${field.name}`;
  44. if (!attributes.has(attr_key)) {
  45. const attribute = { name: field.name };
  46. attributes.set(attr_key, attribute);
  47. operator.attributes.push(attribute);
  48. }
  49. const attribute = attributes.get(attr_key);
  50. const type = field.type;
  51. let defaultValue = field.defaultValue;
  52. if (type instanceof flatc.Enum) {
  53. if (!type.keys.has(defaultValue)) {
  54. throw new Error(`Invalid '${type.name}' default value '${defaultValue}'.`);
  55. }
  56. defaultValue = type.keys.get(defaultValue);
  57. }
  58. attribute.type = type.name === 'bool' ? 'boolean' : type.name + (field.repeated ? '[]' : '');
  59. attribute.default = defaultValue;
  60. }
  61. }
  62. }
  63. json.sort((a, b) => a.name.localeCompare(b.name));
  64. let output = JSON.stringify(json, null, 2);
  65. output = output.replace(/\s {8}/g, ' ');
  66. output = output.replace(/,\s {8}/g, ', ');
  67. output = output.replace(/\s {6}}/g, ' }');
  68. await fs.writeFile(file, output, 'utf-8');
  69. };
  70. main();