Skip to main content

MSE Loss Templates

Status:In progress
Tags:Hls4mlTrainableLoss
Last modified:
Source:hls4ml/templates/vivado/trainable/losses/mse.h

mse.h implements the first trainable loss endpoint. It exposes two functions:

nnet::mse<CONFIG_T>(prediction, ground_truth, loss, loss_grad);
nnet::half_mse<CONFIG_T>(prediction, ground_truth, loss, loss_grad);

Both functions use the same mse_core implementation with compile-time shifts. The template intentionally avoids denominator division because the first supported scalings are powers of two.

Scaling​

FunctionScalar lossGradient seed
msesum((y_hat - y)^2)(y_hat - y) << 1
half_msesum((y_hat - y)^2) >> 1y_hat - y

For the first hardware/software trace comparison, half_mse is the cleaner default because its output gradient is exactly the prediction error.

Config Contract​

The generated config struct must provide:

  • n_out,
  • data_in_t,
  • ground_truth_t,
  • loss_t,
  • grad_out_t.

When trainable tracing is enabled, the writer must also emit trace names for prediction, ground truth, loss, and loss gradient.