use cubek_test_utils::CatalogEntry;
use crate::{DepthwiseStrategy, DepthwiseTiling};
#[derive(Clone, Copy)]
pub struct DepthwiseProblem {
pub batch: usize,
pub channels: usize,
pub size: usize,
pub kernel: usize,
pub stride: usize,
pub dilation: usize,
}
impl DepthwiseProblem {
pub fn padding(&self) -> usize {
self.dilation * (self.kernel - 1) / 2
}
pub fn out_size(&self) -> usize {
let reach = self.dilation * (self.kernel - 1) + 1;
(self.size + 2 * self.padding() - reach) / self.stride + 1
}
pub fn in_shape(&self) -> [usize; 4] {
[self.batch, self.size, self.size, self.channels]
}
pub fn weight_shape(&self) -> [usize; 4] {
[self.channels, self.kernel, self.kernel, 1]
}
pub fn out_shape(&self) -> [usize; 4] {
let out = self.out_size();
[self.batch, out, out, self.channels]
}
}
const B4: [(&str, usize, usize, usize, usize, usize, usize); 14] = [
("b23_1632c_48px_k5_d2", 1632, 48, 5, 1, 2, 7),
("b11_672c_48px_k3", 672, 48, 3, 1, 1, 5),
("b17_960c_48px_k5", 960, 48, 5, 1, 1, 5),
("b3_192c_192px_k3", 192, 192, 3, 1, 1, 3),
("b7_336c_96px_k5", 336, 96, 5, 1, 1, 3),
("b31_2688c_48px_k3_d2", 2688, 48, 3, 1, 2, 1),
("b30_1632c_48px_k3_d2", 1632, 48, 3, 1, 2, 1),
("b22_960c_48px_k5_d2", 960, 48, 5, 1, 2, 1),
("b16_672c_48px_k5", 672, 48, 5, 1, 1, 1),
("b10_336c_96px_k3_s2", 336, 96, 3, 2, 1, 1),
("b6_192c_192px_k5_s2", 192, 192, 5, 2, 1, 1),
("b2_144c_384px_k3_s2", 144, 384, 3, 2, 1, 1),
("b0_48c_384px_k3", 48, 384, 3, 1, 1, 1),
("b1_24c_384px_k3", 24, 384, 3, 1, 1, 1),
];
fn batch() -> usize {
std::env::var("CUBEK_BENCH_BATCH")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(4)
}
pub fn blocks_running(id: &str) -> usize {
B4.iter()
.find(|entry| entry.0 == id)
.map(|entry| entry.6)
.unwrap_or(0)
}
pub fn problems() -> Vec<CatalogEntry<DepthwiseProblem>> {
let batch = batch();
B4.iter()
.map(|&(id, channels, size, kernel, stride, dilation, repeats)| {
let problem = DepthwiseProblem {
batch,
channels,
size,
kernel,
stride,
dilation,
};
let label = format!(
"b{batch} {channels}c {size}px k{kernel} s{stride} d{dilation} (x{repeats})"
);
CatalogEntry::new(id, label, problem)
})
.collect()
}
pub fn strategies() -> Vec<CatalogEntry<DepthwiseStrategy>> {
let mut out = vec![CatalogEntry::new(
"routine",
"the routine's own choice",
DepthwiseStrategy::Routine,
)];
for lines in [1, 2, 4] {
for (rows, cols) in [
(2, 2),
(2, 4),
(4, 2),
(4, 4),
(4, 8),
(8, 2),
(8, 4),
(8, 8),
(16, 4),
] {
out.push(CatalogEntry::new(
format!("r{rows}_c{cols}_l{lines}"),
format!("{rows} rows, {cols} cols, {lines}-wide lines"),
DepthwiseStrategy::Fixed(DepthwiseTiling {
rows,
cols,
chans: 1,
lines,
}),
));
}
}
out
}