Skip to content

Commit

Permalink
perf(frontend/compiler): support ser keyset using path
Browse files Browse the repository at this point in the history
reduce memory usage by avoiding unecessary copy
  • Loading branch information
youben11 committed Dec 10, 2024
1 parent f14090f commit 5979a08
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,21 @@ void mlir::concretelang::python::populateCompilerAPISubmodule(
return pybind11::bytes(keySetSerialize(keySet));
},
"Serialize a Keyset to bytes.")
.def(
"serialize_to_file",
[](Keyset &keySet, const std::string path) {
std::ofstream ofs;
ofs.open(path);
if (!ofs.good()) {
throw std::runtime_error("Failed to open keyset file " + path);
}
auto keysetProto = keySet.toProto();
auto maybeBuffer = keysetProto.writeBinaryToOstream(ofs);
if (maybeBuffer.has_failure()) {
throw std::runtime_error("Failed to serialize keys.");
}
},
"Serialize a Keyset to bytes.")
.def(
"serialize_lwe_secret_key_as_glwe",
[](Keyset &keyset, size_t keyIndex, size_t glwe_dimension,
Expand Down
21 changes: 20 additions & 1 deletion frontends/concrete-python/concrete/fhe/compilation/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def save(self, location: Union[str, Path]):
message = f"Unable to save keys to {location} because it already exists"
raise ValueError(message)

location.write_bytes(self.serialize())
self.serialize_to_file(location)

def load(self, location: Union[str, Path]):
"""
Expand Down Expand Up @@ -171,6 +171,8 @@ def serialize(self) -> bytes:
Serialize keys into bytes.
Serialized keys are not encrypted, so be careful how you store/transfer them!
`serialize_to_file` is supposed to be more performant as it avoid copying the buffer
between the Compiler and the Frontend.
Returns:
bytes:
Expand All @@ -184,6 +186,23 @@ def serialize(self) -> bytes:
serialized_keyset = self._keyset.serialize()
return serialized_keyset

def serialize_to_file(self, path: Path):
"""
Serialize keys into a file.
Serialized keys are not encrypted, so be careful how you store/transfer them!
This is supposed to be more performant than `serialize` as it avoid copying the buffer
between the Compiler and the Frontend.
Args:
path (Path): where to save serialized keys
"""
if self._keyset is None:
message = "Keys cannot be serialized before they are generated"
raise RuntimeError(message)

self._keyset.serialize_to_file(str(path))

@staticmethod
def deserialize(serialized_keys: Union[Path, bytes]) -> "Keys":
"""
Expand Down

0 comments on commit 5979a08

Please sign in to comment.