mslite_script.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const path = require('path');
  2. const flatc = require('./flatc');
  3. const fs = require('fs');
  4. const schema = path.join(__dirname, '..', 'third_party', 'source', 'mindspore', 'mindspore', 'lite', 'schema', 'ops.fbs');
  5. const file = path.join(__dirname, '..', 'source', 'mslite-metadata.json');
  6. const input = fs.readFileSync(file, 'utf-8');
  7. const json = JSON.parse(input);
  8. const operators = new Map();
  9. const attributes = new Map();
  10. for (const operator of json) {
  11. if (operators.has(operator.name)) {
  12. throw new Error("Duplicate operator '" + operator.name + "'.");
  13. }
  14. operators.set(operator.name, operator);
  15. if (operator && operator.attributes) {
  16. for (const attribute of operator.attributes) {
  17. const name = operator.name + ':' + attribute.name;
  18. attributes.set(name, attribute);
  19. }
  20. }
  21. }
  22. const root = new flatc.Root('mslite', [], [ schema ]);
  23. const namespace = root.find('mindspore.schema', flatc.Namespace);
  24. const primitiveType = namespace.find('mindspore.schema.PrimitiveType', flatc.Type);
  25. for (const table of primitiveType.values.values()) {
  26. const op_key = table.name;
  27. if (!operators.has(op_key)) {
  28. const operator = { name: op_key };
  29. operators.set(op_key, operator);
  30. json.push(operator);
  31. }
  32. const operator = operators.get(op_key);
  33. if (table && table.fields.size > 0) {
  34. operator.attributes = operator.attributes || [];
  35. const inputs = operator.inputs;
  36. const outputs = operator.outputs;
  37. delete operator.inputs;
  38. delete operator.outputs;
  39. if (inputs) {
  40. operator.inputs = inputs;
  41. }
  42. if (outputs) {
  43. operator.outputs = outputs;
  44. }
  45. for (const field of table.fields.values()) {
  46. const attr_key = op_key + ':' + field.name;
  47. if (!attributes.has(attr_key)) {
  48. const attribute = { name: field.name };
  49. attributes.set(attr_key, attribute);
  50. operator.attributes.push(attribute);
  51. }
  52. const attribute = attributes.get(attr_key);
  53. const type = field.type;
  54. let defaultValue = field.defaultValue;
  55. if (type instanceof flatc.Enum) {
  56. if (!type.keys.has(defaultValue)) {
  57. throw new Error("Invalid '" + type.name + "' default value '" + defaultValue + "'.");
  58. }
  59. defaultValue = type.keys.get(defaultValue);
  60. }
  61. attribute.type = type.name === 'bool' ? 'boolean' : type.name + (field.repeated ? '[]' : '');
  62. if (attribute.default === undefined) {
  63. attribute.default = defaultValue;
  64. }
  65. }
  66. }
  67. }
  68. json.sort((a, b) => a.name.localeCompare(b.name));
  69. let output = JSON.stringify(json, null, 2);
  70. output = output.replace(/\s {8}/g, ' ');
  71. output = output.replace(/,\s {8}/g, ', ');
  72. output = output.replace(/\s {6}}/g, ' }');
  73. fs.writeFileSync(file, output, 'utf-8');