torch.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /* jshint esversion: 6 */
  2. var torch = torch || {};
  3. torch.ModelFactory = class {
  4. match(context) {
  5. const extension = context.identifier.split('.').pop().toLowerCase();
  6. if (extension == 't7') {
  7. const stream = context.stream;
  8. if (stream.length >= 1 && stream.peek(1)[0] <= 58) {
  9. return true;
  10. }
  11. }
  12. return false;
  13. }
  14. open(context) {
  15. return torch.Metadata.open(context).then((metadata) => {
  16. const identifier = context.identifier;
  17. const buffer = context.stream.peek();
  18. const reader = new torch.T7Reader(buffer, (name) => {
  19. if (name && name != 'nn.JointTrainModule' && !name.startsWith('nn.MSDNet_') && !name.startsWith('onmt.')) {
  20. context.exception(new torch.Error("Unknown type '" + name + "' in '" + identifier + "'."), false);
  21. }
  22. return null;
  23. });
  24. let root = reader.read();
  25. if (root && Array.isArray(root) && root.length == 2 && root[0].__type__ && !root[1].__type__) {
  26. root = root[0];
  27. }
  28. return new torch.Model(metadata, root);
  29. });
  30. }
  31. };
  32. torch.Model = class {
  33. constructor(metadata, root) {
  34. this._graphs = [];
  35. this._graphs.push(new torch.Graph(metadata, root));
  36. }
  37. get graphs() {
  38. return this._graphs;
  39. }
  40. get format() {
  41. return 'Torch v7';
  42. }
  43. };
  44. torch.Graph = class {
  45. constructor(metadata, root) {
  46. this._inputs = [];
  47. this._outputs = [];
  48. this._nodes = [];
  49. this._groups = 'false';
  50. if (Object.prototype.hasOwnProperty.call(root, 'model')) {
  51. root = root.model;
  52. }
  53. const inputs = [];
  54. const outputs = [];
  55. this._loadModule(metadata, root, [], '', inputs, outputs);
  56. this._inputs = this._inputs.concat(inputs.map((input, index) => {
  57. return new torch.Parameter('input' + (index != 0 ? (index + 1).toString() : ''), true, [ input ]);
  58. }));
  59. this._outputs = this._outputs.concat(outputs.map((output, index) => {
  60. return new torch.Parameter('output' + (index != 0 ? (index + 1).toString() : ''), true, [ output ]);
  61. }));
  62. }
  63. get inputs() {
  64. return this._inputs;
  65. }
  66. get outputs() {
  67. return this._outputs;
  68. }
  69. get nodes() {
  70. return this._nodes;
  71. }
  72. get groups() {
  73. return this._groups;
  74. }
  75. _loadModule(metadata, module, groups, key, inputs, outputs) {
  76. if (groups.length > 0) {
  77. this._groups = true;
  78. }
  79. switch (module.__type__) {
  80. case 'nn.Sequential': {
  81. groups.push(key);
  82. let subInputs = inputs;
  83. let subOutputs = [];
  84. const length = module.modules.length;
  85. let index = 0;
  86. for (const subModule of module.modules) {
  87. if (index == length - 1) {
  88. subOutputs = outputs;
  89. }
  90. this._loadModule(metadata, subModule, groups, index.toString(), subInputs, subOutputs);
  91. subInputs = subOutputs;
  92. subOutputs = [];
  93. index++;
  94. }
  95. groups.pop();
  96. break;
  97. }
  98. case 'nn.Parallel':
  99. case 'nn.ParallelTable':
  100. case 'nn.JointTrain': {
  101. groups.push(key);
  102. let newInputs = [];
  103. let newOutputs = [];
  104. let index = 0;
  105. for (const subModule of module.modules) {
  106. const subInputs = [].concat(inputs);
  107. const subOutputs = [].concat(outputs);
  108. this._loadModule(metadata, subModule, groups, index.toString(), subInputs, subOutputs);
  109. if (inputs.length == 0) {
  110. newInputs = newInputs.concat(subInputs);
  111. }
  112. if (outputs.length == 0) {
  113. newOutputs = newOutputs.concat(subOutputs);
  114. }
  115. index++;
  116. }
  117. inputs = inputs.concat(newInputs);
  118. for (const newOutput of newOutputs) {
  119. outputs.push(newOutput);
  120. }
  121. groups.pop();
  122. break;
  123. }
  124. case 'nn.Concat':
  125. case 'nn.ConcatTable': {
  126. const prefix = key;
  127. if (inputs.length == 0) {
  128. inputs.push(new torch.Argument(groups.join('/') + ':' + key + ':in', null, null));
  129. }
  130. let concatInputs = [];
  131. let index = 0;
  132. for (const subModule of module.modules) {
  133. const streamInputs = inputs.map((input) => input);
  134. const streamOutputs = [];
  135. this._loadModule(metadata, subModule, groups, prefix + '.' + index.toString(), streamInputs, streamOutputs);
  136. concatInputs = concatInputs.concat(streamOutputs);
  137. index++;
  138. }
  139. delete module.modules;
  140. delete module.dimension;
  141. this._createNode(metadata, module, groups, key, concatInputs, outputs);
  142. break;
  143. }
  144. case 'nn.Inception': {
  145. delete module.modules; // TODO
  146. delete module.module; // TODO
  147. delete module.transfer; // TODO
  148. delete module.pool; // TODO
  149. this._createNode(metadata, module, groups, key, inputs, outputs);
  150. break;
  151. }
  152. default: {
  153. this._createNode(metadata, module, groups, key, inputs, outputs);
  154. break;
  155. }
  156. }
  157. }
  158. _createNode(metadata, module, group, subIndex, inputs, outputs) {
  159. this._nodes.push(new torch.Node(metadata, module, group, subIndex, inputs, outputs));
  160. }
  161. };
  162. torch.Parameter = class {
  163. constructor(name, visible, args) {
  164. this._name = name;
  165. this._visible = visible;
  166. this._arguments = args;
  167. }
  168. get name() {
  169. return this._name;
  170. }
  171. get visible() {
  172. return this._visible;
  173. }
  174. get arguments() {
  175. return this._arguments;
  176. }
  177. };
  178. torch.Argument = class {
  179. constructor(name, type, initializer) {
  180. if (typeof name !== 'string') {
  181. throw new torch.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  182. }
  183. this._name = name;
  184. this._type = type;
  185. this._initializer = initializer;
  186. }
  187. get name() {
  188. return this._name;
  189. }
  190. get type() {
  191. if (this._initializer) {
  192. return this._initializer.type;
  193. }
  194. return this._type;
  195. }
  196. get initializer() {
  197. return this._initializer;
  198. }
  199. };
  200. torch.Node = class {
  201. constructor(metadata, module, groups, name, inputs, outputs) {
  202. this._group = groups.join('/');
  203. if (module.name && typeof module.name === 'string') {
  204. this._name = module.name;
  205. delete module.name;
  206. }
  207. else {
  208. this._name = this._group ? (this._group + ':' + name) : name;
  209. }
  210. const type = module.__type__ || 'nn.Module';
  211. this._type = metadata.type(type);
  212. let initializers = [];
  213. for (const key of Object.keys(module)) {
  214. const obj = module[key];
  215. if (obj && obj.__type__ && obj.__type__.startsWith('torch.') && obj.__type__.endsWith('Storage')) {
  216. module[key] = obj.data();
  217. }
  218. }
  219. delete module.iSize;
  220. delete module.finput;
  221. delete module.fgradInput;
  222. delete module.output;
  223. delete module.gradInput;
  224. delete module.gradWeight;
  225. delete module.gradBias;
  226. delete module.grad_tmp;
  227. delete module.scaleT;
  228. delete module._input;
  229. delete module._output;
  230. delete module._gradInput;
  231. delete module._gradOutput;
  232. delete module.buffer;
  233. delete module.buffer2;
  234. delete module.tmp_in;
  235. delete module.tmp_out;
  236. delete module.accUpdateGradParameters;
  237. switch (this._type.name) {
  238. case 'nn.Linear':
  239. delete module.addBuffer;
  240. break;
  241. case 'nn.Normalize':
  242. case 'nn.Normalize2':
  243. delete module.addBuffer;
  244. delete module.normp;
  245. delete module.norm;
  246. break;
  247. case 'cudnn.SpatialConvolution':
  248. case 'cudnn.SpatialFullConvolution':
  249. case 'nn.SpatialConvolution':
  250. case 'nn.SpatialConvolutionMM':
  251. case 'nn.SpatialDilatedConvolution':
  252. case 'nn.SpatialFullConvolution':
  253. delete module.ones;
  254. delete module.input_slice;
  255. delete module.output_slice;
  256. delete module.convDescData;
  257. this._updateSize(module, 'adj');
  258. this._updateSize(module, 'd');
  259. this._updateSize(module, 'dilation');
  260. this._updateSize(module, 'k');
  261. this._updateSize(module, 'pad');
  262. break;
  263. case 'cudnn.BatchNormalization':
  264. case 'cudnn.SpatialBatchNormalization':
  265. case 'nn.BatchNormalization':
  266. case 'nn.SpatialBatchNormalization':
  267. case 'nn.InstanceNormalization':
  268. delete module.save_mean;
  269. delete module.save_std;
  270. delete module.gradWeight;
  271. delete module.normalized;
  272. delete module.centered;
  273. delete module.bn; // TODO InstanceNormalization
  274. break;
  275. case 'nn.SpatialCrossMapLRN':
  276. delete module.scale;
  277. break;
  278. case 'cudnn.SpatialMaxPooling':
  279. case 'cudnn.SpatialAveragePooling':
  280. case 'inn.SpatialMaxPooling':
  281. case 'nn.SpatialMaxPooling':
  282. case 'nn.SpatialAveragePooling':
  283. delete module.indices;
  284. this._updateSize(module, 'pad');
  285. this._updateSize(module, 'd');
  286. this._updateSize(module, 'k');
  287. break;
  288. case 'nn.SpatialZeroPadding':
  289. case 'nn.SpatialReflectionPadding':
  290. case 'nn.SpatialReplicationPadding':
  291. this._updateBox(module, 'pad');
  292. break;
  293. case 'nn.Dropout':
  294. delete module.noise;
  295. break;
  296. case 'nn.gModule':
  297. delete module.forwardnodes;
  298. delete module.backwardnodes;
  299. break;
  300. case 'nn.StereoJoin':
  301. delete module.output_L;
  302. break;
  303. }
  304. this._attributes = [];
  305. if (module.__type__) {
  306. for (const key of Object.keys(module)) {
  307. if (key == '__type__' || key == '_type') {
  308. continue;
  309. }
  310. const obj = module[key];
  311. if (Array.isArray(obj) && obj.every(((item) => item && item.__type__ && item.__type__.startsWith('nn.')))) {
  312. continue;
  313. }
  314. if (obj.__type__ && obj.__type__.startsWith('torch.') && obj.__type__.endsWith('Tensor')) {
  315. initializers.push(new torch.Parameter(key, true, [
  316. new torch.Argument(key, null, new torch.Tensor(obj))
  317. ]));
  318. continue;
  319. }
  320. if (key == 'modules' || (obj.__type__ && obj.__type__ != 'Function')) {
  321. continue;
  322. }
  323. this._attributes.push(new torch.Attribute(metadata, type, key, obj));
  324. }
  325. }
  326. this._inputs = [];
  327. if (inputs.length == 0 && this._name) {
  328. inputs.push(new torch.Argument(this._name + ':in', null, null));
  329. }
  330. this._inputs.push(new torch.Parameter('input', true, inputs));
  331. if (outputs.length == 0 && this._name) {
  332. outputs.push(new torch.Argument(this._name, null, null));
  333. }
  334. this._outputs = [];
  335. this._outputs.push(new torch.Parameter('output', true, outputs));
  336. initializers = initializers.filter((argument) => {
  337. if (argument.name == 'weight') {
  338. this._inputs.push(argument);
  339. return false;
  340. }
  341. return true;
  342. });
  343. initializers = initializers.filter((argument) => {
  344. if (argument.name == 'bias') {
  345. this._inputs.push(argument);
  346. return false;
  347. }
  348. return true;
  349. });
  350. this._inputs = this._inputs.concat(initializers);
  351. }
  352. get name() {
  353. return this._name;
  354. }
  355. get type() {
  356. return this._type;
  357. }
  358. get group() {
  359. return this._group;
  360. }
  361. get attributes() {
  362. return this._attributes;
  363. }
  364. get inputs() {
  365. return this._inputs;
  366. }
  367. get outputs() {
  368. return this._outputs;
  369. }
  370. _updateSize(module, name) {
  371. if (Object.prototype.hasOwnProperty.call(module, name + 'W') &&
  372. Object.prototype.hasOwnProperty.call(module, name + 'H')) {
  373. module[name] = [ module[name + 'W'], module[name + 'H'] ];
  374. delete module[name + 'W'];
  375. delete module[name + 'H'];
  376. }
  377. }
  378. _updateBox(module, name) {
  379. if (Object.prototype.hasOwnProperty.call(module, name + '_t') &&
  380. Object.prototype.hasOwnProperty.call(module, name + '_r') &&
  381. Object.prototype.hasOwnProperty.call(module, name + '_b') &&
  382. Object.prototype.hasOwnProperty.call(module, name + '_l')) {
  383. module[name] = [ module[name + '_t'], module[name + '_r'], module[name + '_b'], module[name + '_l'] ];
  384. delete module[name + '_t'];
  385. delete module[name + '_r'];
  386. delete module[name + '_b'];
  387. delete module[name + '_l'];
  388. }
  389. }
  390. };
  391. torch.Attribute = class {
  392. constructor(metadata, type, name, value) {
  393. this._name = name;
  394. this._value = value;
  395. if (name == 'train') {
  396. this._visible = false;
  397. }
  398. const schema = metadata.attribute(type, name);
  399. if (schema) {
  400. if (Object.prototype.hasOwnProperty.call(schema, 'visible')) {
  401. this._visible = schema.visible;
  402. }
  403. else if (Object.prototype.hasOwnProperty.call(schema, 'default')) {
  404. if (JSON.stringify(schema.default) == JSON.stringify(this._value)) {
  405. this._visible = false;
  406. }
  407. }
  408. }
  409. }
  410. get name() {
  411. return this._name;
  412. }
  413. get value() {
  414. return this._value;
  415. }
  416. get visible() {
  417. return this._visible == false ? false : true;
  418. }
  419. };
  420. torch.Tensor = class {
  421. constructor(tensor) {
  422. this._type = new torch.TensorType(tensor);
  423. this._storage = tensor.storage;
  424. this._offset = tensor.storage_offset;
  425. }
  426. get type() {
  427. return this._type;
  428. }
  429. get state() {
  430. return this._context().state || null;
  431. }
  432. get value() {
  433. const context = this._context();
  434. if (context.state) {
  435. return null;
  436. }
  437. context.limit = Number.MAX_SAFE_INTEGER;
  438. return this._decode(context, 0);
  439. }
  440. toString() {
  441. const context = this._context();
  442. if (context.state) {
  443. return '';
  444. }
  445. context.limit = 1000;
  446. const value = this._decode(context, 0);
  447. return JSON.stringify(value, null, 4);
  448. }
  449. _context() {
  450. const context = {};
  451. context.state = null;
  452. context.index = 0;
  453. context.count = 0;
  454. if (!this._storage) {
  455. context.state = 'Tensor data is empty.';
  456. return context;
  457. }
  458. context.data = this._storage.data();
  459. context.index = this._offset;
  460. if (!context.data) {
  461. context.state = 'Tensor data is empty.';
  462. return context;
  463. }
  464. switch (this._type.dataType) {
  465. case 'uint8':
  466. case 'int8':
  467. case 'int16':
  468. case 'int32':
  469. case 'int64':
  470. case 'float32':
  471. case 'float64':
  472. break;
  473. default:
  474. context.state = 'Tensor data type is not implemented.';
  475. break;
  476. }
  477. context.dimensions = this._type.shape.dimensions;
  478. if (!context.dimensions && context.dimensions.length == 0) {
  479. context.state = 'Tensor has no dimensions.';
  480. return context;
  481. }
  482. return context;
  483. }
  484. _decode(context, dimension) {
  485. const results = [];
  486. const size = context.dimensions[dimension];
  487. if (dimension == context.dimensions.length - 1) {
  488. for (let i = 0; i < size; i++) {
  489. if (context.count > context.limit) {
  490. results.push('...');
  491. return results;
  492. }
  493. results.push(context.data[context.index]);
  494. context.index++;
  495. context.count++;
  496. }
  497. }
  498. else {
  499. for (let j = 0; j < size; j++) {
  500. if (context.count > context.limit) {
  501. results.push('...');
  502. return results;
  503. }
  504. results.push(this._decode(context, dimension + 1));
  505. }
  506. }
  507. return results;
  508. }
  509. };
  510. torch.TensorType = class {
  511. constructor(tensor) {
  512. this._dataType = tensor.dataType;
  513. this._shape = new torch.TensorShape(tensor.size);
  514. }
  515. get dataType() {
  516. return this._dataType;
  517. }
  518. get shape() {
  519. return this._shape;
  520. }
  521. toString() {
  522. return (this.dataType || '?') + this._shape.toString();
  523. }
  524. };
  525. torch.TensorShape = class {
  526. constructor(dimensions) {
  527. this._dimensions = dimensions;
  528. }
  529. get dimensions() {
  530. return this._dimensions;
  531. }
  532. toString() {
  533. if (this._dimensions) {
  534. if (this._dimensions.length == 0) {
  535. return '';
  536. }
  537. return '[' + this._dimensions.map((dimension) => dimension.toString()).join(',') + ']';
  538. }
  539. return '';
  540. }
  541. };
  542. torch.Metadata = class {
  543. static open(context) {
  544. if (torch.Metadata._metadata) {
  545. return Promise.resolve(torch.Metadata._metadata);
  546. }
  547. return context.request('torch-metadata.json', 'utf-8', null).then((data) => {
  548. torch.Metadata._metadata = new torch.Metadata(data);
  549. return torch.Metadata._metadata;
  550. }).catch(() => {
  551. torch.Metadata._metadata = new torch.Metadata(null);
  552. return torch.Metadata._metadata;
  553. });
  554. }
  555. constructor(data) {
  556. this._map = new Map();
  557. this._attributeCache = {};
  558. if (data) {
  559. const items = JSON.parse(data);
  560. for (const item of items) {
  561. this._map.set(item.name, item);
  562. }
  563. }
  564. }
  565. type(name) {
  566. if (!this._map.has(name)) {
  567. this._map.set(name, { name: name });
  568. }
  569. return this._map.get(name);
  570. }
  571. attribute(type, name) {
  572. let map = this._attributeCache[type];
  573. if (!map) {
  574. map = {};
  575. const schema = this.type(type);
  576. if (schema && schema.attributes && schema.attributes.length > 0) {
  577. for (const attribute of schema.attributes) {
  578. map[attribute.name] = attribute;
  579. }
  580. }
  581. this._attributeCache[type] = map;
  582. }
  583. return map[name] || null;
  584. }
  585. };
  586. torch.Error = class extends Error {
  587. constructor(message) {
  588. super(message);
  589. this.name = 'Error loading Torch model.';
  590. }
  591. };
  592. torch.T7Reader = class {
  593. constructor(buffer, callback) {
  594. this._callback = callback;
  595. this._memo = new Map();
  596. this._registry = {};
  597. this._registry['bnn.Binary'] = function(reader) { reader.nn(this); };
  598. this._registry['bnn.SpatialConvolution'] = function(reader) { reader.nn(this); };
  599. this._registry['cudnn.BatchNormalization'] = function(reader) { reader.nn(this); };
  600. this._registry['cudnn.BatchBRNNReLU'] = function(reader) { reader.nn(this); };
  601. this._registry['cudnn.BLSTM'] = function(reader) { reader.nn(this); };
  602. this._registry['cudnn.ReLU'] = function(reader) { reader.nn(this); };
  603. this._registry['cudnn.RNN'] = function(reader) { reader.nn(this); };
  604. this._registry['cudnn.Sigmoid'] = function(reader) { reader.nn(this); };
  605. this._registry['cudnn.SoftMax'] = function(reader) { reader.nn(this); };
  606. this._registry['cudnn.LogSoftMax'] = function(reader) { reader.nn(this); };
  607. this._registry['cudnn.SpatialAveragePooling'] = function(reader) { reader.nn(this); };
  608. this._registry['cudnn.SpatialBatchNormalization'] = function(reader) { reader.nn(this); };
  609. this._registry['cudnn.SpatialConvolution'] = function(reader) { reader.nn(this); };
  610. this._registry['cudnn.SpatialFullConvolution'] = function(reader) { reader.nn(this); };
  611. this._registry['cudnn.SpatialMaxPooling'] = function(reader) { reader.nn(this); };
  612. this._registry['cudnn.SpatialSoftMax'] = function(reader) { reader.nn(this); };
  613. this._registry['cudnn.Tanh'] = function(reader) { reader.nn(this); };
  614. this._registry['cudnn.VolumetricAveragePooling'] = function(reader) { reader.nn(this); };
  615. this._registry['cudnn.VolumetricBatchNormalization'] = function(reader) { reader.nn(this); };
  616. this._registry['cudnn.VolumetricConvolution'] = function(reader) { reader.nn(this); };
  617. this._registry['cudnn.VolumetricMaxPooling'] = function(reader) { reader.nn(this); };
  618. this._registry['Dict'] = function(reader) { reader.nn(this); };
  619. this._registry['inn.ConstAffine'] = function(reader) { reader.nn(this); };
  620. this._registry['inn.SpatialMaxPooling'] = function(reader) { reader.nn(this); };
  621. this._registry['nn.Abs'] = function(reader) { reader.nn(this); };
  622. this._registry['nn.AddConstant'] = function(reader) { reader.nn(this); };
  623. this._registry['nn.BatchNormalization'] = function(reader) { reader.nn(this); };
  624. this._registry['nn.BilinearSamplerBHWD'] = function(reader) { reader.nn(this); };
  625. this._registry['nn.BinActiveZ'] = function(reader) { reader.nn(this); }; // allenai/XNOR-Net
  626. this._registry['nn.BCECriterion'] = function(reader) { reader.nn(this); };
  627. this._registry['nn.Bottle'] = function(reader) { reader.nn(this); };
  628. this._registry['nn.Clamp'] = function(reader) { reader.nn(this); };
  629. this._registry['nn.CMul'] = function(reader) { reader.nn(this); };
  630. this._registry['nn.CAddTable'] = function(reader) { reader.nn(this); };
  631. this._registry['nn.CDivTable'] = function(reader) { reader.nn(this); };
  632. this._registry['nn.CMulTable'] = function(reader) { reader.nn(this); };
  633. this._registry['nn.CSubTable'] = function(reader) { reader.nn(this); };
  634. this._registry['nn.Concat'] = function(reader) { reader.nn(this); };
  635. this._registry['nn.Copy'] = function(reader) { reader.nn(this); };
  636. this._registry['nn.ConcatTable'] = function(reader) { reader.nn(this); };
  637. this._registry['nn.Contiguous'] = function(reader) { reader.nn(this); };
  638. this._registry['nn.Constant'] = function(reader) { reader.nn(this); };
  639. this._registry['nn.CostVolMulti'] = function(reader) { reader.nn(this); };
  640. this._registry['nn.DataParallelTable'] = function(reader) { reader.nn(this); };
  641. this._registry['nn.DepthConcat'] = function(reader) { reader.nn(this); };
  642. this._registry['nn.Dropout'] = function(reader) { reader.nn(this); };
  643. this._registry['nn.Exp'] = function(reader) { reader.nn(this); };
  644. this._registry['nn.ExpOut'] = function(reader) { reader.nn(this); };
  645. this._registry['nn.FlattenTable'] = function(reader) { reader.nn(this); };
  646. this._registry['nn.GenNoise'] = function(reader) { reader.nn(this); };
  647. this._registry['nn.Identity'] = function(reader) { reader.nn(this); };
  648. this._registry['nn.Index'] = function(reader) { reader.nn(this); };
  649. this._registry['nn.Inception'] = function(reader) { reader.nn(this); };
  650. this._registry['nn.InstanceNormalization'] = function(reader) { reader.nn(this); };
  651. this._registry['nn.JoinTable'] = function(reader) { reader.nn(this); };
  652. this._registry['nn.JointTrain'] = function(reader) { reader.nn(this); };
  653. this._registry['nn.KeypointCoordinate'] = function(reader) { reader.nn(this); };
  654. this._registry['nn.LeakyReLU'] = function(reader) { reader.nn(this); };
  655. this._registry['nn.Linear'] = function(reader) { reader.nn(this); };
  656. this._registry['nn.LinearNoBias'] = function(reader) { reader.nn(this); };
  657. this._registry['nn.LogSoftMax'] = function(reader) { reader.nn(this); };
  658. this._registry['nn.LookupTable'] = function(reader) { reader.nn(this); };
  659. this._registry['nn.LSTM'] = function(reader) { reader.nn(this); };
  660. this._registry['nn.MaskZero'] = function(reader) { reader.nn(this); };
  661. this._registry['nn.MapTable'] = function(reader) { reader.nn(this); };
  662. this._registry['nn.Max'] = function(reader) { reader.nn(this); };
  663. this._registry['nn.Mean'] = function(reader) { reader.nn(this); };
  664. this._registry['nn.Min'] = function(reader) { reader.nn(this); };
  665. this._registry['nn.MulConstant'] = function(reader) { reader.nn(this); };
  666. this._registry['nn.MM'] = function(reader) { reader.nn(this); };
  667. this._registry['nn.MSECriterion'] = function(reader) { reader.nn(this); };
  668. this._registry['nn.Narrow'] = function(reader) { reader.nn(this); };
  669. this._registry['nn.NarrowTable'] = function(reader) { reader.nn(this); };
  670. this._registry['nn.Normalize'] = function(reader) { reader.nn(this); };
  671. this._registry['nn.Normalize2'] = function(reader) { reader.nn(this); };
  672. this._registry['nn.NoiseFill'] = function(reader) { reader.nn(this); };
  673. this._registry['nn.Padding'] = function(reader) { reader.nn(this); };
  674. this._registry['nn.Parallel'] = function(reader) { reader.nn(this); };
  675. this._registry['nn.ParallelCriterion'] = function(reader) { reader.nn(this); };
  676. this._registry['nn.ParallelTable'] = function(reader) { reader.nn(this); };
  677. this._registry['nn.PixelShuffle'] = function(reader) { reader.nn(this); };
  678. this._registry['nn.Power'] = function(reader) { reader.nn(this); };
  679. this._registry['nn.PReLU'] = function(reader) { reader.nn(this); };
  680. this._registry['nn.Recursor'] = function(reader) { reader.nn(this); };
  681. this._registry['nn.ReLU'] = function(reader) { reader.nn(this); };
  682. this._registry['nn.Replicate'] = function(reader) { reader.nn(this); };
  683. this._registry['nn.Reshape'] = function(reader) { reader.nn(this); };
  684. this._registry['nn.ShaveImage'] = function(reader) { reader.nn(this); };
  685. this._registry['nn.Select'] = function(reader) { reader.nn(this); };
  686. this._registry['nn.SelectTable'] = function(reader) { reader.nn(this); };
  687. this._registry['nn.Sequencer'] = function(reader) { reader.nn(this); };
  688. this._registry['nn.Sequential'] = function(reader) { reader.nn(this); };
  689. this._registry['nn.Sigmoid'] = function(reader) { reader.nn(this); };
  690. this._registry['nn.Sum'] = function(reader) { reader.nn(this); };
  691. this._registry['nn.SoftMax'] = function(reader) { reader.nn(this); };
  692. this._registry['nn.SpatialAveragePooling'] = function(reader) { reader.nn(this); };
  693. this._registry['nn.SpatialBatchNormalization'] = function(reader) { reader.nn(this); };
  694. this._registry['nn.SpatialConvolution'] = function(reader) { reader.nn(this); };
  695. this._registry['nn.SpatialConvolutionMM'] = function(reader) { reader.nn(this); };
  696. this._registry['nn.SpatialCrossMapLRN'] = function(reader) { reader.nn(this); };
  697. this._registry['nn.SpatialDilatedConvolution'] = function(reader) { reader.nn(this); };
  698. this._registry['nn.SpatialDropout'] = function(reader) { reader.nn(this); };
  699. this._registry['nn.SpatialFractionalMaxPooling'] = function(reader) { reader.nn(this); };
  700. this._registry['nn.SpatialFullConvolution'] = function(reader) { reader.nn(this); };
  701. this._registry['nn.SpatialLPPooling'] = function(reader) { reader.nn(this); };
  702. this._registry['nn.SpatialMaxPooling'] = function(reader) { reader.nn(this); };
  703. this._registry['nn.SpatialMaxUnpooling'] = function(reader) { reader.nn(this); };
  704. this._registry['nn.SpatialReflectionPadding'] = function(reader) { reader.nn(this); };
  705. this._registry['nn.SpatialReplicationPadding'] = function(reader) { reader.nn(this); };
  706. this._registry['nn.SpatialSoftMax'] = function(reader) { reader.nn(this); };
  707. this._registry['nn.SpatialSubtractiveNormalization'] = function(reader) { reader.nn(this); };
  708. this._registry['nn.SpatialUpSamplingBilinear'] = function(reader) { reader.nn(this); };
  709. this._registry['nn.SpatialUpSamplingNearest'] = function(reader) { reader.nn(this); };
  710. this._registry['nn.SpatialZeroPadding'] = function(reader) { reader.nn(this); };
  711. this._registry['nn.SplitTable'] = function(reader) { reader.nn(this); };
  712. this._registry['nn.Squeeze'] = function(reader) { reader.nn(this); };
  713. this._registry['nn.Square'] = function(reader) { reader.nn(this); };
  714. this._registry['nn.Sqrt'] = function(reader) { reader.nn(this); };
  715. this._registry['nn.StereoJoin'] = function(reader) { reader.nn(this); };
  716. this._registry['nn.Tanh'] = function(reader) { reader.nn(this); };
  717. this._registry['nn.Transpose'] = function(reader) { reader.nn(this); };
  718. this._registry['nn.TotalVariation'] = function(reader) { reader.nn(this); };
  719. this._registry['nn.Unpool'] = function(reader) { reader.nn(this); };
  720. this._registry['nn.View'] = function(reader) { reader.nn(this); };
  721. this._registry['nn.gModule'] = function(reader) { reader.nn(this); };
  722. this._registry['nngraph.Node'] = function(reader) { reader.nn(this); };
  723. this._registry['graph.Edge'] = function(reader) { reader.nn(this); };
  724. this._registry['graph.Graph'] = function(reader) { reader.nn(this); };
  725. this._registry['torch.ByteTensor'] = function(reader) { reader.tensor(this, 'uint8'); };
  726. this._registry['torch.CharTensor'] = function(reader) { reader.tensor(this, 'int8'); };
  727. this._registry['torch.ShortTensor'] = function(reader) { reader.tensor(this, 'int16'); };
  728. this._registry['torch.IntTensor'] = function(reader) { reader.tensor(this, 'int32'); };
  729. this._registry['torch.LongTensor'] = function(reader) { reader.tensor(this, 'int64'); };
  730. this._registry['torch.FloatTensor'] = function(reader) { reader.tensor(this, 'float32'); };
  731. this._registry['torch.DoubleTensor'] = function(reader) { reader.tensor(this, 'float64'); };
  732. this._registry['torch.CudaByteTensor'] = function(reader) { reader.tensor(this, 'uint8'); };
  733. this._registry['torch.CudaCharTensor'] = function(reader) { reader.tensor(this, 'int8'); };
  734. this._registry['torch.CudaShortTensor'] = function(reader) { reader.tensor(this, 'int16'); };
  735. this._registry['torch.CudaIntTensor'] = function(reader) { reader.tensor(this, 'int32'); };
  736. this._registry['torch.CudaLongTensor'] = function(reader) { reader.tensor(this, 'int64'); };
  737. this._registry['torch.CudaTensor'] = function(reader) { reader.tensor(this, 'float32'); };
  738. this._registry['torch.CudaDoubleTensor'] = function(reader) { reader.tensor(this, 'float64'); };
  739. this._registry['torch.ByteStorage'] = function(reader) { reader.storage(this, 'uint8', 1); };
  740. this._registry['torch.CharStorage'] = function(reader) { reader.storage(this, 'int8', 1); };
  741. this._registry['torch.ShortStorage'] = function(reader) { reader.storage(this, 'int16', 2); };
  742. this._registry['torch.IntStorage'] = function(reader) { reader.storage(this, 'int32', 4); };
  743. this._registry['torch.LongStorage'] = function(reader) { reader.storage(this, 'int64', 8); };
  744. this._registry['torch.FloatStorage'] = function(reader) { reader.storage(this, 'float32', 4); };
  745. this._registry['torch.DoubleStorage'] = function(reader) { reader.storage(this, 'float64', 8); };
  746. this._registry['torch.CudaByteStorage'] = function(reader) { reader.storage(this, 'uint8', 1); };
  747. this._registry['torch.CudaCharStorage'] = function(reader) { reader.storage(this, 'int8', 1); };
  748. this._registry['torch.CudaShortStorage'] = function(reader) { reader.storage(this, 'int16', 2); };
  749. this._registry['torch.CudaIntStorage'] = function(reader) { reader.storage(this, 'int32', 4); };
  750. this._registry['torch.CudaLongStorage'] = function(reader) { reader.storage(this, 'int64', 8); };
  751. this._registry['torch.CudaIntStorage'] = function(reader) { reader.storage(this, 'int32', 4); };
  752. this._registry['torch.CudaStorage'] = function(reader) { reader.storage(this, 'float32', 4); };
  753. this._registry['torch.CudaFloatStorage'] = function(reader) { reader.storage(this, 'float64', 8); };
  754. this._registry['w2nn.AuxiliaryLossTable'] = function(reader) { reader.nn(this); };
  755. this._registry['w2nn.InplaceClip01'] = function(reader) { reader.nn(this); };
  756. this._registry['w2nn.ScaleTable'] = function(reader) { reader.nn(this); };
  757. if (buffer.length == 0) {
  758. throw new torch.Error('File is empty.');
  759. }
  760. if (buffer[0] <= 8) {
  761. this._reader = new torch.BinaryReader(buffer);
  762. }
  763. else {
  764. this._reader = new torch.TextReader(buffer);
  765. this._reader.int32();
  766. this._reader.reset();
  767. }
  768. }
  769. read() {
  770. const type = this.int32();
  771. switch (type) {
  772. case 0: return null;
  773. case 1: return this.float64();
  774. case 2: return this.string();
  775. case 3: return this.table();
  776. case 4: return this.object();
  777. case 5: return this.boolean();
  778. case 6: return this.function();
  779. case 7: return this.function();
  780. case 8: return this.function();
  781. default: throw new torch.Error("File format has invalid type '" + type + "'.");
  782. }
  783. }
  784. boolean() {
  785. return this._reader.boolean();
  786. }
  787. bytes(size) {
  788. return this._reader.bytes(size);
  789. }
  790. int32() {
  791. return this._reader.int32();
  792. }
  793. int64() {
  794. return this._reader.int64();
  795. }
  796. int64s(size) {
  797. return this._reader.int64s(size);
  798. }
  799. float64() {
  800. return this._reader.float64();
  801. }
  802. string() {
  803. return this._reader.string();
  804. }
  805. object() {
  806. const index = this.int32();
  807. if (this._memo.has(index)) {
  808. return this._memo.get(index);
  809. }
  810. let version = this.string();
  811. let name = null;
  812. if (version.startsWith('V ')) {
  813. name = this.string();
  814. version = Number(version.split(' ')[1]);
  815. }
  816. else {
  817. name = version;
  818. version = 0;
  819. }
  820. const obj = { __type__: name };
  821. this._memo.set(index, obj);
  822. let constructor = this._registry[name];
  823. if (constructor) {
  824. constructor.apply(obj, [ this, version ]);
  825. }
  826. else {
  827. constructor = this._callback(name);
  828. if (constructor) {
  829. constructor.apply(obj, [ this, version ]);
  830. }
  831. this.nn(obj);
  832. }
  833. return obj;
  834. }
  835. table() {
  836. const index = this.int32();
  837. if (this._memo.has(index)) {
  838. return this._memo.get(index);
  839. }
  840. const table = {};
  841. this._memo.set(index, table);
  842. const size = this.int32();
  843. let convert = true;
  844. let sum = 0;
  845. for (let i = 0; i < size; i++) {
  846. const key = this.read();
  847. const value = this.read();
  848. table[key] = value;
  849. if (Number.isInteger(key) && key >= 0) {
  850. sum += key;
  851. }
  852. else {
  853. convert = false;
  854. }
  855. }
  856. const n = Object.keys(table).length;
  857. if (convert && (n * (n + 1)) == (2 * sum)) {
  858. const list = [];
  859. for (let j = 0; j < n; j++) {
  860. let item = table[j + 1];
  861. if (item == table) {
  862. item = list;
  863. }
  864. list.push(item);
  865. }
  866. this._memo.set(index, list);
  867. return list;
  868. }
  869. return table;
  870. }
  871. function() {
  872. const index = this.int32();
  873. if (this._memo.has(index)) {
  874. return this._memo.get(index);
  875. }
  876. const size = this.int32();
  877. const dumped = this.bytes(size);
  878. const upvalues = this.read();
  879. const func = { __type__: 'Function', size: size, dumped: dumped, upvalues: upvalues };
  880. this._memo.set(index, func);
  881. return func;
  882. }
  883. nn(obj) {
  884. const attributes = this.read();
  885. if (attributes != null) {
  886. for (const key of Object.keys(attributes)) {
  887. obj[key] = attributes[key];
  888. }
  889. }
  890. }
  891. tensor(obj, dataType) {
  892. const dim = this.int32();
  893. obj.dataType = dataType;
  894. obj.size = this.int64s(dim);
  895. obj.stride = this.int64s(dim);
  896. obj.storage_offset = this.int64() - 1;
  897. obj.storage = this.read();
  898. }
  899. storage(obj, dataType, itemSize) {
  900. obj.dataType = dataType;
  901. obj.itemSize = itemSize;
  902. obj.size = this.int64();
  903. obj.reader = this._reader.storage(obj.size, obj.itemSize, dataType);
  904. obj.data = function() {
  905. if (this.reader) {
  906. const reader = this.reader;
  907. reader.reset();
  908. const size = obj.size;
  909. const array = new Array(size);
  910. for (let i = 0; i < size; i++) {
  911. switch (dataType) {
  912. case 'uint8':
  913. array[i] = this.reader.byte();
  914. break;
  915. case 'int8':
  916. array[i] = this.reader.int8();
  917. break;
  918. case 'int16':
  919. array[i] = this.reader.int16();
  920. break;
  921. case 'int32':
  922. array[i] = this.reader.int32();
  923. break;
  924. case 'int64':
  925. array[i] = this.reader.int64();
  926. break;
  927. case 'float32':
  928. array[i] = this.reader.float32();
  929. break;
  930. case 'float64':
  931. array[i] = this.reader.float64();
  932. break;
  933. }
  934. }
  935. obj._data = array;
  936. delete obj.reader;
  937. }
  938. return obj._data;
  939. };
  940. }
  941. };
  942. torch.BinaryReader = class {
  943. constructor(buffer) {
  944. this._buffer = buffer;
  945. this._dataView = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);
  946. this._position = 0;
  947. this._textDecoder = new TextDecoder('ascii');
  948. }
  949. reset() {
  950. this._position = 0;
  951. }
  952. skip(offset) {
  953. this._position += offset;
  954. if (this._position > this._buffer.length) {
  955. throw new torch.Error('Expected ' + (this._position - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.');
  956. }
  957. }
  958. boolean() {
  959. return this.int32() == 1;
  960. }
  961. bytes(length) {
  962. const position = this._position;
  963. this.skip(length);
  964. return this._buffer.subarray(position, this._position);
  965. }
  966. int8() {
  967. const position = this._position;
  968. this.skip(1);
  969. return this._dataView.getInt8(position, true);
  970. }
  971. int16() {
  972. const position = this._position;
  973. this.skip(2);
  974. return this._dataView.getInt16(position, true);
  975. }
  976. int32() {
  977. const position = this._position;
  978. this.skip(4);
  979. return this._dataView.getInt32(position, true);
  980. }
  981. int64() {
  982. const position = this._position;
  983. this.skip(8);
  984. return this._dataView.getInt64(position, true).toNumber();
  985. }
  986. int64s(size) {
  987. const array = [];
  988. for (let i = 0; i < size; i++) {
  989. array.push(this.int64());
  990. }
  991. return array;
  992. }
  993. float32() {
  994. const position = this._position;
  995. this.skip(4);
  996. return this._dataView.getFloat32(position, true);
  997. }
  998. float64() {
  999. const position = this._position;
  1000. this.skip(8);
  1001. return this._dataView.getFloat64(position, true);
  1002. }
  1003. string() {
  1004. return this._textDecoder.decode(this.bytes(this.int32()));
  1005. }
  1006. storage(size, itemSize) {
  1007. return new torch.BinaryReader(this.bytes(size * itemSize));
  1008. }
  1009. };
  1010. torch.TextReader = class {
  1011. constructor(buffer, separator) {
  1012. this._buffer = buffer;
  1013. this._position = 0;
  1014. this._dataView = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);
  1015. this._textDecoder = new TextDecoder('ascii');
  1016. this._separator = separator || 0x0a;
  1017. }
  1018. reset() {
  1019. this._position = 0;
  1020. }
  1021. line(size) {
  1022. const start = this._position;
  1023. while (this._position < this._buffer.length && size > -1) {
  1024. const c = this._buffer[this._position++];
  1025. if (c == this._separator) {
  1026. return this._buffer.slice(start, this._position - 1);
  1027. }
  1028. else if (this._position == this._buffer.length) {
  1029. return this._buffer.slice(start, this._position);
  1030. }
  1031. size--;
  1032. }
  1033. throw new torch.Error('Line exceeded maximum length.');
  1034. }
  1035. boolean() {
  1036. return this.int32() == 1;
  1037. }
  1038. bytes(size) {
  1039. return this.line(size);
  1040. }
  1041. int8() {
  1042. return this.int64();
  1043. }
  1044. int16() {
  1045. return this.int64();
  1046. }
  1047. int32() {
  1048. return this.int64();
  1049. }
  1050. int64() {
  1051. const token = this._textDecoder.decode(this.line(20));
  1052. const number = Number.parseInt(token, 10);
  1053. if (Number.isNaN(token - number)) {
  1054. throw new torch.Error("Couldn't parse int64 '" + token + "'.");
  1055. }
  1056. return number;
  1057. }
  1058. int64s(size) {
  1059. const array = [];
  1060. if (size > 0) {
  1061. const text = this._textDecoder.decode(this.line(Number.MAX_SAFE_INTEGER));
  1062. for (const token of text.split(' ')) {
  1063. const number = Number.parseInt(token, 10);
  1064. if (Number.isNaN(token - number)) {
  1065. throw new torch.Error("Couldn't parse int64 '" + token + "'.");
  1066. }
  1067. array.push(number);
  1068. }
  1069. }
  1070. return array;
  1071. }
  1072. float32() {
  1073. return this.float64();
  1074. }
  1075. float64() {
  1076. const token = this._textDecoder.decode(this.line(24));
  1077. if (token.startsWith('-nan')) {
  1078. return -NaN;
  1079. }
  1080. if (token.startsWith('nan')) {
  1081. return NaN;
  1082. }
  1083. if (token.startsWith('inf')) {
  1084. return Infinity;
  1085. }
  1086. if (token.startsWith('-inf')) {
  1087. return -Infinity;
  1088. }
  1089. const number = Number.parseFloat(token);
  1090. if (Number.isNaN(token - number)) {
  1091. throw new torch.Error("Couldn't parse float '" + token + "'.");
  1092. }
  1093. return number;
  1094. }
  1095. string() {
  1096. const size = this.int32();
  1097. if (size == 0) {
  1098. return '';
  1099. }
  1100. const data = this.line(size);
  1101. const text = this._textDecoder.decode(data);
  1102. if (size != text.length) {
  1103. throw torch.Error('Invalid text length.');
  1104. }
  1105. return text;
  1106. }
  1107. storage(size, itemSize, dataType) {
  1108. if (size <= 0) {
  1109. throw new torch.Error("Unsupported storage size '" + size + "'.");
  1110. }
  1111. if (dataType === 'uint8') {
  1112. const start = this._position;
  1113. this._position += size;
  1114. const bytes = this._buffer.slice(start, this._position);
  1115. this.line(0);
  1116. return new torch.BinaryReader(bytes);
  1117. }
  1118. const data = this.line(Number.MAX_SAFE_INTEGER);
  1119. return new torch.TextReader(data, 0x20);
  1120. }
  1121. };
  1122. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  1123. module.exports.ModelFactory = torch.ModelFactory;
  1124. }