未验证 提交 25d80791 编写于 作者: 石晓伟 提交者: GitHub

Fix Bitmain Predictor::Clone() (#18599)

* update anakin-engine interfaces for content-dnn

test=develop

* support only-gpu mode of Anakin

modify eltwise parse

test=develop

* modification for thread-safe

test=develop

* Integrated template instance

test=develop

* increase template parameters

test=develop

* support MLU predictor

test=develop

* update anakin cmake files

test=develop

* update TargetWrapper::set_device

* update the initialization of anakin subgraph

test=develop

* use the default constructor of base class

test=develop

* load model from buffer with length

test=develop

* modify the access level of class

test=develop

* support anakin for bitmain arch

test=develop

* remove files

* checkout cmakelists

test=develop

* modify interfaces

test=develop

* add cmake dependments

test=develop

* enforce the outputs of net

test=develop
上级 97549a4f
...@@ -70,9 +70,9 @@ cc_test(test_analysis_predictor SRCS analysis_predictor_tester.cc DEPS analysis_ ...@@ -70,9 +70,9 @@ cc_test(test_analysis_predictor SRCS analysis_predictor_tester.cc DEPS analysis_
if(ANAKIN_FOUND) if(ANAKIN_FOUND)
# Do not turn warnings into errors. # Do not turn warnings into errors.
set_source_files_properties(api.cc api_anakin_engine.cc PROPERTIES COMPILE_FLAGS "-Wno-error") set_source_files_properties(api.cc api_anakin_engine.cc PROPERTIES COMPILE_FLAGS "-Wno-error")
cc_library(inference_anakin_api SRCS api.cc api_anakin_engine.cc) cc_library(inference_anakin_api SRCS api.cc api_anakin_engine.cc DEPS boost xxhash)
target_link_libraries(inference_anakin_api anakin anakin_saber_common) target_link_libraries(inference_anakin_api anakin anakin_saber_common)
cc_library(inference_anakin_api_shared SHARED SRCS api.cc api_anakin_engine.cc) cc_library(inference_anakin_api_shared SHARED SRCS api.cc api_anakin_engine.cc DEPS boost xxhash)
target_link_libraries(inference_anakin_api_shared anakin anakin_saber_common) target_link_libraries(inference_anakin_api_shared anakin anakin_saber_common)
function(anakin_target target_name) function(anakin_target target_name)
target_compile_options(${target_name} BEFORE PUBLIC ${ANAKIN_COMPILE_EXTRA_FLAGS}) target_compile_options(${target_name} BEFORE PUBLIC ${ANAKIN_COMPILE_EXTRA_FLAGS})
......
...@@ -62,8 +62,9 @@ void PaddleInferenceAnakinPredictor<T, P, R>::InitGraph() { ...@@ -62,8 +62,9 @@ void PaddleInferenceAnakinPredictor<T, P, R>::InitGraph() {
} else { } else {
LOG(FATAL) << "Model load error."; LOG(FATAL) << "Model load error.";
} }
auto inputs = this->graph_p_->get_ins(); this->input_names_ = this->graph_p_->get_ins();
for (auto &input_str : inputs) { this->output_names_ = this->graph_p_->get_outs();
for (auto &input_str : this->input_names_) {
if (this->config_.init_inputs_shape.find(input_str) == if (this->config_.init_inputs_shape.find(input_str) ==
this->config_.init_inputs_shape.end()) { this->config_.init_inputs_shape.end()) {
LOG(FATAL) << input_str << " should be set in init_inputs_shape."; LOG(FATAL) << input_str << " should be set in init_inputs_shape.";
...@@ -201,7 +202,7 @@ bool PaddleInferenceAnakinPredictor<T, P, R>::RunImpl( ...@@ -201,7 +202,7 @@ bool PaddleInferenceAnakinPredictor<T, P, R>::RunImpl(
<< "'s type is not float"; << "'s type is not float";
} }
auto d_tensor_p = this->executor_p_->get_in(input.name); auto d_tensor_p = this->executor_p_->get_in(input.name);
auto net_shape = d_tensor_p->shape(); auto net_shape = d_tensor_p->valid_shape();
if (net_shape.size() != input.shape.size()) { if (net_shape.size() != input.shape.size()) {
LOG(FATAL) << " input " << input.name LOG(FATAL) << " input " << input.name
<< "'s shape size should be equal to that of net"; << "'s shape size should be equal to that of net";
...@@ -250,6 +251,10 @@ bool PaddleInferenceAnakinPredictor<T, P, R>::RunImpl( ...@@ -250,6 +251,10 @@ bool PaddleInferenceAnakinPredictor<T, P, R>::RunImpl(
LOG(FATAL) << "At least one output should be set with tensors' names."; LOG(FATAL) << "At least one output should be set with tensors' names.";
} }
for (auto &output : *output_data) { for (auto &output : *output_data) {
if (std::find(this->output_names_.begin(), this->output_names_.end(),
output.name) == this->output_names_.end()) {
LOG(FATAL) << output.name << " is not in the outputs of the graph.";
}
auto *d_tensor_p = this->executor_p_->get_out(output.name); auto *d_tensor_p = this->executor_p_->get_out(output.name);
output.shape = d_tensor_p->valid_shape(); output.shape = d_tensor_p->valid_shape();
if (output.data.length() < d_tensor_p->valid_size() * sizeof(float)) { if (output.data.length() < d_tensor_p->valid_size() * sizeof(float)) {
...@@ -264,20 +269,23 @@ bool PaddleInferenceAnakinPredictor<T, P, R>::RunImpl( ...@@ -264,20 +269,23 @@ bool PaddleInferenceAnakinPredictor<T, P, R>::RunImpl(
return true; return true;
} }
template <typename T, Precision P, OpRunType R> template <typename T, Precision P, OpRunType R>
bool PaddleInferenceAnakinPredictor<T, P, R>::ResetConfig( bool PaddleInferenceAnakinPredictor<T, P, R>::Reset(
const AnakinConfig &config) { PaddleInferenceAnakinPredictor<T, P, R> *predictor) {
this->config_ = config; this->config_ = predictor->GetConfig();
return true; this->graph_p_ = predictor->GetGraph();
} this->input_names_ = predictor->GetInputNames();
template <typename T, Precision P, OpRunType R> this->output_names_ = predictor->GetOutputNames();
anakin::Net<T, P, R> &PaddleInferenceAnakinPredictor<T, P, R>::ResetExecuter(
std::shared_ptr<anakin::graph::Graph<T, P>> graph_p) {
this->graph_p_ = graph_p;
this->ctx_p_ = std::make_shared<anakin::Context<T>>( this->ctx_p_ = std::make_shared<anakin::Context<T>>(
this->config_.device_id, this->config_.data_stream_id, this->config_.device_id, this->config_.data_stream_id,
this->config_.compute_stream_id); this->config_.compute_stream_id);
this->InitNet(); this->InitNet();
return *this->executor_p_; return true;
}
template <typename T, Precision P, OpRunType R>
std::unique_ptr<PaddlePredictor>
PaddleInferenceAnakinPredictor<T, P, R>::New() {
return std::unique_ptr<PaddlePredictor>(
new PaddleInferenceAnakinPredictor<T, P, R>());
} }
// the cloned new Predictor of anakin share the same net weights from original // the cloned new Predictor of anakin share the same net weights from original
// Predictor // Predictor
...@@ -285,21 +293,24 @@ template <typename T, Precision P, OpRunType R> ...@@ -285,21 +293,24 @@ template <typename T, Precision P, OpRunType R>
std::unique_ptr<PaddlePredictor> std::unique_ptr<PaddlePredictor>
PaddleInferenceAnakinPredictor<T, P, R>::Clone() { PaddleInferenceAnakinPredictor<T, P, R>::Clone() {
VLOG(3) << "Anakin Predictor::clone"; VLOG(3) << "Anakin Predictor::clone";
std::unique_ptr<PaddlePredictor> cls( std::unique_ptr<PaddlePredictor> cls = std::move(this->New());
new PaddleInferenceAnakinPredictor<T, P, R>());
// construct executer from other graph
auto anakin_predictor_p = auto anakin_predictor_p =
dynamic_cast<PaddleInferenceAnakinPredictor<T, P, R> *>(cls.get()); dynamic_cast<PaddleInferenceAnakinPredictor<T, P, R> *>(cls.get());
if (!anakin_predictor_p) { if (!anakin_predictor_p) {
LOG(FATAL) << "fail to call Init"; LOG(FATAL) << "fail to call Init";
} }
anakin_predictor_p->ResetConfig(this->config_); anakin_predictor_p->Reset(this);
anakin_predictor_p->ResetExecuter(this->graph_p_);
return cls; return cls;
} }
#ifdef ANAKIN_MLU_PLACE #ifdef ANAKIN_MLU_PLACE
template <Precision P, OpRunType R> template <Precision P, OpRunType R>
std::unique_ptr<PaddlePredictor>
PaddleInferenceAnakinMLUPredictor<P, R>::New() {
return std::unique_ptr<PaddlePredictor>(
new PaddleInferenceAnakinMLUPredictor<P, R>());
}
template <Precision P, OpRunType R>
void PaddleInferenceAnakinMLUPredictor<P, R>::SetContext() { void PaddleInferenceAnakinMLUPredictor<P, R>::SetContext() {
this->ctx_p_ = std::make_shared<anakin::Context<anakin::MLU>>( this->ctx_p_ = std::make_shared<anakin::Context<anakin::MLU>>(
this->config_.device_id, this->config_.data_stream_id, this->config_.device_id, this->config_.data_stream_id,
...@@ -329,6 +340,11 @@ void PaddleInferenceAnakinMLUPredictor<P, R>::Predict() { ...@@ -329,6 +340,11 @@ void PaddleInferenceAnakinMLUPredictor<P, R>::Predict() {
#ifdef ANAKIN_BM_PLACE #ifdef ANAKIN_BM_PLACE
template <Precision P, OpRunType R> template <Precision P, OpRunType R>
std::unique_ptr<PaddlePredictor> PaddleInferenceAnakinBMPredictor<P, R>::New() {
return std::unique_ptr<PaddlePredictor>(
new PaddleInferenceAnakinBMPredictor<P, R>());
}
template <Precision P, OpRunType R>
void PaddleInferenceAnakinBMPredictor<P, R>::OptimizeGraph() { void PaddleInferenceAnakinBMPredictor<P, R>::OptimizeGraph() {
if (!this->graph_p_->fusion_optimize()) { if (!this->graph_p_->fusion_optimize()) {
LOG(FATAL) << "Graph optimization error."; LOG(FATAL) << "Graph optimization error.";
......
...@@ -20,6 +20,7 @@ limitations under the License. */ ...@@ -20,6 +20,7 @@ limitations under the License. */
#pragma once #pragma once
#include <memory> #include <memory>
#include <string>
#include <vector> #include <vector>
#include "framework/core/net/net.h" #include "framework/core/net/net.h"
...@@ -51,10 +52,18 @@ class PaddleInferenceAnakinPredictor : public PaddlePredictor { ...@@ -51,10 +52,18 @@ class PaddleInferenceAnakinPredictor : public PaddlePredictor {
int batch_size = -1) override; int batch_size = -1) override;
std::unique_ptr<PaddlePredictor> Clone() override; std::unique_ptr<PaddlePredictor> Clone() override;
virtual bool ResetConfig(const AnakinConfig& config); bool Reset(PaddleInferenceAnakinPredictor<T, P, R>* predictor);
virtual anakin::Net<T, P, R>& ResetExecuter(
std::shared_ptr<anakin::graph::Graph<T, P>> graph_p);
void InitPredictor(); void InitPredictor();
std::shared_ptr<anakin::graph::Graph<T, P>> GetGraph() {
return this->graph_p_;
}
std::vector<std::string> GetInputNames() override {
return this->input_names_;
}
std::vector<std::string> GetOutputNames() override {
return this->output_names_;
}
const AnakinConfig& GetConfig() const { return this->config_; }
~PaddleInferenceAnakinPredictor() override; ~PaddleInferenceAnakinPredictor() override;
...@@ -65,11 +74,14 @@ class PaddleInferenceAnakinPredictor : public PaddlePredictor { ...@@ -65,11 +74,14 @@ class PaddleInferenceAnakinPredictor : public PaddlePredictor {
virtual void InitNet(); virtual void InitNet();
virtual void SetContext(); virtual void SetContext();
virtual void Predict(); virtual void Predict();
virtual std::unique_ptr<PaddlePredictor> New();
static std::mutex mutex_; static std::mutex mutex_;
AnakinConfig config_; AnakinConfig config_;
std::shared_ptr<anakin::Context<T>> ctx_p_; std::shared_ptr<anakin::Context<T>> ctx_p_;
std::shared_ptr<anakin::graph::Graph<T, P>> graph_p_; std::shared_ptr<anakin::graph::Graph<T, P>> graph_p_;
anakin::Net<T, P, R>* executor_p_{nullptr}; anakin::Net<T, P, R>* executor_p_{nullptr};
std::vector<std::string> input_names_;
std::vector<std::string> output_names_;
private: private:
bool RunImpl(const std::vector<PaddleTensor>& inputs, bool RunImpl(const std::vector<PaddleTensor>& inputs,
...@@ -82,10 +94,12 @@ template <Precision P, OpRunType R> ...@@ -82,10 +94,12 @@ template <Precision P, OpRunType R>
class PaddleInferenceAnakinMLUPredictor final class PaddleInferenceAnakinMLUPredictor final
: public PaddleInferenceAnakinPredictor<anakin::MLU, P, R> { : public PaddleInferenceAnakinPredictor<anakin::MLU, P, R> {
public: public:
PaddleInferenceAnakinMLUPredictor() = default;
explicit PaddleInferenceAnakinMLUPredictor(const AnakinConfig& config) { explicit PaddleInferenceAnakinMLUPredictor(const AnakinConfig& config) {
this->ResetConfig(config); this->config_ = config;
this->InitPredictor(); this->InitPredictor();
} }
std::unique_ptr<PaddlePredictor> New() override;
void SetContext() override; void SetContext() override;
void OptimizeGraph() override; void OptimizeGraph() override;
void InitNet() override; void InitNet() override;
...@@ -98,10 +112,12 @@ template <Precision P, OpRunType R> ...@@ -98,10 +112,12 @@ template <Precision P, OpRunType R>
class PaddleInferenceAnakinBMPredictor final class PaddleInferenceAnakinBMPredictor final
: public PaddleInferenceAnakinPredictor<anakin::BM, P, R> { : public PaddleInferenceAnakinPredictor<anakin::BM, P, R> {
public: public:
PaddleInferenceAnakinBMPredictor() = default;
explicit PaddleInferenceAnakinBMPredictor(const AnakinConfig& config) { explicit PaddleInferenceAnakinBMPredictor(const AnakinConfig& config) {
this->ResetConfig(config); this->config_ = config;
this->InitPredictor(); this->InitPredictor();
} }
std::unique_ptr<PaddlePredictor> New() override;
void OptimizeGraph() override; void OptimizeGraph() override;
void InitNet() override; void InitNet() override;
void Predict() override; void Predict() override;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册