Bläddra i källkod

Convert to Map class

Lutz Roeder 6 år sedan
förälder
incheckning
2a6789c019
5 ändrade filer med 66 tillägg och 68 borttagningar
  1. 3 3
      src/app.js
  2. 12 12
      src/chainer.js
  3. 7 9
      src/cntk.js
  4. 9 9
      src/darknet.js
  5. 35 35
      src/torchscript.js

+ 3 - 3
src/app.js

@@ -556,7 +556,7 @@ class View {
         this._owner = owner;
         this._ready = false;
         this._path = null;
-        this._properties = {};
+        this._properties = new Map();
 
         const size = electron.screen.getPrimaryDisplay().workAreaSize;
         let options = {};
@@ -687,11 +687,11 @@ class View {
             this._openPath = null;
             return;
         }
-        this._properties[name] = value;
+        this._properties.set(name, value);
     }
 
     get(name) {
-        return this._properties[name];
+        return this._properties.get(name);
     }
 
     on(event, callback) {

+ 12 - 12
src/chainer.js

@@ -45,12 +45,12 @@ chainer.ModelFactory = class {
                     let modules = [];
                     let map = new Map();
 
-                    let functionTable = {};
-                    let constructorTable = {};
-                    functionTable['_codecs.encode'] = function(obj /*, econding */) {
+                    let functionTable = new Map();
+                    let constructorTable = new Map();
+                    functionTable.set('_codecs.encode', function(obj /*, econding */) {
                         return obj;
-                    };
-                    constructorTable['numpy.core.multiarray._reconstruct'] = function(subtype, shape, dtype) {
+                    });
+                    constructorTable.set('numpy.core.multiarray._reconstruct', function(subtype, shape, dtype) {
                         this.subtype = subtype;
                         this.shape = shape;
                         this.dtype = dtype;
@@ -85,8 +85,8 @@ chainer.ModelFactory = class {
                             }
                             return array;
                         };
-                    };
-                    constructorTable['numpy.dtype'] = function(obj, align, copy) { 
+                    });
+                    constructorTable.set('numpy.dtype', function(obj, align, copy) { 
                         switch (obj) {
                             case 'i1': this.name = 'int8'; this.itemsize = 1; break;
                             case 'i2': this.name = 'int16'; this.itemsize = 2; break;
@@ -142,15 +142,15 @@ chainer.ModelFactory = class {
                                     throw new chainer.Error("Unknown numpy.dtype setstate length '" + state.length.toString() + "'.");
                             }
                         };
-                    };
+                    });
                     let function_call = (name, args) => {
-                        const func = functionTable[name];
-                        if (func) {
+                        if (functionTable.has(name)) {
+                            const func = functionTable.get(name);
                             return func.apply(null, args);
                         }
                         let obj = { __type__: name };
-                        const constructor = constructorTable[name];
-                        if (constructor) {
+                        if (constructorTable.has(name)) {
+                            const constructor = constructorTable.get(name);
                             constructor.apply(obj, args);
                         }
                         else {

+ 7 - 9
src/cntk.js

@@ -180,11 +180,10 @@ cntk.Graph = class {
                 break;
             }
             case 2: {
-                let nodeMap = {};
+                let nodeMap = new Map();
                 for (let node of obj.primitive_functions) {
-                    nodeMap[node.uid] = node;
+                    nodeMap.set(node.uid, node);
                 }
-                let argumentNames = {};
                 for (let input of obj.inputs) {
                     let argument = new cntk.Argument(version, input);
                     args[input.uid] = argument;
@@ -193,18 +192,17 @@ cntk.Graph = class {
                         let inputName = input.name || input.uid;
                         this._inputs.push(new cntk.Parameter(inputName, [ argument ]));
                     }
-                    argumentNames[input.uid] = input;
                 }
                 for (let block of obj.primitive_functions) {
                     if (block.op == 57 && block.block_function_composite) {
                         let list = [ block.block_function_composite.root ];
                         let nodes = [];
                         while (list.length > 0) {
-                            let name = list.shift();
-                            let node = nodeMap[name];
-                            if (node) {
+                            const name = list.shift();
+                            if (nodeMap.has(name)) {
+                                const node = nodeMap.get(name);
                                 nodes.push(new cntk.Node(metadata, version, node, args));
-                                nodeMap[name] = null;
+                                nodeMap.delete(name);
                                 for (let i = 0; i < node.inputs.length; i++) {
                                     let parts = node.inputs[i].split('_');
                                     if (parts.length >= 3) {
@@ -222,7 +220,7 @@ cntk.Graph = class {
                     }
                 }
                 for (let node of obj.primitive_functions) {
-                    if (nodeMap[node.uid]) {
+                    if (nodeMap.has(node.uid)) {
                         this._nodes.push(new cntk.Node(metadata, version, node, args));
                     }
                 }

+ 9 - 9
src/darknet.js

@@ -448,14 +448,14 @@ darknet.Metadata = class {
     }
 
     constructor(data) {
-        this._map = {};
-        this._attributeCache = {};
+        this._map = new Map();
+        this._attributeCache = new Map();
         if (data) {
             const items = JSON.parse(data);
             if (items) {
                 for (let item of items) {
                     if (item.name && item.schema) {
-                        this._map[item.name] = item.schema;
+                        this._map.set(item.name, item.schema);
                     }
                 }
             }
@@ -463,22 +463,22 @@ darknet.Metadata = class {
     }
 
     getSchema(operator) {
-        return this._map[operator] || null;
+        return this._map.get(operator) || null;
     }
 
     getAttributeSchema(operator, name) {
-        let map = this._attributeCache[operator];
+        let map = this._attributeCache.get(operator);
         if (!map) {
-            map = {};
+            map = new Map();
             let schema = this.getSchema(operator);
             if (schema && schema.attributes && schema.attributes.length > 0) {
                 for (let attribute of schema.attributes) {
-                    map[attribute.name] = attribute;
+                    map.set(attribute.name, attribute);
                 }
             }
-            this._attributeCache[operator] = map;
+            this._attributeCache.set(operator, map);
         }
-        return map[name] || null;
+        return map.get(name) || null;
     }
 };
 

+ 35 - 35
src/torchscript.js

@@ -40,7 +40,7 @@ torchscript.ModelFactory = class {
                     }
                     if (container.model) {
                         container.model = JSON.parse(textDecoder.decode(container.model));
-                    }            
+                    }
                     return torchscript.Metadata.open(host).then((metadata) => {
                         try {
                             return new torchscript.Model(metadata, host, python, container);
@@ -65,9 +65,9 @@ torchscript.ModelFactory = class {
 
     static _openContainer(entries) {
         if (entries && entries.length > 0) {
-            let container = {};
             const version = entries.find((entry) => entry.name == 'version' || entry.name.endsWith('/version'));
             if (version) {
+                let container = {};
                 container.entries = entries;
                 container.prefix = version.name.substring(0, version.name.length - 7);
                 let find = (name) => {
@@ -104,8 +104,8 @@ torchscript.ModelFactory = class {
         if (!data) {
             return null;
         }
-        let functionTable = {};
-        functionTable['collections.OrderedDict'] = function(args) {
+        let functionTable = new Map();
+        functionTable.set('collections.OrderedDict', function(args) {
             let obj = [];
             obj.__setitem__ = function(key, value) {
                 obj.push({ key: key, value: value });
@@ -116,8 +116,8 @@ torchscript.ModelFactory = class {
                 }
             }
             return obj;
-        };
-        functionTable['torch._utils._rebuild_tensor_v2'] = function(storage, storage_offset, size, stride, requires_grad, backward_hooks) {
+        });
+        functionTable.set('torch._utils._rebuild_tensor_v2', function(storage, storage_offset, size, stride, requires_grad, backward_hooks) {
             return {
                 __type__: storage.__type__.replace('Storage', 'Tensor'),
                 storage: storage,
@@ -127,8 +127,8 @@ torchscript.ModelFactory = class {
                 requires_grad:requires_grad,
                 backward_hooks: backward_hooks
             };
-        };
-        functionTable['torch._utils._rebuild_qtensor'] = function(storage, storage_offset, size, stride, quantizer_params, requires_grad, backward_hooks) {
+        });
+        functionTable.set('torch._utils._rebuild_qtensor', function(storage, storage_offset, size, stride, quantizer_params, requires_grad, backward_hooks) {
             return {
                 __type__: storage.__type__.replace('Storage', 'Tensor'),
                 storage: storage,
@@ -139,46 +139,46 @@ torchscript.ModelFactory = class {
                 requires_grad:requires_grad,
                 backward_hooks: backward_hooks
             };
-        }
-        functionTable['torch.jit._pickle.build_intlist'] = function(data) {
+        });
+        functionTable.set('torch.jit._pickle.build_intlist', function(data) {
             return data;
-        }
-        let constructorTable = {};
-        constructorTable['torch.ByteStorage'] = function (size) { 
+        });
+        let constructorTable = new Map();
+        constructorTable.set('torch.ByteStorage', function (size) { 
             this.size = size; this.dataTypeSize = 1; this.dataType = 'uint8'; 
-        };
-        constructorTable['torch.CharStorage'] = function (size) { 
+        });
+        constructorTable.set('torch.CharStorage', function (size) { 
             this.size = size; this.dataTypeSize = 1; this.dataType = 'int8'; 
-        };
-        constructorTable['torch.ShortStorage'] = function (size) { 
+        });
+        constructorTable.set('torch.ShortStorage', function (size) { 
             this.size = size; this.dataTypeSize = 2; this.dataType = 'int16';
-        };
-        constructorTable['torch.IntStorage'] = function (size) { 
+        });
+        constructorTable.set('torch.IntStorage', function (size) { 
             this.size = size; this.dataTypeSize = 4; this.dataType = 'int32';
-        };
-        constructorTable['torch.LongStorage'] = function (size) { 
+        });
+        constructorTable.set('torch.LongStorage', function (size) { 
             this.size = size; this.dataTypeSize = 8; this.dataType = 'int64';
-        };
-        constructorTable['torch.HalfStorage'] = function (size) {
+        });
+        constructorTable.set('torch.HalfStorage', function (size) {
             this.size = size; this.dataTypeSize = 2; this.dataType = 'float16';
-        };
-        constructorTable['torch.FloatStorage'] = function (size) {
+        });
+        constructorTable.set('torch.FloatStorage', function (size) {
             this.size = size; this.dataTypeSize = 4; this.dataType = 'float32';
-        };
-        constructorTable['torch.DoubleStorage'] = function (size) { 
+        });
+        constructorTable.set('torch.DoubleStorage', function (size) { 
             this.size = size; this.dataTypeSize = 8; this.dataType = 'float64';
-        };
-        constructorTable['torch.QInt8Storage'] = function (size) { 
+        });
+        constructorTable.set('torch.QInt8Storage', function (size) { 
             this.size = size; this.dataTypeSize = 1; this.dataType = 'qint8';
-        };
+        });
         let function_call = (name, args) => {
-            let func = functionTable[name];
-            if (func) {
+            if (functionTable.has(name)) {
+                const func = functionTable.get(name);
                 return func.apply(null, args);
             }
             let obj = { __type__: name };
-            let constructor = constructorTable[name];
-            if (constructor) {
+            if (constructorTable.has(name)) {
+                const constructor = constructorTable.get(name);
                 constructor.apply(obj, args);
             }
             else if (!name.startsWith('__torch__.')) {
@@ -1604,7 +1604,7 @@ torchscript.GraphContext = class {
             }
             // exponential_average_factor = 0.10000000000000001
             if (expression.type === 'number') {
-                this._state[target.value] = expression.value;
+                this._state[target.value] = expression;
                 return true;
             }
             const valueExpression = this._evaluateExpression(expression);