Skip to content

Commit

Permalink
Lattice decoder (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Mar 18, 2024
1 parent 7632978 commit 47c1bed
Show file tree
Hide file tree
Showing 19 changed files with 1,911 additions and 14 deletions.
4 changes: 3 additions & 1 deletion kaldi-decoder/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ set(srcs
decodable-ctc.cc
eigen.cc
faster-decoder.cc
lattice-faster-decoder.cc
lattice-simple-decoder.cc
simple-decoder.cc
)


add_library(kaldi-decoder-core ${srcs})

target_link_libraries(kaldi-decoder-core PUBLIC kaldifst_core)
Expand Down
13 changes: 7 additions & 6 deletions kaldi-decoder/csrc/decodable-ctc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@

namespace kaldi_decoder {

DecodableCtc::DecodableCtc(const FloatMatrix &log_probs)
: log_probs_(log_probs) {
DecodableCtc::DecodableCtc(const FloatMatrix &log_probs, int32_t offset /*= 0*/)
: log_probs_(log_probs), offset_(offset) {
p_ = &log_probs_(0, 0);
num_rows_ = log_probs_.rows();
num_cols_ = log_probs_.cols();
}

DecodableCtc::DecodableCtc(const float *p, int32_t num_rows, int32_t num_cols)
: p_(p), num_rows_(num_rows), num_cols_(num_cols) {}
DecodableCtc::DecodableCtc(const float *p, int32_t num_rows, int32_t num_cols,
int32_t offset /*= 0*/)
: p_(p), num_rows_(num_rows), num_cols_(num_cols), offset_(offset) {}

float DecodableCtc::LogLikelihood(int32_t frame, int32_t index) {
// Note: We need to use index - 1 here since
// all the input labels of the H are incremented during graph
// construction
assert(index >= 1);

return *(p_ + frame * num_cols_ + index - 1);
return *(p_ + (frame - offset_) * num_cols_ + index - 1);
}

int32_t DecodableCtc::NumFramesReady() const { return num_rows_; }
int32_t DecodableCtc::NumFramesReady() const { return offset_ + num_rows_; }

int32_t DecodableCtc::NumIndices() const { return num_cols_; }

Expand Down
6 changes: 4 additions & 2 deletions kaldi-decoder/csrc/decodable-ctc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ namespace kaldi_decoder {
class DecodableCtc : public DecodableInterface {
public:
// It copies the input log_probs
explicit DecodableCtc(const FloatMatrix &log_probs);
explicit DecodableCtc(const FloatMatrix &log_probs, int32_t offset = 0);

// It shares the memory with the input array.
//
// @param p Pointer to a 2-d array of shape (num_rows, num_cols).
// The array should be kept alive as long as this object is still
// alive.
DecodableCtc(const float *p, int32_t num_rows, int32_t num_cols);
DecodableCtc(const float *p, int32_t num_rows, int32_t num_cols,
int32_t offset = 0);

float LogLikelihood(int32_t frame, int32_t index) override;

Expand All @@ -38,6 +39,7 @@ class DecodableCtc : public DecodableInterface {
const float *p_ = nullptr; // pointer to a 2-d array
int32_t num_rows_; // number of rows in the 2-d array
int32_t num_cols_; // number of cols in the 2-d array
int32_t offset_ = 0;
};

} // namespace kaldi_decoder
Expand Down
48 changes: 48 additions & 0 deletions kaldi-decoder/csrc/kaldi-math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// kaldi-decoder/csrc/kaldi-math.h

// Copyright 2009-2011 Ondrej Glembek; Microsoft Corporation; Yanmin Qian;
// Jan Silovsky; Saarland University
// 2023 Xiaomi Corporation

// this file is copied and modified from
// kaldi/src/base/kaldi-math.h
#ifndef KALDI_DECODER_CSRC_KALDI_MATH_H_
#define KALDI_DECODER_CSRC_KALDI_MATH_H_

#include <cmath>
#include <limits>

#ifndef DBL_EPSILON
#define DBL_EPSILON 2.2204460492503131e-16
#endif

#ifndef FLT_EPSILON
#define FLT_EPSILON 1.19209290e-7f
#endif

// M_LOG_2PI = log(2*pi)
#ifndef M_LOG_2PI
#define M_LOG_2PI 1.8378770664093454835606594728112
#endif

#define KALDI_ISINF std::isinf
#define KALDI_ISNAN std::isnan

#include "kaldi-decoder/csrc/log.h"

namespace kaldi_decoder {

/// return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
static inline bool ApproxEqual(float a, float b,
float relative_tolerance = 0.001) {
// a==b handles infinities.
if (a == b) return true;
float diff = std::abs(a - b);
if (diff == std::numeric_limits<float>::infinity() || diff != diff)
return false; // diff is +inf or nan.
return (diff <= relative_tolerance * (std::abs(a) + std::abs(b)));
}

} // namespace kaldi_decoder

#endif // KALDI_DECODER_CSRC_KALDI_MATH_H_
13 changes: 13 additions & 0 deletions kaldi-decoder/csrc/lattice-faster-decoder.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// kaldi-decoder/csrc/lattice-faster-decoder.cc

// Copyright 2009-2012 Microsoft Corporation Mirko Hannemann
// 2013-2018 Johns Hopkins University (Author: Daniel Povey)
// 2014 Guoguo Chen
// 2018 Zhehuai Chen
// Copyright (c) 2023 Xiaomi Corporation

// this file is copied and modified from
// kaldi/src/decoder/lattice-faster-decoder.cc

#include "kaldi-decoder/csrc/lattice-faster-decoder.h"
namespace kaldi_decoder {}
Loading

0 comments on commit 47c1bed

Please sign in to comment.