Skip to content

Commit d110fa0

Browse files
author
sarah el kazdadi
committed
feat: implement hybrid Stockham+Cooley-Tukey FFT
0 parents  commit d110fa0

22 files changed

+8065
-0
lines changed

.github/workflows/build.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
6+
env:
7+
CARGO_TERM_COLOR: always
8+
9+
jobs:
10+
cargo-benches:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Install Rust
16+
uses: actions-rs/toolchain@v1
17+
with:
18+
toolchain: stable
19+
override: true
20+
- name: Install FFTW
21+
run: sudo apt install -y libfftw3-dev
22+
- name: Compile benches
23+
run: cargo bench --no-run
24+
25+
cargo-tests:
26+
runs-on: ${{ matrix.os }}
27+
28+
strategy:
29+
matrix:
30+
os: [ubuntu-latest, macos-latest, windows-latest]
31+
32+
steps:
33+
- uses: actions/checkout@v2
34+
- name: Install Rust
35+
uses: actions-rs/toolchain@v1
36+
with:
37+
toolchain: stable
38+
override: true
39+
- name: Test debug
40+
run: cargo test
41+
- name: Test debug serialization
42+
run: cargo test --features=serde
43+
- name: Test debug no-std
44+
run: cargo test --no-default-features
45+
46+
cargo-tests-nightly:
47+
runs-on: ${{ matrix.os }}
48+
49+
strategy:
50+
matrix:
51+
os: [ubuntu-latest, macos-latest, windows-latest]
52+
53+
steps:
54+
- uses: actions/checkout@v2
55+
- name: Install Rust
56+
uses: actions-rs/toolchain@v1
57+
with:
58+
toolchain: nightly
59+
override: true
60+
- name: Test debug nightly
61+
run: cargo test --features=nightly

.github/workflows/lint.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Check formatting using rustfmt
2+
# and lint with clippy
3+
name: Rustfmt and Clippy check
4+
5+
on:
6+
push:
7+
8+
jobs:
9+
rustfmt:
10+
name: rustfmt
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
16+
- uses: actions-rs/toolchain@v1
17+
with:
18+
profile: minimal
19+
toolchain: stable
20+
components: rustfmt
21+
override: true
22+
23+
- name: Run cargo fmt
24+
uses: actions-rs/cargo@v1
25+
with:
26+
command: fmt
27+
args: --all -- --check
28+
29+
clippy:
30+
name: clippy
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- uses: actions/checkout@v2
35+
36+
- uses: actions-rs/toolchain@v1
37+
with:
38+
profile: minimal
39+
toolchain: stable
40+
components: clippy
41+
override: true
42+
43+
- uses: actions-rs/clippy-check@v1
44+
with:
45+
token: ${{ secrets.GITHUB_TOKEN }}
46+
args: --all-targets --features=serde -- --no-deps -D warnings

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/Cargo.lock

Cargo.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[package]
2+
name = "concrete-fft"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["sarah el kazdadi <[email protected]>"]
6+
description = "Concrete-FFT is a pure Rust high performance fast Fourier transform library."
7+
readme = "README.md"
8+
repository = "https://github.com/zama-ai/concrete-fft"
9+
license = "BSD-3-Clause-Clear"
10+
homepage = "https://zama.ai/"
11+
keywords = ["fft"]
12+
13+
[dependencies]
14+
num-complex = "0.4"
15+
dyn-stack = { version = "0.8", default-features = false }
16+
aligned-vec = { version = "0.5", default-features = false }
17+
serde = { version = "1.0", optional = true, default-features = false }
18+
19+
[features]
20+
default = ["std"]
21+
nightly = []
22+
std = []
23+
serde = ["dep:serde", "num-complex/serde"]
24+
25+
[dev-dependencies]
26+
criterion = "0.3"
27+
rustfft = "6.0"
28+
fftw-sys = { version = "0.6", default-features = false, features = ["system"] }
29+
rand = "0.8"
30+
bincode = "1.3"
31+
32+
[[bench]]
33+
name = "fft"
34+
harness = false
35+
36+
[package.metadata.docs.rs]
37+
all-features = true
38+
rustdoc-args = ["--html-in-header", "katex-header.html", "--cfg", "docsrs"]

LICENSE

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
BSD 3-Clause Clear License
2+
3+
Copyright © 2022 ZAMA.
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without modification,
7+
are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice, this
13+
list of conditions and the following disclaimer in the documentation and/or other
14+
materials provided with the distribution.
15+
16+
3. Neither the name of ZAMA nor the names of its contributors may be used to endorse
17+
or promote products derived from this software without specific prior written permission.
18+
19+
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE*.
20+
THIS SOFTWARE IS PROVIDED BY THE ZAMA AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
21+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23+
ZAMA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
*In addition to the rights carried by this license, ZAMA grants to the user a non-exclusive,
31+
free and non-commercial license on all patents filed in its name relating to the open-source
32+
code (the "Patents") for the sole purpose of evaluation, development, research, prototyping
33+
and experimentation.

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Concrete-FFT is a pure Rust high performance fast Fourier transform library
2+
that processes vectors of sizes that are powers of two. It was made to be used
3+
as a backend in Zama's `concrete` library.
4+
5+
This library provides two FFT modules:
6+
- The ordered module FFT applies a forward/inverse FFT that takes its input in standard
7+
order, and outputs the result in standard order. For more detail on what the FFT
8+
computes, check the ordered module-level documentation.
9+
- The unordered module FFT applies a forward FFT that takes its input in standard order,
10+
and outputs the result in a certain permuted order that may depend on the FFT plan. On the
11+
other hand, the inverse FFT takes its input in that same permuted order and outputs its result
12+
in standard order. This is useful for cases where the order of the coefficients in the
13+
Fourier domain is not important. An example is using the Fourier transform for vector
14+
convolution. The only operations that are performed in the Fourier domain are elementwise, and
15+
so the order of the coefficients does not affect the results.
16+
17+
## Features
18+
19+
- `std` (default): This enables runtime arch detection for accelerated SIMD
20+
instructions, and an FFT plan that measures the various implementations to
21+
choose the fastest one at runtime.
22+
- `nightly`: This enables unstable Rust features to further speed up the FFT,
23+
by enabling AVX512F instructions on CPUs that support them. This feature
24+
requires a nightly Rust
25+
toolchain.
26+
- `serde`: This enables serialization and deserialization functions for the
27+
unordered plan. These allow for data in the Fourier domain to be serialized
28+
from the permuted order to the standard order, and deserialized from the
29+
standard order to the permuted order. This is needed since the inverse
30+
transform must be used with the same plan that computed/deserialized the
31+
forward transform (or more specifically, a plan with the same internal base
32+
FFT size).
33+
34+
## Example
35+
36+
```rust
37+
use concrete_fft::c64;
38+
use concrete_fft::ordered::{Plan, Method};
39+
use dyn_stack::{DynStack, GlobalMemBuffer, ReborrowMut};
40+
use num_complex::ComplexFloat;
41+
use std::time::Duration;
42+
43+
const N: usize = 4;
44+
let plan = Plan::new(4, Method::Measure(Duration::from_millis(10)));
45+
let mut scratch_memory = GlobalMemBuffer::new(plan.fft_scratch().unwrap());
46+
let mut stack = DynStack::new(&mut scratch_memory);
47+
48+
let data = [
49+
c64::new(1.0, 0.0),
50+
c64::new(2.0, 0.0),
51+
c64::new(3.0, 0.0),
52+
c64::new(4.0, 0.0),
53+
];
54+
55+
let mut transformed_fwd = data;
56+
plan.fwd(&mut transformed_fwd, stack.rb_mut());
57+
58+
let mut transformed_inv = transformed_fwd;
59+
plan.inv(&mut transformed_inv, stack.rb_mut());
60+
61+
for (actual, expected) in transformed_inv.iter().map(|z| z / N as f64).zip(data) {
62+
assert!((expected - actual).abs() < 1e-9);
63+
}
64+
```
65+
66+
## Links
67+
68+
- [Zama](https://www.zama.ai/)
69+
- [Concrete](https://github.com/zama-ai/concrete)
70+
71+
## License
72+
73+
This software is distributed under the BSD-3-Clause-Clear license with an
74+
exemption that gives rights to use our patents for research, evaluation and
75+
prototyping purposes, as well as for your personal projects.
76+
77+
If you want to use Concrete in a commercial product however, you will need to
78+
purchase a separate commercial licence.
79+
80+
If you have any questions, please contact us at `[email protected].`

0 commit comments

Comments
 (0)