Skip to content

Commit 7c37d2d

Browse files
committed
Add pretty, ui, and feature-gate tests for the enzyme/autodiff frontend
1 parent 624c071 commit 7c37d2d

15 files changed

+744
-0
lines changed

tests/pretty/autodiff_forward.pp

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
//@ needs-enzyme
4+
5+
#![feature(autodiff)]
6+
#[prelude_import]
7+
use ::std::prelude::rust_2015::*;
8+
#[macro_use]
9+
extern crate std;
10+
//@ pretty-mode:expanded
11+
//@ pretty-compare-only
12+
//@ pp-exact:autodiff_forward.pp
13+
14+
// Test that forward mode ad macros are expanded correctly.
15+
16+
use std::autodiff::autodiff;
17+
18+
#[rustc_autodiff]
19+
#[inline(never)]
20+
pub fn f1(x: &[f64], y: f64) -> f64 {
21+
22+
23+
24+
// Not the most interesting derivative, but who are we to judge
25+
26+
// We want to be sure that the same function can be differentiated in different ways
27+
28+
::core::panicking::panic("not implemented")
29+
}
30+
#[rustc_autodiff(Forward, Dual, Const, Dual,)]
31+
#[inline(never)]
32+
pub fn df1(x: &[f64], bx: &[f64], y: f64) -> (f64, f64) {
33+
unsafe { asm!("NOP", options(pure, nomem)); };
34+
::core::hint::black_box(f1(x, y));
35+
::core::hint::black_box((bx,));
36+
::core::hint::black_box((f1(x, y), f64::default()))
37+
}
38+
#[rustc_autodiff]
39+
#[inline(never)]
40+
pub fn f2(x: &[f64], y: f64) -> f64 {
41+
::core::panicking::panic("not implemented")
42+
}
43+
#[rustc_autodiff(Forward, Dual, Const, Const,)]
44+
#[inline(never)]
45+
pub fn df2(x: &[f64], bx: &[f64], y: f64) -> f64 {
46+
unsafe { asm!("NOP", options(pure, nomem)); };
47+
::core::hint::black_box(f2(x, y));
48+
::core::hint::black_box((bx,));
49+
::core::hint::black_box(f2(x, y))
50+
}
51+
#[rustc_autodiff]
52+
#[inline(never)]
53+
pub fn f3(x: &[f64], y: f64) -> f64 {
54+
::core::panicking::panic("not implemented")
55+
}
56+
#[rustc_autodiff(ForwardFirst, Dual, Const, Const,)]
57+
#[inline(never)]
58+
pub fn df3(x: &[f64], bx: &[f64], y: f64) -> f64 {
59+
unsafe { asm!("NOP", options(pure, nomem)); };
60+
::core::hint::black_box(f3(x, y));
61+
::core::hint::black_box((bx,));
62+
::core::hint::black_box(f3(x, y))
63+
}
64+
#[rustc_autodiff]
65+
#[inline(never)]
66+
pub fn f4() {}
67+
#[rustc_autodiff(Forward, None)]
68+
#[inline(never)]
69+
pub fn df4() {
70+
unsafe { asm!("NOP", options(pure, nomem)); };
71+
::core::hint::black_box(f4());
72+
::core::hint::black_box(());
73+
}
74+
#[rustc_autodiff]
75+
#[inline(never)]
76+
#[rustc_autodiff]
77+
#[inline(never)]
78+
#[rustc_autodiff]
79+
#[inline(never)]
80+
pub fn f5(x: &[f64], y: f64) -> f64 {
81+
::core::panicking::panic("not implemented")
82+
}
83+
#[rustc_autodiff(Forward, Const, Dual, Const,)]
84+
#[inline(never)]
85+
pub fn df5_y(x: &[f64], y: f64, by: f64) -> f64 {
86+
unsafe { asm!("NOP", options(pure, nomem)); };
87+
::core::hint::black_box(f5(x, y));
88+
::core::hint::black_box((by,));
89+
::core::hint::black_box(f5(x, y))
90+
}
91+
#[rustc_autodiff(Forward, Dual, Const, Const,)]
92+
#[inline(never)]
93+
pub fn df5_x(x: &[f64], bx: &[f64], y: f64) -> f64 {
94+
unsafe { asm!("NOP", options(pure, nomem)); };
95+
::core::hint::black_box(f5(x, y));
96+
::core::hint::black_box((bx,));
97+
::core::hint::black_box(f5(x, y))
98+
}
99+
#[rustc_autodiff(Reverse, Duplicated, Const, Active,)]
100+
#[inline(never)]
101+
pub fn df5_rev(x: &[f64], dx: &mut [f64], y: f64, dret: f64) -> f64 {
102+
unsafe { asm!("NOP", options(pure, nomem)); };
103+
::core::hint::black_box(f5(x, y));
104+
::core::hint::black_box((dx, dret));
105+
::core::hint::black_box(f5(x, y))
106+
}
107+
fn main() {}

tests/pretty/autodiff_forward.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ needs-enzyme
2+
3+
#![feature(autodiff)]
4+
//@ pretty-mode:expanded
5+
//@ pretty-compare-only
6+
//@ pp-exact:autodiff_forward.pp
7+
8+
// Test that forward mode ad macros are expanded correctly.
9+
10+
use std::autodiff::autodiff;
11+
12+
#[autodiff(df1, Forward, Dual, Const, Dual)]
13+
pub fn f1(x: &[f64], y: f64) -> f64 {
14+
unimplemented!()
15+
}
16+
17+
#[autodiff(df2, Forward, Dual, Const, Const)]
18+
pub fn f2(x: &[f64], y: f64) -> f64 {
19+
unimplemented!()
20+
}
21+
22+
#[autodiff(df3, ForwardFirst, Dual, Const, Const)]
23+
pub fn f3(x: &[f64], y: f64) -> f64 {
24+
unimplemented!()
25+
}
26+
27+
// Not the most interesting derivative, but who are we to judge
28+
#[autodiff(df4, Forward)]
29+
pub fn f4() {}
30+
31+
// We want to be sure that the same function can be differentiated in different ways
32+
#[autodiff(df5_rev, Reverse, Duplicated, Const, Active)]
33+
#[autodiff(df5_x, Forward, Dual, Const, Const)]
34+
#[autodiff(df5_y, Forward, Const, Dual, Const)]
35+
pub fn f5(x: &[f64], y: f64) -> f64 {
36+
unimplemented!()
37+
}
38+
39+
fn main() {}

tests/pretty/autodiff_reverse.pp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
//@ needs-enzyme
4+
5+
#![feature(autodiff)]
6+
#[prelude_import]
7+
use ::std::prelude::rust_2015::*;
8+
#[macro_use]
9+
extern crate std;
10+
//@ pretty-mode:expanded
11+
//@ pretty-compare-only
12+
//@ pp-exact:autodiff_reverse.pp
13+
14+
// Test that reverse mode ad macros are expanded correctly.
15+
16+
use std::autodiff::autodiff;
17+
18+
#[rustc_autodiff]
19+
#[inline(never)]
20+
pub fn f1(x: &[f64], y: f64) -> f64 {
21+
22+
// Not the most interesting derivative, but who are we to judge
23+
24+
25+
// What happens if we already have Reverse in type (enum variant decl) and value (enum variant
26+
// constructor) namespace? > It's expected to work normally.
27+
28+
29+
::core::panicking::panic("not implemented")
30+
}
31+
#[rustc_autodiff(Reverse, Duplicated, Const, Active,)]
32+
#[inline(never)]
33+
pub fn df1(x: &[f64], dx: &mut [f64], y: f64, dret: f64) -> f64 {
34+
unsafe { asm!("NOP", options(pure, nomem)); };
35+
::core::hint::black_box(f1(x, y));
36+
::core::hint::black_box((dx, dret));
37+
::core::hint::black_box(f1(x, y))
38+
}
39+
#[rustc_autodiff]
40+
#[inline(never)]
41+
pub fn f2() {}
42+
#[rustc_autodiff(Reverse, None)]
43+
#[inline(never)]
44+
pub fn df2() {
45+
unsafe { asm!("NOP", options(pure, nomem)); };
46+
::core::hint::black_box(f2());
47+
::core::hint::black_box(());
48+
}
49+
#[rustc_autodiff]
50+
#[inline(never)]
51+
pub fn f3(x: &[f64], y: f64) -> f64 {
52+
::core::panicking::panic("not implemented")
53+
}
54+
#[rustc_autodiff(ReverseFirst, Duplicated, Const, Active,)]
55+
#[inline(never)]
56+
pub fn df3(x: &[f64], dx: &mut [f64], y: f64, dret: f64) -> f64 {
57+
unsafe { asm!("NOP", options(pure, nomem)); };
58+
::core::hint::black_box(f3(x, y));
59+
::core::hint::black_box((dx, dret));
60+
::core::hint::black_box(f3(x, y))
61+
}
62+
enum Foo { Reverse, }
63+
use Foo::Reverse;
64+
#[rustc_autodiff]
65+
#[inline(never)]
66+
pub fn f4(x: f32) { ::core::panicking::panic("not implemented") }
67+
#[rustc_autodiff(Reverse, Const, None)]
68+
#[inline(never)]
69+
pub fn df4(x: f32) {
70+
unsafe { asm!("NOP", options(pure, nomem)); };
71+
::core::hint::black_box(f4(x));
72+
::core::hint::black_box(());
73+
}
74+
#[rustc_autodiff]
75+
#[inline(never)]
76+
pub fn f5(x: *const f32, y: &f32) {
77+
::core::panicking::panic("not implemented")
78+
}
79+
#[rustc_autodiff(Reverse, DuplicatedOnly, Duplicated, None)]
80+
#[inline(never)]
81+
pub unsafe fn df5(x: *const f32, dx: *mut f32, y: &f32, dy: &mut f32) {
82+
unsafe { asm!("NOP", options(pure, nomem)); };
83+
::core::hint::black_box(f5(x, y));
84+
::core::hint::black_box((dx, dy));
85+
}
86+
fn main() {}

tests/pretty/autodiff_reverse.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//@ needs-enzyme
2+
3+
#![feature(autodiff)]
4+
//@ pretty-mode:expanded
5+
//@ pretty-compare-only
6+
//@ pp-exact:autodiff_reverse.pp
7+
8+
// Test that reverse mode ad macros are expanded correctly.
9+
10+
use std::autodiff::autodiff;
11+
12+
#[autodiff(df1, Reverse, Duplicated, Const, Active)]
13+
pub fn f1(x: &[f64], y: f64) -> f64 {
14+
unimplemented!()
15+
}
16+
17+
// Not the most interesting derivative, but who are we to judge
18+
#[autodiff(df2, Reverse)]
19+
pub fn f2() {}
20+
21+
#[autodiff(df3, ReverseFirst, Duplicated, Const, Active)]
22+
pub fn f3(x: &[f64], y: f64) -> f64 {
23+
unimplemented!()
24+
}
25+
26+
enum Foo { Reverse }
27+
use Foo::Reverse;
28+
// What happens if we already have Reverse in type (enum variant decl) and value (enum variant
29+
// constructor) namespace? > It's expected to work normally.
30+
#[autodiff(df4, Reverse, Const)]
31+
pub fn f4(x: f32) {
32+
unimplemented!()
33+
}
34+
35+
#[autodiff(df5, Reverse, DuplicatedOnly, Duplicated)]
36+
pub fn f5(x: *const f32, y: &f32) {
37+
unimplemented!()
38+
}
39+
40+
fn main() {}

0 commit comments

Comments
 (0)