@ -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(
< < " '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(
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(
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>
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() {
# 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. " ;