Skip to content
Snippets Groups Projects
Commit f68e7c0f authored by Yifan Zhao's avatar Yifan Zhao
Browse files

Use piped files properly

parent 6ef2c37e
No related branches found
No related tags found
No related merge requests found
...@@ -35,7 +35,8 @@ class ModelExporter: ...@@ -35,7 +35,8 @@ class ModelExporter:
source_file_name = "hpvm_c.cpp" source_file_name = "hpvm_c.cpp"
metadata_file_name = "ops.json" metadata_file_name = "ops.json"
config_file_name = "tuner_confs.txt" config_file_name = "tuner_confs.txt"
fifo_file_name = "hpvm_fifo" fifo_file_name_r = "hpvm_fifo_r"
fifo_file_name_w = "hpvm_fifo_w"
def __init__( def __init__(
self, self,
...@@ -62,11 +63,13 @@ class ModelExporter: ...@@ -62,11 +63,13 @@ class ModelExporter:
args3 = self.dfg, self.weight_dir, self.dataset_size args3 = self.dfg, self.weight_dir, self.dataset_size
self.compile_args = None self.compile_args = None
self.path_params = {}
if target == "hpvm_tensor": if target == "hpvm_tensor":
if config_file is None: if config_file is None:
raise ValueError( raise ValueError(
f"Config file must be given and exist under hpvm_tensor mode" f"Config file must be given and exist under hpvm_tensor mode"
) )
self.path_params = {"config_file": Path(config_file)}
self.compile_args = ["-t", "tensor", "--conf-file", str(config_file)] self.compile_args = ["-t", "tensor", "--conf-file", str(config_file)]
self.codegen = HpvmCodeGen(*args3, "tensor", None) self.codegen = HpvmCodeGen(*args3, "tensor", None)
elif target == "hpvm_tensor_inspect": elif target == "hpvm_tensor_inspect":
...@@ -74,10 +77,14 @@ class ModelExporter: ...@@ -74,10 +77,14 @@ class ModelExporter:
config_file = output_dir / self.config_file_name config_file = output_dir / self.config_file_name
else: else:
config_file = Path(config_file).absolute() config_file = Path(config_file).absolute()
fifo_file = output_dir / self.fifo_file_name self.path_params = {
"tune_labels_path": (self.weight_dir / self.tuneset_name[1]).as_posix(),
"conf_path": config_file.as_posix(),
"fifo_path_r": (output_dir / self.fifo_file_name_r).as_posix(),
"fifo_path_w": (output_dir / self.fifo_file_name_w).as_posix()
}
self.compile_args = ["-t", "tensor", "--conf-file", str(config_file)] self.compile_args = ["-t", "tensor", "--conf-file", str(config_file)]
inspect_mode_args = {"conf_path": config_file, "fifo_path": fifo_file} self.codegen = HpvmCodeGen(*args3, "tensor", self.path_params)
self.codegen = HpvmCodeGen(*args3, "tensor", inspect_mode_args)
elif target == "hpvm_cudnn": elif target == "hpvm_cudnn":
self.compile_target = "cudnn" self.compile_target = "cudnn"
self.compile_args = ["-t", "cudnn"] self.compile_args = ["-t", "cudnn"]
...@@ -154,6 +161,7 @@ class ModelExporter: ...@@ -154,6 +161,7 @@ class ModelExporter:
"knob_speedup": knob_speedup, "knob_speedup": knob_speedup,
"op_knobs": op_knobs, "op_knobs": op_knobs,
"baseline_knob": baseline_knob, "baseline_knob": baseline_knob,
**self.path_params
}, },
f, f,
indent=2, indent=2,
......
#include <string> #include <string>
#include <array>
#include <hpvm.h> #include <hpvm.h>
#include <tensorTypes.h> #include <tensorTypes.h>
#include <tensorUtils.h> #include <tensorUtils.h>
// Linux C API for FIFO files // For writing binary to file descriptors
#include <cstdio>
// For printing error
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h> const int batch_size = {{batch_size}}, input_size = {{input_size}}, batch_count = input_size / batch_size;
/**** Routines for Handling Piped Execution ***/ /**** Routines for Handling Piped Execution ***/
int fifo_wait(const std::string &filename) {
std::ifstream file{filename}; FILE *open_fifo(const char *path, const char *mode) {
std::string line; auto* fd = fopen(path, mode);
std::getline(file, line); if (!fd) {
std::cerr << "Error opening FIFO file: " << strerror(errno) << '\n';
abort();
}
return fd;
}
int fifo_wait() {
auto* fp = open_fifo("{{fifo_path_r}}", "r");
const int maxn = 100;
char linebuf[maxn];
fgets(linebuf, maxn, fp);
fclose(fp);
std::string line(linebuf);
if (line == "test") if (line == "test")
return 1; return 1;
if (line == "tune") if (line == "tune")
return 2; return 2;
if (line == "stop") if (line == "stop")
return 0; return 0;
std::cout << "Invalid fifo file content \"" << line << "\"\n"; std::cerr << "Invalid fifo file content \"" << line << "\"\n";
abort(); abort();
} }
void fifo_write_finished(const std::string &filename) { void fifo_write_batch(FILE *fp, void *output_ptr) {
std::ofstream file{filename}; auto *output = (Tensor *) output_ptr;
file << "finished\n\0"; const auto &dim = output->dims;
} size_t num_dims = dim.num_dims;
fwrite(&num_dims, sizeof(size_t), 1, fp);
void make_fifo(const std::string &filename) { fwrite(dim.dim_sizes, sizeof(size_t), dim.num_dims, fp);
if (mkfifo(filename.c_str(), 0666) == 0) { fwrite(output->host_data, 1, output->size_in_bytes, fp);
std::ofstream file{filename};
file << "{{conf_path}}\n"; // Write path to config file in FIFO file
return;
}
std::cout << "Error making FIFO file: " << strerror(errno) << '\n';
abort();
} }
{% for node in nodes %} {% for node in nodes %}
...@@ -95,9 +105,6 @@ typedef struct __attribute__((__packed__)) { ...@@ -95,9 +105,6 @@ typedef struct __attribute__((__packed__)) {
struct ret_t r; struct ret_t r;
} RootIn; } RootIn;
const int batch_size = {{batch_size}}, input_size = {{input_size}}, batch_count = input_size / batch_size;
int main(){ int main(){
std::string dir_prefix = "{{prefix}}/"; std::string dir_prefix = "{{prefix}}/";
std::string test_input = dir_prefix + "test_input.bin"; std::string test_input = dir_prefix + "test_input.bin";
...@@ -117,14 +124,15 @@ int main(){ ...@@ -117,14 +124,15 @@ int main(){
args->{{n}}_bytes = 0; args->{{n}}_bytes = 0;
{% endfor %} {% endfor %}
make_fifo("{{fifo_path}}");
int ret = 0; int ret = 0;
while ((ret = fifo_wait("{{fifo_path}}"))) { while ((ret = fifo_wait())) {
__hpvm__init(); __hpvm__init();
startMemTracking(); startMemTracking();
const auto *input_pth = (ret == 1 ? test_input : tune_input).c_str(); const auto *input_pth = (ret == 1 ? test_input : tune_input).c_str();
const auto *labels_pth = (ret == 1 ? test_labels : tune_labels).c_str(); const auto *labels_pth = (ret == 1 ? test_labels : tune_labels).c_str();
// Keep this open so the other side knows we have more batches to write
auto* fp = open_fifo("{{fifo_path_w}}", "wb");
for (int i = 0; i < batch_count; i++){ for (int i = 0; i < batch_count; i++){
int start = i * batch_size, end = start + batch_size; int start = i * batch_size, end = start + batch_size;
copyInputBatch(input_pth, start, end, {{input_shape|join(', ')}}, {{input_name}}); copyInputBatch(input_pth, start, end, {{input_shape|join(', ')}}, {{input_name}});
...@@ -135,10 +143,11 @@ int main(){ ...@@ -135,10 +143,11 @@ int main(){
hpvm_request_tensor(result, 0); hpvm_request_tensor(result, 0);
llvm_hpvm_invokeRtControl(result, labels_pth, start, end); llvm_hpvm_invokeRtControl(result, labels_pth, start, end);
fifo_write_batch(fp, result);
freeBatchMemory(); freeBatchMemory();
} }
fclose(fp);
__hpvm__cleanup(); __hpvm__cleanup();
fifo_write_finished("{{fifo_path}}");
} }
return 0; return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment