You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.7 KiB
92 lines
2.7 KiB
2 years ago
|
// 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"
|
||
2 years ago
|
#include "frontend/frontend_itf.h"
|
||
2 years ago
|
#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
|
||
2 years ago
|
void Accept(const std::vector<kaldi::BaseFloat>& inputs);
|
||
2 years ago
|
|
||
|
void Acceptlikelihood(const kaldi::Matrix<BaseFloat>& likelihood);
|
||
|
|
||
|
// nnet
|
||
|
bool Read(std::vector<kaldi::BaseFloat>* nnet_prob);
|
||
2 years ago
|
bool ReadandCompute(std::vector<kaldi::BaseFloat>* nnet_prob);
|
||
2 years ago
|
static void RunNnetEvaluation(NnetProducer* me);
|
||
2 years ago
|
void RunNnetEvaluationInteral();
|
||
2 years ago
|
void WaitProduce();
|
||
2 years ago
|
|
||
|
void Wait() {
|
||
|
abort_ = true;
|
||
|
condition_variable_.notify_one();
|
||
|
if (thread_.joinable()) thread_.join();
|
||
|
}
|
||
2 years ago
|
|
||
|
bool Empty() const { return cache_.empty(); }
|
||
|
|
||
2 years ago
|
void SetInputFinished() {
|
||
2 years ago
|
LOG(INFO) << "set finished";
|
||
|
frontend_->SetFinished();
|
||
2 years ago
|
condition_variable_.notify_one();
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// the compute thread exit
|
||
|
bool IsFinished() const { return finished_; }
|
||
|
|
||
|
~NnetProducer() {
|
||
2 years ago
|
if (thread_.joinable()) thread_.join();
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
void Reset() {
|
||
2 years ago
|
if (frontend_ != NULL) frontend_->Reset();
|
||
|
if (nnet_ != NULL) nnet_->Reset();
|
||
2 years ago
|
VLOG(3) << "feature cache reset: cache size: " << cache_.size();
|
||
|
cache_.clear();
|
||
2 years ago
|
finished_ = false;
|
||
2 years ago
|
}
|
||
|
|
||
|
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_;
|
||
2 years ago
|
std::mutex mutex_;
|
||
|
std::mutex read_mutex_;
|
||
|
std::condition_variable condition_variable_;
|
||
|
std::condition_variable condition_read_ready_;
|
||
|
std::thread thread_;
|
||
|
bool finished_;
|
||
|
bool abort_;
|
||
2 years ago
|
|
||
|
DISALLOW_COPY_AND_ASSIGN(NnetProducer);
|
||
|
};
|
||
|
|
||
|
} // namespace ppspeech
|