mslite-script.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import * as flatc from './flatc.js';
  2. import * as fs from 'fs/promises';
  3. import * as path from 'path';
  4. import * as url from 'url';
  5. const main = async () => {
  6. const dirname = path.dirname(url.fileURLToPath(import.meta.url));
  7. const schema = path.join(dirname, '..', 'third_party', 'source', 'mindspore', 'mindspore', 'lite', 'schema', 'ops.fbs');
  8. const file = path.join(dirname, '..', 'source', 'mslite-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('mslite');
  26. await root.load([], [schema]);
  27. const namespace = root.find('mindspore.schema', flatc.Namespace);
  28. const primitiveType = namespace.find('mindspore.schema.PrimitiveType', flatc.Type);
  29. for (const table of primitiveType.values.values()) {
  30. const op_key = table.name;
  31. if (!operators.has(op_key)) {
  32. const operator = { name: op_key };
  33. operators.set(op_key, operator);
  34. json.push(operator);
  35. }
  36. const operator = operators.get(op_key);
  37. if (table && table.fields.size > 0) {
  38. operator.attributes = operator.attributes || [];
  39. const inputs = operator.inputs;
  40. const outputs = operator.outputs;
  41. delete operator.inputs;
  42. delete operator.outputs;
  43. if (inputs) {
  44. operator.inputs = inputs;
  45. }
  46. if (outputs) {
  47. operator.outputs = outputs;
  48. }
  49. for (const field of table.fields.values()) {
  50. const attr_key = `${op_key}:${field.name}`;
  51. if (!attributes.has(attr_key)) {
  52. const attribute = { name: field.name };
  53. attributes.set(attr_key, attribute);
  54. operator.attributes.push(attribute);
  55. }
  56. const attribute = attributes.get(attr_key);
  57. const type = field.type;
  58. let defaultValue = field.defaultValue;
  59. if (type instanceof flatc.Enum) {
  60. if (!type.keys.has(defaultValue)) {
  61. throw new Error(`Invalid '${type.name}' default value '${defaultValue}'.`);
  62. }
  63. defaultValue = type.keys.get(defaultValue);
  64. }
  65. attribute.type = type.name === 'bool' ? 'boolean' : type.name + (field.repeated ? '[]' : '');
  66. if (attribute.default === undefined) {
  67. attribute.default = defaultValue;
  68. }
  69. }
  70. }
  71. }
  72. json.sort((a, b) => a.name.localeCompare(b.name));
  73. let output = JSON.stringify(json, null, 2);
  74. output = output.replace(/\s {8}/g, ' ');
  75. output = output.replace(/,\s {8}/g, ', ');
  76. output = output.replace(/\s {6}}/g, ' }');
  77. await fs.writeFile(file, output, 'utf-8');
  78. };
  79. await main();