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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Tversky Loss - GPU-accelerated generalized Dice Loss
//!
//! **Deep Debt Principles**:
//! - ✅ Pure WGSL implementation (new shader!)
//! - ✅ Safe Rust wrapper (no unsafe code)
//! - ✅ Hardware-agnostic via WebGPU
//! - ✅ Complete implementation (production-ready for medical imaging)
//!
//! ## Algorithm
//!
//! ```text
//! TP = true positives (intersection: pred * target)
//! FP = false positives (pred_sum - intersection)
//! FN = false negatives (target_sum - intersection)
//!
//! Tversky Index = TP / (TP + alpha*FP + beta*FN)
//! Tversky Loss = 1 - Tversky Index
//! ```
//!
//! **Special Cases**:
//! - `alpha = beta = 0.5`: Equivalent to Dice Loss
//! - `alpha = beta = 1.0`: Equivalent to Tanimoto coefficient
//! - `alpha < beta`: Penalize false negatives more (recall-focused)
//! - `alpha > beta`: Penalize false positives more (precision-focused)
//!
//! **Implementation**: GPU reduction with workgroup shared memory
//!
//! **Key Properties**:
//! - Fine-grained control over FP/FN trade-off
//! - Handles class imbalance
//! - Directly optimizes precision-recall balance
//! - Generalizes Dice Loss
//!
//! **Used By**: Medical imaging, imbalanced segmentation, cost-sensitive tasks
//!
//! ## Usage
//!
//! ```rust,ignore
//! use barracuda::tensor::Tensor;
//!
//! let predictions = Tensor::randn(vec![4, 256, 256]).await?; // [batch, H, W]
//! let targets = Tensor::randn(vec![4, 256, 256]).await?;
//!
//! // alpha < beta: Penalize false negatives more (medical imaging)
//! let loss = predictions.tversky_loss(&targets, 0.3, 0.7, 1.0)?;
//! ```
use crate::device::compute_pipeline::ComputeDispatch;
use crate::error::{BarracudaError, Result};
use crate::tensor::Tensor;
/// Tversky loss parameters for WGSL shader
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct TverskyParams {
alpha: f32,
beta: f32,
smoothing: f32,
batch_size: u32,
elements_per_sample: u32,
}
/// Tversky Loss operation
///
/// **Deep Debt**: Uses new WGSL shader with workgroup reduction
pub struct TverskyLoss {
predictions: Tensor,
targets: Tensor,
alpha: f32,
beta: f32,
smoothing: f32,
}
impl TverskyLoss {
/// Create new Tversky loss operation
/// **Deep Debt**: Validates inputs and hyperparameters
/// # Errors
/// Returns [`Err`] if prediction and target shapes do not match, alpha or beta are not in [0, 1],
/// or smoothing is negative.
pub fn new(
predictions: Tensor,
targets: Tensor,
alpha: f32,
beta: f32,
smoothing: f32,
) -> Result<Self> {
// Validate shapes match
if predictions.shape() != targets.shape() {
return Err(BarracudaError::shape_mismatch(
predictions.shape().to_vec(),
targets.shape().to_vec(),
));
}
// Validate hyperparameters
if !(0.0..=1.0).contains(&alpha) {
return Err(BarracudaError::invalid_op(
"TverskyLoss",
format!("alpha must be in [0, 1], got {alpha}"),
));
}
if !(0.0..=1.0).contains(&beta) {
return Err(BarracudaError::invalid_op(
"TverskyLoss",
format!("beta must be in [0, 1], got {beta}"),
));
}
if smoothing < 0.0 {
return Err(BarracudaError::invalid_op(
"TverskyLoss",
format!("smoothing must be non-negative, got {smoothing}"),
));
}
Ok(Self {
predictions,
targets,
alpha,
beta,
smoothing,
})
}
/// WGSL shader source
fn shader() -> &'static str {
static SHADER: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
include_str!("../shaders/loss/tversky_loss_f64.wgsl").to_string()
});
&SHADER
}
/// Execute Tversky loss (GPU reduction)
///
/// **Deep Debt**: Efficient workgroup reduction for large segmentations
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn execute(self) -> Result<Tensor> {
let device = self.predictions.device();
// Determine batch and elements
let total_size = self.predictions.len();
let batch_size = if self.predictions.shape().is_empty() {
1
} else {
self.predictions.shape()[0]
};
let elements_per_sample = total_size / batch_size;
// Create parameters
let params = TverskyParams {
alpha: self.alpha,
beta: self.beta,
smoothing: self.smoothing,
batch_size: batch_size as u32,
elements_per_sample: elements_per_sample as u32,
};
let params_buffer = device.create_uniform_buffer("tversky_loss_params", ¶ms);
let output_buffer = device.create_buffer_f32(batch_size)?;
ComputeDispatch::new(device, "tversky_loss")
.shader(Self::shader(), "main")
.storage_read(0, self.predictions.buffer())
.storage_read(1, self.targets.buffer())
.storage_rw(2, &output_buffer)
.uniform(3, ¶ms_buffer)
.dispatch_1d(batch_size as u32)
.submit()?;
let output_data = crate::utils::read_buffer(device, &output_buffer, batch_size)?;
Ok(Tensor::new(output_data, vec![batch_size], device.clone()))
}
}
// ═══════════════════════════════════════════════════════════════
// TENSOR API INTEGRATION
// ═══════════════════════════════════════════════════════════════
impl Tensor {
/// Tversky loss for segmentation (generalized Dice)
/// **Deep Debt**: Essential for imbalanced segmentation, control FP/FN trade-off
/// # Arguments
/// - `targets`: Ground truth [same shape as predictions]
/// - `alpha`: Weight for false positives (0.0-1.0, typically 0.3-0.7)
/// - `beta`: Weight for false negatives (0.0-1.0, typically 0.3-0.7)
/// - `smoothing`: Smoothing factor (typically 1.0)
/// # Returns
/// - Loss tensor [`batch_size`] (one value per batch)
/// # Example
/// ```rust,ignore
/// // Penalize false negatives more (medical imaging)
/// let loss = preds.tversky_loss(&targets, 0.3, 0.7, 1.0)?;
/// // Equivalent to Dice Loss
/// let loss = preds.tversky_loss(&targets, 0.5, 0.5, 1.0)?;
/// ```
/// # Note
/// - `alpha < beta`: Recall-focused (minimize false negatives)
/// - `alpha > beta`: Precision-focused (minimize false positives)
/// - `alpha = beta = 0.5`: Equivalent to Dice Loss
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn tversky_loss(
self,
targets: &Self,
alpha: f32,
beta: f32,
smoothing: f32,
) -> Result<Self> {
TverskyLoss::new(self, targets.clone(), alpha, beta, smoothing)?.execute()
}
}
// ═══════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_tversky_loss_gpu_basic() {
let device = crate::device::test_pool::get_test_device().await;
let batch = 2;
let h = 16;
let w = 16;
// Perfect predictions
let preds =
Tensor::from_vec_on(vec![1.0; batch * h * w], vec![batch, h, w], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![1.0; batch * h * w], vec![batch, h, w], device)
.await
.unwrap();
let loss = preds.tversky_loss(&targets, 0.5, 0.5, 1.0).unwrap();
assert_eq!(loss.shape(), &[batch]);
let data = loss.to_vec().unwrap();
// Perfect prediction should have low loss
assert!(data.iter().all(|&x| x < 0.1));
}
#[tokio::test]
async fn test_tversky_loss_gpu_equivalent_to_dice() {
let device = crate::device::test_pool::get_test_device().await;
let batch = 1;
let size = 100;
let preds = Tensor::from_vec_on(vec![0.8; batch * size], vec![batch, size], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![1.0; batch * size], vec![batch, size], device)
.await
.unwrap();
// Tversky with alpha=beta=0.5 should be equivalent to Dice
let tversky_loss = preds.clone().tversky_loss(&targets, 0.5, 0.5, 1.0).unwrap();
let dice_loss = preds.dice_loss(&targets, 1.0).unwrap();
let tversky_data = tversky_loss.to_vec().unwrap();
let dice_data = dice_loss.to_vec().unwrap();
// Should be approximately equal (different GPU computations may vary in float precision)
let diff = (tversky_data[0] - dice_data[0]).abs();
assert!(
diff < 0.01,
"Tversky (alpha=beta=0.5) should be close to Dice, got diff={diff}"
);
}
#[tokio::test]
async fn test_tversky_loss_gpu_recall_focused() {
let device = crate::device::test_pool::get_test_device().await;
let batch = 1;
let size = 100;
let preds = Tensor::from_vec_on(vec![0.5; batch * size], vec![batch, size], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![1.0; batch * size], vec![batch, size], device)
.await
.unwrap();
// alpha < beta: Penalize false negatives more (recall-focused)
let recall_loss = preds.clone().tversky_loss(&targets, 0.3, 0.7, 1.0).unwrap();
// alpha > beta: Penalize false positives more (precision-focused)
let precision_loss = preds.tversky_loss(&targets, 0.7, 0.3, 1.0).unwrap();
let recall_data = recall_loss.to_vec().unwrap();
let precision_data = precision_loss.to_vec().unwrap();
// Recall-focused should have higher loss (more penalty on FN)
assert!(recall_data[0] > precision_data[0]);
}
#[tokio::test]
async fn test_tversky_loss_gpu_mismatch() {
let device = crate::device::test_pool::get_test_device().await;
let batch = 1;
let size = 100;
// Complete mismatch
let preds = Tensor::from_vec_on(vec![1.0; batch * size], vec![batch, size], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![0.0; batch * size], vec![batch, size], device)
.await
.unwrap();
let loss = preds.tversky_loss(&targets, 0.5, 0.5, 1.0).unwrap();
let data = loss.to_vec().unwrap();
// High loss for mismatch
assert!(data[0] > 0.5);
}
#[tokio::test]
async fn test_tversky_loss_gpu_medical_scale() {
let device = crate::device::test_pool::get_test_device().await;
// Medical imaging scale: 128x128 slices
let batch = 4;
let h = 128;
let w = 128;
let preds =
Tensor::from_vec_on(vec![0.8; batch * h * w], vec![batch, h, w], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![1.0; batch * h * w], vec![batch, h, w], device)
.await
.unwrap();
// Medical imaging: Penalize false negatives more
let loss = preds.tversky_loss(&targets, 0.3, 0.7, 1.0).unwrap();
assert_eq!(loss.shape(), &[batch]);
let data = loss.to_vec().unwrap();
assert!(
data.iter()
.all(|&x| x.is_finite() && (0.0..=1.0).contains(&x))
);
}
#[tokio::test]
async fn test_tversky_loss_gpu_validation() {
let device = crate::device::test_pool::get_test_device().await;
let preds = Tensor::from_vec_on(vec![1.0; 100], vec![100], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![1.0; 100], vec![100], device)
.await
.unwrap();
// Invalid alpha (> 1.0) should error
assert!(preds.clone().tversky_loss(&targets, 1.5, 0.5, 1.0).is_err());
// Invalid beta (< 0.0) should error
assert!(
preds
.clone()
.tversky_loss(&targets, 0.5, -0.1, 1.0)
.is_err()
);
// Invalid smoothing (< 0.0) should error
assert!(preds.tversky_loss(&targets, 0.5, 0.5, -1.0).is_err());
}
}