|
|
|
@ -12,12 +12,18 @@ 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. */
|
|
|
|
|
|
|
|
|
|
#ifndef PADDLE_FLUID_OPERATORS_NGRAPH_NGRAPH_ENGINE_H_
|
|
|
|
|
#define PADDLE_FLUID_OPERATORS_NGRAPH_NGRAPH_ENGINE_H_
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "paddle/fluid/framework/operator.h"
|
|
|
|
|
#include "paddle/fluid/framework/program_desc.h"
|
|
|
|
|
#include "paddle/fluid/framework/var_desc.h"
|
|
|
|
|
|
|
|
|
|
#include "ngraph/ngraph.hpp"
|
|
|
|
|
|
|
|
|
@ -33,29 +39,47 @@ enum class OpState { /* nGraph support state on ops */
|
|
|
|
|
UNKNOWN /* Output all for debug purpose */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// cache engine repetitives
|
|
|
|
|
struct EngineCache {
|
|
|
|
|
std::shared_ptr<ngraph::Function> ngraph_function;
|
|
|
|
|
std::set<std::string> persistables;
|
|
|
|
|
std::vector<std::string> var_in;
|
|
|
|
|
std::vector<std::string> var_out;
|
|
|
|
|
std::vector<size_t> var_in_updates;
|
|
|
|
|
bool is_test = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// perform graph build through bridge and execute computation
|
|
|
|
|
class NgraphEngine {
|
|
|
|
|
public:
|
|
|
|
|
explicit NgraphEngine(const framework::Scope& scope,
|
|
|
|
|
const platform::Place& place,
|
|
|
|
|
const std::string& serialized_graph,
|
|
|
|
|
const std::vector<int>& interval);
|
|
|
|
|
const framework::ExecutionContext& ctx);
|
|
|
|
|
|
|
|
|
|
void Run(const framework::Scope& scope, const platform::Place& place) const;
|
|
|
|
|
|
|
|
|
|
static void EnableNgraph(const framework::ProgramDesc& program);
|
|
|
|
|
static const framework::BlockDesc* p_bdesc;
|
|
|
|
|
static std::vector<std::string> feed_vars, fetch_vars;
|
|
|
|
|
|
|
|
|
|
static void FuseNgraphOps(
|
|
|
|
|
const framework::BlockDesc& prog,
|
|
|
|
|
std::vector<std::unique_ptr<framework::OperatorBase>>* ops);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static std::unordered_map<std::string, std::shared_ptr<ngraph::Function>>
|
|
|
|
|
func_cache_;
|
|
|
|
|
static std::unordered_map<std::string, EngineCache> engine_cache;
|
|
|
|
|
static std::unordered_map<
|
|
|
|
|
std::string, std::vector<std::shared_ptr<ngraph::runtime::Tensor>>>
|
|
|
|
|
t_in_cache_;
|
|
|
|
|
static framework::Variable* pre_var_ptr;
|
|
|
|
|
|
|
|
|
|
const framework::Scope& scope_;
|
|
|
|
|
const platform::Place& place_;
|
|
|
|
|
std::vector<std::shared_ptr<framework::OperatorBase>> fused_ops_;
|
|
|
|
|
std::unordered_map<std::string, ngraph::element::Type> var_type_map_;
|
|
|
|
|
std::unordered_set<std::string> persistables_;
|
|
|
|
|
std::unordered_set<std::string> fetches_;
|
|
|
|
|
std::set<std::string> persistables_;
|
|
|
|
|
std::unordered_set<std::string> post_op_inputs_;
|
|
|
|
|
OpState ng_op_state_ = OpState::UNKNOWN;
|
|
|
|
|
OpState op_state_ = OpState::UNKNOWN;
|
|
|
|
|
bool is_test_{true};
|
|
|
|
|
std::string func_cache_key_;
|
|
|
|
|
|
|
|
|
|
// ngraph backend eg. CPU
|
|
|
|
@ -66,6 +90,8 @@ class NgraphEngine {
|
|
|
|
|
std::vector<std::string> var_in_;
|
|
|
|
|
// var_name of outputs from fetch in order
|
|
|
|
|
std::vector<std::string> var_out_;
|
|
|
|
|
// non-persitable var_in
|
|
|
|
|
std::vector<size_t> var_in_updates_;
|
|
|
|
|
// map input vars to nodes
|
|
|
|
|
std::shared_ptr<
|
|
|
|
|
std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>
|
|
|
|
@ -74,20 +100,23 @@ class NgraphEngine {
|
|
|
|
|
std::shared_ptr<
|
|
|
|
|
std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>
|
|
|
|
|
var_node_map_;
|
|
|
|
|
// prepare info for nraph engine
|
|
|
|
|
void Prepare(const framework::BlockDesc& block,
|
|
|
|
|
const std::vector<int>& interval);
|
|
|
|
|
// prepare info for ngraph engine need
|
|
|
|
|
void Prepare(const std::vector<int>& interval);
|
|
|
|
|
// get ngraph engine input and output list
|
|
|
|
|
void BuildNgIO(const std::vector<framework::OpDesc*>& op_descs,
|
|
|
|
|
const std::vector<int>& interval);
|
|
|
|
|
// get ngraph input and define ngraph input parameters
|
|
|
|
|
void GetNgInputShape(std::shared_ptr<framework::OperatorBase> op);
|
|
|
|
|
void GetNgInputShape();
|
|
|
|
|
// Call ngraph bridge to map ops
|
|
|
|
|
void BuildNgNodes();
|
|
|
|
|
// get the ngraph input and output var list
|
|
|
|
|
void BuildNgIO();
|
|
|
|
|
// run paddle RuntimeInferShape to get the tensor shape
|
|
|
|
|
void RunInferShape();
|
|
|
|
|
// build ngraph function call
|
|
|
|
|
void BuildNgFunction();
|
|
|
|
|
void BuildNgFunction(const std::vector<int>& interval);
|
|
|
|
|
// Check cache for ngraph function or otherwise build the function
|
|
|
|
|
void GetNgFunction();
|
|
|
|
|
void GetNgFunction(std::string engine_key, const std::vector<int>& interval);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace operators
|
|
|
|
|
} // namespace paddle
|
|
|
|
|
#endif // PADDLE_FLUID_OPERATORS_NGRAPH_NGRAPH_ENGINE_H_
|
|
|
|
|