Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
simd_generalized_loss.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
10#include "momentum/common/log.h"
11
12#include <drjit/math.h>
13
14#include <cmath>
15
16namespace momentum {
17
18namespace {
19
20// SIMD mirrors of the scalar helpers in generalized_loss.cpp. Each takes a Packet of squared
21// errors and broadcasts the scalar `invC2` over the lanes via DrJit's overloaded operators.
22// Math intrinsics are dispatched through `drjit::sqrt/log/exp/pow` to vectorize across the packet
23// instead of falling back to per-lane `std::*` calls.
24
25// L2: returns invC2 * sqrError (no 0.5 factor — kept separate so the result can be reused as the
26// inner term of the L1/Cauchy/Welsch/General formulas).
27template <typename T>
28Packet<T> L2Loss(const Packet<T>& sqrError, T invC2) {
29 return sqrError * invC2;
30}
31
32// d/dsqrError of L2Loss is the constant invC2 — broadcast implicitly to a Packet at the call site.
33template <typename T>
34Packet<T> derivL2Loss(const Packet<T>& /* sqrError */, T invC2) {
35 return invC2;
36}
37
38// L1 (pseudo-Huber, alpha = 1): sqrt(invC2 * sqrError + 1) - 1.
39// The trailing `- 1` is intentionally kept so value() at sqrError=0 is exactly 0.
40template <typename T>
41Packet<T> L1Loss(const Packet<T>& sqrError, T invC2) {
42 return drjit::sqrt(L2Loss(sqrError, invC2) + T(1)) - T(1);
43}
44
45template <typename T>
46Packet<T> derivL1Loss(const Packet<T>& sqrError, T invC2) {
47 return T(0.5) * invC2 / drjit::sqrt(L2Loss(sqrError, invC2) + T(1));
48}
49
50// Cauchy / Lorentzian (alpha = 0): log(0.5 * invC2 * sqrError + 1).
51template <typename T>
52Packet<T> CauchyLoss(const Packet<T>& sqrError, T invC2) {
53 return drjit::log(T(0.5) * L2Loss(sqrError, invC2) + T(1));
54}
55
56template <typename T>
57Packet<T> derivCauchyLoss(const Packet<T>& sqrError, T invC2) {
58 return invC2 / (invC2 * sqrError + T(2));
59}
60
61// Welsch (alpha -> -inf limit): 1 - exp(-0.5 * invC2 * sqrError). Bounded in [0, 1).
62template <typename T>
63Packet<T> WelschLoss(const Packet<T>& sqrError, T invC2) {
64 return T(1) - drjit::exp(T(-0.5) * L2Loss(sqrError, invC2));
65}
66
67template <typename T>
68Packet<T> derivWelschLoss(const Packet<T>& sqrError, T invC2) {
69 return (T(0.5) * invC2) * drjit::exp(T(-0.5) * L2Loss(sqrError, invC2));
70}
71
72} // namespace
73
74template <typename T>
75SimdGeneralizedLossT<T>::SimdGeneralizedLossT(const T& a, const T& c) : Base(a, c) {}
76
77// Branching is on the scalar `lossType_` cached at construction (Base classifies alpha into one
78// of L2/L1/Cauchy/Welsch/General). All packet lanes follow the same branch — no per-lane mask or
79// `drjit::select` is needed because alpha is uniform across the packet.
80template <typename T>
82 switch (this->lossType_) {
84 return L2Loss(sqrError, this->invC2_);
86 return L1Loss(sqrError, this->invC2_);
88 return CauchyLoss(sqrError, this->invC2_);
90 // Welsch closed form is selected only when alpha == kWelsch.
91 return WelschLoss(sqrError, this->invC2_);
93 // General Barron loss for alpha not near 0, 1, 2, or kWelsch. The packet `pow` is the slow
94 // path; the special cases above exist precisely to avoid it. The scalar prefactor
95 // `|alpha - 2| / alpha` is computed once outside the packet expression.
96 return (drjit::pow(
97 L2Loss(sqrError, this->invC2_) / std::fabs(this->alpha_ - T(2)) + T(1),
98 T(0.5) * this->alpha_) -
99 T(1)) *
100 (std::fabs(this->alpha_ - T(2)) / this->alpha_);
101 default: {
102 // Unreachable in practice — lossType_ is set in Base's ctor to one of the enumerators above.
103 // Return an explicitly zero-initialized packet (rather than a default-constructed one, which
104 // can leave lanes uninitialized in DrJit) so callers never propagate garbage if the enum is
105 // corrupted via memory damage or future churn.
106 MT_LOGE("Invalid lossType_ ({})", static_cast<int>(this->lossType_));
107 return drjit::zeros<Packet<T>>();
108 }
109 }
110}
111
112// Same lane-uniform switch structure as value(); see notes there. Returns d(loss)/d(sqrError) so
113// callers can chain-rule into the Jacobian of the underlying residual.
114template <typename T>
116 switch (this->lossType_) {
118 return derivL2Loss(sqrError, this->invC2_);
120 return derivL1Loss(sqrError, this->invC2_);
122 return derivCauchyLoss(sqrError, this->invC2_);
124 return derivWelschLoss(sqrError, this->invC2_);
126 // General Barron derivative; uses packet `pow` and is the slow path.
127 return (T(0.5) * this->invC2_) *
128 drjit::pow(
129 L2Loss(sqrError, this->invC2_) / std::fabs(this->alpha_ - T(2)) + T(1),
130 T(0.5) * this->alpha_ - T(1));
131 default: {
132 // See value() — same explicit zero-initialization to avoid uninitialized lanes.
133 MT_LOGE("Invalid lossType_ ({})", static_cast<int>(this->lossType_));
134 return drjit::zeros<Packet<T>>();
135 }
136 }
137}
138
139template class SimdGeneralizedLossT<float>;
140template class SimdGeneralizedLossT<double>;
141
142} // namespace momentum
const T invC2_
Stored as 1/c^2 (instead of c) as a small optimization, since the loss formulas use this quantity dir...
SIMD (vectorized) variant of GeneralizedLossT that operates on DrJit Packet<T> lanes instead of indiv...
Packet< T > value(const Packet< T > &sqrError) const
Returns the loss value for each lane of sqrError.
SimdGeneralizedLossT(const T &a=Base::kL2, const T &c=T(1))
Constructs a SIMD generalized loss with shape a and scale c.
Packet< T > deriv(const Packet< T > &sqrError) const
Returns d(loss)/d(sqrError) for each lane of sqrError.
#define MT_LOGE(...)
Definition log.h:181
drjit::Packet< T, kSimdPacketSize > Packet
A fixed-size SIMD packet of kSimdPacketSize elements of type T.
Definition simd.h:71