From 06dd26c523dd3b12364373fd52728ae711bb9877 Mon Sep 17 00:00:00 2001 From: "Mayeul@Zama" Date: Mon, 7 Nov 2022 10:09:15 +0100 Subject: [PATCH] dep(optimizer): remove derive_more dependency --- concrete-optimizer/Cargo.toml | 1 - .../src/dag/operator/operator.rs | 21 +++++++++-- .../dag/solo_key/symbolic_variance.rs | 35 +++++++++++++++++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/concrete-optimizer/Cargo.toml b/concrete-optimizer/Cargo.toml index 6f96e3292..f7aacca2e 100644 --- a/concrete-optimizer/Cargo.toml +++ b/concrete-optimizer/Cargo.toml @@ -8,7 +8,6 @@ edition = "2021" [dependencies] concrete-commons = { git = "ssh://git@github.com/zama-ai/concrete-core.git", rev = "715d4a18a59d13a5a51fee14bc1f6578de665c20" } concrete-npe = { git = "ssh://git@github.com/zama-ai/concrete-core.git", rev = "715d4a18a59d13a5a51fee14bc1f6578de665c20" } -derive_more = "0.99.17" file-lock = "2.1.6" static_init = "1.0.3" serde = { version = "1.0", features = ["derive"] } diff --git a/concrete-optimizer/src/dag/operator/operator.rs b/concrete-optimizer/src/dag/operator/operator.rs index d08cdefab..c0071c72e 100644 --- a/concrete-optimizer/src/dag/operator/operator.rs +++ b/concrete-optimizer/src/dag/operator/operator.rs @@ -1,5 +1,4 @@ use crate::dag::operator::tensor::{ClearTensor, Shape}; -use derive_more::{Add, AddAssign}; pub type Weights = ClearTensor; @@ -12,7 +11,7 @@ impl FunctionTable { pub const UNKWOWN: Self = Self { values: vec![] }; } -#[derive(Clone, Copy, PartialEq, Add, AddAssign, Debug)] +#[derive(Clone, Copy, PartialEq, Debug)] pub struct LevelledComplexity { pub lwe_dim_cost_factor: f64, pub fixed_cost: f64, @@ -35,6 +34,24 @@ impl LevelledComplexity { } } +impl std::ops::Add for LevelledComplexity { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { + lwe_dim_cost_factor: self.lwe_dim_cost_factor + rhs.lwe_dim_cost_factor, + fixed_cost: self.fixed_cost + rhs.fixed_cost, + } + } +} + +impl std::ops::AddAssign for LevelledComplexity { + fn add_assign(&mut self, rhs: Self) { + self.lwe_dim_cost_factor += rhs.lwe_dim_cost_factor; + self.fixed_cost += rhs.fixed_cost; + } +} + impl std::ops::Mul for LevelledComplexity { type Output = Self; fn mul(self, factor: u64) -> Self { diff --git a/concrete-optimizer/src/optimization/dag/solo_key/symbolic_variance.rs b/concrete-optimizer/src/optimization/dag/solo_key/symbolic_variance.rs index 1f5a83a85..830303e8e 100644 --- a/concrete-optimizer/src/optimization/dag/solo_key/symbolic_variance.rs +++ b/concrete-optimizer/src/optimization/dag/solo_key/symbolic_variance.rs @@ -1,4 +1,5 @@ -use derive_more::{Add, AddAssign, Sum}; +use std::iter::Sum; + /** * A variance that is represented as a linear combination of base variances. * Only the linear coefficient are known. @@ -12,7 +13,7 @@ use derive_more::{Add, AddAssign, Sum}; * Each linear coefficient is a variance factor. * There are homogenious to squared weight (or summed square weights or squared norm2). */ -#[derive(Clone, Copy, Add, AddAssign, Sum, Debug, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)] pub struct SymbolicVariance { pub lut_coeff: f64, pub input_coeff: f64, @@ -31,6 +32,36 @@ pub enum VarianceOrigin { Mixed, } +impl std::ops::Add for SymbolicVariance { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { + lut_coeff: self.lut_coeff + rhs.lut_coeff, + input_coeff: self.input_coeff + rhs.input_coeff, + } + } +} + +impl std::ops::AddAssign for SymbolicVariance { + fn add_assign(&mut self, rhs: Self) { + self.lut_coeff += rhs.lut_coeff; + self.input_coeff += rhs.input_coeff; + } +} + +impl Sum for SymbolicVariance { + fn sum>(iter: I) -> Self { + let mut accumulator = Self::ZERO; + + for item in iter { + accumulator += item; + } + + accumulator + } +} + impl std::ops::Mul for SymbolicVariance { type Output = Self; fn mul(self, sq_weight: f64) -> Self {