-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[Speechx] add nnet prob cache && make 2 thread decode work #2769
Changes from all commits
40095d3
f880229
a2b5eb1
c2d9c0c
8cc5671
cd49b31
ed82b13
28fc05b
0547d79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 此处是否可以使用 memcpy相关函数 替换 两层 for循环;担心有效率问题 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不可以,Matrix内存非连续,不过后续会去掉kaldi::matrix这种,所以出现这种for的赋值了。 |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 和上面类似的问题 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo remove later,feature 统一后,vector, Vector 等数据holder会统一处理。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么注释掉了?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
api一些使用暂时还没有改完全,基本框架通了,只验证了recognizer_main, 等整体改完,在验证这几个_main.