Skip to content

Commit

Permalink
cherry pick the CreateExecutable updates (#671)
Browse files Browse the repository at this point in the history
* close floating point check in popart

* create pipeline resource when computation created. not do it at the time of set the cache computation item

fix pixel bert detect application runtime error

* add weiming build script fix

* add inplementation of CreateExecutable

move complie_and_run to createExecute, and more error handle logic

Co-authored-by: yanwei <[email protected]>
  • Loading branch information
yanwei-gr and yanwei authored Nov 9, 2021
1 parent 5cf3b6f commit e0f07fa
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 75 deletions.
29 changes: 28 additions & 1 deletion ODLA/platforms/odla_popart/odla_compute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ odla_status odla_SetComputationItem(odla_computation comp, odla_item_type type,
case 1001: // load cache directly, need set path of cache file
PopartConfig::instance()->set_load_cache(true);
PopartConfig::instance()->set_cache_path(reinterpret_cast<char*>(value));
PopartConfig::instance()->extract_config_from_cache();
break;
default:
std::cerr << "Unsupported property type: " << type << std::endl;
Expand All @@ -73,6 +72,26 @@ odla_status odla_SetComputationItem(odla_computation comp, odla_item_type type,
return ODLA_SUCCESS;
}

odla_status odla_CreateExecutable(odla_executable* executable,
odla_context context, odla_computation comp) {
popart::logging::info("Start to create Executable...");
if (comp == nullptr) {
popart::logging::err(
"Failed to create Executable... Computation haven't been intialized.");
return ODLA_FAILURE;
} else {
if (comp->session) {
return comp->compile_and_export();
} else {
_odla_computation::instance()->init(true); // set is_compile to true
// this comp init will create
// executable
_odla_computation::instance()->compile_and_export();
}
}
return ODLA_SUCCESS;
}

odla_status odla_StoreExecutable(const odla_char* file_name,
odla_executable executable) {
return ODLA_SUCCESS;
Expand All @@ -97,6 +116,13 @@ odla_status odla_CreateComputation(odla_computation* comp) {
}
// Read the config file
if (!PopartConfig::instance()->inited()) {
if (PopartConfig::instance()->load_cache()) {
odla_status ret = PopartConfig::instance()->extract_config_from_cache();
if (ret == ODLA_FAILURE) {
popart::logging::err("load config from cache failed");
return ret;
}
}
PopartConfig::instance()->load_config(std::getenv("ODLA_POPART_CONFIG"));
}
_odla_computation::instance()->set_executor();
Expand All @@ -106,6 +132,7 @@ odla_status odla_CreateComputation(odla_computation* comp) {
QManager::instance()->getQ()->init(
PopartConfig::instance()->queue_capacity());
}

return ODLA_SUCCESS;
}

Expand Down
124 changes: 78 additions & 46 deletions ODLA/platforms/odla_popart/odla_popart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,6 @@
_odla_computation* _odla_computation::instance_ = nullptr;
std::mutex _odla_computation::comp_mutex_;

void compile_and_export_cache(std::string catch_file_name,
std::string config_file_name) {
std::fstream catch_fs(catch_file_name,
std::ios_base::out | std::ifstream::binary);
std::fstream config_fs;
std::string config_string;
if (config_file_name.size() > 0) {
config_fs.open(config_file_name, std::ios_base::in | std::ifstream::binary);
if (!config_fs.is_open()) {
popart::logging::warn(
"invalid config file name:[ {} ] will use default config",
config_file_name);
config_string = PopartConfig::instance()->get_default_config_string();
}
std::ostringstream config_ss;
config_ss << config_fs.rdbuf();
config_string = config_ss.str();
} else {
config_string = PopartConfig::instance()->get_default_config_string();
}

int config_size = config_string.size();
catch_fs.write((char*)&config_size, sizeof(config_string.size()));
catch_fs.write(config_string.c_str(), config_string.size());

_odla_computation::instance()->session->compileAndExport(catch_fs.flush());
catch_fs.flush();
catch_fs.close();
}

void compute_loop(odla_computation comp) {
// setup the stepio with allbacks
popart::StepIOCallback stepio(input_callback, input_complete_callback,
Expand Down Expand Up @@ -119,7 +89,59 @@ void compute_loop(odla_computation comp) {
comp->thread_done();
}

void _odla_computation::init() {
odla_status _odla_computation::compile_and_export() {
popart::logging::warn("Start compile and export");
const std::string& cache_file_name =
PopartConfig::instance()->get_cache_path();
std::string file_suffix(".popart");
int file_prefix = cache_file_name.rfind(file_suffix);
if (file_prefix == std::string::npos ||
file_prefix + file_suffix.size() < cache_file_name.size()) {
popart::logging::err("Bad cache file name");
return ODLA_FAILURE;
}
if (file_prefix == std::string::npos) {
file_prefix = cache_file_name.size() - 1;
}
std::string config_file_name(cache_file_name.substr(0, file_prefix) +
".json");
std::fstream cache_fs(cache_file_name,
std::ios_base::out | std::ifstream::binary);
if (!cache_fs.is_open()) {
popart::logging::err("Open or create cache file falied");
return ODLA_FAILURE;
}
std::fstream config_fs;
std::string config_string;
if (config_file_name.size() > 0) {
config_fs.open(config_file_name, std::ios_base::in | std::ifstream::binary);
if (!config_fs.is_open()) {
popart::logging::warn(
"invalid config file name:[ {} ] will use default config",
config_file_name);
PopartConfig::instance()->use_default();
config_string = PopartConfig::instance()->get_default_config_string();
} else {
std::ostringstream config_ss;
config_ss << config_fs.rdbuf();
config_string = config_ss.str();
}
} else {
config_string = PopartConfig::instance()->get_default_config_string();
}

int config_size = config_string.size();
cache_fs.write((char*)&config_size, sizeof(config_size));
cache_fs.write(config_string.c_str(), config_string.size());

_odla_computation::instance()->session->compileAndExport(cache_fs.flush());

cache_fs.flush();
cache_fs.close();
config_fs.close();
}

void _odla_computation::init(bool is_compile) {
if (!session) {
std::lock_guard<std::mutex> guard(init_mutex_);
if (!session) {
Expand Down Expand Up @@ -167,21 +189,31 @@ void _odla_computation::init() {
auto new_session = popart::InferenceSession::createFromOnnxModel(
proto, data_flow, device, popart::InputShapeInfo(), session_opts_);

if (PopartConfig::instance()->load_cache()) {
popart::logging::info("Load cachefile from existing stream");
auto cache_fs = PopartConfig::instance()->get_cache_fs();
new_session->loadExecutableFromStream(*(cache_fs.get()));
}
new_session->prepareDevice();
new_session->setRandomSeed(0); // Init seed
new_session->weightsFromHost(); // Copy weights from host to IPU
// If in parallel mode, start the thread
ExecutionMode mode = PopartConfig::instance()->execution_mode();
if (PIPELINE == mode || PARALLEL == mode) {
std::thread parallel_thread(compute_loop, this);
thread_state_ = RUNNING;
popart::logging::warn("Parallel loop has been started");
parallel_thread.detach();
if (!is_compile) {
if (PopartConfig::instance()->load_cache()) {
popart::logging::info("Load cachefile from existing stream");
auto cache_fs = PopartConfig::instance()->get_cache_fs();
if (cache_fs->is_open()) {
try {
new_session->loadExecutableFromStream(*(cache_fs.get()));
} catch (std::exception& e) {
popart::logging::err("bad cache file, will compile the graph");
}
}
}

new_session->prepareDevice();
new_session->setRandomSeed(0); // Init seed
new_session->weightsFromHost(); // Copy weights from host to IPU

// If in parallel mode, start the thread
ExecutionMode mode = PopartConfig::instance()->execution_mode();
if (PIPELINE == mode || PARALLEL == mode) {
std::thread parallel_thread(compute_loop, this);
thread_state_ = RUNNING;
popart::logging::warn("Parallel loop has been started");
parallel_thread.detach();
}
}
session =
std::move(new_session); // set session after all initialization done.
Expand Down
4 changes: 3 additions & 1 deletion ODLA/platforms/odla_popart/odla_popart.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,15 @@ struct _odla_computation {
thread_state_(DONE) {
builder->setAttribute(popart::sVirtualGraphAttribute, 0);
}
void init();
void init(bool is_compile = false);
std::string set_pipeline_stage();
void set_session_opts();
void set_executor();
void set_opts();
bool use_pipeline();
bool hold();
odla_status compile_and_export();

inline Execution* executor() { return executor_; }
inline bool is_done() { return thread_state_ != RUNNING; }
inline void mark_done() {
Expand Down
56 changes: 35 additions & 21 deletions ODLA/platforms/odla_popart/popart_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ void PopartConfig::use_default() {
queue_capacity_ = 1024 * 1024;
debug_ = false;
default_config_string_ =
"{ \
\"version\":\"1.0.0\", \
\"amp\":0.6, \
\"batches_per_step\":1, \
\"execution_mode\":\"sequence\", \
\"ipu_num\":1, \
\"load_onnx\":false, \
\"load_onnx_path\":\"test-load-time.onnx\", \
\"queue_type\":\"LockFreeQueue\", \
\"queue_capacity\":1048576, \
\"debug\": false \
}";
"{\n\
\"version\":\"1.0.0\",\n\
\"amp\":0.6,\n\
\"batches_per_step\":1,\n\
\"execution_mode\":\"sequence\",\n\
\"ipu_num\":1,\n\
\"load_onnx\":false, \n\
\"load_onnx_path\":\"test-load-time.onnx\",\n\
\"queue_type\":\"LockFreeQueue\",\n\
\"queue_capacity\":1048576,\n\
\"debug\": false\n\
}\n";
}

void PopartConfig::load_config(const char* file_path) {
Expand Down Expand Up @@ -206,14 +206,28 @@ bool PopartConfig::get_pipeline_setting(const std::string& node_name,
return false;
}

void PopartConfig::extract_config_from_cache() {
cache_fs =
std::make_shared<std::ifstream>(cache_path_, std::ifstream::binary);
odla_status PopartConfig::extract_config_from_cache() {
cache_fs = std::make_shared<std::fstream>(
cache_path_,
std::ios_base::in | std::ios_base::out | std::ifstream::binary);
int config_len = 0;
cache_fs->read((char*)&config_len, sizeof(config_len));
std::vector<char> config_data_buffer(config_len);
cache_fs->read(config_data_buffer.data(), config_len);
std::string config_string(config_data_buffer.begin(),
config_data_buffer.end());
load_from_string(config_string);
popart::logging::info("load config from cache file: {}", cache_path_.c_str());
if (!cache_fs->is_open()) {
popart::logging::err("cache file is not exist");
return ODLA_FAILURE;
}
if (cache_fs->read((char*)&config_len, sizeof(config_len))) {
std::vector<char> config_data_buffer(config_len);
if (cache_fs->read(config_data_buffer.data(), config_len)) {
std::string config_string(config_data_buffer.begin(),
config_data_buffer.end());
try {
load_from_string(config_string);
} catch (std::exception& e) {
popart::logging::err("load from cached config string failed.");
return ODLA_FAILURE;
}
}
}
return ODLA_SUCCESS;
}
14 changes: 8 additions & 6 deletions ODLA/platforms/odla_popart/popart_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string>
#include <vector>

#include "ODLA/odla_common.h"
#include "json.hpp"
/**
* The configuration format like follows:
Expand Down Expand Up @@ -82,10 +83,9 @@ class PopartConfig {

bool inited_;

std::shared_ptr<std::ifstream> cache_fs;
std::shared_ptr<std::fstream> cache_fs;

static PopartConfig* instance_;
void use_default();
void load_from_file(const std::string& file_path);

public:
Expand All @@ -99,6 +99,8 @@ class PopartConfig {
inited_(false),
ipu_num_(1) {}
~PopartConfig() {}

void use_default();
static PopartConfig* instance() { return instance_; }
const std::string& version() { return version_; }
inline float amp() { return amp_; };
Expand All @@ -118,11 +120,11 @@ class PopartConfig {
inline int queue_capacity() { return queue_capacity_; }
inline bool debug() { return debug_; }
inline bool inited() { return inited_; }
inline std::shared_ptr<std::ifstream> get_cache_fs() { return cache_fs; }
inline void set_cache_fs(std::shared_ptr<std::ifstream> fs) { cache_fs = fs; }
inline std::shared_ptr<std::fstream> get_cache_fs() { return cache_fs; }
inline void set_cache_fs(std::shared_ptr<std::fstream> fs) { cache_fs = fs; }

inline bool load_cache() { return load_cache_; }
inline const std::string load_cache_path() { return cache_path_; }
inline const std::string& get_cache_path() { return cache_path_; }
inline void set_load_cache(bool is_load_cache) {
load_cache_ = is_load_cache;
}
Expand All @@ -135,7 +137,7 @@ class PopartConfig {
void load_config(const char* file_path);
bool get_pipeline_setting(const std::string& node_name, int64_t& ipu_idx,
int64_t& pipeline_stage);
void extract_config_from_cache();
odla_status extract_config_from_cache();

private:
void set_pipeline_setting(const std::string& name_pattern, int ipu_idx,
Expand Down

0 comments on commit e0f07fa

Please sign in to comment.