1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! # Shape Contracts
//!
//! This is a ``no_std`` inline contract programming library for tensor geometry
//! for the [burner](https://burn.dev) tensor framework.
//!
//! Contract programming, or [Design by Contract](https://en.wikipedia.org/wiki/Design_by_contract),
//! is a programming paradigm that specifies the rights and obligations of
//! software components.
//!
//! The goal of this library is to make in-line geometry contracts:
//! * Easy to Read, Write, and Use,
//! * Performant at Runtime (so they can always be enabled),
//! * Verbose and Helpful in their error messages.
//!
//! ## Features
//!
//! - ``burner``: Shape support for [burner](https://burn.dev) types:
//! - `&Tensor`, `&Shape`, `Shape`.
//!
//! ## API
//!
//! Users will primarily use the macros:
//! - [`unpack_shape_contract`],
//! - [`assert_shape_contract`], and
//! - [`assert_shape_contract_periodically`].
//!
//! For example:
//! ```rust,no_run
//! use bunsen::contracts::unpack_shape_contract;
//!
//! let shape = [12, 3 * 4, 5 * 4, 3];
//!
//! // In release builds, this has a benchmark of ~160ns:
//! let [b, h_wins, w_wins, c] = unpack_shape_contract!(
//! [
//! "batch",
//! "height" = "h_wins" * "window_size",
//! "width" = "w_wins" * "window_size",
//! "channels"
//! ],
//! &shape,
//! &["batch", "h_wins", "w_wins", "channels"],
//! &[("window_size", 4)],
//! );
//!
//! assert_eq!(b, 12);
//! assert_eq!(h_wins, 3);
//! assert_eq!(w_wins, 4);
//! assert_eq!(c, 3);
//! ```
//!
//! In turn, these macros wrap the layer 2 api:
//! * [`shape_contract`] - a macro for defining shape contracts from
//! expressions.
//! * [`define_shape_contract`] - a macro for defining a static contract.
//! * [`ShapeContract`] - the constructed contract type.
//! * [`ShapeContract::assert_shape`] - assert a contract.
//! * [`ShapeContract::unpack_shape`] - assert a contract, and unpack geometry
//! components.
//! * [`run_periodically`] - a macro for running code on an incrementally
//! lengthening schedule.
//!
//! ### `ShapeView` Support
//!
//! The shape methods take a [`ShapeView`] parameter; with implementations
//! for:
//! * ``&[usize]``, ``&[usize; D]``,
//! * ``&[u32]``, ``&[u32; D]``,
//! * ``&[i32]``, ``&[i32; D]``,
//! * ``&Vec<usize>``,
//! * ``&Vec<u32>``,
//! * ``&Vec<i32>``
//!
//! With ``features = ["burner"]``:
//! * ``burner::prelude::Shape``,
//! * ``&burner::prelude::Shape``,
//! * ``&burner::prelude::Tensor``
//!
//! ## Speed and Stack Design
//!
//! Contracts are only useful when they are fast enough to be always enabled.
//!
//! As a result, this library is designed to be fast at runtime,
//! focusing on `static` contracts and using stack over heap wherever possible.
//!
//! Benchmarks on release builds are available under ``cargo bench -p
//! bunsen-contracts``:
//!
//! ```terminaloutput
//! Running benches/shape_contracts (target/release/deps/contracts-86950340ff3748c1)
//! unpack_shape time: [176.03 ns 177.39 ns 178.81 ns]
//! Found 2 outliers among 100 measurements (2.00%)
//! 1 (1.00%) high mild
//! 1 (1.00%) high severe
//!
//! assert_shape time: [166.57 ns 168.00 ns 169.60 ns]
//! Found 2 outliers among 100 measurements (2.00%)
//! 1 (1.00%) high mild
//! 1 (1.00%) high severe
//!
//! assert_shape_every_nth/assert_shape_every_nth
//! time: [4.4057 ns 4.4769 ns 4.5726 ns]
//! Found 14 outliers among 100 measurements (14.00%)
//! 6 (6.00%) high mild
//! 8 (8.00%) high severe
//! ```
//!
//! ## `shape_contract`! macro
//!
//! The `shape_contract!` macro is a compile-time macro that parses a shape
//! contract from a shape contract pattern:
//!
//! ```rust
//! use bunsen::contracts::{
//! ShapeContract,
//! shape_contract,
//! };
//! static CONTRACT: ShapeContract =
//! shape_contract![_, "w" = "x" + "y", ..., "z" ^ 2];
//! ```
//!
//! A shape pattern is made of one or more dimension matcher terms:
//! - `_`: for any shape; ignores the size, but requires the dimension to
//! exist.,
//! - `...`: for ellipsis; matches any number of dimensions, only one ellipsis
//! is allowed,
//! - a dim expression.
//!
//! ```bnf
//! ShapeContract => <LabeledExpr> { ',' <LabeledExpr> }* ','?
//! LabeledExpr => {Param "="}? <Expr>
//! Expr => <Term> { <AddOp> <Term> }
//! Term => <Power> { <MulOp> <Power> }
//! Power => <Factor> [ ^ <usize> ]
//! Factor => <Param> | ( '(' <Expression> ')' ) | NegOp <Factor>
//! Param => '"' <identifier> '"'
//! identifier => { <alpha> | "_" } { <alphanumeric> | "_" }*
//! NegOp => '+' | '-'
//! AddOp => '+' | '-'
//! MulOp => '*'
//! ```
//!
//! ## Usage Example
//!
//! ```rust,ignore
//! use burner::prelude::{Tensor, Backend};
//! use burner::tensor::BasicOps;
//! use bunsen::contracts::{unpack_shape_contract, assert_shape_contract_periodically};
//!
//! /// Window Partition
//! ///
//! /// ## Parameters
//! ///
//! /// - `tensor`: Input tensor of shape (B, h_wins * window_size, w_wins * window_size, C).
//! /// - `window_size`: Window size.
//! ///
//! /// ## Returns
//! ///
//! /// Output tensor of shape (B * h_windows * w_windows, window_size, window_size, C).
//! ///
//! /// ## Panics
//! ///
//! /// Panics if the input tensor does not have 4 dimensions.
//! pub fn window_partition<B: Backend, K>(
//! tensor: Tensor<B, 4, K>,
//! window_size: usize,
//! ) -> Tensor<B, 4, K>
//! where
//! K: BasicOps<B>,
//! {
//! // In release builds, this has a benchmark of ~160ns:
//! let [b, h_wins, w_wins, c] = unpack_shape_contract!(
//! [
//! "batch",
//! "height" = "h_wins" * "window_size",
//! "width" = "w_wins" * "window_size",
//! "channels"
//! ],
//! &tensor,
//! &["batch", "h_wins", "w_wins", "channels"],
//! &[("window_size", window_size)],
//! );
//!
//! let tensor = tensor
//! .reshape([b, h_wins, window_size, w_wins, window_size, c])
//! .swap_dims(2, 3)
//! .reshape([b * h_wins * w_wins, window_size, window_size, c]);
//!
//! // Run an amortized check on the output shape.
//! //
//! // `run_periodically!{}` runs the first 10 times,
//! // then on an incrementally lengthening schedule,
//! // until it reaches its default period of 1000.
//! //
//! // Due to amortization, in release builds, this averages ~4ns:
//! assert_shape_contract_periodically!(
//! [
//! "batch" * "h_wins" * "w_wins",
//! "window_size",
//! "window_size",
//! "channels"
//! ],
//! &tensor,
//! &[
//! ("batch", b),
//! ("h_wins", h_wins),
//! ("w_wins", w_wins),
//! ("window_size", window_size),
//! ("channels", c),
//! ]
//! );
//!
//! tensor
//! }
//! ```
//! ## Error Messages
//!
//! Error messages are verbose and helpful.
//!
//! ```rust
//! use bunsen::contracts::{
//! ShapeContract,
//! shape_contract,
//! };
//! use indoc::indoc;
//!
//! fn example() {
//! static CONTRACT: ShapeContract = shape_contract![
//! ...,
//! "height" = "h_wins" * "window",
//! "width" = "w_wins" * "window",
//! "color",
//! ];
//!
//! let h_wins = 2;
//! let w_wins = 3;
//! let window = 4;
//! let color = 3;
//!
//! let shape = [1, 2, 3, h_wins * window, w_wins * window, color];
//!
//! let [h, w] = CONTRACT.unpack_shape(
//! &shape,
//! &["h_wins", "w_wins"],
//! &[("window", window), ("color", color)],
//! );
//! assert_eq!(h, h_wins);
//! assert_eq!(w, w_wins);
//!
//! assert_eq!(
//! CONTRACT
//! .try_unpack_shape(
//! &shape,
//! &["h_wins", "w_wins"],
//! &[("window", window + 1), ("color", color),]
//! )
//! .unwrap_err(),
//! indoc! {r#"
//! Shape Error:: 8 !~ height=(h_wins*window) :: No integer solution.
//! shape:
//! [1, 2, 3, 8, 12, 3]
//! expected:
//! [..., height=(h_wins*window), width=(w_wins*window), color]
//! {"window": 5, "color": 3}"#
//! },
//! );
//! }
//! ```
pub use ;
pub use *;
pub use *;
pub use *;
pub use *;