-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cc0c12
commit 0cc5102
Showing
43 changed files
with
3,231 additions
and
2 deletions.
There are no files selected for viewing
Binary file added
BIN
+94 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_add.onnx
Binary file not shown.
Binary file added
BIN
+90 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_add_broadcast.onnx
Binary file not shown.
Binary file added
BIN
+90 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_add_broadcast_scalar.onnx
Binary file not shown.
Binary file added
BIN
+70 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_exp.onnx
Binary file not shown.
Binary file added
BIN
+96 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_less.onnx
Binary file not shown.
Binary file added
BIN
+70 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_log.onnx
Binary file not shown.
Binary file added
BIN
+110 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul.onnx
Binary file not shown.
Binary file added
BIN
+143 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_abcdex_abcef.onnx
Binary file not shown.
Binary file added
BIN
+134 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_abcdx_abde.onnx
Binary file not shown.
Binary file added
BIN
+122 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_abcx_acd.onnx
Binary file not shown.
Binary file added
BIN
+118 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_abcx_cd.onnx
Binary file not shown.
Binary file added
BIN
+102 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_abx_b.onnx
Binary file not shown.
Binary file added
BIN
+98 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_ax_a.onnx
Binary file not shown.
Binary file added
BIN
+102 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_ax_ab.onnx
Binary file not shown.
Binary file added
BIN
+110 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_identity_matrix.onnx
Binary file not shown.
Binary file added
BIN
+116 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_initializer_A.onnx
Binary file not shown.
Binary file added
BIN
+116 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_initializer_B.onnx
Binary file not shown.
Binary file added
BIN
+297 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_multi_nodes.onnx
Binary file not shown.
Binary file added
BIN
+110 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_non_square_matrix.onnx
Binary file not shown.
Binary file added
BIN
+110 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_matmul_zero_matrix.onnx
Binary file not shown.
Binary file added
BIN
+107 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_mod.onnx
Binary file not shown.
Binary file added
BIN
+94 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_mul.onnx
Binary file not shown.
Binary file added
BIN
+90 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_mul_broadcast.onnx
Binary file not shown.
Binary file added
BIN
+90 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_mul_broadcast_scalar.onnx
Binary file not shown.
Binary file added
BIN
+84 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_recip.onnx
Binary file not shown.
Binary file added
BIN
+107 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_reducemax_keepdim.onnx
Binary file not shown.
Binary file added
BIN
+99 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_reducemax_not_keepdim.onnx
Binary file not shown.
Binary file added
BIN
+107 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_reducesum_keepdim.onnx
Binary file not shown.
Binary file added
BIN
+99 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_reducesum_not_keepdim.onnx
Binary file not shown.
Binary file added
BIN
+70 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_sin.onnx
Binary file not shown.
Binary file added
BIN
+72 Bytes
packages/orion-graph/crates/orion-test-utils/tests/models/test_sqrt.onnx
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
scarb 2.6.4 | ||
scarb nightly-2024-06-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
scarb nightly-2024-06-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "orion_numbers" | ||
version = "0.1.0" | ||
edition = "2023_11" | ||
|
||
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html | ||
|
||
[dependencies] | ||
|
||
[scripts] | ||
test = "scarb cairo-test -f test" | ||
|
||
[cairo] | ||
enable-gas=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pub mod core; | ||
pub mod math; | ||
pub mod trig; | ||
pub mod erf; | ||
pub mod helpers; | ||
pub mod lut; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
use orion_numbers::f16x16::{math, trig, erf}; | ||
use core::traits::TryInto; | ||
use core::option::OptionTrait; | ||
use core::traits::Mul; | ||
|
||
pub type f16x16 = i32; | ||
|
||
// CONSTANTS | ||
pub const TWO: f16x16 = 131072; // 2 ** 17 | ||
pub const ONE: f16x16 = 65536; // 2 ** 16 | ||
pub const HALF: f16x16 = 32768; // 2 ** 15 | ||
pub const MAX: f16x16 = 2147483647; // 2 ** 31 -1 | ||
pub const MIN: f16x16 = -2147483648; // 2 ** 31 | ||
|
||
#[generate_trait] | ||
pub impl f16x16Impl of FixedTrait { | ||
fn ZERO() -> f16x16 { | ||
0 | ||
} | ||
|
||
fn HALF() -> f16x16 { | ||
HALF | ||
} | ||
|
||
fn ONE() -> f16x16 { | ||
ONE | ||
} | ||
|
||
fn MAX() -> f16x16 { | ||
MAX | ||
} | ||
|
||
fn MIN() -> f16x16 { | ||
MIN | ||
} | ||
|
||
fn new_unscaled(x: i32) -> f16x16 { | ||
x * ONE | ||
} | ||
|
||
fn new(x: i32) -> f16x16 { | ||
x | ||
} | ||
|
||
fn from_felt(x: felt252) -> f16x16 { | ||
x.try_into().unwrap() | ||
} | ||
|
||
fn from_unscaled_felt(x: felt252) -> f16x16 { | ||
return Self::from_felt(x * ONE.into()); | ||
} | ||
|
||
fn abs(self: f16x16) -> f16x16 { | ||
math::abs(self) | ||
} | ||
|
||
fn acos(self: f16x16) -> f16x16 { | ||
trig::acos_fast(self) | ||
} | ||
|
||
fn acosh(self: f16x16) -> f16x16 { | ||
trig::acosh(self) | ||
} | ||
|
||
fn asin(self: f16x16) -> f16x16 { | ||
trig::asin_fast(self) | ||
} | ||
|
||
fn asinh(self: f16x16) -> f16x16 { | ||
trig::asinh(self) | ||
} | ||
|
||
fn atan(self: f16x16) -> f16x16 { | ||
trig::atan_fast(self) | ||
} | ||
|
||
fn atanh(self: f16x16) -> f16x16 { | ||
trig::atanh(self) | ||
} | ||
|
||
fn add(lhs: f16x16, rhs: f16x16) -> f16x16 { | ||
math::add(lhs, rhs) | ||
} | ||
|
||
fn ceil(self: f16x16) -> f16x16 { | ||
math::ceil(self) | ||
} | ||
|
||
fn cos(self: f16x16) -> f16x16 { | ||
trig::cos_fast(self) | ||
} | ||
|
||
fn cosh(self: f16x16) -> f16x16 { | ||
trig::cosh(self) | ||
} | ||
|
||
fn div(lhs: f16x16, rhs: f16x16) -> f16x16 { | ||
math::div(lhs, rhs) | ||
} | ||
|
||
// Calculates the natural exponent of x: e^x | ||
fn exp(self: f16x16) -> f16x16 { | ||
math::exp(self) | ||
} | ||
|
||
// Calculates the binary exponent of x: 2^x | ||
fn exp2(self: f16x16) -> f16x16 { | ||
math::exp2(self) | ||
} | ||
|
||
fn floor(self: f16x16) -> f16x16 { | ||
math::floor(self) | ||
} | ||
|
||
// Calculates the natural logarithm of x: ln(x) | ||
// self must be greater than zero | ||
fn ln(self: f16x16) -> f16x16 { | ||
math::ln(self) | ||
} | ||
|
||
// Calculates the binary logarithm of x: log2(x) | ||
// self must be greather than zero | ||
fn log2(self: f16x16) -> f16x16 { | ||
math::log2(self) | ||
} | ||
|
||
// Calculates the base 10 log of x: log10(x) | ||
// self must be greater than zero | ||
fn log10(self: f16x16) -> f16x16 { | ||
math::log10(self) | ||
} | ||
|
||
fn mul(lhs: f16x16, rhs: f16x16) -> f16x16 { | ||
math::mul(lhs, rhs) | ||
} | ||
|
||
// Calclates the value of x^y and checks for overflow before returning | ||
// self is a fixed point value | ||
// b is a fixed point value | ||
fn pow(self: f16x16, b: f16x16) -> f16x16 { | ||
math::pow(self, b) | ||
} | ||
|
||
fn round(self: f16x16) -> f16x16 { | ||
math::round(self) | ||
} | ||
|
||
fn sin(self: f16x16) -> f16x16 { | ||
trig::sin_fast(self) | ||
} | ||
|
||
fn sinh(self: f16x16) -> f16x16 { | ||
trig::sinh(self) | ||
} | ||
|
||
// Calculates the square root of a fixed point value | ||
// x must be positive | ||
fn sqrt(self: f16x16) -> f16x16 { | ||
math::sqrt(self) | ||
} | ||
|
||
fn tan(self: f16x16) -> f16x16 { | ||
trig::tan_fast(self) | ||
} | ||
|
||
fn tanh(self: f16x16) -> f16x16 { | ||
trig::tanh(self) | ||
} | ||
|
||
fn sign(self: f16x16) -> f16x16 { | ||
math::sign(self) | ||
} | ||
|
||
fn sub(lhs: f16x16, rhs: f16x16) -> f16x16 { | ||
math::sub(lhs, rhs) | ||
} | ||
|
||
fn NaN() -> f16x16 { | ||
-0 | ||
} | ||
|
||
fn is_nan(self: f16x16) -> bool { | ||
self == -0 | ||
} | ||
|
||
fn INF() -> f16x16 { | ||
MAX | ||
} | ||
|
||
fn POS_INF() -> f16x16 { | ||
MAX | ||
} | ||
|
||
fn NEG_INF() -> f16x16 { | ||
MIN | ||
} | ||
|
||
fn is_inf(self: f16x16) -> bool { | ||
self == MAX | ||
} | ||
|
||
fn is_pos_inf(self: f16x16) -> bool { | ||
self == MAX | ||
} | ||
|
||
fn is_neg_inf(self: f16x16) -> bool { | ||
self == MIN | ||
} | ||
|
||
fn erf(self: f16x16) -> f16x16 { | ||
erf::erf(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use orion_numbers::f16x16::{core::{FixedTrait, f16x16, ONE}, lut}; | ||
|
||
const ERF_COMPUTATIONAL_ACCURACY: i32 = 100; | ||
const ROUND_CHECK_NUMBER: i32 = 10; | ||
// Values > MAX_ERF_NUMBER return 1 | ||
const MAX_ERF_NUMBER: i32 = 229376; | ||
// Values <= ERF_TRUNCATION_NUMBER -> two decimal places, and values > ERF_TRUNCATION_NUMBER -> one | ||
// decimal place | ||
const ERF_TRUNCATION_NUMBER: i32 = 131072; | ||
|
||
pub fn erf(x: f16x16) -> f16x16 { | ||
// Lookup | ||
// 1. if x.mag < 3.5 { lookup table } | ||
// 2. else{ return 1} | ||
let mut erf_value: i32 = 0; | ||
|
||
if x.abs() < MAX_ERF_NUMBER { | ||
erf_value = lut::erf_lut(x.abs()); | ||
} else { | ||
erf_value = ONE; | ||
} | ||
|
||
FixedTrait::mul(erf_value, x.sign()) | ||
} | ||
|
||
|
||
// Tests | ||
// | ||
// | ||
// -------------------------------------------------------------------------------------------------------------- | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{erf, f16x16}; | ||
|
||
#[test] | ||
#[available_gas(1000000000)] | ||
fn test_erf() { | ||
// 1.0 | ||
let f1 = 65536; | ||
// 0.134 | ||
let f2 = 8832; | ||
// 0.520 | ||
let f3 = 34078; | ||
// 2.0 | ||
let f4 = 131072; | ||
// 3.5 | ||
let f5 = 229376; | ||
// 5.164 | ||
let f6 = 338428; | ||
|
||
let f1_erf = erf(f1); | ||
let f2_erf = erf(f2); | ||
let f3_erf = erf(f3); | ||
let f4_erf = erf(f4); | ||
let f5_erf = erf(f5); | ||
let f6_erf = erf(f6); | ||
|
||
assert(f1_erf == 55227, 'f1_erf it works!'); | ||
assert(f2_erf == 10285, 'f2_erf it works!'); | ||
assert(f3_erf == 35251, 'f3_erf it works!'); | ||
assert(f4_erf == 65229, 'f4_erf it works!'); | ||
assert(f5_erf == 65536, 'f5_erf it works!'); | ||
assert(f6_erf == 65536, 'f6_erf it works!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use orion_numbers::f16x16::core::{FixedTrait, f16x16, ONE, HALF}; | ||
|
||
const DEFAULT_PRECISION: i32 = 7; // 1e-4 | ||
|
||
// To use `DEFAULT_PRECISION`, final arg is: `Option::None(())`. | ||
// To use `custom_precision` of 430_i32: `Option::Some(430_i32)`. | ||
pub fn assert_precise( | ||
result: f16x16, expected: felt252, msg: felt252, custom_precision: Option<i32> | ||
) { | ||
let precision = match custom_precision { | ||
Option::Some(val) => val, | ||
Option::None => DEFAULT_PRECISION, | ||
}; | ||
|
||
let diff = (result - FixedTrait::from_felt(expected)); | ||
|
||
if (diff > precision) { | ||
println!("{}", result); | ||
assert(diff <= precision, msg); | ||
} | ||
} | ||
|
||
pub fn assert_relative( | ||
result: f16x16, expected: felt252, msg: felt252, custom_precision: Option<i32> | ||
) { | ||
let precision = match custom_precision { | ||
Option::Some(val) => val, | ||
Option::None => DEFAULT_PRECISION, | ||
}; | ||
|
||
let diff = result - FixedTrait::from_felt(expected); | ||
let rel_diff = diff / result; | ||
|
||
if (rel_diff > precision) { | ||
println!("{}", result); | ||
assert(rel_diff <= precision, msg); | ||
} | ||
} | ||
|
Oops, something went wrong.