crossEntropy method
Implementation
Tensor crossEntropy(List<int> targets) {
// logits are [T, V]
final int T = shape[0];
final int V = shape[1];
if (targets.length != T) {
throw ArgumentError(
"Target length ${targets.length} must match Logits T: $T",
);
}
// Allocate native memory for targets
final ptr = calloc<ffi.Int32>(T);
for (int i = 0; i < T; i++) {
ptr[i] = targets[i];
}
// Call the engine
final handle = engine.crossEntropyLoss(_handle, ptr, T, V);
// Clean up the host pointer (C++ has already copied it to GPU)
calloc.free(ptr);
return Tensor._raw(handle, [1, 1]); // Returns a scalar loss
}