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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! BCE Loss - GPU-accelerated Binary Cross Entropy loss
//!
//! **Deep Debt Principles**:
//! - ✅ Pure WGSL implementation (uses existing shader!)
//! - ✅ Safe Rust wrapper (no unsafe code)
//! - ✅ Hardware-agnostic via WebGPU
//! - ✅ Complete implementation (production-ready for binary classification)
//!
//! ## Algorithm
//!
//! ```text
//! BCE(p, t) = -[t * log(p) + (1 - t) * log(1 - p)]
//! where p = predicted probability, t = target (0 or 1)
//! ```
//!
//! **Key Properties**:
//! - Standard loss for binary classification
//! - Works with sigmoid outputs (probabilities)
//! - Convex and smooth (easy optimization)
//! - Foundation for cross-entropy variants
//!
//! **Used By**: Logistic regression, binary classification, GANs
//!
//! ## Usage
//!
//! ```rust,ignore
//! use barracuda::tensor::Tensor;
//!
//! let predictions = Tensor::randn(vec![1000]).await?; // Probabilities [0, 1]
//! let targets = Tensor::randn(vec![1000]).await?; // Binary labels {0, 1}
//!
//! let loss = predictions.bce_loss(&targets)?;
//! ```
use crate::device::compute_pipeline::ComputeDispatch;
use crate::error::{BarracudaError, Result};
use crate::tensor::Tensor;
/// f64 is the canonical source — math is universal, precision is silicon.
static SHADER_F64: &str = include_str!("../shaders/loss/bce_loss_f64.wgsl");
static SHADER_F32: std::sync::LazyLock<String> =
std::sync::LazyLock::new(|| SHADER_F64.to_string());
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct BCELossParams {
epsilon: f32, // Numerical stability
reduction_mode: u32, // 0=mean, 1=sum, 2=none
size: u32,
_padding: u32,
}
/// Binary cross-entropy loss for binary classification.
pub struct BCELoss {
predictions: Tensor,
targets: Tensor,
}
impl BCELoss {
/// Create BCE loss. Predictions and targets must have matching shapes.
/// # Errors
/// Returns [`Err`] if predictions and targets shapes differ.
pub fn new(predictions: Tensor, targets: Tensor) -> Result<Self> {
// Validate shapes match
if predictions.shape() != targets.shape() {
return Err(BarracudaError::shape_mismatch(
predictions.shape().to_vec(),
targets.shape().to_vec(),
));
}
Ok(Self {
predictions,
targets,
})
}
fn wgsl_shader() -> &'static str {
&SHADER_F32
}
/// Execute BCE loss computation on GPU.
/// # Errors
/// Returns [`Err`] if buffer allocation fails, GPU dispatch fails, or the device is lost.
pub fn execute(self) -> Result<Tensor> {
let device = self.predictions.device();
let size = self.predictions.shape().iter().product::<usize>();
let params = BCELossParams {
epsilon: 1e-7,
reduction_mode: 0, // mean (not used in per-element version)
size: size as u32,
_padding: 0,
};
let output_buffer = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("bce_loss_output"),
size: (size * std::mem::size_of::<f32>()) as u64,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let params_buffer = device.create_uniform_buffer("bce_loss_params", ¶ms);
ComputeDispatch::new(device, "BCE Loss")
.shader(Self::wgsl_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(size as u32)
.submit()?;
Ok(Tensor::from_buffer(
output_buffer,
self.predictions.shape().to_vec(),
device.clone(),
))
}
}
// ═══════════════════════════════════════════════════════════════
// TENSOR API INTEGRATION
// ═══════════════════════════════════════════════════════════════
impl Tensor {
/// Binary Cross Entropy loss for binary classification
/// **Deep Debt**: Foundation for binary classification and GANs
/// # Arguments
/// - `targets`: Ground truth binary labels [same shape, values in {0, 1}]
/// # Returns
/// - Loss tensor [same shape as input]
/// # Example
/// ```rust,ignore
/// // Standard binary classification
/// let loss = predictions.bce_loss(&targets)?;
/// // With sigmoid activation
/// let probs = logits.sigmoid()?;
/// let loss = probs.bce_loss(&targets)?;
/// ```
/// # Note
/// - Predictions should be probabilities in [0, 1] (use sigmoid first!)
/// - Targets should be binary {0, 1}
/// - Numerically stable with epsilon=1e-7
/// - Standard for logistic regression and binary GANs
/// # Errors
/// Returns [`Err`] if shapes differ, buffer allocation fails, GPU dispatch fails, or the device is lost.
pub fn bce_loss(self, targets: &Self) -> Result<Self> {
BCELoss::new(self, targets.clone())?.execute()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_bce_loss_basic() {
let device = crate::device::test_pool::get_test_device().await;
// Perfect predictions (p=1 for t=1, p=0 for t=0)
let predictions = Tensor::from_vec_on(vec![0.9, 0.1, 0.9, 0.1], vec![4], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![1.0, 0.0, 1.0, 0.0], vec![4], device.clone())
.await
.unwrap();
let loss = predictions.bce_loss(&targets).unwrap();
let data = loss.to_vec().unwrap();
// Loss should be small for good predictions
assert!(data.iter().all(|&x| x >= 0.0 && x.is_finite()));
assert!(data.iter().all(|&x| x < 1.0)); // All losses should be < 1.0
}
#[tokio::test]
async fn test_bce_loss_validation() {
let device = crate::device::test_pool::get_test_device().await;
// Shape mismatch should fail
let predictions = Tensor::from_vec_on(vec![0.5; 10], vec![10], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![1.0; 5], vec![5], device.clone())
.await
.unwrap();
assert!(predictions.bce_loss(&targets).is_err());
}
#[tokio::test]
async fn test_bce_loss_perfect_prediction() {
let device = crate::device::test_pool::get_test_device().await;
// Nearly perfect predictions
let predictions = Tensor::from_vec_on(vec![0.99, 0.01], vec![2], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![1.0, 0.0], vec![2], device.clone())
.await
.unwrap();
let loss = predictions.bce_loss(&targets).unwrap();
let data = loss.to_vec().unwrap();
// Very small loss for near-perfect predictions
assert!(data.iter().all(|&x| x < 0.1));
}
#[tokio::test]
async fn test_bce_loss_worst_prediction() {
let device = crate::device::test_pool::get_test_device().await;
// Worst predictions (completely wrong)
let predictions = Tensor::from_vec_on(vec![0.01, 0.99], vec![2], device.clone())
.await
.unwrap();
let targets = Tensor::from_vec_on(vec![1.0, 0.0], vec![2], device.clone())
.await
.unwrap();
let loss = predictions.bce_loss(&targets).unwrap();
let data = loss.to_vec().unwrap();
// Large loss for bad predictions
assert!(data.iter().all(|&x| x > 1.0));
}
#[tokio::test]
async fn test_bce_loss_large_batch() {
let device = crate::device::test_pool::get_test_device().await;
// Large batch
let predictions: Vec<f32> = (0..1000)
.map(|i| if i % 2 == 0 { 0.8 } else { 0.2 })
.collect();
let targets: Vec<f32> = (0..1000)
.map(|i| if i % 2 == 0 { 1.0 } else { 0.0 })
.collect();
let pred_tensor = Tensor::from_vec_on(predictions, vec![1000], device.clone())
.await
.unwrap();
let target_tensor = Tensor::from_vec_on(targets, vec![1000], device.clone())
.await
.unwrap();
let loss = pred_tensor.bce_loss(&target_tensor).unwrap();
let data = loss.to_vec().unwrap();
assert_eq!(data.len(), 1000);
assert!(data.iter().all(|&x| x.is_finite() && x >= 0.0));
}
}