diff --git a/.ipynb_checkpoints/gizaTest1-checkpoint.ipynb b/.ipynb_checkpoints/gizaTest1-checkpoint.ipynb new file mode 100644 index 0000000..81832cc --- /dev/null +++ b/.ipynb_checkpoints/gizaTest1-checkpoint.ipynb @@ -0,0 +1,739 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "afb5d31a-296a-4fb1-90ac-f9a2136e27a4", + "metadata": {}, + "source": [ + "### testing the XGBoost Diabetes example to transpile\n", + "##### used conda env giza from ll laptop" + ] + }, + { + "cell_type": "markdown", + "id": "78186bb5-98ce-471c-93d3-ca19386ca744", + "metadata": {}, + "source": [ + "## Create and Train an XGBoost Model\n", + "### We'll start by creating a simple XGBoost model using Scikit-Learn and train it on diabetes dataset" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5650a200-f157-4c3d-b352-282380f05abd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
XGBRegressor(base_score=None, booster=None, callbacks=None,\n",
+       "             colsample_bylevel=None, colsample_bynode=None,\n",
+       "             colsample_bytree=None, device=None, early_stopping_rounds=None,\n",
+       "             enable_categorical=False, eval_metric=None, feature_types=None,\n",
+       "             gamma=None, grow_policy=None, importance_type=None,\n",
+       "             interaction_constraints=None, learning_rate=None, max_bin=None,\n",
+       "             max_cat_threshold=None, max_cat_to_onehot=None,\n",
+       "             max_delta_step=None, max_depth=6, max_leaves=None,\n",
+       "             min_child_weight=None, missing=nan, monotone_constraints=None,\n",
+       "             multi_strategy=None, n_estimators=2, n_jobs=None,\n",
+       "             num_parallel_tree=None, random_state=None, ...)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "XGBRegressor(base_score=None, booster=None, callbacks=None,\n", + " colsample_bylevel=None, colsample_bynode=None,\n", + " colsample_bytree=None, device=None, early_stopping_rounds=None,\n", + " enable_categorical=False, eval_metric=None, feature_types=None,\n", + " gamma=None, grow_policy=None, importance_type=None,\n", + " interaction_constraints=None, learning_rate=None, max_bin=None,\n", + " max_cat_threshold=None, max_cat_to_onehot=None,\n", + " max_delta_step=None, max_depth=6, max_leaves=None,\n", + " min_child_weight=None, missing=nan, monotone_constraints=None,\n", + " multi_strategy=None, n_estimators=2, n_jobs=None,\n", + " num_parallel_tree=None, random_state=None, ...)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import xgboost as xgb\n", + "from sklearn.datasets import load_diabetes\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "data = load_diabetes()\n", + "X, y = data.data, data.target\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", + "\n", + "# Increase the number of trees and maximum depth\n", + "n_estimators = 2 # Increase the number of trees\n", + "max_depth = 6 # Increase the maximum depth of each tree\n", + "\n", + "xgb_reg = xgb.XGBRegressor(n_estimators=n_estimators, max_depth=max_depth)\n", + "xgb_reg.fit(X_train, y_train)" + ] + }, + { + "cell_type": "markdown", + "id": "10569dc0-0494-4370-b84f-365d4a18dfd3", + "metadata": {}, + "source": [ + "## Save the model\n", + "### Save the model in Json format" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "abe18478-37c7-4b40-b549-8e8dd3324c0e", + "metadata": {}, + "outputs": [], + "source": [ + "from giza.zkcook import serialize_model\n", + "serialize_model(xgb_reg, \"xgb_diabetes.json\")" + ] + }, + { + "cell_type": "markdown", + "id": "becd66e2-de26-4358-adcc-fc058a6698e8", + "metadata": {}, + "source": [ + "## Transpile your model to Orion Cairo\n", + "### We will use Giza-CLI to transpile our saved model to Orion Cairo." + ] + }, + { + "cell_type": "markdown", + "id": "31c49d75-4ff7-4b3b-b4f3-8fe09c06dd34", + "metadata": {}, + "source": [ + "\n", + "$ giza transpile xgb_diabetes.json --output-path xgb_diabetes\n", + "[giza][2024-05-29 23:43:03.612] No model id provided, checking if model exists ✅\n", + "[giza][2024-05-29 23:43:03.621] Model name is: xgb_diabetes\n", + "[giza][2024-05-29 23:43:04.421] Model Created with id -> 665! ✅\n", + "[giza][2024-05-29 23:43:05.200] Version Created with id -> 1! ✅\n", + "[giza][2024-05-29 23:43:05.208] Sending model for transpilation ✅\n", + "[giza][2024-05-29 23:43:16.618] Transpilation is fully compatible. Version compiled and Sierra is saved at Giza ✅\n", + "[giza][2024-05-29 23:43:17.750] Downloading model ✅\n", + "[giza][2024-05-29 23:43:17.758] model saved at: xgb_diabetes\n", + "(giza3)" + ] + }, + { + "cell_type": "markdown", + "id": "bd00d799-97cb-44a2-bde5-03a433b5f507", + "metadata": {}, + "source": [ + "## Deploy an inference endpoint\n", + "### Now that our model is transpiled to Cairo we can deploy an endpoint to run verifiable inferences. We will use Giza CLI again to run and deploy an endpoint. Ensure to replace model-id and version-id with your ids provided during transpilation.\n" + ] + }, + { + "cell_type": "markdown", + "id": "f989d383-1aaf-4bf1-b6be-8fbb63e61e1c", + "metadata": {}, + "source": [ + "giza endpoints deploy --model-id 665 --version-id 1\n", + "\n", + "$ giza endpoints deploy --model-id 665 --version-id 1\n", + "▰▱▱▱▱▱▱ Creating endpoint!\n", + "[giza][2024-05-29 23:53:51.319] Endpoint is successful ✅\n", + "[giza][2024-05-29 23:53:51.335] Endpoint created with id -> 234 ✅\n", + "[giza][2024-05-29 23:53:51.346] Endpoint created with endpoint URL: https://endpoint-giza1-665-1-7ee56cfd-7i3yxzspbq-ew.a.run.app 🎉\n", + "(giza3)" + ] + }, + { + "cell_type": "markdown", + "id": "377dcf19-aff5-456a-9776-bb30dcc7f954", + "metadata": {}, + "source": [ + "## Run a verifiable inference\n", + "##### To streamline verifiable inference, you might consider using the endpoint URL obtained after transpilation. However, this approach requires manual serialization of the input for the Cairo program and handling the deserialization process. To make this process more user-friendly and keep you within a Python environment, we've introduced a Python SDK designed to facilitate the creation of ML workflows and execution of verifiable predictions. When you initiate a prediction, our system automatically retrieves the endpoint URL you deployed earlier, converts your input into Cairo-compatible format, executes the prediction, and then converts the output back into a numpy object. " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "5dbf104f-5868-4084-a496-321ae40e4aee", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\chuck.raghavan\\AppData\\Roaming\\Python\\Python311\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🚀 Starting deserialization process...\n", + "✅ Deserialization completed! 🎉\n", + "Predicted value for input 0.09256398319871433 is 175.58781\n", + "Proof ID: 10c164e6c2364ab6b5491702127979a6\n" + ] + } + ], + "source": [ + "import xgboost as xgb\n", + "from sklearn.datasets import load_diabetes\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "from giza.agents.model import GizaModel\n", + "\n", + "\n", + "MODEL_ID = 665 # Update with your model ID\n", + "VERSION_ID = 1 # Update with your version ID\n", + "\n", + "def prediction(input, model_id, version_id):\n", + " model = GizaModel(id=model_id, version=version_id)\n", + "\n", + " (result, proof_id) = model.predict(\n", + " input_feed={\"input\": input}, verifiable=True, model_category=\"XGB\"\n", + " )\n", + "\n", + " return result, proof_id\n", + "\n", + "\n", + "def execution():\n", + " # The input data type should match the model's expected input\n", + " input = X_test[1, :]\n", + "\n", + " (result, proof_id) = prediction(input, MODEL_ID, VERSION_ID)\n", + "\n", + " print(f\"Predicted value for input {input.flatten()[0]} is {result}\")\n", + "\n", + " return result, proof_id\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + " data = load_diabetes()\n", + " X, y = data.data, data.target\n", + "\n", + " X_train, X_test, y_train, y_test = train_test_split(\n", + " X, y, test_size=0.2, random_state=42\n", + " )\n", + " _, proof_id = execution()\n", + " print(f\"Proof ID: {proof_id}\")" + ] + }, + { + "cell_type": "markdown", + "id": "7b44f1d7-6254-4380-aaa4-ecd141fc17e7", + "metadata": {}, + "source": [ + "## Download the proof\n", + "#### Initiating a verifiable inference sets off a proving job on our server, sparing you the complexities of installing and configuring the prover yourself. Upon completion, you can download your proof.\n", + "\n", + "First, let's check the status of the proving job to ensure that it has been completed." + ] + }, + { + "cell_type": "markdown", + "id": "f25f8d23-0d83-412a-9e3b-fca989972f0a", + "metadata": {}, + "source": [ + "$ giza endpoints get-proof --endpoint-id 234 --proof-id \"10c164e6c2364ab6b5491702127979a6\"\n", + "[giza][2024-05-30 00:40:39.691] Getting proof from endpoint 234 ✅\n", + "{\n", + " \"id\": 967,\n", + " \"job_id\": 1121,\n", + " \"metrics\": {\n", + " \"proving_time\": 17.249508\n", + " },\n", + " \"created_date\": \"2024-05-30T07:33:12.532659\"\n", + "}" + ] + }, + { + "cell_type": "markdown", + "id": "4f427ceb-61d7-4ffc-ba4f-15d4dad1ec2c", + "metadata": {}, + "source": [ + "Once the proof is ready, you can download it." + ] + }, + { + "cell_type": "markdown", + "id": "43c280b5-8b29-4a7d-8b0e-a5b90e408852", + "metadata": {}, + "source": [ + "$ giza endpoints download-proof --endpoint-id 234 --proof-id \"10c164e6c2364ab6b5491702127979a6\" --output-path zk_xgboost.proof\n", + "[giza][2024-05-30 00:51:52.048] Getting proof from endpoint 234 ✅\n", + "[giza][2024-05-30 00:51:53.800] Proof downloaded to zk_xgboost.proof ✅\n", + "(giza3)" + ] + }, + { + "cell_type": "markdown", + "id": "a36fb812-b3ef-4c4e-a142-c3c9263be989", + "metadata": {}, + "source": [ + "## Verify the proof\n", + "#### Finally, you can verify the proof." + ] + }, + { + "cell_type": "markdown", + "id": "139ac5f2-0bd8-40d7-88c3-75e162febfb8", + "metadata": {}, + "source": [ + "$ giza verify --proof-id 967\n", + "[giza][2024-05-30 00:56:05.847] Verifying proof...\n", + "[giza][2024-05-30 00:56:07.140] Verification result: True\n", + "[giza][2024-05-30 00:56:07.145] Verification time: 0.454667226\n", + "(giza3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8859806a-01d8-40d9-a445-cbe3c8a733fd", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "giza3", + "language": "python", + "name": "giza3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Gizathon-Template-ETF-PCI-Prediction-For-Portfolio-Balancing-Sathya-Chakravarti-V2.docx b/Gizathon-Template-ETF-PCI-Prediction-For-Portfolio-Balancing-Sathya-Chakravarti-V2.docx new file mode 100644 index 0000000..86bc518 Binary files /dev/null and b/Gizathon-Template-ETF-PCI-Prediction-For-Portfolio-Balancing-Sathya-Chakravarti-V2.docx differ diff --git a/Gizathon-Template-ETF-PCI-Prediction-For-Portfolio-Balancing-Sathya-Chakravarti.docx b/Gizathon-Template-ETF-PCI-Prediction-For-Portfolio-Balancing-Sathya-Chakravarti.docx new file mode 100644 index 0000000..0988009 Binary files /dev/null and b/Gizathon-Template-ETF-PCI-Prediction-For-Portfolio-Balancing-Sathya-Chakravarti.docx differ diff --git a/GizathonHackathon.md b/GizathonHackathon.md new file mode 100644 index 0000000..bfa4e95 --- /dev/null +++ b/GizathonHackathon.md @@ -0,0 +1,1541 @@ +# GizaHackathon + +https://x.com/gizatechxyz + +https://docs.gizatech.xyz/ + +https://github.com/gizatechxyz/ + + + +Code when registered. + +**bq6uees282cg8gx41gbq6ueeqeuoktjv** + +## Day 1 - 5/20/24 - Welcome to Gizathon Agents + +https://www.youtube.com/watch?v=U_E_gdeg_uc&t=3137s + + + + + +i asked question: + +https://docs.gizatech.xyz/tutorials/ai-agents + + + + + +![image-20240520100825088](./Images/image-20240520100825088.png) + + + + + +![image-20240520101119441](./Images/image-20240520101119441.png) + + + + + + + + + +![image-20240520101328479](./Images/image-20240520101328479.png) + + + + + + + + + + + +![image-20240520101659807](./Images/image-20240520101659807.png) + + + +![image-20240525141136902](./Images/image-20240525141136902.png) + + + +![image-20240520101905906](./Images/image-20240520101905906.png) + + + + + + + + + +​ ![image-20240520101937506](./Images/image-20240520101937506.png) + +![image-20240520102237619](./Images/image-20240520102237619.png) + + + + + + + + + +![image-20240520102247386](./Images/image-20240520102247386.png) + + + + + +![image-20240520102453489](./Images/image-20240520102453489.png) + + + + + +![image-20240520102649162](./Images/image-20240520102649162.png) + + + + + + + +![image-20240520102918909](./Images/image-20240520102918909.png) + + + +one way to think about trust-minimized ML use-cases in the financial sector is ML as gating logic. Currently in web3 we have radical permissionlessness which makes it very hard for decentralized systems to discern "qualities" of entities which they interact with (users, assets etc). with ML you are able to provide a trust minimized mechanism to overcome this homogeneity. + +C + +Cem F10:32 AM + +the use-cases on the other side are: automated risk budgeted digital assets, decentralized insurance, disintermediated KYC (know-your-peer?) etc. to your question ML serves a key function to provide a "world-model" to decentralized applications (awareness of "reality"). + +this is obv useful beyond finance (for instance decentralized social, recommender systems, decentralized content moderation etc) + + + +Make sure you have data and reliable. + + + +look for what has public value; not trading bot; + +Finding the right models of participation. Right gating criteria. + + + +Are there any examples of use cases with real world assets? -- Not yet. + + + + + +We are the first hackers!! + +as tips! for easy data access when you build your Agents: + +\- Datasets: https://docs.gizatech.xyz/products/datasets/hub +\- Model Complexity Reducer: https://docs.gizatech.xyz/tools/auto-zkml + + + +we are the first users touching this product. + + + +https://x.com/gizatechxyz + + + +### Good info + +#### Renç | Giza *—* 5/20/24 -Today at 10:51 AM + +Sharing all the relevant links we discussed during the first workshop! + +- **Agent Tutorials** (https://docs.gizatech.xyz/tutorials/ai-agents): How to build an agent, step by step +- **Giza Datasets** (https://docs.gizatech.xyz/products/datasets/hub): ML datasets that you can build and train your models with for web3 agents +- **Model Complexity Reducer** (https://www.gizatech.xyz/collection/maximizing-efficiency-with-model-complexity-reducer-(mcr)): The MCR package provides a robust solution for reducing model complexity while maintaining high performance +- **Giza Twitter** (https://x.com/gizatechxyz): We want to give what you build in the next weeks as much as visibility as possible, mention us on Twitter and tell us what you are building, we will amplify on our channels + + + +### important points - + +- they have some data sets - so we can use that instead of getting our own data?? +- train your model - easiest part - automated tools. +- Intent - integration module for initial experience. + + + +- leverage their automated tool - best parameters and verifiable proof. + + + +Q - what are the best use cases - + +- Financial products, structured smart assets - rebalance themselves. + - Uniswap agent - provides impermanent loss protected account for its users. At the model side it has volatality prediction model & on strategy side it tells if predictoin is above certain threshold, rebalance my liquidity pool to protect me from impermanant loss. + - Working on Startnet protocol - yield aggregator where on its core model it has a yield forecasting model where machine leraning model to predict yield in each protocol across all landing protocols on starknet and has an allocation engine that basically combines current yield data and the model prediction to determine the most optimal asset allcation across the various yield avenues. Maximises yield and reduces Txn cost and manages risk efficiently. + - also in diff derivatives area. buy future gas contracts. market make gas market. + - arbitrage agents + - decentralized social media as example -- farcaster and lens protocol and we can have verifiable... bot indentification and cleanse the platform autonomously. + + + +make sure you data available. meaningful data. + +Intent creation and smart contract creation. LP rebalancing; + + + +needs to have public value. +if you create trading bot to get most - you dont want that public. So not good use case. + +Instead create something of public value. + + + +there are no real world asset cases as of now. + +we are the 1st developers to do this. + +1st developers to even touch this product. + +we are all Giza brains. + + + +## Day 2- 5/22/24 - Full Stack Giza Workshop + + + +https://www.youtube.com/watch?v=BHdP5oSISQE + + + +Timestamps: + +[00:00:00](https://www.youtube.com/watch?v=BHdP5oSISQE&t=0s) Introduction + +[00:02:57](https://www.youtube.com/watch?v=BHdP5oSISQE&t=177s) Giza’s Products + +[00:05:26](https://www.youtube.com/watch?v=BHdP5oSISQE&t=326s) Giza Datasets + +[00:12:42](https://www.youtube.com/watch?v=BHdP5oSISQE&t=762s) ZKML Giza Platform + +[00:14:54](https://www.youtube.com/watch?v=BHdP5oSISQE&t=894s) XGBoost + +[00:31:25](https://www.youtube.com/watch?v=BHdP5oSISQE&t=1885s) Q&A + + [00:45:40](https://www.youtube.com/watch?v=BHdP5oSISQE&t=2740s) Doc Walkaround + +[00:48:53](https://www.youtube.com/watch?v=BHdP5oSISQE&t=2933s) Conclusion + + + +https://docs.gizatech.xyz/ + + + +### 3 main products + +https://docs.gizatech.xyz/products/all-products + +![image-20240525115811302](./Images/image-20240525115811302.png) + +#### Platform + +To get ZK proof. + +#### AI Agent + +Bridge between zero knowledge machine learning to smart contracts. + +#### DataSets + +curated library for Web3. + + + +### Datasets + + + +https://docs.gizatech.xyz/products/datasets + +takes more than 1/2 of the time to get data. + +right data, right format etc. 1/2 the time. + +web3 data - challenges - relatively difficult to collect that. you need to know how blockchain works, rpc, smart contracts etc. + +Giza datasets is low hanging fruit. valuable tool. + + + +history - all chats if its social platform. etc. + +SDK is ready to run in python. + +#### dataset hub + +![image-20240525120809497](./Images/image-20240525120809497.png) + + + + + +dataset hub and then loader. + +#### Dataset Loader + + + +![image-20240525120851866](./Images/image-20240525120851866.png) + + + +![image-20240525121040580](./Images/image-20240525121040580.png) + + + +liquidity. + + + +![image-20240525121117861](./Images/image-20240525121117861.png) + + + +#### Polar vs Panda + + + + + +#### certifi + +![image-20240525121221062](./Images/image-20240525121221062.png) + + + + + + + +![image-20240525121305446](./Images/image-20240525121305446.png) + + + +#### Eager mode + + + +#### Cache management + + + +### Hub + + + +![image-20240525121429280](./Images/image-20240525121429280.png) + + + + + + + +### Platform + + + +#### install + +https://docs.gizatech.xyz/tutorials/zkml/verifiable-xgboost + + + +![image-20240525122040197](./Images/image-20240525122040197.png) + + + +##### install giza cli + +then install giza agent. + + + + + +https://docs.gizatech.xyz/welcome/installation + +install instrutions + +![image-20240525122201291](./Images/image-20240525122201291.png) + + + + + +##### then create user account + +##### api-key + +its optional but highly recommended. + + + +main idea of workshop - + +Train a simple XGBoost model on the diabetes data set. + +then we will see how we transpike that into ZK circuit. + +Then how to deploy an inference and run a verifiable inference. + + + + + +#### Create and Train an XGBoost Model + + + +https://docs.gizatech.xyz/tutorials/zkml/verifiable-xgboost + +```python +import xgboost as xgb +from sklearn.datasets import load_diabetes +from sklearn.model_selection import train_test_split + +data = load_diabetes() +X, y = data.data, data.target + +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Increase the number of trees and maximum depth +n_estimators = 2 # Increase the number of trees +max_depth = 6 # Increase the maximum depth of each tree + +xgb_reg = xgb.XGBRegressor(n_estimators=n_estimators, max_depth=max_depth) +xgb_reg.fit(X_train, y_train) +``` + + + +#### Save the model + +```python +from giza.zkcook import serialize_model +serialize_model(xgb_reg, "xgb_diabetes.json") +``` + + + +#### Transpile your model to Orion Cairo + +We will use Giza-CLI to transpile our saved model to Orion Cairo. + + + +```bash +! giza transpile xgb_diabetes.json --output-path xgb_diabetes + +>>>> +[giza][2024-05-10 17:14:48.565] No model id provided, checking if model exists ✅ +[giza][2024-05-10 17:14:48.567] Model name is: xgb_diabetes +[giza][2024-05-10 17:14:49.081] Model already exists, using existing model ✅ +[giza][2024-05-10 17:14:49.083] Model found with id -> 588! ✅ +[giza][2024-05-10 17:14:49.777] Version Created with id -> 2! ✅ +[giza][2024-05-10 17:14:49.780] Sending model for transpilation ✅ +[giza][2024-05-10 17:15:00.670] Transpilation is fully compatible. Version compiled and Sierra is saved at Giza ✅ +⠙ Transpiling Model... +[giza][2024-05-10 17:15:01.337] Downloading model ✅ +[giza][2024-05-10 17:15:01.339] model saved at: xgb_diabetes +``` + + + +creates instance of model ID and instance of your version. + +model ID is 588. Version here is 2. + +i think transpiles to Cairo version of XGBoost. + +`scab` is the project mgr of Cairo. + + + +![image-20240525124048894](./Images/image-20240525124048894.png) + + + +we also compiled your Cairo version of the project as intermediary representation of Cairo. + + + +##### 3 platforms support + +platform supports 3 kinds of models / frameworks currently to transpile to ZK Circuits. + +XGBoost + +LightGBM + +ONNX - deep learning focused model. + +if you do on PyTorch or TensorFlow you can easily convert to onnx and based on that graph transpile from our platform. + +##### limitations + +https://docs.gizatech.xyz/products/platform/model-compatibility + +ONNX is large framework. + +big graphs big model. - can become too big. + +doc has limitations section + +Highly recommend XGBoost + + + +now we are ready to deploy and verify your inference. + + + +#### Deploy an inference endpoint + +Now that our model is transpiled to Cairo we can deploy an endpoint to run verifiable inferences. We will use Giza CLI again to run and deploy an endpoint. Ensure to replace `model-id` and `version-id` with your ids provided during transpilation. + + + +```bash +! giza endpoints deploy --model-id 588 --version-id 2 + +>>>> +▰▰▰▰▰▰▰ Creating endpoint!t! +[giza][2024-05-10 17:15:21.628] Endpoint is successful ✅ +[giza][2024-05-10 17:15:21.635] Endpoint created with id -> 190 ✅ +[giza][2024-05-10 17:15:21.636] Endpoint created with endpoint URL: https://endpoint-raphael-doukhan-588-2-72c9b3b8-7i3yxzspbq-ew.a.run.app 🎉 +``` + + + +you get URL - where when we do a Curl request it will run the model - cairo prigram and in parallel start proving job that will run and prove your program. + +For better UX its better create workflow and use directly in your python code. So we created an SDK for that. + + + +#### Run a verifiable inference + +To streamline verifiable inference, you might consider using the endpoint URL obtained after transpilation. However, this approach requires manual serialization of the input for the Cairo program and handling the deserialization process. To make this process more user-friendly and keep you within a Python environment, we've introduced a Python SDK designed to facilitate the creation of ML workflows and execution of verifiable predictions. When you initiate a prediction, our system automatically retrieves the endpoint URL you deployed earlier, converts your input into Cairo-compatible format, executes the prediction, and then converts the output back into a numpy object + +```python +import xgboost as xgb +from sklearn.datasets import load_diabetes +from sklearn.model_selection import train_test_split + +from giza.agents.model import GizaModel + + +MODEL_ID = 588 # Update with your model ID +VERSION_ID = 2 # Update with your version ID + +def prediction(input, model_id, version_id): + model = GizaModel(id=model_id, version=version_id) + + (result, proof_id) = model.predict( + input_feed={"input": input}, verifiable=True, model_category="XGB" + ) + + return result, proof_id + + +def execution(): + # The input data type should match the model's expected input + input = X_test[1, :] + + (result, proof_id) = prediction(input, MODEL_ID, VERSION_ID) + + print(f"Predicted value for input {input.flatten()[0]} is {result}") + + return result, proof_id + + +if __name__ == "__main__": + data = load_diabetes() + X, y = data.data, data.target + + X_train, X_test, y_train, y_test = train_test_split( + X, y, test_size=0.2, random_state=42 + ) + _, proof_id = execution() + print(f"Proof ID: {proof_id}") +``` + + + +```bash +🚀 Starting deserialization process... +✅ Deserialization completed! 🎉 +(175.58781, '546f8817fa454db78982463868440e8c') +``` + + + +returns request ID that can be used to download your proof. + + + +this predict function predicts an inference. + + + +##### `verifiable=True` + +this will actually do the proof part. + +for development experience its better sometimes dont start proving job each time. + +why - you are limited in times proving jobs you can start in a day etc. + + + +https://docs.gizatech.xyz/products/platform/known-limitations + + + +![image-20240525125419274](./Images/image-20240525125419274.png) + + + + + +##### Dry run parameter + +![image-20240525125657865](./Images/image-20240525125657865.png) + + + +will also NOT start the proving job. + + + + + +#### Download the proof + + + +(175.58781, '546f8817fa454db78982463868440e8c') + +returns request ID that can be used to download your proof. + + + +Initiating a verifiable inference sets off a proving job on our server, sparing you the complexities of installing and configuring the prover yourself. Upon completion, you can download your proof. + +First, let's check the status of the proving job to ensure that it has been completed. + + + +Remember to substitute `endpoint-id` and `proof-id` with the specific IDs assigned to you throughout this tutorial. + +```bash +$ giza endpoints get-proof --endpoint-id 190 --proof-id "546f8817fa454db78982463868440e8c" + +>>> +[giza][2024-03-19 11:51:45.470] Getting proof from endpoint 190 ✅ +{ + "id": 664, + "job_id": 831, + "metrics": { + "proving_time": 15.083126 + }, + "created_date": "2024-03-19T10:41:11.120310" +} +``` + +Once the proof is ready, you can download it. + +```bash +$ giza endpoints download-proof --endpoint-id 190 --proof-id "546f8817fa454db78982463868440e8c" --output-path zk_xgboost.proof + +>>>> +[giza][2024-03-19 11:55:49.713] Getting proof from endpoint 190 ✅ +[giza][2024-03-19 11:55:50.493] Proof downloaded to zk_xgboost.proof ✅ +``` + +Better to surround the proof-id in double quotes (") when using the alphanumerical id + + + + + +![image-20240525130321177](./Images/image-20240525130321177.png) + + + +#### Verify the proof + +Platinum prover. + +or verify with Giza verifiable Api. + +Finally, you can verify the proof. + +Copy + +``` +$ giza verify --proof-id 664 + +>>>> +[giza][2024-05-21 10:08:59.315] Verifying proof... +[giza][2024-05-21 10:09:00.268] Verification result: True +[giza][2024-05-21 10:09:00.270] Verification time: 0.437505093 +``` + + + +Q - what is the proof + +A - Cairo proves we executed the program correctly. you did not use anotehr model or bias the result etc. Smart contract can trust you. + +proof is NOT done onchain. Only done offChain. Right now its verified offchain but in future we will have verifier onChain. + +Prover but still is offChain to save computation. + +in the ZKML proof has the program hash, so verify can agree. + + + + + +![image-20240525130917217](./Images/image-20240525130917217.png) + + + + + +![](./Images/image-20240525130942905-1716667783593-1.png) + + + +How to integrate Agent and smart contract - session tomorrow. + + + +Q - do the input arguments for the virifiable inference remain private? + +A - For the moment we dont handle private inputs. Plan around August. we need to support Noir . now we are Cairo. Cairo hints we can do some privacy. But right now dont. + + + +we dont support YAMA + +Big models will fail right now. + +Right now best to use XGBoost. + + + + + +in 2 months we will add OnChain verification. + + + +### Reduce complexity + + + +#### MCR model + +https://docs.gizatech.xyz/tools/zkcook + + + +https://github.com/gizatechxyz/zkcook/blob/main/tutorials/reduce_model_complexity.ipynb + + + + + +complexity reducer. + +you pass your model to mcr and it transforms i think. + +```python +model, transformer = mcr(model = lgbm_reg, + X_train = X_train, + y_train = y_train, + X_eval = X_test, + y_eval = y_test, + eval_metric = 'rmse', + transform_features = True) +``` + + + +we encourage you to use MCR. + +Cairo is adding lot of stuff on top. Thats why right now our models need to be smaller, something like that. + +Due to proving being added. + + + +### Benchmark + +how much to run , prove etc. + +https://docs.gizatech.xyz/tools/benchmark-cli + + + +![image-20240525134800752](./Images/image-20240525134800752.png) + + + + + +### QuickStart + +highly recommend start from here. + +https://docs.gizatech.xyz/welcome/quickstart + + + + + +## Session 3- 5/22/24 - Mental Models for ZKML and its Applications + + + +![image-20240525141725467](./Images/image-20240525141725467.png) + + + + + + + +![image-20240525141746844](./Images/image-20240525141746844.png) + + + +![image-20240525141819962](./Images/image-20240525141819962.png) + + + + + +#### Deterministic. + +![image-20240525141836925](./Images/image-20240525141836925.png) + + + +just pipeline of operations. + +Input - high dimensional tensor or array. + +you feed into this model that does different matrix sort of operations until it gets ouput. + +Very simplistic definition. + +there are temporal and attention models do little differently. + + + +#### Non-deterministic + +![image-20240525142348171](./Images/image-20240525142348171.png) + +Non-deterministic is LLM models - ChatGpt. you get different models. + +but its not actually non-deterministic - it has random sample from a deterministic distribution of outputs. + +if you give 1000s of prompts you will see some with same output. So output is not deterministic but underlying distribution is. + +but ZKML is deterministic. + + + +#### how to Prove + +How we can do that? + +we are proving this program/model. + +Cairo + +![image-20240525142544072](./Images/image-20240525142544072.png) + +Cairo is a generalized turing complete provable language developed by Starkware. + +On top of Cairo we have our own ZKML library which is called Orion which used its own numbers, tensors, matric operations etc. + +we are not doing proof of training. but in future will be important. + + + + + +![image-20240525142948742](./Images/image-20240525142948742.png) + + + +some models are either blacklisted or whitelisted. + +Credit scoring - you want to prove you are not manipulating the results. So you could have a model that's subscribed to a system & you need to prove to users or regulatory branch that you are using this model and correct model. + +![image-20240525143244923](./Images/image-20240525143244923.png) + + + +Since its ZKML proof you can either not show the model parameters and/or dont want to show the input itself. You have the freedom to do show. + + + +AI Competitions is example. + +i dont want to share my model parameters. + +Same finance you dont to share the entire input or the proprietary input. + + + +![image-20240525143545880](./Images/image-20240525143545880.png) + + + +Proof generation is more complex than Inference or more computationally expensive is another way to describe it. + +Which is much more expensive than proof verification. + +![image-20240525143932690](./Images/image-20240525143932690.png) + + + + + + + +![image-20240525143944209](./Images/image-20240525143944209.png) + + + +So proof Generation you do on Cloud where computation is cheap. and then you send the proof where computation is expensive. + +Turns out entire system is cheaper. + + + + + +![image-20240525144141254](./Images/image-20240525144141254.png) + + + +Blockchains are expensive. because ML are complex computations. + +IoT network, - ML models running there are expensive. + +Instead we only send proof there and verification is cheaper. + + + + + +![image-20240525144308860](./Images/image-20240525144308860.png) + + + +allows to host that service. + +Agent is protocol agnostic. + + + +model owner can be different , agent owner can be different and protocol can be different. + +you can recursively improve the model and generate new data. + + + +Q - where are the agents running? + +A - essentially running on Google cloud. but you can run whereever you want. But right now you are running on our google cloud. + +Agent - you ask what's best restaurant in Berlin and it answers but also books reservation. Does some action. + +Giza - we are not talking about LLM but ZKML agent. + +So the agent can be used not just model owner. but anyone can use to perform tasks from Agent knowing its verifiable. Acts onChain only when its verified. + + + + + + + + + +## Session 4 - 5/22/24 Fundamentals of On-chain Agents + + + + + +https://github.com/gizatechxyz/Giza-Hub/blob/main/awesome-giza-agents/uni-v3-lp-agent/model_training.py + + + +https://github.com/gizatechxyz/Giza-Hub/blob/main/awesome-giza-agents/uni-v3-lp-agent/action_agent.py + + + +45:00 - you can have agent interact with any blockchain. its chain agnostic. + +we cant run LLMs agent. - too large. + + + + + +## Session 5 - 5/24/24 Agents Gizathon Partner Panel + + + + + +### ZkSynch; + +Vasillis - + +most established L2. + +proud of some of native research we have - + +pay master and account abstraction - + +- agents can tap into those and become more user friendly and more intentional + + + + + + + +https://encodeclub.notion.site/Sponsor-Resources-07acb0201a5b4b3ab3a505070e897582 + + + +### Starkware; + +Lana - + +is behind the Stark protocol; heavily used by companies that scale blockchain. + +Language used is Cairo. what is used by Giza models and agents. + +Cairo uses stark protocol under the cover. that allows to scale any computation. + +real world assets. It need not be all DeFi. you can measure noise pollution, temperature. + + + +### Lambda; + +Nick- + +we are infrastructure providers. + +https://lambda.p2p.org/ + +we run the nodes that people delegate. + +multi chain analytics system. + +We are trying to support use case - Staking data. we have data set with performance of operators with rewards and they suggest you to predict automating decision to about delegating one of them. + +LST protocols and bring more risk. so how to predict. + +They will help with domain knowledge. + + + +### Linea + +Emily + +Evm equivalent ZK rollup bootstrapped by Consensus. + +started as research project - at consensus. They own Metamask. + +Bezu - of the ETH client is built inhouse at Consensus. + +Linea is only multi-client L2. + +general purpose chain. + +bounty is more creative. + +Social & gaming? how does AI fit into. + + + +### Enzyme; + +Luca - + +leading protocol in oncHain asset mgmt; used to be known as Melanport. + +Disrupt fund mgmt industry & bring it onChain. + +one of the use case- + +Definitely automation comes up a lot. + +more open Canvas. + + + + + +Cem Dagdelen (Giza) + + + + + +vasillis - + +Try to be creative. try to break the rules. + +Agent - 1st philosophically and then technology. + +right place to experiment. + + + +Lana - + +zoom out. Prove-able code. + +current apps dont have integrity. no accountabilty. With stack we have - giza, cairo, Zk tech, we can demand accountability from ML; + +Value of ZL/ML for the bounty. + + + +Luca - + +you cna create a vault on enzyme that has ability to delegate mgmt to 3rd party who is a human but here can be delegating to the bot. owner of vault can confidently delegate permissions. experiment with asset mgmt. + +Asset mgmt personal portfolio or community of people. + +Imagine people inputing like Text instruction like the automation and then the AI collects and re-elaborates many different input in sort of social environment and turns that into some of action for benefit of community. + +ex sensin twitter and doing something. + +tomorrow real estate, real world assets. + +Composed of diff tokenized - set of -- + +best onChain Automated mgmt. + +me - basket like S&P within the chain. + + + +### Native use Case - + + + +Lana - ex bridge hack was suspected and they stopped the malicious Txn - censored. Can we do that with provability. + + + + + + + +## My idea - + +S&P basket of tokens - + +User just says level 1-10. Risk-reward. keeps it simple for user. - inspired by Emily. + +Take the top Market caps only for top 20 tokens in the ecosystem. + +and based on current data ML will give the basket. + +Also every week re-balances the basket. + + + +we look at most momentum rising. + +Also do some Sharpe analysis if you want to look at Volatality. + + + + + +### SS-partner - + +Common agreement – insurer hospital entity model. Patient – + +Pressure – consumer is saying – I don’t know what I am paying for. To be transparent. + +Privacy and transparency. + +Doctor has 1000 patients. Dataset govt puts up. Predicted price has been XX. Factors counting towards this. + +Dataset somehow get it from public dataset. + +Pooled public money; + +Insurance company is medicare. + +Risk parameters. Either one can be proposer and other acceptor. + +Only thing not transparent. --- remote town in New Hampshire. + + + + + + + + + +# INSTALLS + +### issues + +https://chatgpt.com/share/56c053e3-80df-410b-8e10-b93d2a5433b1 + + + + + + + + + +### giza-sdk + +### giza-cli + + + +### datasets + +### agent + +```bash +# conda create -n giza2 python=3.11.0 + + +#did all pip install with --user option on my LA-Laptop. +pip install --user giza-sdk + +pip install --user giza-cli + +$ conda activate giza3 + + + + +C:\Users\XX\AppData\Roaming\Python\Python311\Scripts + +export PATH="$PATH:/c/Users/chuck.raghavan/AppData/Roaming/Python/Python311/Scripts" + + + +``` + + + + + +### Setup + +From your terminal, create a Giza user through our CLI in order to access the Giza Platform: + +Copy + +```bash +giza users create +user giza1 +aol email. + + + + +$ giza users create +Enter your username 😎: giza1 +Enter your password 🥷 : +Confirm your password 👉🏻 : +Enter your email 📧: r...@aol.com +[giza][2024-05-29 22:29:16.664] Creating user in Giza ✅ +[giza][2024-05-29 22:29:17.921] User created ✅. Check for a verification email 📧 +``` + +After creating your user, log into Giza: + +Copy + +#### giza user + +```bash +giza users login + +$ giza users login +Enter your username 😎: giza1 +Enter your password 🥷 : +[giza][2024-05-29 22:32:13.750] Log into Giza +[giza][2024-05-29 22:32:14.461] Creating default giza dir +[giza][2024-05-29 22:32:14.465] Credentials written to: C:\Users\XX\.giza\.credentials.json +[giza][2024-05-29 22:32:14.469] Successfully logged into Giza ✅ +(giza3) +``` + +*Optional*: you can create an API Key for your user in order to not regenerate your access token every few hours. + +Copy + +#### gize api-key + +```bash +giza users create-api-key + +$ giza users create-api-key +[giza][2024-05-29 22:34:07.987] Creating API Key ✅ +[giza][2024-05-29 22:34:08.252] API Key written to: C:\Users\XXXX\.giza\.api_key.json +[giza][2024-05-29 22:34:08.260] Successfully created API Key. It will be used for future requests ✅ +(giza3) +``` + + + +### Jupyter kernel + +```bash +$ python -m ipykernel install --name giza3 --display-name "giza3" +C:\Python311\python.exe: No module named ipykernel + +$ which python +/c/Users/XX/AppData/Local/anaconda3/envs/giza3/python + +/c/Users/XX/AppData/Local/anaconda3/envs/giza3/python -m ipykernel install --name giza3 --display-name "giza3" + + +$ /c/Users/xxxx/AppData/Local/anaconda3/envs/giza3/python -m ipykernel install --name giza3 --display-name "giza3" +0.01s - Debugger warning: It seems that frozen modules are being used, which may +0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off +0.00s - to python to disable frozen modules. +0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation. +Installed kernelspec giza3 in C:\ProgramData\jupyter\kernels\giza3 +``` + + + + + +### transpile + +### Model ID + +### Version + +``` +$ giza transpile xgb_diabetes.json --output-path xgb_diabetes +[giza][2024-05-29 23:43:03.612] No model id provided, checking if model exists ✅ +[giza][2024-05-29 23:43:03.621] Model name is: xgb_diabetes +[giza][2024-05-29 23:43:04.421] Model Created with id -> 665! ✅ +[giza][2024-05-29 23:43:05.200] Version Created with id -> 1! ✅ +[giza][2024-05-29 23:43:05.208] Sending model for transpilation ✅ +[giza][2024-05-29 23:43:16.618] Transpilation is fully compatible. Version compiled and Sierra is saved at Giza ✅ +[giza][2024-05-29 23:43:17.750] Downloading model ✅ +[giza][2024-05-29 23:43:17.758] model saved at: xgb_diabetes +(giza3) +``` + + + +### Deploy an inference endpoint + + + +```bash +giza endpoints deploy --model-id 665 --version-id 1 + + +$ giza endpoints deploy --model-id 665 --version-id 1 +▰▱▱▱▱▱▱ Creating endpoint! +[giza][2024-05-29 23:53:51.319] Endpoint is successful ✅ +[giza][2024-05-29 23:53:51.335] Endpoint created with id -> 234 ✅ +[giza][2024-05-29 23:53:51.346] Endpoint created with endpoint URL: https://endpoint-giza1-665-1-7ee56cfd-7i3yxzspbq-ew.a.run.app 🎉 +(giza3) +``` + + + + + +### Ape account + +``` +ape accounts generate +ape accounts generate giza1 + + +$ ape accounts generate giza1 +Enhance the security of your account by adding additional random input: +Show mnemonic? [Y/n]: Y +Create Passphrase to encrypt account: +Repeat for confirmation: +INFO: Newly generated mnemonic is:XXX +SUCCESS: A new account '0x39BaE1e8dEc32d94181bCDa1DC25218Ef57a12c2' with HDPath m/44'/60'/0'/0/0 has been added with the id 'giza1' + +sent .1 ETH from my other Sepolia; + +``` + + + +### Create and Agent using the CLI + + + +```bash +giza agents create --model-id --version-id --name --description + +giza agents create --model-id 665 --version-id 1 --name gizaTest1 --description diabetesTest + +--model-id 665 --version-id 1 +``` + + + +### issues with Pip + +had multiple versions. Finally had it working. + +https://chatgpt.com/share/56c053e3-80df-410b-8e10-b93d2a5433b1 + + + +``` + + +C:/Users/XX/AppData/Local/anaconda3/envs/giza3/Scripts/pip install varint + + + + + +``` + + + + + +### Run a verifiable inference + +![image-20240530011707152](./Images/image-20240530011707152.png) + + + + + +### Download the proof + +```bash +$ giza endpoints get-proof --endpoint-id 234 --proof-id "10c164e6c2364ab6b5491702127979a6" + + Endpoint created with id -> 234 + +$ giza endpoints get-proof --endpoint-id 234 --proof-id "10c164e6c2364ab6b5491702127979a6" +[giza][2024-05-30 00:40:39.691] Getting proof from endpoint 234 ✅ +{ + "id": 967, + "job_id": 1121, + "metrics": { + "proving_time": 17.249508 + }, + "created_date": "2024-05-30T07:33:12.532659" +} +(giza3) + + +Once the proof is ready, you can download it. + + + +$ giza endpoints download-proof --endpoint-id 234 --proof-id "10c164e6c2364ab6b5491702127979a6" --output-path zk_xgboost.proof + +>>>> +[giza][2024-03-19 11:55:49.713] Getting proof from endpoint 190 ✅ +[giza][2024-03-19 11:55:50.493] Proof downloaded to zk_xgboost.proof ✅ +``` + + + + + + + +![image-20240530004110556](./Images/image-20240530004110556.png) + + + + + + + +![image-20240530005207866](./Images/image-20240530005207866.png) + + + +``` +$ giza endpoints download-proof --endpoint-id 234 --proof-id "10c164e6c2364ab6b5491702127979a6" --output-path zk_xgboost.proof +[giza][2024-05-30 00:51:52.048] Getting proof from endpoint 234 ✅ +[giza][2024-05-30 00:51:53.800] Proof downloaded to zk_xgboost.proof ✅ +``` + + + +### Verify the proof + + + +```bash +giza verify --proof-id 967 + +$ giza verify --proof-id 967 +[giza][2024-05-30 00:56:05.847] Verifying proof... +[giza][2024-05-30 00:56:07.140] Verification result: True +[giza][2024-05-30 00:56:07.145] Verification time: 0.454667226 +(giza3) + +``` + diff --git a/Images/hackathonFlowChart.png b/Images/hackathonFlowChart.png new file mode 100644 index 0000000..e90dcae Binary files /dev/null and b/Images/hackathonFlowChart.png differ diff --git a/Images/image-20240520100825088.png b/Images/image-20240520100825088.png new file mode 100644 index 0000000..c1e9b61 Binary files /dev/null and b/Images/image-20240520100825088.png differ diff --git a/Images/image-20240520101119441.png b/Images/image-20240520101119441.png new file mode 100644 index 0000000..4946fb5 Binary files /dev/null and b/Images/image-20240520101119441.png differ diff --git a/Images/image-20240520101328479.png b/Images/image-20240520101328479.png new file mode 100644 index 0000000..2cd8ec2 Binary files /dev/null and b/Images/image-20240520101328479.png differ diff --git a/Images/image-20240520101659807.png b/Images/image-20240520101659807.png new file mode 100644 index 0000000..02d0cc3 Binary files /dev/null and b/Images/image-20240520101659807.png differ diff --git a/Images/image-20240520101905906.png b/Images/image-20240520101905906.png new file mode 100644 index 0000000..07ae9ee Binary files /dev/null and b/Images/image-20240520101905906.png differ diff --git a/Images/image-20240520101937506.png b/Images/image-20240520101937506.png new file mode 100644 index 0000000..8ffb64d Binary files /dev/null and b/Images/image-20240520101937506.png differ diff --git a/Images/image-20240520102234001.png b/Images/image-20240520102234001.png new file mode 100644 index 0000000..178ee95 Binary files /dev/null and b/Images/image-20240520102234001.png differ diff --git a/Images/image-20240520102237619.png b/Images/image-20240520102237619.png new file mode 100644 index 0000000..178ee95 Binary files /dev/null and b/Images/image-20240520102237619.png differ diff --git a/Images/image-20240520102247386.png b/Images/image-20240520102247386.png new file mode 100644 index 0000000..5baa560 Binary files /dev/null and b/Images/image-20240520102247386.png differ diff --git a/Images/image-20240520102453489.png b/Images/image-20240520102453489.png new file mode 100644 index 0000000..789e918 Binary files /dev/null and b/Images/image-20240520102453489.png differ diff --git a/Images/image-20240520102649162.png b/Images/image-20240520102649162.png new file mode 100644 index 0000000..6a15c47 Binary files /dev/null and b/Images/image-20240520102649162.png differ diff --git a/Images/image-20240520102918909.png b/Images/image-20240520102918909.png new file mode 100644 index 0000000..e76667b Binary files /dev/null and b/Images/image-20240520102918909.png differ diff --git a/Images/image-20240525115811302.png b/Images/image-20240525115811302.png new file mode 100644 index 0000000..e7af20a Binary files /dev/null and b/Images/image-20240525115811302.png differ diff --git a/Images/image-20240525120809497.png b/Images/image-20240525120809497.png new file mode 100644 index 0000000..3fb47ee Binary files /dev/null and b/Images/image-20240525120809497.png differ diff --git a/Images/image-20240525120851866.png b/Images/image-20240525120851866.png new file mode 100644 index 0000000..7ea0ae1 Binary files /dev/null and b/Images/image-20240525120851866.png differ diff --git a/Images/image-20240525121040580.png b/Images/image-20240525121040580.png new file mode 100644 index 0000000..c5323c2 Binary files /dev/null and b/Images/image-20240525121040580.png differ diff --git a/Images/image-20240525121117861.png b/Images/image-20240525121117861.png new file mode 100644 index 0000000..883759e Binary files /dev/null and b/Images/image-20240525121117861.png differ diff --git a/Images/image-20240525121221062.png b/Images/image-20240525121221062.png new file mode 100644 index 0000000..f457ce6 Binary files /dev/null and b/Images/image-20240525121221062.png differ diff --git a/Images/image-20240525121305446.png b/Images/image-20240525121305446.png new file mode 100644 index 0000000..fcfaa04 Binary files /dev/null and b/Images/image-20240525121305446.png differ diff --git a/Images/image-20240525121429280.png b/Images/image-20240525121429280.png new file mode 100644 index 0000000..4cbeb47 Binary files /dev/null and b/Images/image-20240525121429280.png differ diff --git a/Images/image-20240525122040197.png b/Images/image-20240525122040197.png new file mode 100644 index 0000000..d7a3606 Binary files /dev/null and b/Images/image-20240525122040197.png differ diff --git a/Images/image-20240525122201291.png b/Images/image-20240525122201291.png new file mode 100644 index 0000000..aa3defc Binary files /dev/null and b/Images/image-20240525122201291.png differ diff --git a/Images/image-20240525124048894.png b/Images/image-20240525124048894.png new file mode 100644 index 0000000..4502e07 Binary files /dev/null and b/Images/image-20240525124048894.png differ diff --git a/Images/image-20240525125419274.png b/Images/image-20240525125419274.png new file mode 100644 index 0000000..a153b3c Binary files /dev/null and b/Images/image-20240525125419274.png differ diff --git a/Images/image-20240525125657865.png b/Images/image-20240525125657865.png new file mode 100644 index 0000000..c0e08ad Binary files /dev/null and b/Images/image-20240525125657865.png differ diff --git a/Images/image-20240525130321177.png b/Images/image-20240525130321177.png new file mode 100644 index 0000000..fe3f90f Binary files /dev/null and b/Images/image-20240525130321177.png differ diff --git a/Images/image-20240525130917217.png b/Images/image-20240525130917217.png new file mode 100644 index 0000000..a1333d9 Binary files /dev/null and b/Images/image-20240525130917217.png differ diff --git a/Images/image-20240525130942905-1716667783593-1.png b/Images/image-20240525130942905-1716667783593-1.png new file mode 100644 index 0000000..3f1dd0a Binary files /dev/null and b/Images/image-20240525130942905-1716667783593-1.png differ diff --git a/Images/image-20240525130942905.png b/Images/image-20240525130942905.png new file mode 100644 index 0000000..3f1dd0a Binary files /dev/null and b/Images/image-20240525130942905.png differ diff --git a/Images/image-20240525134800752.png b/Images/image-20240525134800752.png new file mode 100644 index 0000000..cd25e5e Binary files /dev/null and b/Images/image-20240525134800752.png differ diff --git a/Images/image-20240525141136902.png b/Images/image-20240525141136902.png new file mode 100644 index 0000000..ce53c00 Binary files /dev/null and b/Images/image-20240525141136902.png differ diff --git a/Images/image-20240525141725467.png b/Images/image-20240525141725467.png new file mode 100644 index 0000000..64a357a Binary files /dev/null and b/Images/image-20240525141725467.png differ diff --git a/Images/image-20240525141746844.png b/Images/image-20240525141746844.png new file mode 100644 index 0000000..eabf531 Binary files /dev/null and b/Images/image-20240525141746844.png differ diff --git a/Images/image-20240525141819962.png b/Images/image-20240525141819962.png new file mode 100644 index 0000000..0e494a6 Binary files /dev/null and b/Images/image-20240525141819962.png differ diff --git a/Images/image-20240525141836925.png b/Images/image-20240525141836925.png new file mode 100644 index 0000000..7f59fb8 Binary files /dev/null and b/Images/image-20240525141836925.png differ diff --git a/Images/image-20240525142348171.png b/Images/image-20240525142348171.png new file mode 100644 index 0000000..1678f1f Binary files /dev/null and b/Images/image-20240525142348171.png differ diff --git a/Images/image-20240525142544072.png b/Images/image-20240525142544072.png new file mode 100644 index 0000000..1404021 Binary files /dev/null and b/Images/image-20240525142544072.png differ diff --git a/Images/image-20240525142948742.png b/Images/image-20240525142948742.png new file mode 100644 index 0000000..cfe5dc4 Binary files /dev/null and b/Images/image-20240525142948742.png differ diff --git a/Images/image-20240525143244923.png b/Images/image-20240525143244923.png new file mode 100644 index 0000000..5fbdac7 Binary files /dev/null and b/Images/image-20240525143244923.png differ diff --git a/Images/image-20240525143545880.png b/Images/image-20240525143545880.png new file mode 100644 index 0000000..9aaccfe Binary files /dev/null and b/Images/image-20240525143545880.png differ diff --git a/Images/image-20240525143932690.png b/Images/image-20240525143932690.png new file mode 100644 index 0000000..8f2e261 Binary files /dev/null and b/Images/image-20240525143932690.png differ diff --git a/Images/image-20240525143944209.png b/Images/image-20240525143944209.png new file mode 100644 index 0000000..6b876f6 Binary files /dev/null and b/Images/image-20240525143944209.png differ diff --git a/Images/image-20240525144141254.png b/Images/image-20240525144141254.png new file mode 100644 index 0000000..186da31 Binary files /dev/null and b/Images/image-20240525144141254.png differ diff --git a/Images/image-20240525144308860.png b/Images/image-20240525144308860.png new file mode 100644 index 0000000..5caf11b Binary files /dev/null and b/Images/image-20240525144308860.png differ diff --git a/Images/image-20240530003409871.png b/Images/image-20240530003409871.png new file mode 100644 index 0000000..7066aaa Binary files /dev/null and b/Images/image-20240530003409871.png differ diff --git a/Images/image-20240530004110556.png b/Images/image-20240530004110556.png new file mode 100644 index 0000000..29ecb9d Binary files /dev/null and b/Images/image-20240530004110556.png differ diff --git a/Images/image-20240530005104615.png b/Images/image-20240530005104615.png new file mode 100644 index 0000000..32d930d Binary files /dev/null and b/Images/image-20240530005104615.png differ diff --git a/Images/image-20240530005207866.png b/Images/image-20240530005207866.png new file mode 100644 index 0000000..274aa8e Binary files /dev/null and b/Images/image-20240530005207866.png differ diff --git a/Images/image-20240530011707152.png b/Images/image-20240530011707152.png new file mode 100644 index 0000000..76cc51a Binary files /dev/null and b/Images/image-20240530011707152.png differ diff --git a/gizaTest1.ipynb b/gizaTest1.ipynb new file mode 100644 index 0000000..81832cc --- /dev/null +++ b/gizaTest1.ipynb @@ -0,0 +1,739 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "afb5d31a-296a-4fb1-90ac-f9a2136e27a4", + "metadata": {}, + "source": [ + "### testing the XGBoost Diabetes example to transpile\n", + "##### used conda env giza from ll laptop" + ] + }, + { + "cell_type": "markdown", + "id": "78186bb5-98ce-471c-93d3-ca19386ca744", + "metadata": {}, + "source": [ + "## Create and Train an XGBoost Model\n", + "### We'll start by creating a simple XGBoost model using Scikit-Learn and train it on diabetes dataset" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5650a200-f157-4c3d-b352-282380f05abd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
XGBRegressor(base_score=None, booster=None, callbacks=None,\n",
+       "             colsample_bylevel=None, colsample_bynode=None,\n",
+       "             colsample_bytree=None, device=None, early_stopping_rounds=None,\n",
+       "             enable_categorical=False, eval_metric=None, feature_types=None,\n",
+       "             gamma=None, grow_policy=None, importance_type=None,\n",
+       "             interaction_constraints=None, learning_rate=None, max_bin=None,\n",
+       "             max_cat_threshold=None, max_cat_to_onehot=None,\n",
+       "             max_delta_step=None, max_depth=6, max_leaves=None,\n",
+       "             min_child_weight=None, missing=nan, monotone_constraints=None,\n",
+       "             multi_strategy=None, n_estimators=2, n_jobs=None,\n",
+       "             num_parallel_tree=None, random_state=None, ...)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "XGBRegressor(base_score=None, booster=None, callbacks=None,\n", + " colsample_bylevel=None, colsample_bynode=None,\n", + " colsample_bytree=None, device=None, early_stopping_rounds=None,\n", + " enable_categorical=False, eval_metric=None, feature_types=None,\n", + " gamma=None, grow_policy=None, importance_type=None,\n", + " interaction_constraints=None, learning_rate=None, max_bin=None,\n", + " max_cat_threshold=None, max_cat_to_onehot=None,\n", + " max_delta_step=None, max_depth=6, max_leaves=None,\n", + " min_child_weight=None, missing=nan, monotone_constraints=None,\n", + " multi_strategy=None, n_estimators=2, n_jobs=None,\n", + " num_parallel_tree=None, random_state=None, ...)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import xgboost as xgb\n", + "from sklearn.datasets import load_diabetes\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "data = load_diabetes()\n", + "X, y = data.data, data.target\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", + "\n", + "# Increase the number of trees and maximum depth\n", + "n_estimators = 2 # Increase the number of trees\n", + "max_depth = 6 # Increase the maximum depth of each tree\n", + "\n", + "xgb_reg = xgb.XGBRegressor(n_estimators=n_estimators, max_depth=max_depth)\n", + "xgb_reg.fit(X_train, y_train)" + ] + }, + { + "cell_type": "markdown", + "id": "10569dc0-0494-4370-b84f-365d4a18dfd3", + "metadata": {}, + "source": [ + "## Save the model\n", + "### Save the model in Json format" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "abe18478-37c7-4b40-b549-8e8dd3324c0e", + "metadata": {}, + "outputs": [], + "source": [ + "from giza.zkcook import serialize_model\n", + "serialize_model(xgb_reg, \"xgb_diabetes.json\")" + ] + }, + { + "cell_type": "markdown", + "id": "becd66e2-de26-4358-adcc-fc058a6698e8", + "metadata": {}, + "source": [ + "## Transpile your model to Orion Cairo\n", + "### We will use Giza-CLI to transpile our saved model to Orion Cairo." + ] + }, + { + "cell_type": "markdown", + "id": "31c49d75-4ff7-4b3b-b4f3-8fe09c06dd34", + "metadata": {}, + "source": [ + "\n", + "$ giza transpile xgb_diabetes.json --output-path xgb_diabetes\n", + "[giza][2024-05-29 23:43:03.612] No model id provided, checking if model exists ✅\n", + "[giza][2024-05-29 23:43:03.621] Model name is: xgb_diabetes\n", + "[giza][2024-05-29 23:43:04.421] Model Created with id -> 665! ✅\n", + "[giza][2024-05-29 23:43:05.200] Version Created with id -> 1! ✅\n", + "[giza][2024-05-29 23:43:05.208] Sending model for transpilation ✅\n", + "[giza][2024-05-29 23:43:16.618] Transpilation is fully compatible. Version compiled and Sierra is saved at Giza ✅\n", + "[giza][2024-05-29 23:43:17.750] Downloading model ✅\n", + "[giza][2024-05-29 23:43:17.758] model saved at: xgb_diabetes\n", + "(giza3)" + ] + }, + { + "cell_type": "markdown", + "id": "bd00d799-97cb-44a2-bde5-03a433b5f507", + "metadata": {}, + "source": [ + "## Deploy an inference endpoint\n", + "### Now that our model is transpiled to Cairo we can deploy an endpoint to run verifiable inferences. We will use Giza CLI again to run and deploy an endpoint. Ensure to replace model-id and version-id with your ids provided during transpilation.\n" + ] + }, + { + "cell_type": "markdown", + "id": "f989d383-1aaf-4bf1-b6be-8fbb63e61e1c", + "metadata": {}, + "source": [ + "giza endpoints deploy --model-id 665 --version-id 1\n", + "\n", + "$ giza endpoints deploy --model-id 665 --version-id 1\n", + "▰▱▱▱▱▱▱ Creating endpoint!\n", + "[giza][2024-05-29 23:53:51.319] Endpoint is successful ✅\n", + "[giza][2024-05-29 23:53:51.335] Endpoint created with id -> 234 ✅\n", + "[giza][2024-05-29 23:53:51.346] Endpoint created with endpoint URL: https://endpoint-giza1-665-1-7ee56cfd-7i3yxzspbq-ew.a.run.app 🎉\n", + "(giza3)" + ] + }, + { + "cell_type": "markdown", + "id": "377dcf19-aff5-456a-9776-bb30dcc7f954", + "metadata": {}, + "source": [ + "## Run a verifiable inference\n", + "##### To streamline verifiable inference, you might consider using the endpoint URL obtained after transpilation. However, this approach requires manual serialization of the input for the Cairo program and handling the deserialization process. To make this process more user-friendly and keep you within a Python environment, we've introduced a Python SDK designed to facilitate the creation of ML workflows and execution of verifiable predictions. When you initiate a prediction, our system automatically retrieves the endpoint URL you deployed earlier, converts your input into Cairo-compatible format, executes the prediction, and then converts the output back into a numpy object. " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "5dbf104f-5868-4084-a496-321ae40e4aee", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\chuck.raghavan\\AppData\\Roaming\\Python\\Python311\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🚀 Starting deserialization process...\n", + "✅ Deserialization completed! 🎉\n", + "Predicted value for input 0.09256398319871433 is 175.58781\n", + "Proof ID: 10c164e6c2364ab6b5491702127979a6\n" + ] + } + ], + "source": [ + "import xgboost as xgb\n", + "from sklearn.datasets import load_diabetes\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "from giza.agents.model import GizaModel\n", + "\n", + "\n", + "MODEL_ID = 665 # Update with your model ID\n", + "VERSION_ID = 1 # Update with your version ID\n", + "\n", + "def prediction(input, model_id, version_id):\n", + " model = GizaModel(id=model_id, version=version_id)\n", + "\n", + " (result, proof_id) = model.predict(\n", + " input_feed={\"input\": input}, verifiable=True, model_category=\"XGB\"\n", + " )\n", + "\n", + " return result, proof_id\n", + "\n", + "\n", + "def execution():\n", + " # The input data type should match the model's expected input\n", + " input = X_test[1, :]\n", + "\n", + " (result, proof_id) = prediction(input, MODEL_ID, VERSION_ID)\n", + "\n", + " print(f\"Predicted value for input {input.flatten()[0]} is {result}\")\n", + "\n", + " return result, proof_id\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + " data = load_diabetes()\n", + " X, y = data.data, data.target\n", + "\n", + " X_train, X_test, y_train, y_test = train_test_split(\n", + " X, y, test_size=0.2, random_state=42\n", + " )\n", + " _, proof_id = execution()\n", + " print(f\"Proof ID: {proof_id}\")" + ] + }, + { + "cell_type": "markdown", + "id": "7b44f1d7-6254-4380-aaa4-ecd141fc17e7", + "metadata": {}, + "source": [ + "## Download the proof\n", + "#### Initiating a verifiable inference sets off a proving job on our server, sparing you the complexities of installing and configuring the prover yourself. Upon completion, you can download your proof.\n", + "\n", + "First, let's check the status of the proving job to ensure that it has been completed." + ] + }, + { + "cell_type": "markdown", + "id": "f25f8d23-0d83-412a-9e3b-fca989972f0a", + "metadata": {}, + "source": [ + "$ giza endpoints get-proof --endpoint-id 234 --proof-id \"10c164e6c2364ab6b5491702127979a6\"\n", + "[giza][2024-05-30 00:40:39.691] Getting proof from endpoint 234 ✅\n", + "{\n", + " \"id\": 967,\n", + " \"job_id\": 1121,\n", + " \"metrics\": {\n", + " \"proving_time\": 17.249508\n", + " },\n", + " \"created_date\": \"2024-05-30T07:33:12.532659\"\n", + "}" + ] + }, + { + "cell_type": "markdown", + "id": "4f427ceb-61d7-4ffc-ba4f-15d4dad1ec2c", + "metadata": {}, + "source": [ + "Once the proof is ready, you can download it." + ] + }, + { + "cell_type": "markdown", + "id": "43c280b5-8b29-4a7d-8b0e-a5b90e408852", + "metadata": {}, + "source": [ + "$ giza endpoints download-proof --endpoint-id 234 --proof-id \"10c164e6c2364ab6b5491702127979a6\" --output-path zk_xgboost.proof\n", + "[giza][2024-05-30 00:51:52.048] Getting proof from endpoint 234 ✅\n", + "[giza][2024-05-30 00:51:53.800] Proof downloaded to zk_xgboost.proof ✅\n", + "(giza3)" + ] + }, + { + "cell_type": "markdown", + "id": "a36fb812-b3ef-4c4e-a142-c3c9263be989", + "metadata": {}, + "source": [ + "## Verify the proof\n", + "#### Finally, you can verify the proof." + ] + }, + { + "cell_type": "markdown", + "id": "139ac5f2-0bd8-40d7-88c3-75e162febfb8", + "metadata": {}, + "source": [ + "$ giza verify --proof-id 967\n", + "[giza][2024-05-30 00:56:05.847] Verifying proof...\n", + "[giza][2024-05-30 00:56:07.140] Verification result: True\n", + "[giza][2024-05-30 00:56:07.145] Verification time: 0.454667226\n", + "(giza3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8859806a-01d8-40d9-a445-cbe3c8a733fd", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "giza3", + "language": "python", + "name": "giza3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tmp/cachedir/cache.db b/tmp/cachedir/cache.db new file mode 100644 index 0000000..6a54b12 Binary files /dev/null and b/tmp/cachedir/cache.db differ diff --git a/xgb_diabetes.json b/xgb_diabetes.json new file mode 100644 index 0000000..810b1d2 --- /dev/null +++ b/xgb_diabetes.json @@ -0,0 +1 @@ +{"model_type": "xgboost", "opt_type": 0, "learner": {"attributes": {}, "feature_names": [], "feature_types": [], "gradient_booster": {"model": {"gbtree_model_param": {"num_parallel_tree": "1", "num_trees": "2"}, "iteration_indptr": [0, 1, 2], "tree_info": [0, 0], "trees": [{"base_weights": [2.1551962e-06, -35.52351, 51.44785, -52.829765, 10.741674, 37.05116, 112.99444, -71.40181, -43.39292, -15.884881, 26.283365, 21.806906, 74.52042, 39.07488, 64.01196, -56.952522, -26.160921, -7.253008, -49.402424, -40.396854, 7.90817, -28.78045, 35.786476, -27.487764, 28.654728, -22.552406, 84.64206, -3.8604815, 75.23053, -66.86708, 4.0107665, -17.541075, 12.952693, -54.664833, -2.3628876, -13.7741785, -20.87677, -12.047309, 21.537113, -9.776771, -1.4604813, 20.830248, 25.867748, 7.1975937, -41.394474, 14.356332, 55.59948, -9.147308, 0.18951875, 89.78801, -10.460482, 3.939519, 82.22582, -9.776771, -21.553297, -3.5473084, 5.552692, -1.1826932, -15.165723, -16.843483, 9.3395195, -15.547309, 4.944158, 1.5632302, -12.247309, -1.965722, 10.353461, -8.589396, 11.130086, 7.089519, -1.8473084, -15.403683, 4.239519, 9.326403, -2.733646, 24.205349, 4.217307, -3.2604814, 28.090105, 6.939519, 26.465866], "categories": [], "categories_nodes": [], "categories_segments": [], "categories_sizes": [], "default_left": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "id": 0, "left_children": [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, -1, 27, 29, -1, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, -1, 51, 53, 55, 57, -1, 59, 61, 63, -1, -1, 65, -1, -1, 67, -1, 69, 71, 73, 75, -1, -1, 77, -1, -1, 79, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "loss_changes": [648800.9, 168709.69, 124293.125, 23716.688, 24419.168, 66346.64, 14482.875, 6449.4062, 22228.703, 13150.189, 20391.217, 29817.914, 35315.11, 0.0, 8730.457, 17796.484, 0.0, 9058.359, 21902.016, 7590.4355, 8725.924, 387.8042, 22344.129, 5603.8916, 28482.074, 755.4624, 22195.578, 0.0, 2395.4258, 1923.8828, 1366.7615, 6085.4937, 0.0, 11906.922, 10174.322, 3807.5496, 0.0, 0.0, 3870.5864, 0.0, 0.0, 21985.516, 0.0, 1023.4474, 5145.992, 20309.305, 25959.742, 0.0, 0.0, 12630.375, 0.0, 0.0, 438.6836, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], "parents": [2147483647, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 14, 14, 15, 15, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 28, 28, 29, 29, 30, 30, 31, 31, 33, 33, 34, 34, 35, 35, 38, 38, 41, 41, 43, 43, 44, 44, 45, 45, 46, 46, 49, 49, 52, 52], "right_children": [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, -1, 28, 30, -1, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, -1, 52, 54, 56, 58, -1, 60, 62, 64, -1, -1, 66, -1, -1, 68, -1, 70, 72, 74, 76, -1, -1, 78, -1, -1, 80, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "split_conditions": [0.0056499788, 0.0062067355, 0.07355214, -0.042570855, -0.023450946, 0.036201265, 0.022485405, -0.038719688, -0.051103264, 0.02472964, -0.04354178, -0.03980883, -0.00061173533, 39.07488, 0.016318427, -0.04559945, -26.160921, -0.004221514, 0.045345243, -0.02991782, -0.03321323, 0.0002609183, 0.03799897, -0.026833475, 0.0379505, 0.019913213, 0.19878799, -3.8604815, 0.08540807, -0.07563562, -0.04183994, 0.0030644094, 12.952693, 0.10794366, -0.034521777, 0.009438663, -20.87677, -12.047309, -0.028674295, -9.776771, -1.4604813, 0.01482098, 25.867748, -0.04183994, 0.020446286, 0.034750905, 0.038075905, -9.147308, 0.18951875, -0.071772285, -10.460482, 3.939519, -0.06726771, -9.776771, -21.553297, -3.5473084, 5.552692, -1.1826932, -15.165723, -16.843483, 9.3395195, -15.547309, 4.944158, 1.5632302, -12.247309, -1.965722, 10.353461, -8.589396, 11.130086, 7.089519, -1.8473084, -15.403683, 4.239519, 9.326403, -2.733646, 24.205349, 4.217307, -3.2604814, 28.090105, 6.939519, 26.465866], "split_indices": [2, 8, 2, 8, 2, 9, 5, 4, 4, 8, 3, 8, 8, 0, 4, 4, 0, 8, 5, 2, 3, 2, 7, 6, 3, 0, 5, 0, 2, 9, 0, 9, 0, 3, 8, 4, 0, 0, 6, 0, 0, 8, 0, 0, 4, 2, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "split_type": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "sum_hessian": [353.0, 209.0, 144.0, 152.0, 57.0, 118.0, 26.0, 49.0, 103.0, 21.0, 36.0, 85.0, 33.0, 18.0, 8.0, 28.0, 21.0, 15.0, 88.0, 10.0, 11.0, 5.0, 31.0, 10.0, 75.0, 3.0, 30.0, 1.0, 7.0, 24.0, 4.0, 13.0, 2.0, 79.0, 9.0, 6.0, 4.0, 2.0, 9.0, 4.0, 1.0, 25.0, 6.0, 3.0, 7.0, 50.0, 25.0, 2.0, 1.0, 29.0, 1.0, 1.0, 6.0, 4.0, 20.0, 2.0, 2.0, 10.0, 3.0, 78.0, 1.0, 2.0, 7.0, 4.0, 2.0, 3.0, 6.0, 6.0, 19.0, 1.0, 2.0, 6.0, 1.0, 29.0, 21.0, 15.0, 10.0, 1.0, 28.0, 1.0, 5.0], "tree_param": {"num_deleted": "0", "num_feature": "10", "num_nodes": "81", "size_leaf_vector": "1"}}, {"base_weights": [0.033973694, -23.928804, 39.55442, -39.92107, 3.6668823, 28.630907, 81.78349, -45.921833, -19.072487, -0.1979466, 18.967451, 10.280193, 47.480137, 24.659119, 26.368975, -36.62225, -60.225487, -1.4896207, -37.661182, 23.232634, -8.137161, 33.818333, -5.9450235, -28.651901, 52.621094, 12.051253, -3.281408, -5.3892593, -45.36009, -63.17431, -1.8156382, -15.503915, 15.430917, -44.61493, 7.3433585, -3.5389023, 56.34661, 5.487673, -28.235365, 53.727676, -3.121504, 25.000788, -17.273375, -0.4412842, -11.166572, 20.886992, 59.07768, 5.6961465, -5.458666, -14.175051, 7.9385905, -7.547262, -19.693068, -10.78396, 9.966923, 10.955309, -7.894888, -14.670368, 1.6660401, 3.4066155, -0.102104194, 8.510123, -6.9113336, 4.736274, 22.19846, -7.007573, 4.790467, -4.967204, -18.15296, 19.900429, 0.7423542, -6.6570506, 7.8785605, -7.566439, 4.074084, 11.221738, -3.9727972, 24.584923, 14.716818], "categories": [], "categories_nodes": [], "categories_segments": [], "categories_sizes": [], "default_left": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "id": 1, "left_children": [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, -1, 21, 23, 25, -1, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, -1, -1, 47, 49, 51, 53, 55, -1, 57, 59, 61, 63, 65, 67, 69, 71, -1, 73, -1, -1, 75, 77, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "loss_changes": [336191.47, 97677.58, 59471.297, 16639.188, 18887.38, 36868.758, 7261.0938, 12160.359, 10729.702, 14698.639, 0.0, 21588.09, 22251.445, 2648.082, 0.0, 18313.352, 6947.1562, 14148.437, 5334.828, 17929.645, 16311.191, 17083.014, 35827.305, 877.0115, 9010.75, 0.0, 0.0, 5340.2256, 8445.039, 2009.5938, 4781.9624, 9398.17, 0.0, 3282.084, 96.34645, 8119.083, 5273.9688, 11256.677, 8043.7695, 9528.148, 5625.47, 0.0, 7944.5674, 0.0, 0.0, 6133.1597, 5377.6875, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], "parents": [2147483647, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 11, 11, 12, 12, 13, 13, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 42, 42, 45, 45, 46, 46], "right_children": [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, -1, 22, 24, 26, -1, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, -1, -1, 48, 50, 52, 54, 56, -1, 58, 60, 62, 64, 66, 68, 70, 72, -1, 74, -1, -1, 76, 78, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], "split_conditions": [0.009961227, -0.0023983933, 0.07355214, 0.03081083, 0.1081111, 0.017293394, -0.052737556, 0.05068012, 0.019186998, -0.016704442, 18.967451, 0.030439656, -0.033245593, -0.060002632, 26.368975, 0.000778808, 0.03333708, 0.00806271, 0.019632837, -0.0018820165, 0.000778808, 0.05068012, -0.06917231, -0.020044709, 0.018583724, 12.051253, -3.281408, -0.045472477, 0.07351515, -0.07626374, 0.001750522, -0.0010776975, 15.430917, -0.0118968515, 0.045340985, 0.0020044427, -0.0010914305, -0.005696818, 0.0024165425, 0.008100982, -0.03603757, 25.000788, 0.05787118, -0.4412842, -11.166572, 0.045345243, -0.0010776975, 5.6961465, -5.458666, -14.175051, 7.9385905, -7.547262, -19.693068, -10.78396, 9.966923, 10.955309, -7.894888, -14.670368, 1.6660401, 3.4066155, -0.102104194, 8.510123, -6.9113336, 4.736274, 22.19846, -7.007573, 4.790467, -4.967204, -18.15296, 19.900429, 0.7423542, -6.6570506, 7.8785605, -7.566439, 4.074084, 11.221738, -3.9727972, 24.584923, 14.716818], "split_indices": [2, 8, 2, 0, 7, 3, 0, 1, 6, 4, 0, 2, 8, 0, 0, 6, 3, 4, 9, 0, 6, 1, 6, 0, 2, 0, 0, 0, 3, 2, 0, 9, 0, 8, 0, 8, 3, 4, 2, 3, 6, 0, 5, 0, 0, 5, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "split_type": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "sum_hessian": [353.0, 220.0, 133.0, 139.0, 81.0, 107.0, 26.0, 107.0, 32.0, 77.0, 4.0, 55.0, 52.0, 3.0, 23.0, 67.0, 40.0, 17.0, 15.0, 19.0, 58.0, 22.0, 33.0, 3.0, 49.0, 2.0, 1.0, 15.0, 52.0, 38.0, 2.0, 14.0, 3.0, 13.0, 2.0, 11.0, 8.0, 35.0, 23.0, 14.0, 8.0, 3.0, 30.0, 1.0, 2.0, 9.0, 40.0, 5.0, 10.0, 51.0, 1.0, 3.0, 35.0, 1.0, 1.0, 2.0, 12.0, 12.0, 1.0, 1.0, 1.0, 4.0, 7.0, 3.0, 5.0, 9.0, 26.0, 18.0, 5.0, 11.0, 3.0, 5.0, 3.0, 24.0, 6.0, 6.0, 3.0, 10.0, 30.0], "tree_param": {"num_deleted": "0", "num_feature": "10", "num_nodes": "79", "size_leaf_vector": "1"}}]}, "name": "gbtree"}, "learner_model_param": {"base_score": "1.5373654E2", "boost_from_average": "1", "num_class": "0", "num_feature": "10", "num_target": "1"}, "objective": {"name": "reg:squarederror", "reg_loss_param": {"scale_pos_weight": "1"}}}, "version": [2, 0, 3]} \ No newline at end of file diff --git a/xgb_diabetes/Scarb.lock b/xgb_diabetes/Scarb.lock new file mode 100644 index 0000000..a2f060d --- /dev/null +++ b/xgb_diabetes/Scarb.lock @@ -0,0 +1,6 @@ +# Code generated by scarb DO NOT EDIT. +version = 1 + +[[package]] +name = "xgb_diabetes" +version = "0.1.0" diff --git a/xgb_diabetes/Scarb.toml b/xgb_diabetes/Scarb.toml new file mode 100644 index 0000000..fd06098 --- /dev/null +++ b/xgb_diabetes/Scarb.toml @@ -0,0 +1,6 @@ +[package] +name = "xgb_diabetes" +version = "0.1.0" + +[cairo] +enable-gas = false diff --git a/xgb_diabetes/src/lib.cairo b/xgb_diabetes/src/lib.cairo new file mode 100644 index 0000000..a74edb8 --- /dev/null +++ b/xgb_diabetes/src/lib.cairo @@ -0,0 +1,31 @@ +mod xgb_inference; + +fn main(input_vector: Span) -> i32 { + let tree_0 = xgb_inference::Tree { + base_weights: array![0, -3552351, 5144785, -5282977, 1074167, 3705116, 11299444, -7140181, -4339292, -1588488, 2628337, 2180691, 7452042, 3907488, 6401196, -5695252, -2616092, -725301, -4940242, -4039685, 790817, -2878045, 3578648, -2748776, 2865473, -2255241, 8464206, -386048, 7523053, -6686708, 401077, -1754108, 1295269, -5466483, -236289, -1377418, -2087677, -1204731, 2153711, -977677, -146048, 2083025, 2586775, 719759, -4139447, 1435633, 5559948, -914731, 18952, 8978801, -1046048, 393952, 8222582, -977677, -2155330, -354731, 555269, -118269, -1516572, -1684348, 933952, -1554731, 494416, 156323, -1224731, -196572, 1035346, -858940, 1113009, 708952, -184731, -1540368, 423952, 932640, -273365, 2420535, 421731, -326048, 2809011, 693952, 2646587].span(), + left_children: array![1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 0, 27, 29, 0, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 0, 51, 53, 55, 57, 0, 59, 61, 63, 0, 0, 65, 0, 0, 67, 0, 69, 71, 73, 75, 0, 0, 77, 0, 0, 79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].span(), + right_children: array![2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 0, 28, 30, 0, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 52, 54, 56, 58, 0, 60, 62, 64, 0, 0, 66, 0, 0, 68, 0, 70, 72, 74, 76, 0, 0, 78, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].span(), + split_indices: array![2, 8, 2, 8, 2, 9, 5, 4, 4, 8, 3, 8, 8, 0, 4, 4, 0, 8, 5, 2, 3, 2, 7, 6, 3, 0, 5, 0, 2, 9, 0, 9, 0, 3, 8, 4, 0, 0, 6, 0, 0, 8, 0, 0, 4, 2, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].span(), + split_conditions: array![565, 621, 7355, -4257, -2345, 3620, 2249, -3872, -5110, 2473, -4354, -3981, -61, 3907488, 1632, -4560, -2616092, -422, 4535, -2992, -3321, 26, 3800, -2683, 3795, 1991, 19879, -386048, 8541, -7564, -4184, 306, 1295269, 10794, -3452, 944, -2087677, -1204731, -2867, -977677, -146048, 1482, 2586775, -4184, 2045, 3475, 3808, -914731, 18952, -7177, -1046048, 393952, -6727, -977677, -2155330, -354731, 555269, -118269, -1516572, -1684348, 933952, -1554731, 494416, 156323, -1224731, -196572, 1035346, -858940, 1113009, 708952, -184731, -1540368, 423952, 932640, -273365, 2420535, 421731, -326048, 2809011, 693952, 2646587].span() +}; + let tree_1 = xgb_inference::Tree { + base_weights: array![3397, -2392880, 3955442, -3992107, 366688, 2863091, 8178349, -4592183, -1907249, -19795, 1896745, 1028019, 4748014, 2465912, 2636898, -3662225, -6022549, -148962, -3766118, 2323263, -813716, 3381833, -594502, -2865190, 5262109, 1205125, -328141, -538926, -4536009, -6317431, -181564, -1550392, 1543092, -4461493, 734336, -353890, 5634661, 548767, -2823537, 5372768, -312150, 2500079, -1727338, -44128, -1116657, 2088699, 5907768, 569615, -545867, -1417505, 793859, -754726, -1969307, -1078396, 996692, 1095531, -789489, -1467037, 166604, 340662, -10210, 851012, -691133, 473627, 2219846, -700757, 479047, -496720, -1815296, 1990043, 74235, -665705, 787856, -756644, 407408, 1122174, -397280, 2458492, 1471682].span(), + left_children: array![1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 0, 21, 23, 25, 0, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 0, 0, 47, 49, 51, 53, 55, 0, 57, 59, 61, 63, 65, 67, 69, 71, 0, 73, 0, 0, 75, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].span(), + right_children: array![2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 0, 22, 24, 26, 0, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 0, 0, 48, 50, 52, 54, 56, 0, 58, 60, 62, 64, 66, 68, 70, 72, 0, 74, 0, 0, 76, 78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].span(), + split_indices: array![2, 8, 2, 0, 7, 3, 0, 1, 6, 4, 0, 2, 8, 0, 0, 6, 3, 4, 9, 0, 6, 1, 6, 0, 2, 0, 0, 0, 3, 2, 0, 9, 0, 8, 0, 8, 3, 4, 2, 3, 6, 0, 5, 0, 0, 5, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].span(), + split_conditions: array![996, -240, 7355, 3081, 10811, 1729, -5274, 5068, 1919, -1670, 1896745, 3044, -3325, -6000, 2636898, 78, 3334, 806, 1963, -188, 78, 5068, -6917, -2004, 1858, 1205125, -328141, -4547, 7352, -7626, 175, -108, 1543092, -1190, 4534, 200, -109, -570, 242, 810, -3604, 2500079, 5787, -44128, -1116657, 4535, -108, 569615, -545867, -1417505, 793859, -754726, -1969307, -1078396, 996692, 1095531, -789489, -1467037, 166604, 340662, -10210, 851012, -691133, 473627, 2219846, -700757, 479047, -496720, -1815296, 1990043, 74235, -665705, 787856, -756644, 407408, 1122174, -397280, 2458492, 1471682].span() +}; + let num_trees: u32 = 2; + let base_score: i32 = 15373654; + let opt_type: u8 = 0; + let trees: Span = array![tree_0, tree_1].span(); + let mut result: i32 = xgb_inference::accumulate_scores_from_trees(num_trees, trees, input_vector, 0, 0); + + if opt_type == 1 { + // Implement logic here + } else { + result = result + base_score; + } + + return result; +} \ No newline at end of file diff --git a/xgb_diabetes/src/xgb_inference.cairo b/xgb_diabetes/src/xgb_inference.cairo new file mode 100644 index 0000000..ed557e7 --- /dev/null +++ b/xgb_diabetes/src/xgb_inference.cairo @@ -0,0 +1,38 @@ +use core::array::ArrayTrait; + + +#[derive(Copy, Drop)] +pub struct Tree { + pub base_weights: Span, + pub left_children: Span, + pub right_children: Span, + pub split_indices: Span, + pub split_conditions: Span +} + +pub fn navigate_tree_and_accumulate_score(tree: Tree, features: Span, node: u32) -> i32 { + if *tree.left_children[node] == 0 { + if *tree.right_children[node] == 0{ + return *tree.base_weights[node]; + } + } + let mut next_node: u32 = 0; + let feature_index = *tree.split_indices[node]; + let threshold = *tree.split_conditions[node]; + if *features.at(feature_index) < threshold{ + next_node = *tree.left_children[node]; + } + else{ + next_node = *tree.right_children[node]; + } + navigate_tree_and_accumulate_score(tree, features, next_node) +} + +pub fn accumulate_scores_from_trees(num_trees: u32, trees: Span, features: Span, index:u32, accumulated_score:i32) -> i32{ + if index >= num_trees{ + return accumulated_score; + } + let tree: Tree = *trees[index]; + let score_from_tree: i32 = navigate_tree_and_accumulate_score(tree, features, 0); + accumulate_scores_from_trees(num_trees, trees, features, index + 1, accumulated_score + score_from_tree) +} diff --git a/zk_xgboost.proof b/zk_xgboost.proof new file mode 100644 index 0000000..345383b Binary files /dev/null and b/zk_xgboost.proof differ