Skip to content

Commit

Permalink
Merge branch 'develop' into all-contributors/add-HappyTomatoo
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelDkhn authored Nov 22, 2023
2 parents 987acd0 + b07a376 commit db4bbe9
Show file tree
Hide file tree
Showing 329 changed files with 12,328 additions and 96 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@
"contributions": [
"bug"
]
},
{
"login": "bilgin-kocak",
"name": "Bilgin Koçak",
"avatar_url": "https://avatars.githubusercontent.com/u/30844607?v=4",
"profile": "https://www.bilginkocak.com/",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ See [LICENSE](https://github.com/franalgaba/onnx-cairo/blob/main/LICENSE/README.

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Thanks goes to these wonderful people:

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
Expand Down Expand Up @@ -100,6 +100,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Bal7hazar"><img src="https://avatars.githubusercontent.com/u/97087040?v=4?s=100" width="100px;" alt="Bal7hazar"/><br /><sub><b>Bal7hazar</b></sub></a><br /><a href="https://github.com/gizatechxyz/orion/issues?q=author%3ABal7hazar" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://okhaimie.com"><img src="https://avatars.githubusercontent.com/u/57156589?v=4?s=100" width="100px;" alt="Tony Stark"/><br /><sub><b>Tony Stark</b></sub></a><br /><a href="https://github.com/gizatechxyz/orion/commits?author=okhaimie-dev" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/HappyTomatoo"><img src="https://avatars.githubusercontent.com/u/143050661?v=4?s=100" width="100px;" alt="HappyTomatoo"/><br /><sub><b>HappyTomatoo</b></sub></a><br /><a href="https://github.com/gizatechxyz/orion/issues?q=author%3AHappyTomatoo" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://www.bilginkocak.com/"><img src="https://avatars.githubusercontent.com/u/30844607?v=4?s=100" width="100px;" alt="Bilgin Koçak"/><br /><sub><b>Bilgin Koçak</b></sub></a><br /><a href="https://github.com/gizatechxyz/orion/commits?author=bilgin-kocak" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
3 changes: 2 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ homepage = "https://github.com/gizatechxyz/orion"

[dependencies]
alexandria_data_structures = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "f37d73d" }
alexandria_sorting = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "f37d73d" }
cubit = { git = "https://github.com/influenceth/cubit.git", rev = "b459053" }

[scripts]
sierra = "cairo-compile . -r"
docgen = "cd docgen && cargo run"
nodegen = "python3 nodegen/node/__init__.py"
nodegen = "python3 nodegen/node/__init__.py"
8 changes: 8 additions & 0 deletions docgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ fn main() {
let trait_name: &str = "XGBoostRegressorTrait";
doc_trait(trait_path, doc_path, label);
doc_functions(trait_path, doc_path, trait_name, label);

// TREE ENSEMBLE CLASSIFIER DOC
let trait_path = "src/operators/ml/tree_ensemble/tree_ensemble_classifier.cairo";
let doc_path = "docs/framework/operators/machine-learning/tree-ensemble-classifier";
let label = "tree_ensemble_classifier";
let trait_name: &str = "TreeEnsembleClassifierTrait";
doc_trait(trait_path, doc_path, label);
doc_functions(trait_path, doc_path, trait_name, label);
}

fn doc_trait(trait_path: &str, doc_path: &str, label: &str) {
Expand Down
12 changes: 12 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
* [tensor.quantize\_linear](framework/operators/tensor/tensor.quantize\_linear.md)
* [tensor.dequantize\_linear](framework/operators/tensor/tensor.dequantize\_linear.md)
* [tensor.qlinear\_add](framework/operators/tensor/tensor.qlinear\_add.md)
* [tensor.qlinear\_mul](framework/operators/tensor/tensor.qlinear\_mul.md)
* [tensor.qlinear\_matmul](framework/operators/tensor/tensor.qlinear\_matmul.md)
* [tensor.qlinear\_concat](framework/operators/tensor/tensor.qlinear\_concat.md)
* [tensor.nonzero](framework/operators/tensor/tensor.nonzero.md)
* [tensor.squeeze](framework/operators/tensor/tensor.squeeze.md)
* [tensor.unsqueeze](framework/operators/tensor/tensor.unsqueeze.md)
Expand All @@ -97,9 +99,17 @@
* [tensor.bitwise_and](framework/operators/tensor/tensor.bitwise_and.md)
* [tensor.round](framework/operators/tensor/tensor.round.md)
* [tensor.scatter](framework/operators/tensor/tensor.scatter.md)
* [tensor.array_feature_extractor](framework/operators/tensor/tensor.array\_feature\_extractor.md)
* [tensor.binarizer](framework/operators/tensor/tensor.binarizer.md)
* [tensor.reduce\_sum\_square](framework/operators/tensor/tensor.reduce\_sum\_square.md)
* [tensor.reduce\_l2](framework/operators/tensor/tensor.reduce\_l2.md)
* [tensor.reduce\_l1](framework/operators/tensor/tensor.reduce\_l1.md)
* [tensor.sequence\_at](framework/operators/tensor/tensor.sequence\_at.md)
* [tensor.reduce\_min](framework/operators/tensor/tensor.reduce\_min.md)
* [tensor.sequence\_construct](framework/operators/tensor/tensor.sequence\_construct.md)
* [tensor.shrink](framework/operators/tensor/tensor.shrink.md)
* [tensor.sequence\_empty](framework/operators/tensor/tensor.sequence\_empty.md)
* [tensor.reduce_mean](framework/operators/tensor/tensor.reduce\_mean.md)
* [Neural Network](framework/operators/neural-network/README.md)
* [nn.relu](framework/operators/neural-network/nn.relu.md)
* [nn.leaky\_relu](framework/operators/neural-network/nn.leaky\_relu.md)
Expand All @@ -113,6 +123,8 @@
* [nn.thresholded\_relu](framework/operators/neural-network/nn.thresholded_relu.md)
* [nn.gemm](framework/operators/neural-network/nn.gemm.md)
* [Machine Learning](framework/operators/machine-learning/README.md)
* [Tree Ensemble Classifier](framework/operators/machine-learning/tree-ensemble-classifier/README.md)
* [tree_ensemble_classifier.predict](framework/operators/machine-learning/tree-ensemble-classifier/tree_ensemble_classifier.predict.md)
* [Tree Regressor](framework/operators/machine-learning/tree-regressor/README.md)
* [tree.predict](framework/operators/machine-learning/tree-regressor/tree.predict.md)
* [Tree Classifier](framework/operators/machine-learning/tree-classifier/README.md)
Expand Down
27 changes: 18 additions & 9 deletions docs/framework/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ You can see below the list of current supported ONNX Operators:
| [Gather](operators/tensor/tensor.gather.md) | :white\_check\_mark: |
| [QuantizeLinear](operators/tensor/tensor.quantize\_linear.md) | :white\_check\_mark: |
| [DequantizeLinear](operators/tensor/tensor.quantize\_linear.md) | :white\_check\_mark: |
| [QlinearAdd](operators/tensor/tensor.qlinear\_add.md) | :white\_check\_mark: |
| [QLinearMatmul](operators/tensor/tensor.qlinear\_matmul.md) | :white\_check\_mark: |
| [QLinearConcat](operators/tensor/tensor.qlinear\_concat.md) | :white\_check\_mark: |
| [QlinearAdd](operators/tensor/tensor.qlinear\_add.md) | :white\_check\_mark: |
| [QlinearMul](operators/tensor/tensor.qlinear\_mul.md) | :white\_check\_mark: |
| [Nonzero](operators/tensor/tensor.nonzero.md) | :white\_check\_mark: |
| [Squeeze](operators/tensor/tensor.squeeze.md) | :white\_check\_mark: |
| [Unsqueeze](operators/tensor/tensor.unsqueeze.md) | :white\_check\_mark: |
Expand All @@ -68,18 +70,25 @@ You can see below the list of current supported ONNX Operators:
| [Xor](operators/tensor/tensor.xor.md) | :white\_check\_mark: |
| [Or](operators/tensor/tensor.or.md) | :white\_check\_mark: |
| [Gemm](operators/neural-network/nn.gemm.md) | :white\_check\_mark: |
| [MinInTensor](operators/tensor/tensor.min\_in\_tensor.md) | :white\_check\_mark: |
| [MinInTensor](operators/tensor/tensor.min\_in\_tensor.md) | :white\_check\_mark: |
| [Min](operators/tensor/tensor.min.md) | :white\_check\_mark: |
| [Where](operators/tensor/tensor.where.md) | :white\_check\_mark: |
| [BitwiseAnd](operators/tensor/tensor.bitwise_and.md) | :white\_check\_mark: |
| [Round](operators/tensor/tensor.round.md) | :white\_check\_mark: |
| [Where](operators/tensor/tensor.where.md) | :white\_check\_mark: |
| [BitwiseAnd](operators/tensor/tensor.bitwise_and.md) | :white\_check\_mark: |
| [Round](operators/tensor/tensor.round.md) | :white\_check\_mark: |
| [MaxInTensor](operators/tensor/tensor.max\_in\_tensor.md) | :white\_check\_mark: |
| [Max](operators/tensor/tensor.max.md) | :white\_check\_mark: |
| [Max](operators/tensor/tensor.max.md) | :white\_check\_mark: |
| [ReduceSumSquare](operators/tensor/tensor.reduce\_sum\_square.md) | :white\_check\_mark: |
| [Trilu](operators/tensor/tensor.trilu.md) | :white\_check\_mark: |
| [Trilu](operators/tensor/tensor.trilu.md) | :white\_check\_mark: |
| [Scatter](operators/tensor/scatter.max.md) | :white\_check\_mark: |
| [ConstantOfShape](operators/tensor/tensor.constant_of_shape.md) | :white\_check\_mark: |
| [ArrayFeatureExtractor](operators/tensor/tensor.array\_feature\_extractor.md) | :white\_check\_mark: |
| [Binarizer](operators/tensor/tensor.binarizer.md) | :white\_check\_mark: |
| [ConstantOfShape](operators/tensor/tensor.constant_of_shape.md) | :white\_check\_mark: |
| [ReduceL1](operators/tensor/tensor.reduce\_l1.md) | :white\_check\_mark: |
| [ReduceL2](operators/tensor/tensor.reduce\_l2.md) | :white\_check\_mark: |
| [SequenceAt](operators/tensor/tensor.sequence\_at.md) | :white\_check\_mark: |
| [SequenceConstruct](operators/tensor/tensor.sequence\_construct.md) | :white\_check\_mark: |
| [Shrink](operators/tensor/tensor.shrink.md) | :white\_check\_mark: |
| [SequenceEmpty](operators/tensor/tensor.sequence\_empty.md) | :white\_check\_mark: |
| [ReduceL2](operators/tensor/tensor.reduce\_l2.md) | :white\_check\_mark: |

Current Operators support: **75/156 (48%)**
Current Operators support: **81/156 (52%)**
2 changes: 1 addition & 1 deletion docs/framework/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Orion supports <mark style="color:orange;">**Cairo and Scarb v2.3.0**</mark>

**Step 1: Install Cairo**

There are different ways to install Cairo. Use the one that suits you best: [Cairo installer](https://cairo-book.github.io/ch01-01-installation.html).
There are different ways to install Cairo. Use the one that suits you best: [Cairo installer.](https://book.cairo-lang.org/ch01-01-installation.html)

**Step 2: Setup Language Server**

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Tree Ensemble Classifier

`TreeEnsembleClassifierTrait` provides a trait definition for tree ensemble classification problem.

```rust
use orion::operators::ml::TreeEnsembleClassifierTrait;
```

### Data types

Orion supports currently only fixed point data types for `TreeEnsembleClassifierTrait`.

| Data type | dtype |
| -------------------- | ------------------------------------------------------------- |
| Fixed point (signed) | `TreeRegressorTrait<FP8x23 \| FP16x16 \| FP64x64 \| FP32x32>` |

### How to construct `TreeEnsembleClassifier`

You can utilize [this notebook](https://colab.research.google.com/drive/1qem56rUKJcNongXsLZ16_869q8395prz#scrollTo=V3qGW_kfXudk) to translate parameters from your ONNX TreeEnsembleClassifier model into Cairo code. Efforts are underway to integrate this functionality into Giza-CLI, aiming to enhance the user experience.


***

| function | description |
| --- | --- |
| [`tree_ensemble_classifier.predict`](tree_ensemble_classifier.predict.md) | Returns the top class for each of N inputs. |

Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
# TreeEnsembleClassifier::predict

```rust
fn predict(ref self: TreeEnsembleClassifier<T>, X: Tensor<T>) -> (Span<usize>, MutMatrix::<T>);
```

Tree Ensemble classifier. Returns the top class for each of N inputs.

## Args

* `self`: TreeEnsembleClassifier<T> - A TreeEnsembleClassifier object.
* `X`: Input 2D tensor.

## Returns

* N Top class for each point
* The class score Matrix for each class, for each point.

## Type Constraints

`TreeEnsembleClassifier` and `X` must be fixed points

## Examples

```rust
use orion::numbers::FP16x16;
use orion::operators::tensor::{Tensor, TensorTrait, FP16x16Tensor, U32Tensor};
use orion::operators::ml::{NODE_MODES, TreeEnsembleAttributes, TreeEnsemble};
use orion::operators::ml::{
TreeEnsembleClassifier, POST_TRANSFORM, TreeEnsembleClassifierTrait
};
use orion::operators::matrix::{MutMatrix, MutMatrixImpl};

fn tree_ensemble_classifier_helper(
post_transform: POST_TRANSFORM
) -> (TreeEnsembleClassifier<FP16x16>, Tensor<FP16x16>) {
let class_ids: Span<usize> = array![0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]
.span();

let class_nodeids: Span<usize> = array![2, 2, 2, 3, 3, 3, 4, 4, 4, 1, 1, 1, 3, 3, 3, 4, 4, 4]
.span();

let class_treeids: Span<usize> = array![0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
.span();

let class_weights: Span<FP16x16> = array![
FP16x16 { mag: 30583, sign: false },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 2185, sign: false },
FP16x16 { mag: 13107, sign: false },
FP16x16 { mag: 15729, sign: false },
FP16x16 { mag: 3932, sign: false },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 32768, sign: false },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 32768, sign: false },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 29491, sign: false },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 3277, sign: false },
FP16x16 { mag: 6746, sign: false },
FP16x16 { mag: 12529, sign: false },
FP16x16 { mag: 13493, sign: false },
]
.span();

let classlabels: Span<usize> = array![0, 1, 2].span();

let nodes_falsenodeids: Span<usize> = array![4, 3, 0, 0, 0, 2, 0, 4, 0, 0].span();

let nodes_featureids: Span<usize> = array![1, 0, 0, 0, 0, 1, 0, 0, 0, 0].span();

let nodes_missing_value_tracks_true: Span<usize> = array![0, 0, 0, 0, 0, 0, 0, 0, 0, 0].span();

let nodes_modes: Span<NODE_MODES> = array![
NODE_MODES::BRANCH_LEQ,
NODE_MODES::BRANCH_LEQ,
NODE_MODES::LEAF,
NODE_MODES::LEAF,
NODE_MODES::LEAF,
NODE_MODES::BRANCH_LEQ,
NODE_MODES::LEAF,
NODE_MODES::BRANCH_LEQ,
NODE_MODES::LEAF,
NODE_MODES::LEAF,
]
.span();

let nodes_nodeids: Span<usize> = array![0, 1, 2, 3, 4, 0, 1, 2, 3, 4].span();

let nodes_treeids: Span<usize> = array![0, 0, 0, 0, 0, 1, 1, 1, 1, 1].span();

let nodes_truenodeids: Span<usize> = array![1, 2, 0, 0, 0, 1, 0, 3, 0, 0].span();

let nodes_values: Span<FP16x16> = array![
FP16x16 { mag: 81892, sign: false },
FP16x16 { mag: 19992, sign: true },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 110300, sign: true },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 44245, sign: true },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 0, sign: false },
]
.span();

let tree_ids: Span<usize> = array![0, 1].span();

let mut root_index: Felt252Dict<usize> = Default::default();
root_index.insert(0, 0);
root_index.insert(1, 5);

let mut node_index: Felt252Dict<usize> = Default::default();
node_index
.insert(2089986280348253421170679821480865132823066470938446095505822317253594081284, 0);
node_index
.insert(2001140082530619239661729809084578298299223810202097622761632384561112390979, 1);
node_index
.insert(2592670241084192212354027440049085852792506518781954896144296316131790403900, 2);
node_index
.insert(2960591271376829378356567803618548672034867345123727178628869426548453833420, 3);
node_index
.insert(458933264452572171106695256465341160654132084710250671055261382009315664425, 4);
node_index
.insert(1089549915800264549621536909767699778745926517555586332772759280702396009108, 5);
node_index
.insert(1321142004022994845681377299801403567378503530250467610343381590909832171180, 6);
node_index
.insert(2592987851775965742543459319508348457290966253241455514226127639100457844774, 7);
node_index
.insert(2492755623019086109032247218615964389726368532160653497039005814484393419348, 8);
node_index
.insert(1323616023845704258113538348000047149470450086307731200728039607710316625916, 9);

let atts = TreeEnsembleAttributes {
nodes_falsenodeids,
nodes_featureids,
nodes_missing_value_tracks_true,
nodes_modes,
nodes_nodeids,
nodes_treeids,
nodes_truenodeids,
nodes_values
};

let mut ensemble: TreeEnsemble<FP16x16> = TreeEnsemble {
atts, tree_ids, root_index, node_index
};

let base_values: Option<Span<FP16x16>> = Option::None;

let mut classifier: TreeEnsembleClassifier<FP16x16> = TreeEnsembleClassifier {
ensemble,
class_ids,
class_nodeids,
class_treeids,
class_weights,
classlabels,
base_values,
post_transform
};

let mut X: Tensor<FP16x16> = TensorTrait::new(
array![3, 3].span(),
array![
FP16x16 { mag: 65536, sign: true },
FP16x16 { mag: 52429, sign: true },
FP16x16 { mag: 39322, sign: true },
FP16x16 { mag: 26214, sign: true },
FP16x16 { mag: 13107, sign: true },
FP16x16 { mag: 0, sign: false },
FP16x16 { mag: 13107, sign: false },
FP16x16 { mag: 26214, sign: false },
FP16x16 { mag: 39322, sign: false },
]
.span()
);

(classifier, X)
}

fn test_tree_ensemble_classifier_multi_pt_softmax() -> (Span<usize>, MutMatrix::<FP16x16>) {
let (mut classifier, X) = tree_ensemble_classifier_helper(POST_TRANSFORM::SOFTMAX);

let (labels, scores) = TreeEnsembleClassifierTrait::predict(ref classifier, X);
(labels, scores)
}

>>>
([0, 0, 1],
[
[0.545123, 0.217967, 0.23691],
[0.416047, 0.284965, 0.298988],
[0.322535, 0.366664, 0.310801],
])
```
Loading

0 comments on commit db4bbe9

Please sign in to comment.