Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lattice decoder #10

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

set(KALDI_DECODER_VERSION "0.2.3")
set(KALDI_DECODER_VERSION "0.2.4")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
Expand Down
16 changes: 8 additions & 8 deletions cmake/kaldifst.cmake
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
function(download_kaldifst)
include(FetchContent)

set(kaldifst_URL "https://github.com/k2-fsa/kaldifst/archive/refs/tags/v1.7.6.tar.gz")
set(kaldifst_URL2 "https://huggingface.co/csukuangfj/kaldi-hmm-gmm-cmake-deps/resolve/main/kaldifst-1.7.6.tar.gz")
set(kaldifst_HASH "SHA256=79280c0bb08b5ed1a2ab7c21320a2b071f1f0eb10d2f047e8d6f027f0d32b4d2")
set(kaldifst_URL "https://github.com/k2-fsa/kaldifst/archive/refs/tags/v1.7.10.tar.gz")
set(kaldifst_URL2 "https://hub.nuaa.cf/k2-fsa/kaldifst/archive/refs/tags/v1.7.10.tar.gz")
set(kaldifst_HASH "SHA256=7f7b3173a6584a6b1987f65ae7af2ac453d66b845f875a9d31074b8d2cd0de54")

# If you don't have access to the Internet,
# please pre-download kaldifst
set(possible_file_locations
$ENV{HOME}/Downloads/kaldifst-1.7.6.tar.gz
${PROJECT_SOURCE_DIR}/kaldifst-1.7.6.tar.gz
${PROJECT_BINARY_DIR}/kaldifst-1.7.6.tar.gz
/tmp/kaldifst-1.7.6.tar.gz
/star-fj/fangjun/download/github/kaldifst-1.7.6.tar.gz
$ENV{HOME}/Downloads/kaldifst-1.7.10.tar.gz
${CMAKE_SOURCE_DIR}/kaldifst-1.7.10.tar.gz
${CMAKE_BINARY_DIR}/kaldifst-1.7.10.tar.gz
/tmp/kaldifst-1.7.10.tar.gz
/star-fj/fangjun/download/github/kaldifst-1.7.10.tar.gz
)

foreach(f IN LISTS possible_file_locations)
Expand Down
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
Loading