vyre-libs 0.7.2

vyre Category A library ecosystem - pure-IR compositions over foundation IR and primitive-owned kernels
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Floating depthwise causal convolution for sequence models.

use thiserror::Error;
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program, UnOp};

use crate::region::wrap_anonymous;

const OP_ID: &str = "vyre-libs::nn::depthwise_causal_conv1d";

/// Optional post-convolution activation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CausalConvActivation {
    /// Return the affine convolution result.
    None,
    /// Apply SiLU in F32 before source-dtype conversion.
    Silu,
}

/// Invalid depthwise causal convolution construction.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum DepthwiseCausalConv1dError {
    /// One required tensor dimension is zero.
    #[error("depthwise causal convolution requires nonzero batch, channels, sequence, and kernel dimensions")]
    EmptyShape,
    /// One flattened tensor size exceeds u32 indexing.
    #[error("depthwise causal convolution tensor element count overflows u32; split the tensor")]
    ElementCountOverflow,
    /// Streaming state is unnecessary for a pointwise kernel.
    #[error("causal convolution state update requires kernel >= 2")]
    StateKernelTooShort,
    /// Source dtype lacks the required floating conversion contract.
    #[error("depthwise causal convolution supports F16, BF16, or F32 tensors; got {dtype:?}")]
    UnsupportedDtype {
        /// Rejected dtype.
        dtype: DataType,
    },
}

/// Build channel-major left-padded depthwise causal convolution.
///
/// `input` and `output` use `[batch, channels, sequence]`; `weight` uses
/// `[channels, kernel]`; optional `bias` uses `[channels]`; optional `mask`
/// uses U32 `[batch, sequence]`, where zero excludes an input position.
/// Accumulation and optional SiLU execute in F32 before one output conversion.
///
/// # Errors
///
/// Returns [`DepthwiseCausalConv1dError`] for empty/overflowing shapes or an
/// unsupported source dtype.
#[allow(clippy::too_many_arguments)]
pub fn depthwise_causal_conv1d(
    input: &str,
    weight: &str,
    bias: Option<&str>,
    mask: Option<&str>,
    output: &str,
    batch: u32,
    channels: u32,
    sequence: u32,
    kernel: u32,
    activation: CausalConvActivation,
    dtype: DataType,
) -> Result<Program, DepthwiseCausalConv1dError> {
    if batch == 0 || channels == 0 || sequence == 0 || kernel == 0 {
        return Err(DepthwiseCausalConv1dError::EmptyShape);
    }
    if !matches!(dtype, DataType::F16 | DataType::BF16 | DataType::F32) {
        return Err(DepthwiseCausalConv1dError::UnsupportedDtype { dtype });
    }
    let channel_sequence = channels
        .checked_mul(sequence)
        .ok_or(DepthwiseCausalConv1dError::ElementCountOverflow)?;
    let total = batch
        .checked_mul(channel_sequence)
        .ok_or(DepthwiseCausalConv1dError::ElementCountOverflow)?;
    let weight_count = channels
        .checked_mul(kernel)
        .ok_or(DepthwiseCausalConv1dError::ElementCountOverflow)?;
    let mask_count = batch
        .checked_mul(sequence)
        .ok_or(DepthwiseCausalConv1dError::ElementCountOverflow)?;

    let index = Expr::var("index");
    let batch_index = Expr::div(index.clone(), Expr::u32(channel_sequence));
    let within_batch = Expr::sub(
        index.clone(),
        Expr::mul(batch_index.clone(), Expr::u32(channel_sequence)),
    );
    let channel = Expr::div(within_batch.clone(), Expr::u32(sequence));
    let time = Expr::sub(
        within_batch.clone(),
        Expr::mul(channel.clone(), Expr::u32(sequence)),
    );
    let initial = bias.map_or_else(
        || Expr::f32(0.0),
        |name| Expr::cast(DataType::F32, Expr::load(name, channel.clone())),
    );
    let lag = Expr::sub(Expr::u32(kernel - 1), Expr::var("kernel_index"));
    let position = Expr::sub(Expr::var("time"), lag.clone());
    let input_index = Expr::add(
        Expr::mul(Expr::var("batch"), Expr::u32(channel_sequence)),
        Expr::add(
            Expr::mul(Expr::var("channel"), Expr::u32(sequence)),
            Expr::var("position"),
        ),
    );
    let product = Expr::mul(
        Expr::cast(DataType::F32, Expr::load(input, input_index)),
        Expr::cast(
            DataType::F32,
            Expr::load(
                weight,
                Expr::add(
                    Expr::mul(Expr::var("channel"), Expr::u32(kernel)),
                    Expr::var("kernel_index"),
                ),
            ),
        ),
    );
    let accumulate = Node::assign("accumulator", Expr::add(Expr::var("accumulator"), product));
    let valid_body = if let Some(mask_name) = mask {
        vec![
            Node::let_bind("position", position),
            Node::if_then(
                Expr::ne(
                    Expr::load(
                        mask_name,
                        Expr::add(
                            Expr::mul(Expr::var("batch"), Expr::u32(sequence)),
                            Expr::var("position"),
                        ),
                    ),
                    Expr::u32(0),
                ),
                vec![accumulate],
            ),
        ]
    } else {
        vec![Node::let_bind("position", position), accumulate]
    };
    let activated = match activation {
        CausalConvActivation::None => Expr::var("accumulator"),
        CausalConvActivation::Silu => {
            let value = Expr::var("accumulator");
            Expr::div(
                value.clone(),
                Expr::add(
                    Expr::f32(1.0),
                    Expr::UnOp {
                        op: UnOp::Exp,
                        operand: Box::new(Expr::UnOp {
                            op: UnOp::Negate,
                            operand: Box::new(value),
                        }),
                    },
                ),
            )
        }
    };
    let body = vec![
        Node::let_bind("index", Expr::InvocationId { axis: 0 }),
        Node::if_then(
            Expr::lt(index.clone(), Expr::u32(total)),
            vec![
                Node::let_bind("batch", batch_index),
                Node::let_bind("within_batch", within_batch),
                Node::let_bind("channel", channel),
                Node::let_bind("time", time),
                Node::let_bind("accumulator", initial),
                Node::loop_for(
                    "kernel_index",
                    Expr::u32(0),
                    Expr::u32(kernel),
                    vec![Node::if_then(Expr::ge(Expr::var("time"), lag), valid_body)],
                ),
                Node::Store {
                    buffer: output.into(),
                    index,
                    value: Expr::cast(dtype.clone(), activated),
                },
            ],
        ),
    ];

    let mut buffers = Vec::with_capacity(5);
    buffers.push(
        BufferDecl::storage(input, 0, BufferAccess::ReadOnly, dtype.clone()).with_count(total),
    );
    buffers.push(
        BufferDecl::storage(weight, 1, BufferAccess::ReadOnly, dtype.clone())
            .with_count(weight_count),
    );
    let mut binding = 2;
    if let Some(name) = bias {
        buffers.push(
            BufferDecl::storage(name, binding, BufferAccess::ReadOnly, dtype.clone())
                .with_count(channels),
        );
        binding += 1;
    }
    if let Some(name) = mask {
        buffers.push(
            BufferDecl::storage(name, binding, BufferAccess::ReadOnly, DataType::U32)
                .with_count(mask_count),
        );
        binding += 1;
    }
    buffers.push(BufferDecl::output(output, binding, dtype).with_count(total));
    Ok(Program::wrapped(
        buffers,
        [64, 1, 1],
        vec![wrap_anonymous(OP_ID, body)],
    ))
}

/// Build short-chunk convolution and its next explicit loop-carried state.
///
/// State tensors use `[batch, channels, kernel - 1]`. The next state is a
/// separate output, so runtimes may recycle prior-state storage only after all
/// reads finish.
#[allow(clippy::too_many_arguments)]
pub fn depthwise_causal_conv1d_update(
    input: &str,
    weight: &str,
    bias: Option<&str>,
    state_input: &str,
    output: &str,
    state_output: &str,
    batch: u32,
    channels: u32,
    chunk: u32,
    kernel: u32,
    activation: CausalConvActivation,
    dtype: DataType,
) -> Result<Program, DepthwiseCausalConv1dError> {
    if kernel < 2 {
        return Err(DepthwiseCausalConv1dError::StateKernelTooShort);
    }
    if batch == 0 || channels == 0 || chunk == 0 {
        return Err(DepthwiseCausalConv1dError::EmptyShape);
    }
    if !matches!(dtype, DataType::F16 | DataType::BF16 | DataType::F32) {
        return Err(DepthwiseCausalConv1dError::UnsupportedDtype { dtype });
    }
    let state_len = kernel - 1;
    let channel_chunk = channels
        .checked_mul(chunk)
        .ok_or(DepthwiseCausalConv1dError::ElementCountOverflow)?;
    let output_count = batch
        .checked_mul(channel_chunk)
        .ok_or(DepthwiseCausalConv1dError::ElementCountOverflow)?;
    let channel_state = channels
        .checked_mul(state_len)
        .ok_or(DepthwiseCausalConv1dError::ElementCountOverflow)?;
    let state_count = batch
        .checked_mul(channel_state)
        .ok_or(DepthwiseCausalConv1dError::ElementCountOverflow)?;
    let weight_count = channels
        .checked_mul(kernel)
        .ok_or(DepthwiseCausalConv1dError::ElementCountOverflow)?;

    let initial = bias.map_or_else(
        || Expr::f32(0.0),
        |name| Expr::cast(DataType::F32, Expr::load(name, Expr::var("output_channel"))),
    );
    let state_sample = Expr::load(
        state_input,
        Expr::add(
            Expr::mul(Expr::var("output_batch"), Expr::u32(channel_state)),
            Expr::add(
                Expr::mul(Expr::var("output_channel"), Expr::u32(state_len)),
                Expr::var("combined"),
            ),
        ),
    );
    let input_sample = Expr::load(
        input,
        Expr::add(
            Expr::mul(Expr::var("output_batch"), Expr::u32(channel_chunk)),
            Expr::add(
                Expr::mul(Expr::var("output_channel"), Expr::u32(chunk)),
                Expr::sub(Expr::var("combined"), Expr::u32(state_len)),
            ),
        ),
    );
    let weight_sample = Expr::load(
        weight,
        Expr::add(
            Expr::mul(Expr::var("output_channel"), Expr::u32(kernel)),
            Expr::var("kernel_index"),
        ),
    );
    let activated = match activation {
        CausalConvActivation::None => Expr::var("accumulator"),
        CausalConvActivation::Silu => {
            let value = Expr::var("accumulator");
            Expr::div(
                value.clone(),
                Expr::add(
                    Expr::f32(1.0),
                    Expr::UnOp {
                        op: UnOp::Exp,
                        operand: Box::new(Expr::UnOp {
                            op: UnOp::Negate,
                            operand: Box::new(value),
                        }),
                    },
                ),
            )
        }
    };
    let output_body = vec![
        Node::let_bind(
            "output_batch",
            Expr::div(Expr::var("dispatch_index"), Expr::u32(channel_chunk)),
        ),
        Node::let_bind(
            "output_remainder",
            Expr::sub(
                Expr::var("dispatch_index"),
                Expr::mul(Expr::var("output_batch"), Expr::u32(channel_chunk)),
            ),
        ),
        Node::let_bind(
            "output_channel",
            Expr::div(Expr::var("output_remainder"), Expr::u32(chunk)),
        ),
        Node::let_bind(
            "output_time",
            Expr::sub(
                Expr::var("output_remainder"),
                Expr::mul(Expr::var("output_channel"), Expr::u32(chunk)),
            ),
        ),
        Node::let_bind("accumulator", initial),
        Node::loop_for(
            "kernel_index",
            Expr::u32(0),
            Expr::u32(kernel),
            vec![
                Node::let_bind(
                    "combined",
                    Expr::add(Expr::var("output_time"), Expr::var("kernel_index")),
                ),
                Node::let_bind("sample", Expr::f32(0.0)),
                Node::if_then(
                    Expr::lt(Expr::var("combined"), Expr::u32(state_len)),
                    vec![Node::assign(
                        "sample",
                        Expr::cast(DataType::F32, state_sample),
                    )],
                ),
                Node::if_then(
                    Expr::ge(Expr::var("combined"), Expr::u32(state_len)),
                    vec![Node::assign(
                        "sample",
                        Expr::cast(DataType::F32, input_sample),
                    )],
                ),
                Node::assign(
                    "accumulator",
                    Expr::add(
                        Expr::var("accumulator"),
                        Expr::mul(
                            Expr::var("sample"),
                            Expr::cast(DataType::F32, weight_sample),
                        ),
                    ),
                ),
            ],
        ),
        Node::Store {
            buffer: output.into(),
            index: Expr::var("dispatch_index"),
            value: Expr::cast(dtype.clone(), activated),
        },
    ];

    let prior_state_tail = Expr::load(
        state_input,
        Expr::add(
            Expr::mul(Expr::var("state_batch"), Expr::u32(channel_state)),
            Expr::add(
                Expr::mul(Expr::var("state_channel"), Expr::u32(state_len)),
                Expr::var("state_combined"),
            ),
        ),
    );
    let input_tail = Expr::load(
        input,
        Expr::add(
            Expr::mul(Expr::var("state_batch"), Expr::u32(channel_chunk)),
            Expr::add(
                Expr::mul(Expr::var("state_channel"), Expr::u32(chunk)),
                Expr::sub(Expr::var("state_combined"), Expr::u32(state_len)),
            ),
        ),
    );
    let state_body = vec![
        Node::let_bind(
            "state_batch",
            Expr::div(Expr::var("dispatch_index"), Expr::u32(channel_state)),
        ),
        Node::let_bind(
            "state_remainder",
            Expr::sub(
                Expr::var("dispatch_index"),
                Expr::mul(Expr::var("state_batch"), Expr::u32(channel_state)),
            ),
        ),
        Node::let_bind(
            "state_channel",
            Expr::div(Expr::var("state_remainder"), Expr::u32(state_len)),
        ),
        Node::let_bind(
            "state_offset",
            Expr::sub(
                Expr::var("state_remainder"),
                Expr::mul(Expr::var("state_channel"), Expr::u32(state_len)),
            ),
        ),
        Node::let_bind(
            "state_combined",
            Expr::add(Expr::u32(chunk), Expr::var("state_offset")),
        ),
        Node::let_bind("next_state_value", Expr::f32(0.0)),
        Node::if_then(
            Expr::lt(Expr::var("state_combined"), Expr::u32(state_len)),
            vec![Node::assign(
                "next_state_value",
                Expr::cast(DataType::F32, prior_state_tail),
            )],
        ),
        Node::if_then(
            Expr::ge(Expr::var("state_combined"), Expr::u32(state_len)),
            vec![Node::assign(
                "next_state_value",
                Expr::cast(DataType::F32, input_tail),
            )],
        ),
        Node::Store {
            buffer: state_output.into(),
            index: Expr::var("dispatch_index"),
            value: Expr::cast(dtype.clone(), Expr::var("next_state_value")),
        },
    ];
    let body = vec![
        Node::let_bind("dispatch_index", Expr::InvocationId { axis: 0 }),
        Node::if_then(
            Expr::lt(Expr::var("dispatch_index"), Expr::u32(output_count)),
            output_body,
        ),
        Node::if_then(
            Expr::lt(Expr::var("dispatch_index"), Expr::u32(state_count)),
            state_body,
        ),
    ];

    let mut buffers = vec![
        BufferDecl::storage(input, 0, BufferAccess::ReadOnly, dtype.clone())
            .with_count(output_count),
        BufferDecl::storage(weight, 1, BufferAccess::ReadOnly, dtype.clone())
            .with_count(weight_count),
        BufferDecl::storage(state_input, 2, BufferAccess::ReadWrite, dtype.clone())
            .with_count(state_count),
    ];
    let mut binding = 3;
    if let Some(name) = bias {
        buffers.push(
            BufferDecl::storage(name, binding, BufferAccess::ReadOnly, dtype.clone())
                .with_count(channels),
        );
        binding += 1;
    }
    buffers.push(BufferDecl::output(output, binding, dtype.clone()).with_count(output_count));
    buffers.push(
        BufferDecl::storage(state_output, binding + 1, BufferAccess::ReadWrite, dtype)
            .with_count(state_count),
    );
    Ok(Program::wrapped(
        buffers,
        [64, 1, 1],
        vec![wrap_anonymous(
            "vyre-libs::nn::depthwise_causal_conv1d_update",
            body,
        )],
    ))
}