()
| 14192 | return convertObject(obj); |
| 14193 | } |
| 14194 | LEGACY_deserialize() { |
| 14195 | // https://github.com/pytorch/pytorch/blob/5e69e11d098a2cfccc8a59377c431e9c71cab9a8/torch/csrc/jit/serialization/import_legacy.cpp#L88 |
| 14196 | const execution = this._compilation_unit.execution; |
| 14197 | const caffe2 = execution.proto.caffe2; |
| 14198 | const torch = execution.import('torch'); |
| 14199 | const stream = this._reader.get_record('model.json'); |
| 14200 | const buffer = stream.peek(); |
| 14201 | const decoder = new TextDecoder('utf-8'); |
| 14202 | const content = decoder.decode(buffer); |
| 14203 | const obj = JSON.parse(content); |
| 14204 | const model = execution.proto.torch.ModelDef.decodeJson(obj); |
| 14205 | const tensorTypeMap = new Map([ |
| 14206 | [caffe2.TensorProto.DataType.FLOAT, 'Float'], |
| 14207 | [caffe2.TensorProto.DataType.FLOAT16, 'Half'], |
| 14208 | [caffe2.TensorProto.DataType.DOUBLE, 'Double'], |
| 14209 | [caffe2.TensorProto.DataType.INT8, 'Char'], |
| 14210 | [caffe2.TensorProto.DataType.INT32, 'Int'], |
| 14211 | [caffe2.TensorProto.DataType.INT64, 'Long'] |
| 14212 | ]); |
| 14213 | const tensor_table = (model.tensors || []).map((constant) => { |
| 14214 | const key = constant.data.key; |
| 14215 | if (!tensorTypeMap.has(constant.data_type)) { |
| 14216 | throw new python.Error(`Unsupported tensor data type '${constant.data_type}'.`); |
| 14217 | } |
| 14218 | const type = tensorTypeMap.get(constant.data_type); |
| 14219 | const shape = constant.dims ? constant.dims.map((dim) => parseInt(dim, 10)) : null; |
| 14220 | const strides = constant.strides ? constant.strides.map((dim) => parseInt(dim, 10)) : null; |
| 14221 | const storage_type = execution.resolve(`torch.${type}Storage`); |
| 14222 | const size = (shape || []).reduce((a, b) => a * b, 1); |
| 14223 | const offset = parseInt(constant.offset, 10) || 0; |
| 14224 | const storage = new storage_type(size); |
| 14225 | const itemsize = storage.dtype.itemsize(); |
| 14226 | const stream = this._reader.get_record(key); |
| 14227 | if (stream) { |
| 14228 | const buffer = stream.peek(); |
| 14229 | const length = size * itemsize; |
| 14230 | const data = buffer.slice(offset, offset + length); |
| 14231 | storage._set_cdata(data); |
| 14232 | } |
| 14233 | const tensor = torch._utils._rebuild_tensor(storage, 0, shape, strides); |
| 14234 | tensor.name = key; |
| 14235 | return tensor; |
| 14236 | }); |
| 14237 | execution.builtins.CONSTANTS = {}; |
| 14238 | for (let i = 0; i < tensor_table.length; i++) { |
| 14239 | execution.builtins.CONSTANTS[`c${i}`] = tensor_table[i]; |
| 14240 | } |
| 14241 | const attributes = []; |
| 14242 | if (this._reader.has_record('attributes.pkl')) { |
| 14243 | const stream = this._reader.get_record('attributes.pkl'); |
| 14244 | const buffer = stream.peek(); |
| 14245 | const unpickler = new pickle.Unpickler(buffer); |
| 14246 | const obj = unpickler.load(); |
| 14247 | attributes.push(...obj); |
| 14248 | } |
| 14249 | this._LEGACY_moduleStack = ['__torch__']; |
| 14250 | const module_def = model.main_module; |
| 14251 | for (const tensor of tensor_table) { |
no test coverage detected