| 1 | #include <pybind11/pybind11.h> |
| 2 | #include <pybind11/numpy.h> |
| 3 | #include <cfenv> |
| 4 | #include <limits> |
| 5 | |
| 6 | namespace py = pybind11; |
| 7 | using ssize_t = Py_ssize_t; |
| 8 | |
| 9 | double logaddexp(double a, double b) |
| 10 | { |
| 11 | return |
| 12 | a == -std::numeric_limits<double>::infinity() ? b |
| 13 | : b == -std::numeric_limits<double>::infinity() ? a |
| 14 | : std::max(a, b) + std::log1p(std::exp(-std::abs(b - a))); |
| 15 | } |
| 16 | |
| 17 | double logsumexp(double const* v, ssize_t n) |
| 18 | { |
| 19 | auto max = *std::max_element(v, v + n); |
| 20 | if (std::isinf(max)) { |
| 21 | return max; |
| 22 | } |
| 23 | auto acc = 0.; |
| 24 | for (auto i = 0; i < n; ++i) { |
| 25 | acc += std::exp(v[i] - max); |
| 26 | } |
| 27 | return std::log(acc) + max; |
| 28 | } |
| 29 | |
| 30 | py::array_t<double> log( |
| 31 | py::array_t<double, py::array::c_style | py::array::forcecast> x_) |
| 32 | { |
| 33 | auto n = x_.size(); |
| 34 | auto ptr = x_.data(); |
| 35 | auto log = py::array_t<double>{{n}}; |
| 36 | auto log_ptr = log.mutable_data(); |
| 37 | for (auto i = 0; i < n; ++i) { |
| 38 | *(log_ptr++) = std::log(*(ptr++)); |
| 39 | } |
| 40 | if (std::fetestexcept(FE_DIVBYZERO)) { |
| 41 | std::feclearexcept(FE_DIVBYZERO); // log(0) = -inf, ignore exception. |
| 42 | } |
| 43 | return log.reshape(std::vector<ssize_t>(x_.shape(), x_.shape() + x_.ndim())); |
| 44 | } |
| 45 | |
| 46 | std::tuple<double, py::array_t<double>, py::array_t<double>> forward_scaling( |
| 47 | py::array_t<double> startprob_, |
| 48 | py::array_t<double> transmat_, |
| 49 | py::array_t<double> frameprob_) |
| 50 | { |
| 51 | auto min_sum = 1e-300; |
| 52 | |
| 53 | auto startprob = startprob_.unchecked<1>(); |
| 54 | auto transmat = transmat_.unchecked<2>(); |
| 55 | auto frameprob = frameprob_.unchecked<2>(); |
| 56 | auto ns = frameprob.shape(0), nc = frameprob.shape(1); |
| 57 | if (startprob.shape(0) != nc |
| 58 | || transmat.shape(0) != nc || transmat.shape(1) != nc) { |
| 59 | throw std::invalid_argument{"shape mismatch"}; |
| 60 | } |
| 61 | auto fwdlattice_ = py::array_t<double>{{ns, nc}}; |
| 62 | auto fwd = fwdlattice_.mutable_unchecked<2>(); |
| 63 | auto scaling_ = py::array_t<double>{{ns}}; |
| 64 | auto scaling = scaling_.mutable_unchecked<1>(); |
| 65 | auto log_prob = 0.; |
| 66 | { |
| 67 | py::gil_scoped_release nogil; |
| 68 | std::fill_n(fwd.mutable_data(0, 0), fwd.size(), 0); |
| 69 | for (auto i = 0; i < nc; ++i) { |
| 70 | fwd(0, i) = startprob(i) * frameprob(0, i); |
| 71 | } |
| 72 | auto sum = std::accumulate(&fwd(0, 0), &fwd(0, nc), 0.); |
| 73 | if (sum < min_sum) { |
| 74 | throw std::range_error{"forward pass failed with underflow; " |
| 75 | "consider using implementation='log' instead"}; |
| 76 | } |
| 77 | auto scale = scaling(0) = 1. / sum; |
| 78 | log_prob -= std::log(scale); |
| 79 | for (auto i = 0; i < nc; ++i) { |
| 80 | fwd(0, i) *= scale; |
| 81 | } |
| 82 | for (auto t = 1; t < ns; ++t) { |
| 83 | for (auto j = 0; j < nc; ++j) { |
| 84 | for (auto i = 0; i < nc; ++i) { |
| 85 | fwd(t, j) += fwd(t - 1, i) * transmat(i, j); |
| 86 | } |
| 87 | fwd(t, j) *= frameprob(t, j); |
| 88 | } |
| 89 | auto sum = std::accumulate(&fwd(t, 0), &fwd(t, nc), 0.); |
| 90 | if (sum < min_sum) { |
| 91 | throw std::range_error{"forward pass failed with underflow; " |
| 92 | "consider using implementation='log' instead"}; |
| 93 | } |
| 94 | auto scale = scaling(t) = 1. / sum; |
| 95 | log_prob -= std::log(scale); |
| 96 | for (auto j = 0; j < nc; ++j) { |
| 97 | fwd(t, j) *= scale; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | return {log_prob, fwdlattice_, scaling_}; |
| 102 | } |
| 103 | |
| 104 | std::tuple<double, py::array_t<double>> forward_log( |
| 105 | py::array_t<double> startprob_, |
| 106 | py::array_t<double> transmat_, |
| 107 | py::array_t<double> log_frameprob_) |
| 108 | { |
| 109 | auto log_startprob_ = log(startprob_); |
| 110 | auto log_startprob = log_startprob_.unchecked<1>(); |
| 111 | auto log_transmat_ = log(transmat_); |
| 112 | auto log_transmat = log_transmat_.unchecked<2>(); |
| 113 | auto log_frameprob = log_frameprob_.unchecked<2>(); |
| 114 | auto ns = log_frameprob.shape(0), nc = log_frameprob.shape(1); |
| 115 | if (log_startprob.shape(0) != nc |
| 116 | || log_transmat.shape(0) != nc || log_transmat.shape(1) != nc) { |
| 117 | throw std::invalid_argument{"shape mismatch"}; |
| 118 | } |
| 119 | auto buf = std::vector<double>(nc); |
| 120 | auto fwdlattice_ = py::array_t<double>{{ns, nc}}; |
| 121 | auto fwd = fwdlattice_.mutable_unchecked<2>(); |
| 122 | { |
| 123 | py::gil_scoped_release nogil; |
| 124 | for (auto i = 0; i < nc; ++i) { |
| 125 | fwd(0, i) = log_startprob(i) + log_frameprob(0, i); |
| 126 | } |
| 127 | for (auto t = 1; t < ns; ++t) { |
| 128 | for (auto j = 0; j < nc; ++j) { |
| 129 | for (auto i = 0; i < nc; ++i) { |
| 130 | buf[i] = fwd(t - 1, i) + log_transmat(i, j); |
| 131 | } |
| 132 | fwd(t, j) = logsumexp(buf.data(), nc) + log_frameprob(t, j); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | auto log_prob = logsumexp(&fwd(ns - 1, 0), nc); |
| 137 | return {log_prob, fwdlattice_}; |
| 138 | } |
| 139 | |
| 140 | py::array_t<double> backward_scaling( |
| 141 | py::array_t<double> startprob_, |
| 142 | py::array_t<double> transmat_, |
| 143 | py::array_t<double> frameprob_, |
| 144 | py::array_t<double> scaling_) |
| 145 | { |
| 146 | auto startprob = startprob_.unchecked<1>(); |
| 147 | auto transmat = transmat_.unchecked<2>(); |
| 148 | auto frameprob = frameprob_.unchecked<2>(); |
| 149 | auto scaling = scaling_.unchecked<1>(); |
| 150 | auto ns = frameprob.shape(0), nc = frameprob.shape(1); |
| 151 | if (startprob.shape(0) != nc |
| 152 | || transmat.shape(0) != nc || transmat.shape(1) != nc |
| 153 | || scaling.shape(0) != ns) { |
| 154 | throw std::invalid_argument{"shape mismatch"}; |
| 155 | } |
| 156 | auto bwdlattice_ = py::array_t<double>{{ns, nc}}; |
| 157 | auto bwd = bwdlattice_.mutable_unchecked<2>(); |
| 158 | py::gil_scoped_release nogil; |
| 159 | std::fill_n(bwd.mutable_data(0, 0), bwd.size(), 0); |
| 160 | for (auto i = 0; i < nc; ++i) { |
| 161 | bwd(ns - 1, i) = scaling(ns - 1); |
| 162 | } |
| 163 | for (auto t = ns - 2; t >= 0; --t) { |
| 164 | for (auto i = 0; i < nc; ++i) { |
| 165 | for (auto j = 0; j < nc; ++j) { |
| 166 | bwd(t, i) += transmat(i, j) * frameprob(t + 1, j) * bwd(t + 1, j); |
| 167 | } |
| 168 | bwd(t, i) *= scaling(t); |
| 169 | } |
| 170 | } |
| 171 | return bwdlattice_; |
| 172 | } |
| 173 | |
| 174 | py::array_t<double> backward_log( |
| 175 | py::array_t<double> startprob_, |
| 176 | py::array_t<double> transmat_, |
| 177 | py::array_t<double> log_frameprob_) |
| 178 | { |
| 179 | auto log_startprob_ = log(startprob_); |
| 180 | auto log_startprob = log_startprob_.unchecked<1>(); |
| 181 | auto log_transmat_ = log(transmat_); |
| 182 | auto log_transmat = log_transmat_.unchecked<2>(); |
| 183 | auto log_frameprob = log_frameprob_.unchecked<2>(); |
| 184 | auto ns = log_frameprob.shape(0), nc = log_frameprob.shape(1); |
| 185 | if (log_startprob.shape(0) != nc |
| 186 | || log_transmat.shape(0) != nc || log_transmat.shape(1) != nc) { |
| 187 | throw std::invalid_argument{"shape mismatch"}; |
| 188 | } |
| 189 | auto buf = std::vector<double>(nc); |
| 190 | auto bwdlattice_ = py::array_t<double>{{ns, nc}}; |
| 191 | auto bwd = bwdlattice_.mutable_unchecked<2>(); |
| 192 | py::gil_scoped_release nogil; |
| 193 | for (auto i = 0; i < nc; ++i) { |
| 194 | bwd(ns - 1, i) = 0; |
| 195 | } |
| 196 | for (auto t = ns - 2; t >= 0; --t) { |
| 197 | for (auto i = 0; i < nc; ++i) { |
| 198 | for (auto j = 0; j < nc; ++j) { |
| 199 | buf[j] = log_transmat(i, j) + log_frameprob(t + 1, j) + bwd(t + 1, j); |
| 200 | } |
| 201 | bwd(t, i) = logsumexp(buf.data(), nc); |
| 202 | } |
| 203 | } |
| 204 | return bwdlattice_; |
| 205 | } |
| 206 | |
| 207 | py::array_t<double> compute_scaling_xi_sum( |
| 208 | py::array_t<double> fwdlattice_, |
| 209 | py::array_t<double> transmat_, |
| 210 | py::array_t<double> bwdlattice_, |
| 211 | py::array_t<double> frameprob_) |
| 212 | { |
| 213 | auto fwd = fwdlattice_.unchecked<2>(); |
| 214 | auto transmat = transmat_.unchecked<2>(); |
| 215 | auto bwd = bwdlattice_.unchecked<2>(); |
| 216 | auto frameprob = frameprob_.unchecked<2>(); |
| 217 | auto ns = frameprob.shape(0), nc = frameprob.shape(1); |
| 218 | if (fwd.shape(0) != ns || fwd.shape(1) != nc |
| 219 | || transmat.shape(0) != nc || transmat.shape(1) != nc |
| 220 | || bwd.shape(0) != ns || bwd.shape(1) != nc) { |
| 221 | throw std::invalid_argument{"shape mismatch"}; |
| 222 | } |
| 223 | auto xi_sum_ = py::array_t<double>{{nc, nc}}; |
| 224 | auto xi_sum = xi_sum_.mutable_unchecked<2>(); |
| 225 | std::fill_n(xi_sum.mutable_data(0, 0), xi_sum.size(), 0); |
| 226 | py::gil_scoped_release nogil; |
| 227 | for (auto t = 0; t < ns - 1; ++t) { |
| 228 | for (auto i = 0; i < nc; ++i) { |
| 229 | for (auto j = 0; j < nc; ++j) { |
| 230 | xi_sum(i, j) += fwd(t, i) |
| 231 | * transmat(i, j) |
| 232 | * frameprob(t + 1, j) |
| 233 | * bwd(t + 1, j); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | return xi_sum_; |
| 238 | } |
| 239 | |
| 240 | py::array_t<double> compute_log_xi_sum( |
| 241 | py::array_t<double> fwdlattice_, |
| 242 | py::array_t<double> transmat_, |
| 243 | py::array_t<double> bwdlattice_, |
| 244 | py::array_t<double> log_frameprob_) |
| 245 | { |
| 246 | auto fwd = fwdlattice_.unchecked<2>(); |
| 247 | auto log_transmat_ = log(transmat_); |
| 248 | auto log_transmat = log_transmat_.unchecked<2>(); |
| 249 | auto bwd = bwdlattice_.unchecked<2>(); |
| 250 | auto log_frameprob = log_frameprob_.unchecked<2>(); |
| 251 | auto ns = log_frameprob.shape(0), nc = log_frameprob.shape(1); |
| 252 | if (fwd.shape(0) != ns || fwd.shape(1) != nc |
| 253 | || log_transmat.shape(0) != nc || log_transmat.shape(1) != nc |
| 254 | || bwd.shape(0) != ns || bwd.shape(1) != nc) { |
| 255 | throw std::invalid_argument{"shape mismatch"}; |
| 256 | } |
| 257 | auto log_prob = logsumexp(&fwd(ns - 1, 0), nc); |
| 258 | auto log_xi_sum_ = py::array_t<double>{{nc, nc}}; |
| 259 | auto log_xi_sum = log_xi_sum_.mutable_unchecked<2>(); |
| 260 | std::fill_n(log_xi_sum.mutable_data(0, 0), log_xi_sum.size(), |
| 261 | -std::numeric_limits<double>::infinity()); |
| 262 | py::gil_scoped_release nogil; |
| 263 | for (auto t = 0; t < ns - 1; ++t) { |
| 264 | for (auto i = 0; i < nc; ++i) { |
| 265 | for (auto j = 0; j < nc; ++j) { |
| 266 | auto log_xi = fwd(t, i) |
| 267 | + log_transmat(i, j) |
| 268 | + log_frameprob(t + 1, j) |
| 269 | + bwd(t + 1, j) |
| 270 | - log_prob; |
| 271 | log_xi_sum(i, j) = logaddexp(log_xi_sum(i, j), log_xi); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | return log_xi_sum_; |
| 276 | } |
| 277 | |
| 278 | std::tuple<double, py::array_t<ssize_t>> viterbi( |
| 279 | py::array_t<double> startprob_, |
| 280 | py::array_t<double> transmat_, |
| 281 | py::array_t<double> log_frameprob_) |
| 282 | { |
| 283 | auto log_startprob_ = log(startprob_); |
| 284 | auto log_startprob = log_startprob_.unchecked<1>(); |
| 285 | auto log_transmat_ = log(transmat_); |
| 286 | auto log_transmat = log_transmat_.unchecked<2>(); |
| 287 | auto log_frameprob = log_frameprob_.unchecked<2>(); |
| 288 | auto ns = log_frameprob.shape(0), nc = log_frameprob.shape(1); |
| 289 | if (log_startprob.shape(0) != nc |
| 290 | || log_transmat.shape(0) != nc || log_transmat.shape(1) != nc) { |
| 291 | throw std::invalid_argument{"shape mismatch"}; |
| 292 | } |
| 293 | auto state_sequence_ = py::array_t<ssize_t>{{ns}}; |
| 294 | auto viterbi_lattice_ = py::array_t<double>{{ns, nc}}; |
| 295 | auto state_sequence = state_sequence_.mutable_unchecked<1>(); |
| 296 | auto viterbi_lattice = viterbi_lattice_.mutable_unchecked<2>(); |
| 297 | { |
| 298 | py::gil_scoped_release nogil; |
| 299 | for (auto i = 0; i < nc; ++i) { |
| 300 | viterbi_lattice(0, i) = log_startprob(i) + log_frameprob(0, i); |
| 301 | } |
| 302 | for (auto t = 1; t < ns; ++t) { |
| 303 | for (auto i = 0; i < nc; ++i) { |
| 304 | auto max = -std::numeric_limits<double>::infinity(); |
| 305 | for (auto j = 0; j < nc; ++j) { |
| 306 | max = std::max(max, viterbi_lattice(t - 1, j) + log_transmat(j, i)); |
| 307 | } |
| 308 | viterbi_lattice(t, i) = max + log_frameprob(t, i); |
| 309 | } |
| 310 | } |
| 311 | auto row = &viterbi_lattice(ns - 1, 0); |
| 312 | auto prev = state_sequence(ns - 1) = std::max_element(row, row + nc) - row; |
| 313 | for (auto t = ns - 2; t >= 0; --t) { |
| 314 | auto max = std::make_pair(-std::numeric_limits<double>::infinity(), 0); |
| 315 | for (auto i = 0; i < nc; ++i) { |
| 316 | max = std::max(max, {viterbi_lattice(t, i) + log_transmat(i, prev), i}); |
| 317 | } |
| 318 | state_sequence(t) = prev = max.second; |
| 319 | } |
| 320 | } |
| 321 | return {viterbi_lattice(ns - 1, state_sequence(ns - 1)), state_sequence_}; |
| 322 | } |
| 323 | |
| 324 | PYBIND11_MODULE(_hmmc, m) { |
| 325 | m |
| 326 | .def("forward_scaling", forward_scaling) |
| 327 | .def("forward_log", forward_log) |
| 328 | .def("backward_scaling", backward_scaling) |
| 329 | .def("backward_log", backward_log) |
| 330 | .def("compute_scaling_xi_sum", compute_scaling_xi_sum) |
| 331 | .def("compute_log_xi_sum", compute_log_xi_sum) |
| 332 | .def("viterbi", viterbi) |
| 333 | ; |
| 334 | } |