flax.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Experimental
  2. var flax = {};
  3. var python = require('./python');
  4. flax.ModelFactory = class {
  5. match(context) {
  6. const stream = context.stream;
  7. if (stream.length > 4) {
  8. const code = stream.peek(1)[0];
  9. if (code === 0xDE || code === 0xDF || ((code & 0x80) === 0x80)) {
  10. return 'msgpack.map';
  11. }
  12. }
  13. return null;
  14. }
  15. open(context) {
  16. return Promise.resolve().then(() => {
  17. const stream = context.stream;
  18. const packed = stream.peek();
  19. // https://github.com/google/flax/blob/main/flax/serialization.py
  20. const ext_hook = (code, data) => {
  21. switch (code) {
  22. case 1: { // _MsgpackExtType.ndarray
  23. const tuple = execution.invoke('msgpack.unpackb', [ data ]);
  24. const dtype = execution.invoke('numpy.dtype', [ tuple[1] ]);
  25. dtype.byteorder = '<';
  26. return execution.invoke('numpy.ndarray', [ tuple[0], dtype, tuple[2] ]);
  27. }
  28. default: {
  29. throw new flax.Error("Unsupported MessagePack extension '" + code + "'.");
  30. }
  31. }
  32. };
  33. const execution = new python.Execution();
  34. const obj = execution.invoke('msgpack.unpackb', [ packed, ext_hook ]);
  35. return new flax.Model(obj);
  36. });
  37. }
  38. };
  39. flax.Model = class {
  40. constructor(obj) {
  41. this._graphs = [ new flax.Graph(obj) ];
  42. }
  43. get format() {
  44. return 'Flax';
  45. }
  46. get graphs() {
  47. return this._graphs;
  48. }
  49. };
  50. flax.Graph = class {
  51. constructor(obj) {
  52. const layers = new Map();
  53. const layer = (path) => {
  54. const name = path.join('.');
  55. if (!layers.has(name)) {
  56. layers.set(name, {});
  57. }
  58. return layers.get(name);
  59. };
  60. const flatten = (path, obj) => {
  61. for (const entry of Object.entries(obj)) {
  62. const name = entry[0];
  63. const value = entry[1];
  64. if (flax.Utility.isTensor(value)) {
  65. const obj = layer(path);
  66. obj[name] = value;
  67. }
  68. else if (Array.isArray(value)) {
  69. const obj = layer(path);
  70. obj[name] = value;
  71. }
  72. else if (Object(value) === value) {
  73. flatten(path.concat(name), value);
  74. }
  75. else {
  76. const obj = layer(path);
  77. obj[name] = value;
  78. }
  79. }
  80. };
  81. if (Array.isArray(obj)) {
  82. layer([]).value = obj;
  83. }
  84. else {
  85. flatten([], obj);
  86. }
  87. this._nodes = Array.from(layers).map((entry) => new flax.Node(entry[0], entry[1]));
  88. }
  89. get inputs() {
  90. return [];
  91. }
  92. get outputs() {
  93. return [];
  94. }
  95. get nodes() {
  96. return this._nodes;
  97. }
  98. };
  99. flax.Parameter = class {
  100. constructor(name, args) {
  101. this._name = name;
  102. this._arguments = args;
  103. }
  104. get name() {
  105. return this._name;
  106. }
  107. get visible() {
  108. return true;
  109. }
  110. get arguments() {
  111. return this._arguments;
  112. }
  113. };
  114. flax.Argument = class {
  115. constructor(name, initializer) {
  116. if (typeof name !== 'string') {
  117. throw new flax.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  118. }
  119. this._name = name;
  120. this._initializer = initializer || null;
  121. }
  122. get name() {
  123. return this._name;
  124. }
  125. get type() {
  126. return this._initializer.type;
  127. }
  128. get initializer() {
  129. return this._initializer;
  130. }
  131. };
  132. flax.Node = class {
  133. constructor(name, layer) {
  134. this._name = name;
  135. this._type = { name: 'Module' };
  136. this._attributes = [];
  137. this._inputs = [];
  138. for (const entry of Object.entries(layer)) {
  139. const name = entry[0];
  140. const value = entry[1];
  141. if (flax.Utility.isTensor(value)) {
  142. const tensor = new flax.Tensor(value);
  143. const argument = new flax.Argument(this._name + '.' + name, tensor);
  144. const parameter = new flax.Parameter(name, [ argument ]);
  145. this._inputs.push(parameter);
  146. }
  147. else if (Array.isArray(value)) {
  148. const attribute = new flax.Attribute(name, value);
  149. this._attributes.push(attribute);
  150. }
  151. else {
  152. const attribute = new flax.Attribute(name, value);
  153. this._attributes.push(attribute);
  154. }
  155. }
  156. }
  157. get type() {
  158. return this._type;
  159. }
  160. get name() {
  161. return this._name;
  162. }
  163. get inputs() {
  164. return this._inputs;
  165. }
  166. get outputs() {
  167. return [];
  168. }
  169. get attributes() {
  170. return this._attributes;
  171. }
  172. };
  173. flax.Attribute = class {
  174. constructor(name, value) {
  175. this._name = name;
  176. this._value = value;
  177. }
  178. get name() {
  179. return this._name;
  180. }
  181. get value() {
  182. return this._value;
  183. }
  184. };
  185. flax.TensorType = class {
  186. constructor(dataType, shape) {
  187. this._dataType = dataType;
  188. this._shape = shape;
  189. }
  190. get dataType() {
  191. return this._dataType || '?';
  192. }
  193. get shape() {
  194. return this._shape;
  195. }
  196. toString() {
  197. return this.dataType + this._shape.toString();
  198. }
  199. };
  200. flax.TensorShape = class {
  201. constructor(dimensions) {
  202. this._dimensions = dimensions;
  203. }
  204. get dimensions() {
  205. return this._dimensions;
  206. }
  207. toString() {
  208. if (!this._dimensions || this._dimensions.length == 0) {
  209. return '';
  210. }
  211. return '[' + this._dimensions.join(',') + ']';
  212. }
  213. };
  214. flax.Tensor = class {
  215. constructor(array) {
  216. this._type = new flax.TensorType(array.dtype.__name__, new flax.TensorShape(array.shape));
  217. this._data = array.tobytes();
  218. this._byteorder = array.dtype.byteorder;
  219. this._itemsize = array.dtype.itemsize;
  220. }
  221. get type() {
  222. return this._type;
  223. }
  224. get layout() {
  225. switch (this._type.dataType) {
  226. case 'string':
  227. case 'object':
  228. return '|';
  229. default:
  230. return this._byteorder;
  231. }
  232. }
  233. get values() {
  234. switch (this._type.dataType) {
  235. case 'string': {
  236. if (this._data instanceof Uint8Array) {
  237. const data = this._data;
  238. const decoder = new TextDecoder('utf-8');
  239. const size = this._type.shape.dimensions.reduce((a, b) => a * b, 1);
  240. this._data = new Array(size);
  241. let offset = 0;
  242. for (let i = 0; i < size; i++) {
  243. const buffer = data.subarray(offset, offset + this._itemsize);
  244. const index = buffer.indexOf(0);
  245. this._data[i] = decoder.decode(index >= 0 ? buffer.subarray(0, index) : buffer);
  246. offset += this._itemsize;
  247. }
  248. }
  249. return this._data;
  250. }
  251. case 'object': {
  252. return this._data;
  253. }
  254. default:
  255. return this._data;
  256. }
  257. }
  258. };
  259. flax.Utility = class {
  260. static isTensor(obj) {
  261. return obj && obj.__class__ && obj.__class__.__module__ === 'numpy' && obj.__class__.__name__ === 'ndarray';
  262. }
  263. };
  264. flax.Error = class extends Error {
  265. constructor(message) {
  266. super(message);
  267. this.name = 'Error loading Flax model.';
  268. }
  269. };
  270. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  271. module.exports.ModelFactory = flax.ModelFactory;
  272. }