// Part of the Concrete Compiler Project, under the BSD3 License with Zama // Exceptions. See // https://github.com/zama-ai/concrete-compiler-internal/blob/master/LICENSE.txt // for license information. #include "CompilerAPIModule.h" #include "concretelang-c/Support/CompilerEngine.h" #include "concretelang/Dialect/FHE/IR/FHEOpsDialect.h.inc" #include "concretelang/Support/Jit.h" #include "concretelang/Support/JitLambdaSupport.h" #include #include #include #include #include #include #include #include #include using mlir::concretelang::CompilationOptions; using mlir::concretelang::JitLambdaSupport; using mlir::concretelang::LambdaArgument; /// Populate the compiler API python module. void mlir::concretelang::python::populateCompilerAPISubmodule( pybind11::module &m) { m.doc() = "Concretelang compiler python API"; m.def("round_trip", [](std::string mlir_input) { return roundTrip(mlir_input.c_str()); }); m.def("terminate_parallelization", &terminateParallelization); pybind11::class_(m, "CompilationOptions") .def(pybind11::init( [](std::string funcname) { return CompilationOptions(funcname); })) .def("set_funcname", [](CompilationOptions &options, std::string funcname) { options.clientParametersFuncName = funcname; }) .def("set_verify_diagnostics", [](CompilationOptions &options, bool b) { options.verifyDiagnostics = b; }) .def("auto_parallelize", [](CompilationOptions &options, bool b) { options.autoParallelize = b; }) .def("loop_parallelize", [](CompilationOptions &options, bool b) { options.loopParallelize = b; }) .def("dataflow_parallelize", [](CompilationOptions &options, bool b) { options.dataflowParallelize = b; }); pybind11::class_( m, "JitCompilationResult"); pybind11::class_(m, "JITLambda"); pybind11::class_(m, "JITLambdaSupport") .def(pybind11::init([](std::string runtimeLibPath) { return jit_lambda_support(runtimeLibPath.c_str()); })) .def("compile", [](JITLambdaSupport_C &support, std::string mlir_program, CompilationOptions options) { return jit_compile(support, mlir_program.c_str(), options); }) .def("load_client_parameters", [](JITLambdaSupport_C &support, mlir::concretelang::JitCompilationResult &result) { return jit_load_client_parameters(support, result); }) .def( "load_server_lambda", [](JITLambdaSupport_C &support, mlir::concretelang::JitCompilationResult &result) { return jit_load_server_lambda(support, result); }, pybind11::return_value_policy::reference) .def("server_call", [](JITLambdaSupport_C &support, concretelang::JITLambda *lambda, clientlib::PublicArguments &publicArguments) { return jit_server_call(support, lambda, publicArguments); }); pybind11::class_( m, "LibraryCompilationResult") .def(pybind11::init([](std::string libraryPath, std::string funcname) { return mlir::concretelang::LibraryCompilationResult{ libraryPath, funcname, }; })); pybind11::class_(m, "LibraryLambda"); pybind11::class_(m, "LibraryLambdaSupport") .def(pybind11::init([](std::string outputPath) { return library_lambda_support(outputPath.c_str()); })) .def("compile", [](LibraryLambdaSupport_C &support, std::string mlir_program, mlir::concretelang::CompilationOptions options) { return library_compile(support, mlir_program.c_str(), options); }) .def("load_client_parameters", [](LibraryLambdaSupport_C &support, mlir::concretelang::LibraryCompilationResult &result) { return library_load_client_parameters(support, result); }) .def( "load_server_lambda", [](LibraryLambdaSupport_C &support, mlir::concretelang::LibraryCompilationResult &result) { return library_load_server_lambda(support, result); }, pybind11::return_value_policy::reference) .def("server_call", [](LibraryLambdaSupport_C &support, serverlib::ServerLambda lambda, clientlib::PublicArguments &publicArguments) { return library_server_call(support, lambda, publicArguments); }); class ClientSupport {}; pybind11::class_(m, "ClientSupport") .def(pybind11::init()) .def_static( "key_set", [](clientlib::ClientParameters clientParameters, clientlib::KeySetCache *cache) { auto optCache = cache == nullptr ? llvm::None : llvm::Optional(*cache); return key_set(clientParameters, optCache); }, pybind11::arg().none(false), pybind11::arg().none(true)) .def_static("encrypt_arguments", [](clientlib::ClientParameters clientParameters, clientlib::KeySet &keySet, std::vector args) { std::vector argsRef; for (auto i = 0u; i < args.size(); i++) { argsRef.push_back(args[i].ptr.get()); } return encrypt_arguments(clientParameters, keySet, argsRef); }) .def_static("decrypt_result", [](clientlib::KeySet &keySet, clientlib::PublicResult &publicResult) { return decrypt_result(keySet, publicResult); }); pybind11::class_(m, "KeySetCache") .def(pybind11::init()); pybind11::class_(m, "ClientParameters"); pybind11::class_(m, "KeySet"); pybind11::class_(m, "PublicArguments"); pybind11::class_(m, "PublicResult"); pybind11::class_(m, "LambdaArgument") .def_static("from_tensor", [](std::vector tensor, std::vector dims) { return lambdaArgumentFromTensorU8(tensor, dims); }) .def_static("from_tensor", [](std::vector tensor, std::vector dims) { return lambdaArgumentFromTensorU16(tensor, dims); }) .def_static("from_tensor", [](std::vector tensor, std::vector dims) { return lambdaArgumentFromTensorU32(tensor, dims); }) .def_static("from_tensor", [](std::vector tensor, std::vector dims) { return lambdaArgumentFromTensorU64(tensor, dims); }) .def_static("from_scalar", lambdaArgumentFromScalar) .def("is_tensor", [](lambdaArgument &lambda_arg) { return lambdaArgumentIsTensor(lambda_arg); }) .def("get_tensor_data", [](lambdaArgument &lambda_arg) { return lambdaArgumentGetTensorData(lambda_arg); }) .def("get_tensor_shape", [](lambdaArgument &lambda_arg) { return lambdaArgumentGetTensorDimensions(lambda_arg); }) .def("is_scalar", [](lambdaArgument &lambda_arg) { return lambdaArgumentIsScalar(lambda_arg); }) .def("get_scalar", [](lambdaArgument &lambda_arg) { return lambdaArgumentGetScalar(lambda_arg); }); }