inference analyzer as bin (#12450)
parent
31a2c87688
commit
dcfbc6a661
@ -0,0 +1,33 @@
|
|||||||
|
// Copyright (c) 2018 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.
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file implements analysizer -- an executation help to analyze and
|
||||||
|
* optimize trained model.
|
||||||
|
*/
|
||||||
|
#include "paddle/fluid/inference/analysis/analyzer.h"
|
||||||
|
#include <gflags/gflags.h>
|
||||||
|
#include <glog/logging.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||||
|
using paddle::inference::analysis::Analyzer;
|
||||||
|
using paddle::inference::analysis::Argument;
|
||||||
|
|
||||||
|
Argument argument;
|
||||||
|
Analyzer analyzer;
|
||||||
|
analyzer.Run(&argument);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
// Copyright (c) 2018 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 "paddle/fluid/inference/analysis/model_store_pass.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "paddle/fluid/inference/analysis/analyzer.h"
|
||||||
|
#include "paddle/fluid/inference/analysis/argument.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace inference {
|
||||||
|
namespace analysis {
|
||||||
|
|
||||||
|
void ModelStorePass::Run(DataFlowGraph *x) {
|
||||||
|
if (!argument_->fluid_model_param_path) {
|
||||||
|
PADDLE_ENFORCE_NOT_NULL(argument_->fluid_model_dir);
|
||||||
|
argument_->fluid_model_param_path.reset(
|
||||||
|
new std::string(*argument_->fluid_model_dir + "param"));
|
||||||
|
}
|
||||||
|
PADDLE_ENFORCE_NOT_NULL(argument_->model_output_store_path);
|
||||||
|
// Directly copy param file to destination.
|
||||||
|
std::stringstream ss;
|
||||||
|
// NOTE these commands only works on linux.
|
||||||
|
ss << "mkdir -p " << *argument_->model_output_store_path;
|
||||||
|
LOG(INFO) << "run command: " << ss.str();
|
||||||
|
PADDLE_ENFORCE_EQ(system(ss.str().c_str()), 0);
|
||||||
|
ss.str("");
|
||||||
|
|
||||||
|
ss << "cp " << *argument_->fluid_model_dir << "/*"
|
||||||
|
<< " " << *argument_->model_output_store_path;
|
||||||
|
LOG(INFO) << "run command: " << ss.str();
|
||||||
|
PADDLE_ENFORCE_EQ(system(ss.str().c_str()), 0);
|
||||||
|
|
||||||
|
// Store program
|
||||||
|
PADDLE_ENFORCE_NOT_NULL(argument_->transformed_program_desc,
|
||||||
|
"program desc is not transformed, should call "
|
||||||
|
"DataFlowGraphToFluidPass first.");
|
||||||
|
const std::string program_output_path =
|
||||||
|
*argument_->model_output_store_path + "/__model__";
|
||||||
|
std::ofstream file(program_output_path, std::ios::binary);
|
||||||
|
PADDLE_ENFORCE(file.is_open(), "failed to open %s to write.",
|
||||||
|
program_output_path);
|
||||||
|
const std::string serialized_message =
|
||||||
|
argument_->transformed_program_desc->SerializeAsString();
|
||||||
|
file.write(serialized_message.c_str(), serialized_message.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace analysis
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,51 @@
|
|||||||
|
// Copyright (c) 2018 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.
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file defines ModelStorePass, which store the runtime DFG to a Paddle
|
||||||
|
* model in the disk, and that model can be reloaded for prediction.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "paddle/fluid/inference/analysis/pass.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace inference {
|
||||||
|
namespace analysis {
|
||||||
|
|
||||||
|
class ModelStorePass : public DataFlowGraphPass {
|
||||||
|
public:
|
||||||
|
bool Initialize(Argument* argument) override {
|
||||||
|
if (!argument) {
|
||||||
|
LOG(ERROR) << "invalid argument";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
argument_ = argument;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Run(DataFlowGraph* x) override;
|
||||||
|
|
||||||
|
std::string repr() const override { return "DFG-store-pass"; }
|
||||||
|
std::string description() const override {
|
||||||
|
return R"DD(This file defines ModelStorePass, which store the runtime DFG to a Paddle
|
||||||
|
model in the disk, and that model can be reloaded for prediction again.)DD";
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Argument* argument_{nullptr};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace analysis
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,43 @@
|
|||||||
|
// Copyright (c) 2018 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 "paddle/fluid/inference/analysis/model_store_pass.h"
|
||||||
|
|
||||||
|
#include <gflags/gflags.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include "paddle/fluid/inference/analysis/analyzer.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace inference {
|
||||||
|
namespace analysis {
|
||||||
|
|
||||||
|
DEFINE_string(inference_model_dir, "", "Model path");
|
||||||
|
|
||||||
|
TEST(DFG_StorePass, test) {
|
||||||
|
Analyzer analyzer;
|
||||||
|
Argument argument(FLAGS_inference_model_dir);
|
||||||
|
argument.model_output_store_path.reset(
|
||||||
|
new std::string("./_dfg_store_pass_tmp"));
|
||||||
|
// disable storage in alalyzer
|
||||||
|
FLAGS_inference_analysis_output_storage_path = "";
|
||||||
|
analyzer.Run(&argument);
|
||||||
|
|
||||||
|
ModelStorePass pass;
|
||||||
|
pass.Initialize(&argument);
|
||||||
|
pass.Run(argument.main_dfg.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace analysis
|
||||||
|
} // namespace inference
|
||||||
|
} // namespace paddle
|
Loading…
Reference in new issue