Package: concrete-ml
📒 Read documentation|💛 Community support|📚 FHE resourcesConcrete ML is a Privacy-Preserving Machine Learning (PPML) open-source set of tools built on top ofConcretebyZama. It simplifies the use of fully homomorphic encryption (FHE) for data scientists to help them automatically turn machine learning models into their homomorphic equivalent. Concrete ML was designed with ease-of-use in mind, so that data scientists can use it without knowledge of cryptography. Notably, the Concrete ML model classes are similar to those in scikit-learn and it is also possible to convert PyTorch models to FHE.Main features.Data scientists can use models with APIs which are close to the frameworks they use, while additional options to those models allow them to run inference or training on encrypted data with FHE.Concrete ML features:built-in models, which are ready-to-use FHE-friendly models with a user interface that is equivalent to their the scikit-learn and XGBoost counterpartssupport for customs models that can use quantization aware training. These are developed by the user using PyTorch or keras/tensorflow and are imported into Concrete ML through ONNXInstallation.Depending on your OS, Concrete ML may be installed with Docker or with pip:OS / HWAvailable on DockerAvailable on pipLinuxYesYesWindowsYesComing soonWindows Subsystem for LinuxYesYesmacOS 11+ (Intel)YesYesmacOS 11+ (Apple Silicon: M1, M2, etc.)YesYesNote: Concrete ML only supports Python3.8,3.9and3.10.Concrete ML can be installed on Kaggle (see question on community for more details) and on Google Colab.DockerTo install with Docker, pull theconcrete-mlimage as follows:docker pull zamafhe/concrete-ml:latestPipTo install Concrete ML from PyPi, run the following:pip install -U pip wheel setuptools
pip install concrete-mlYou can find more detailed installation instructions inthis part of the documentationA simple Concrete ML example with scikit-learn.A simple example which is very close to scikit-learn is as follows, for a logistic regression :fromsklearn.datasetsimportmake_classificationfromsklearn.model_selectionimporttrain_test_splitfromconcrete.ml.sklearnimportLogisticRegression# Lets create a synthetic data-setx,y=make_classification(n_samples=100,class_sep=2,n_features=30,random_state=42)# Split the data-set into a train and test setX_train,X_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)# Now we train in the clear and quantize the weightsmodel=LogisticRegression(n_bits=8)model.fit(X_train,y_train)# We can simulate the predictions in the cleary_pred_clear=model.predict(X_test)# We then compile on a representative setmodel.compile(X_train)# Finally we run the inference on encrypted inputs !y_pred_fhe=model.predict(X_test,fhe="execute")print("In clear  :",y_pred_clear)print("In FHE    :",y_pred_fhe)print(f"Similarity:{int((y_pred_fhe==y_pred_clear).mean()*100)}%")# Output:# In clear  : [0 0 0 0 1 0 1 0 1 1 0 0 1 0 0 1 1 1 0 0]# In FHE    : [0 0 0 0 1 0 1 0 1 1 0 0 1 0 0 1 1 1 0 0]# Similarity: 100%It is also possible to call encryption, model prediction, and decryption functions separately as follows.
Executing these steps separately is equivalent to callingpredict_probaon the model instance.# Predict probability for a single exampley_proba_fhe=model.predict_proba(X_test[[0]],fhe="execute")# Quantize an original float inputq_input=model.quantize_input(X_test[[0]])# Encrypt the inputq_input_enc=model.fhe_circuit.encrypt(q_input)# Execute the linear product in FHEq_y_enc=model.fhe_circuit.run(q_input_enc)# Decrypt the result (integer)q_y=model.fhe_circuit.decrypt(q_y_enc)# De-quantize and post-process the resulty0=model.post_processing(model.dequantize_output(q_y))print("Probability with `predict_proba`: ",y_proba_fhe)print("Probability with encrypt/run/decrypt calls: ",y0)This example is explained in more detail in thelinear model documentation. Concrete ML built-in models
have APIs that are almost identical to their scikit-learn counterparts. It is also possible to convert PyTorch networks to FHE with the Concrete ML conversion APIs. Please refer to thelinear models,tree-based modelsandneural networksdocumentation for more examples, showing the scikit-learn-like API of the built-in
models.Documentation.Full, comprehensive documentation is available here:https://docs.zama.ai/concrete-ml.Online demos and tutorials.Various tutorials are given forbuilt-in modelsand fordeep learningIn addition, several complete use-cases are explored:Encrypted Large Language Model: convert a user-defined part of a Large Language Model for encrypted text generation. Shows the trade-off between quantization and accuracy for text generation and shows how to run the model in FHE.Credit Scoring: predict the chance of a given loan applicant defaulting on loan repayment while keeping the user's data private. Shows how Concrete ML models easily replace their scikit-learn equivalentsHealth diagnosis: based on a patient's symptoms, history and other health factors, give
a diagnosis using FHE to preserve the privacy of the patient.Private inference for federated learned models: private training of a Logistic Regression model and then import the model into Concrete ML and perform encrypted predictionTitanic: solve theKaggle Titanic competition. Implemented with XGBoost from Concrete ML, this example comes as a companion of theKaggle notebook, and was the subject of a blogpost inKDnuggets.Sentiment analysis with transformers: predict if an encrypted tweet / short message is positive, negative or neutral, using FHE. Thelive interactivedemo is available on Hugging Face. Thisblog postexplains how this demo works!CIFAR10 FHE-friendly model with Brevitas: train a VGG9 FHE-compatible neural network using Brevitas, and a script to run the neural network in FHE. Execution in FHE takes ~4 minutes per image and shows an accuracy of 88.7%.CIFAR10 / CIFAR100 FHE-friendly models with Transfer Learning approach: series of three notebooks, that convert a pre-trained FP32 VGG11 neural network into a quantized model using Brevitas. The model is fine-tuned on the CIFAR data-sets, converted for FHE execution with Concrete ML and evaluated using FHE simulation. For CIFAR10 and CIFAR100, respectively, our simulations show an accuracy of 90.2% and 68.2%.FHE neural network splitting for client/server deployment: explains how to split a computationally-intensive neural network model in two parts. First, we execute the first part on the client side in the clear, and the output of this step is encrypted. Next, to complete the computation, the second part of the model is evaluated with FHE. This tutorial also shows the impact of FHE speed/accuracy trade-off on CIFAR10, limiting PBS to 8-bit, and thus achieving 62% accuracy.Encrypted image filtering: filter encrypted images by applying filters such as black-and-white, ridge detection, or your own filter.If you have built awesome projects using Concrete ML, feel free to let us know and we'll link to them!ContributingTo contribute to Concrete ML, please refer to thissection of the documentation.Citing Concrete MLTo cite Concrete ML, notably in academic papers, please use the following entry, which list authors by order of first commit:@Misc{ConcreteML,
  title={Concrete {ML}: a Privacy-Preserving Machine Learning Library using Fully Homomorphic Encryption for Data Scientists},
  author={Zama},
  year={2022},
  note={\url{https://github.com/zama-ai/concrete-ml}},
}Need support?License.This software is distributed under the BSD-3-Clause-Clear license. If you have any questions, please contact us athello@zama.ai.