[Speechx] add nnet prob cache && make 2 thread decode work (#2769)
* add nnet cache && make 2 thread work * do not compile websocketpull/2854/head
parent
f8caaf46c8
commit
5046d8ee94
@ -0,0 +1,84 @@
|
|||||||
|
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "nnet/nnet_producer.h"
|
||||||
|
|
||||||
|
namespace ppspeech {
|
||||||
|
|
||||||
|
using kaldi::Vector;
|
||||||
|
using kaldi::BaseFloat;
|
||||||
|
|
||||||
|
NnetProducer::NnetProducer(std::shared_ptr<NnetBase> nnet,
|
||||||
|
std::shared_ptr<FrontendInterface> frontend)
|
||||||
|
: nnet_(nnet), frontend_(frontend) {}
|
||||||
|
|
||||||
|
void NnetProducer::Accept(const kaldi::VectorBase<kaldi::BaseFloat>& inputs) {
|
||||||
|
frontend_->Accept(inputs);
|
||||||
|
bool result = false;
|
||||||
|
do {
|
||||||
|
result = Compute();
|
||||||
|
} while (result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NnetProducer::Acceptlikelihood(
|
||||||
|
const kaldi::Matrix<BaseFloat>& likelihood) {
|
||||||
|
std::vector<BaseFloat> prob;
|
||||||
|
prob.resize(likelihood.NumCols());
|
||||||
|
for (size_t idx = 0; idx < likelihood.NumRows(); ++idx) {
|
||||||
|
for (size_t col = 0; col < likelihood.NumCols(); ++col) {
|
||||||
|
prob[col] = likelihood(idx, col);
|
||||||
|
cache_.push_back(prob);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NnetProducer::Read(std::vector<kaldi::BaseFloat>* nnet_prob) {
|
||||||
|
bool flag = cache_.pop(nnet_prob);
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NnetProducer::Compute() {
|
||||||
|
Vector<BaseFloat> features;
|
||||||
|
if (frontend_ == NULL || frontend_->Read(&features) == false) {
|
||||||
|
// no feat or frontend_ not init.
|
||||||
|
VLOG(3) << "no feat avalible";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
CHECK_GE(frontend_->Dim(), 0);
|
||||||
|
VLOG(2) << "Forward in " << features.Dim() / frontend_->Dim() << " feats.";
|
||||||
|
|
||||||
|
NnetOut out;
|
||||||
|
nnet_->FeedForward(features, frontend_->Dim(), &out);
|
||||||
|
int32& vocab_dim = out.vocab_dim;
|
||||||
|
Vector<BaseFloat>& logprobs = out.logprobs;
|
||||||
|
size_t nframes = logprobs.Dim() / vocab_dim;
|
||||||
|
VLOG(2) << "Forward out " << nframes << " decoder frames.";
|
||||||
|
std::vector<BaseFloat> logprob(vocab_dim);
|
||||||
|
// remove later.
|
||||||
|
for (size_t idx = 0; idx < nframes; ++idx) {
|
||||||
|
for (size_t prob_idx = 0; prob_idx < vocab_dim; ++prob_idx) {
|
||||||
|
logprob[prob_idx] = logprobs(idx * vocab_dim + prob_idx);
|
||||||
|
}
|
||||||
|
cache_.push_back(logprob);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NnetProducer::AttentionRescoring(const std::vector<std::vector<int>>& hyps,
|
||||||
|
float reverse_weight,
|
||||||
|
std::vector<float>* rescoring_score) {
|
||||||
|
nnet_->AttentionRescoring(hyps, reverse_weight, rescoring_score);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ppspeech
|
@ -0,0 +1,73 @@
|
|||||||
|
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "base/common.h"
|
||||||
|
#include "base/safe_queue.h"
|
||||||
|
#include "frontend/audio/frontend_itf.h"
|
||||||
|
#include "nnet/nnet_itf.h"
|
||||||
|
|
||||||
|
namespace ppspeech {
|
||||||
|
|
||||||
|
class NnetProducer {
|
||||||
|
public:
|
||||||
|
explicit NnetProducer(std::shared_ptr<NnetBase> nnet,
|
||||||
|
std::shared_ptr<FrontendInterface> frontend = NULL);
|
||||||
|
|
||||||
|
// Feed feats or waves
|
||||||
|
void Accept(const kaldi::VectorBase<kaldi::BaseFloat>& inputs);
|
||||||
|
|
||||||
|
void Acceptlikelihood(const kaldi::Matrix<BaseFloat>& likelihood);
|
||||||
|
|
||||||
|
// nnet
|
||||||
|
bool Read(std::vector<kaldi::BaseFloat>* nnet_prob);
|
||||||
|
|
||||||
|
bool Empty() const { return cache_.empty(); }
|
||||||
|
|
||||||
|
void SetFinished() {
|
||||||
|
LOG(INFO) << "set finished";
|
||||||
|
// std::unique_lock<std::mutex> lock(mutex_);
|
||||||
|
frontend_->SetFinished();
|
||||||
|
|
||||||
|
// read the last chunk data
|
||||||
|
Compute();
|
||||||
|
// ready_feed_condition_.notify_one();
|
||||||
|
LOG(INFO) << "compute last feats done.";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsFinished() const { return frontend_->IsFinished(); }
|
||||||
|
|
||||||
|
void Reset() {
|
||||||
|
frontend_->Reset();
|
||||||
|
nnet_->Reset();
|
||||||
|
VLOG(3) << "feature cache reset: cache size: " << cache_.size();
|
||||||
|
cache_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AttentionRescoring(const std::vector<std::vector<int>>& hyps,
|
||||||
|
float reverse_weight,
|
||||||
|
std::vector<float>* rescoring_score);
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool Compute();
|
||||||
|
|
||||||
|
std::shared_ptr<FrontendInterface> frontend_;
|
||||||
|
std::shared_ptr<NnetBase> nnet_;
|
||||||
|
SafeQueue<std::vector<kaldi::BaseFloat>> cache_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(NnetProducer);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace ppspeech
|
@ -0,0 +1,123 @@
|
|||||||
|
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "recognizer/u2_recognizer.h"
|
||||||
|
#include "decoder/param.h"
|
||||||
|
#include "kaldi/feat/wave-reader.h"
|
||||||
|
#include "kaldi/util/table-types.h"
|
||||||
|
|
||||||
|
DEFINE_string(wav_rspecifier, "", "test feature rspecifier");
|
||||||
|
DEFINE_string(result_wspecifier, "", "test result wspecifier");
|
||||||
|
DEFINE_double(streaming_chunk, 0.36, "streaming feature chunk size");
|
||||||
|
DEFINE_int32(sample_rate, 16000, "sample rate");
|
||||||
|
|
||||||
|
void decode_func(std::shared_ptr<ppspeech::U2Recognizer> recognizer) {
|
||||||
|
while (!recognizer->IsFinished()) {
|
||||||
|
recognizer->Decode();
|
||||||
|
usleep(100);
|
||||||
|
}
|
||||||
|
recognizer->Decode();
|
||||||
|
recognizer->Rescoring();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
gflags::SetUsageMessage("Usage:");
|
||||||
|
gflags::ParseCommandLineFlags(&argc, &argv, false);
|
||||||
|
google::InitGoogleLogging(argv[0]);
|
||||||
|
google::InstallFailureSignalHandler();
|
||||||
|
FLAGS_logtostderr = 1;
|
||||||
|
|
||||||
|
int32 num_done = 0, num_err = 0;
|
||||||
|
double tot_wav_duration = 0.0;
|
||||||
|
double tot_decode_time = 0.0;
|
||||||
|
|
||||||
|
kaldi::SequentialTableReader<kaldi::WaveHolder> wav_reader(
|
||||||
|
FLAGS_wav_rspecifier);
|
||||||
|
kaldi::TokenWriter result_writer(FLAGS_result_wspecifier);
|
||||||
|
|
||||||
|
int sample_rate = FLAGS_sample_rate;
|
||||||
|
float streaming_chunk = FLAGS_streaming_chunk;
|
||||||
|
int chunk_sample_size = streaming_chunk * sample_rate;
|
||||||
|
LOG(INFO) << "sr: " << sample_rate;
|
||||||
|
LOG(INFO) << "chunk size (s): " << streaming_chunk;
|
||||||
|
LOG(INFO) << "chunk size (sample): " << chunk_sample_size;
|
||||||
|
|
||||||
|
ppspeech::U2RecognizerResource resource =
|
||||||
|
ppspeech::U2RecognizerResource::InitFromFlags();
|
||||||
|
std::shared_ptr<ppspeech::U2Recognizer> recognizer_ptr(
|
||||||
|
new ppspeech::U2Recognizer(resource));
|
||||||
|
|
||||||
|
for (; !wav_reader.Done(); wav_reader.Next()) {
|
||||||
|
std::thread recognizer_thread(decode_func, recognizer_ptr);
|
||||||
|
std::string utt = wav_reader.Key();
|
||||||
|
const kaldi::WaveData& wave_data = wav_reader.Value();
|
||||||
|
LOG(INFO) << "utt: " << utt;
|
||||||
|
LOG(INFO) << "wav dur: " << wave_data.Duration() << " sec.";
|
||||||
|
double dur = wave_data.Duration();
|
||||||
|
tot_wav_duration += dur;
|
||||||
|
|
||||||
|
int32 this_channel = 0;
|
||||||
|
kaldi::SubVector<kaldi::BaseFloat> waveform(wave_data.Data(),
|
||||||
|
this_channel);
|
||||||
|
int tot_samples = waveform.Dim();
|
||||||
|
LOG(INFO) << "wav len (sample): " << tot_samples;
|
||||||
|
|
||||||
|
int sample_offset = 0;
|
||||||
|
kaldi::Timer timer;
|
||||||
|
kaldi::Timer local_timer;
|
||||||
|
|
||||||
|
while (sample_offset < tot_samples) {
|
||||||
|
int cur_chunk_size =
|
||||||
|
std::min(chunk_sample_size, tot_samples - sample_offset);
|
||||||
|
|
||||||
|
kaldi::Vector<kaldi::BaseFloat> wav_chunk(cur_chunk_size);
|
||||||
|
for (int i = 0; i < cur_chunk_size; ++i) {
|
||||||
|
wav_chunk(i) = waveform(sample_offset + i);
|
||||||
|
}
|
||||||
|
// wav_chunk = waveform.Range(sample_offset + i, cur_chunk_size);
|
||||||
|
|
||||||
|
recognizer_ptr->Accept(wav_chunk);
|
||||||
|
if (cur_chunk_size < chunk_sample_size) {
|
||||||
|
recognizer_ptr->SetFinished();
|
||||||
|
}
|
||||||
|
|
||||||
|
// no overlap
|
||||||
|
sample_offset += cur_chunk_size;
|
||||||
|
}
|
||||||
|
CHECK(sample_offset == tot_samples);
|
||||||
|
|
||||||
|
recognizer_thread.join();
|
||||||
|
std::string result = recognizer_ptr->GetFinalResult();
|
||||||
|
recognizer_ptr->Reset();
|
||||||
|
if (result.empty()) {
|
||||||
|
// the TokenWriter can not write empty string.
|
||||||
|
++num_err;
|
||||||
|
LOG(INFO) << " the result of " << utt << " is empty";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG(INFO) << utt << " " << result;
|
||||||
|
LOG(INFO) << " RTF: " << local_timer.Elapsed() / dur << " dur: " << dur
|
||||||
|
<< " cost: " << local_timer.Elapsed();
|
||||||
|
|
||||||
|
result_writer.Write(utt, result);
|
||||||
|
|
||||||
|
++num_done;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG(INFO) << "Done " << num_done << " out of " << (num_err + num_done);
|
||||||
|
LOG(INFO) << "total wav duration is: " << tot_wav_duration << " sec";
|
||||||
|
LOG(INFO) << "total decode cost:" << tot_decode_time << " sec";
|
||||||
|
LOG(INFO) << "RTF is: " << tot_decode_time / tot_wav_duration;
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
add_subdirectory(websocket)
|
#add_subdirectory(websocket)
|
||||||
|
@ -0,0 +1,71 @@
|
|||||||
|
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "base/common.h"
|
||||||
|
|
||||||
|
namespace ppspeech {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class SafeQueue {
|
||||||
|
public:
|
||||||
|
explicit SafeQueue(size_t capacity = 0);
|
||||||
|
void push_back(const T& in);
|
||||||
|
bool pop(T* out);
|
||||||
|
bool empty() const { return buffer_.empty(); }
|
||||||
|
size_t size() const { return buffer_.size(); }
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::mutex mutex_;
|
||||||
|
std::condition_variable condition_;
|
||||||
|
std::deque<T> buffer_;
|
||||||
|
size_t capacity_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
SafeQueue<T>::SafeQueue(size_t capacity) : capacity_(capacity) {}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void SafeQueue<T>::push_back(const T& in) {
|
||||||
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
|
if (capacity_ > 0 && buffer_.size() == capacity_) {
|
||||||
|
condition_.wait(lock, [this] { return capacity_ >= buffer_.size(); });
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer_.push_back(in);
|
||||||
|
condition_.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool SafeQueue<T>::pop(T* out) {
|
||||||
|
if (buffer_.empty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
|
condition_.wait(lock, [this] { return buffer_.size() > 0; });
|
||||||
|
*out = std::move(buffer_.front());
|
||||||
|
buffer_.pop_front();
|
||||||
|
condition_.notify_one();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void SafeQueue<T>::clear() {
|
||||||
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
|
buffer_.clear();
|
||||||
|
condition_.notify_one();
|
||||||
|
}
|
||||||
|
} // namespace ppspeech
|
Loading…
Reference in new issue