def json_loads_manifest(js): """ Returns: {str: tensor} """ manifest = ops_pb2.TensorManifest() Parse(js, manifest) return {t.uuid.uuid: t.info for t in manifest.pairs}
def json_dumps_manifest(values): """ Arguments: values: {str: np.array} """ manifest = ops_pb2.TensorManifest() for uuid_val, a in values.items(): pair = manifest.pairs.add() if isinstance(uuid_val, six.binary_type): pair.uuid.uuid = uuid_val else: pair.uuid.uuid = uuid_val.encode() pair.info.dtype = dtype_to_protobuf(a.dtype) pair.info.shape.extend(a.shape) return MessageToJson(manifest)
def test_json_dumps_manifest(): # create a list of variables with varying lengths and dtype x = np.zeros((2, 3)) js = serde_weights.json_dumps_manifest({'x': x}) # now deserialize js formatted manifest and make sure it has the right data in it manifest = ops_pb2.TensorManifest() Parse(js, manifest) assert len(manifest.pairs) == 1 t = manifest.pairs[0] assert t.uuid.uuid == six.b('x') assert t.info.dtype == ops_pb2.FLOAT64 assert t.info.shape == [2, 3]