Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Joe McCain III <jo3mccain@icloud.com>
  • Loading branch information
FL03 committed Jan 29, 2025
1 parent f2a3e9b commit 19c9de5
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 244 deletions.
58 changes: 28 additions & 30 deletions .github/workflows/crates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,41 @@ permissions:

jobs:
features:
env:
PKG_PREFIX: ${{ github.event.repository.name }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
-
name: rustup
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
cache-key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
cache-workspaces: true
run: |
rustup self update
rustup update
-
name: publish (features)
run: |
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-math
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-core
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-data
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-derive
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-macros
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-gnn
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-kan
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-linear
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-s4
cargo publish --all-features -v -p ${{ env.PKG_PREFIX }}-transformers
publish:
needs: features
env:
PACKAGE: ${{ github.event.repository.name }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
cargo publish --all-features -v -p concision-math
cargo publish --all-features -v -p concision-core
cargo publish --all-features -v -p concision-data
cargo publish --all-features -v -p concision-derive
cargo publish --all-features -v -p concision-macros
cargo publish --all-features -v -p concision-kan
cargo publish --all-features -v -p concision-gnn
cargo publish --all-features -v -p concision-linear
cargo publish --all-features -v -p concision-s4
cargo publish --all-features -v -p concision-transformers
cargo publish --all-features -v -p concision
-
name: rustup
uses: actions-rust-lang/setup-rust-toolchain@v1
name: cache
uses: actions/cache@v4
with:
cache-key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
cache-workspaces: true
-
name: publish (${{ env.PACKAGE }})
run: cargo publish --all-features -v -p ${{ env.PACKAGE }}
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
path: |
~/.cargo/registry
~/.cargo/git
target
restore-keys: |
${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
${{ runner.os }}-cargo-
${{ runner.os }}-
26 changes: 16 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ version = "0.1.16"

[workspace.dependencies]
concision-core = { default-features = false, path = "core", version = "0.1.16" }
scsys = { default-features = false, features = ["derive"], version = "0.2.3" }
scsys = { default-features = false, features = ["derive"], version = "0.2.3" }

approx = "0.5"
itertools = "0.14"
lazy_static = "1"
ndarray = { default-features = false, version = "0.16" }
approx = "0.5"
itertools = "0.14"
lazy_static = "1"
ndarray = { default-features = false, version = "0.16" }
ndarray-stats = "0.6"
num = { default-features = false, version = "0.4" }
paste = "1"
num = { default-features = false, version = "0.4" }
paste = "1"
rand = { default-features = false, version = "0.9" }
rand_distr = { default-features = false, version = "0.5" }
serde = { default-features = false, features = ["derive"], version = "1" }
serde_derive = { default-features = false, version = "1" }
smart-default = "0.7"
strum = { default-features = false, features = ["derive"], version = "0.26" }
thiserror = { default-features = false, version = "2" }
uuid = { default-features = false, version = "0.8" }
strum = { default-features = false, features = ["derive"], version = "0.26" }
tracing = "0.1"
tracing-subscriber = "0.3"
thiserror = { default-features = false, version = "2" }
uuid = { default-features = false, version = "1" }

[workspace]
default-members = ["concision"]
Expand Down
35 changes: 15 additions & 20 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[package]
authors.workspace = true
build = "build.rs"
name = "concision-core"

authors.workspace = true
categories.workspace = true
description.workspace = true
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "concision-core"
readme.workspace = true
repository.workspace = true
version.workspace = true
Expand Down Expand Up @@ -44,9 +45,11 @@ blas = [
]

rand = [
"dep:ndarray-rand",
"dep:rand",
"dep:rand_distr",
"concision-math/rand",
"rand?/small_rng",
"rand?/thread_rng",
"num/rand",
"uuid/rng",
"uuid/v4",
Expand All @@ -63,8 +66,8 @@ serde = [
"serde?/derive",
"ndarray/serde",
"num/serde",
"rand?/serde1",
"rand_distr?/serde1",
"rand?/serde",
"rand_distr?/serde",
"scsys/serde",
"uuid/serde",
]
Expand Down Expand Up @@ -127,21 +130,13 @@ smart-default.workspace = true
strum.workspace = true
thiserror.workspace = true
# Optional dependencies
approx = { optional = true, version = "0.5" }
ndarray-rand = { optional = true, version = "0.15" }
rand = { default-features = false, optional = true, version = "0.8" }
rand_distr = { default-features = false, optional = true, version = "0.4" }
tracing = { optional = true, version = "0.1" }

[dependencies.serde]
default-features = false
optional = true
version = "1"

[dependencies.uuid]
default-features = false
features = [ "v5", "v8" ]
version = "1"
approx = { optional = true, workspace = true }
rand = { default-features = false, optional = true, workspace = true }
rand_distr = { default-features = false, optional = true, workspace = true }
serde = { default-features = false, optional = true, workspace = true }
tracing = { optional = true, workspace = true }
uuid = { default-features = false, features = [ "v5", "v8" ], workspace = true }


[package.metadata.docs.rs]
all-features = true
Expand Down
24 changes: 7 additions & 17 deletions core/src/init/distr/xavier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,24 @@ where

/// Uniform Xavier initializers use a uniform distribution to initialize the weights of a neural network
/// within a given range.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct XavierUniform<X>
where
X: SampleUniform,
X: Float + SampleUniform,
{
boundary: X,
distr: Uniform<X>,
}

impl<X> XavierUniform<X>
where
X: Float + SampleUniform,
{
pub fn new(inputs: usize, outputs: usize) -> Self {
Self {
boundary: boundary(inputs, outputs),
}
pub fn new(inputs: usize, outputs: usize) -> Result<Uniform<X>, rand_distr::uniform::Error> {
let limit = boundary::<X>(inputs, outputs);
Uniform::new(-limit, limit)
}

pub fn boundary(&self) -> X {
self.boundary
}

pub fn distr(&self) -> Uniform<X>
where
X: Float,
{
let bnd = self.boundary();
Uniform::new(-bnd, bnd)
pub const fn distr(&self) -> &Uniform<X> {
&self.distr
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ pub mod distr {
}
}

#[doc(no_inline)]
pub use ndarray_rand as ndrand;
type UniformResult<T = ()> = Result<T, rand_distr::uniform::Error>;

#[doc(hidden)]
#[doc(no_inline)]
pub use rand;
#[doc(no_inline)]
Expand Down
31 changes: 14 additions & 17 deletions core/src/init/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use crate::init::distr::*;

use core::ops::Neg;
use ndarray::{ArrayBase, DataOwned, Dimension, RawData, ShapeBuilder};
use ndarray_rand::RandomExt;
use num::complex::ComplexDistribution;
use num::traits::Float;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use rand::{
Rng, SeedableRng,
rngs::{SmallRng, StdRng},
};
use rand_distr::uniform::{SampleUniform, Uniform};
use rand_distr::{Bernoulli, BernoulliError, Distribution, Normal, NormalError, StandardNormal};

Expand Down Expand Up @@ -112,31 +113,28 @@ where
Ok(Self::rand(shape, distr))
}
/// A [uniform](rand_distr::uniform::Uniform) generator with values between u(-dk, dk)
fn uniform<Sh>(shape: Sh, dk: A) -> Self
fn uniform<Sh>(shape: Sh, dk: A) -> super::UniformResult<Self>
where
A: Copy + Neg<Output = A> + SampleUniform,
Sh: ShapeBuilder<Dim = D>,
<A as SampleUniform>::Sampler: Clone,
Self::Data: DataOwned<Elem = A>,
{
Self::rand(shape, Uniform::new(dk.neg(), dk))
Uniform::new(dk.neg(), dk).map(|distr| Self::rand(shape, distr))
}

fn uniform_from_seed<Sh>(shape: Sh, start: A, stop: A, key: u64) -> Self
fn uniform_from_seed<Sh>(shape: Sh, start: A, stop: A, key: u64) -> super::UniformResult<Self>
where
A: Clone + SampleUniform,
Sh: ShapeBuilder<Dim = D>,
<A as SampleUniform>::Sampler: Clone,
Self::Data: DataOwned<Elem = A>,
{
Self::rand_with(
shape,
Uniform::new(start, stop),
&mut StdRng::seed_from_u64(key),
)
Uniform::new(start, stop)
.map(|distr| Self::rand_with(shape, distr, &mut StdRng::seed_from_u64(key)))
}
/// Generate a random array with values between u(-a, a) where a is the reciprocal of the value at the given axis
fn uniform_along<Sh>(shape: Sh, axis: usize) -> Self
fn uniform_along<Sh>(shape: Sh, axis: usize) -> super::UniformResult<Self>
where
A: Copy + Float + SampleUniform,
Sh: ShapeBuilder<Dim = D>,
Expand All @@ -148,14 +146,14 @@ where
Self::uniform(dim, dk)
}
/// A [uniform](rand_distr::uniform::Uniform) generator with values between u(-dk, dk)
fn uniform_between<Sh>(shape: Sh, a: A, b: A) -> Self
fn uniform_between<Sh>(shape: Sh, a: A, b: A) -> super::UniformResult<Self>
where
A: Clone + SampleUniform,
Sh: ShapeBuilder<Dim = D>,
<A as SampleUniform>::Sampler: Clone,
Self::Data: DataOwned<Elem = A>,
{
Self::rand(shape, Uniform::new(a, b))
Uniform::new(a, b).map(|distr| Self::rand(shape, distr))
}
}
/*
Expand All @@ -166,7 +164,6 @@ impl<S, A, D> Initialize<A, D> for ArrayBase<S, D>
where
D: Dimension,
S: RawData<Elem = A>,
ArrayBase<S, D>: RandomExt<S, A, D>,
{
type Data = S;

Expand All @@ -176,7 +173,7 @@ where
Sh: ShapeBuilder<Dim = D>,
S: DataOwned,
{
Self::random(shape, distr)
Self::rand_with(shape, distr, &mut SmallRng::from_rng(&mut rand::rng()))
}

fn rand_with<Sh, Ds, R>(shape: Sh, distr: Ds, rng: &mut R) -> Self
Expand All @@ -186,7 +183,7 @@ where
Sh: ShapeBuilder<Dim = D>,
S: DataOwned,
{
Self::random_using(shape, distr, rng)
Self::from_shape_simple_fn(shape, move || distr.sample(rng))
}
}

Expand Down
30 changes: 16 additions & 14 deletions core/src/init/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
Appellation: utils <mod>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use ndarray::*;
use ndarray_rand::RandomExt;
use super::Initialize;
use ndarray::{Array, ArrayBase, DataOwned, Dimension, IntoDimension, RawData, ShapeBuilder};
use num::Num;
use num::complex::{Complex, ComplexDistribution};
use rand::distributions::uniform::{SampleUniform, Uniform};
use rand::rngs::StdRng;
use rand::{SeedableRng, rngs};
use rand_distr::{Distribution, StandardNormal};
use rand_distr::{
Distribution, StandardNormal,
uniform::{SampleUniform, Uniform},
};

/// Generate a random array of complex numbers with real and imaginary parts in the range [0, 1)
pub fn randc<A, S, D>(shape: impl IntoDimension<Dim = D>) -> ArrayBase<S, D>
Expand All @@ -20,7 +21,7 @@ where
ComplexDistribution<A, A>: Distribution<S::Elem>,
{
let distr = ComplexDistribution::<A, A>::new(A::one(), A::one());
ArrayBase::random(shape, distr)
ArrayBase::rand(shape, distr)
}

/// Given a shape, generate a random array using the StandardNormal distribution
Expand All @@ -31,7 +32,7 @@ where
Sh: ShapeBuilder<Dim = D>,
StandardNormal: Distribution<S::Elem>,
{
ArrayBase::random(shape, StandardNormal)
ArrayBase::rand(shape, StandardNormal)
}

pub fn stdnorm_from_seed<S, D, Sh>(shape: Sh, seed: u64) -> ArrayBase<S, D>
Expand All @@ -41,22 +42,23 @@ where
Sh: ShapeBuilder<Dim = D>,
StandardNormal: Distribution<S::Elem>,
{
ArrayBase::random_using(shape, StandardNormal, &mut StdRng::seed_from_u64(seed))
ArrayBase::rand_with(
shape,
StandardNormal,
&mut rngs::StdRng::seed_from_u64(seed),
)
}
/// Creates a random array from a uniform distribution using a given key
pub fn uniform_from_seed<T, D>(
key: u64,
start: T,
stop: T,
shape: impl IntoDimension<Dim = D>,
) -> Array<T, D>
) -> super::UniformResult<Array<T, D>>
where
D: Dimension,
T: SampleUniform,
{
Array::random_using(
shape,
Uniform::new(start, stop),
&mut rngs::StdRng::seed_from_u64(key),
)
Uniform::new(start, stop)
.map(|distr| ArrayBase::rand_with(shape, &distr, &mut rngs::StdRng::seed_from_u64(key)))
}
Loading

0 comments on commit 19c9de5

Please sign in to comment.