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:
source_file_name = "hpvm_c.cpp"
metadata_file_name = "ops.json"
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__(
self,
......@@ -62,11 +63,13 @@ class ModelExporter:
args3 = self.dfg, self.weight_dir, self.dataset_size
self.compile_args = None
self.path_params = {}
if target == "hpvm_tensor":
if config_file is None:
raise ValueError(
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.codegen = HpvmCodeGen(*args3, "tensor", None)
elif target == "hpvm_tensor_inspect":
......@@ -74,10 +77,14 @@ class ModelExporter:
config_file = output_dir / self.config_file_name
else:
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)]
inspect_mode_args = {"conf_path": config_file, "fifo_path": fifo_file}
self.codegen = HpvmCodeGen(*args3, "tensor", inspect_mode_args)
self.codegen = HpvmCodeGen(*args3, "tensor", self.path_params)
elif target == "hpvm_cudnn":
self.compile_target = "cudnn"
self.compile_args = ["-t", "cudnn"]
......@@ -154,6 +161,7 @@ class ModelExporter:
"knob_speedup": knob_speedup,
"op_knobs": op_knobs,
"baseline_knob": baseline_knob,
**self.path_params
},
f,
indent=2,
......
#include <string>
#include <array>
#include <hpvm.h>
#include <tensorTypes.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 <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 ***/
int fifo_wait(const std::string &filename) {
std::ifstream file{filename};
std::string line;
std::getline(file, line);
FILE *open_fifo(const char *path, const char *mode) {
auto* fd = fopen(path, mode);
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")
return 1;
if (line == "tune")
return 2;
if (line == "stop")
return 0;
std::cout << "Invalid fifo file content \"" << line << "\"\n";
std::cerr << "Invalid fifo file content \"" << line << "\"\n";
abort();
}
void fifo_write_finished(const std::string &filename) {
std::ofstream file{filename};
file << "finished\n\0";
}
void make_fifo(const std::string &filename) {
if (mkfifo(filename.c_str(), 0666) == 0) {
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();
void fifo_write_batch(FILE *fp, void *output_ptr) {
auto *output = (Tensor *) output_ptr;
const auto &dim = output->dims;
size_t num_dims = dim.num_dims;
fwrite(&num_dims, sizeof(size_t), 1, fp);
fwrite(dim.dim_sizes, sizeof(size_t), dim.num_dims, fp);
fwrite(output->host_data, 1, output->size_in_bytes, fp);
}
{% for node in nodes %}
......@@ -95,9 +105,6 @@ typedef struct __attribute__((__packed__)) {
struct ret_t r;
} RootIn;
const int batch_size = {{batch_size}}, input_size = {{input_size}}, batch_count = input_size / batch_size;
int main(){
std::string dir_prefix = "{{prefix}}/";
std::string test_input = dir_prefix + "test_input.bin";
......@@ -117,14 +124,15 @@ int main(){
args->{{n}}_bytes = 0;
{% endfor %}
make_fifo("{{fifo_path}}");
int ret = 0;
while ((ret = fifo_wait("{{fifo_path}}"))) {
while ((ret = fifo_wait())) {
__hpvm__init();
startMemTracking();
const auto *input_pth = (ret == 1 ? test_input : tune_input).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++){
int start = i * batch_size, end = start + batch_size;
copyInputBatch(input_pth, start, end, {{input_shape|join(', ')}}, {{input_name}});
......@@ -135,10 +143,11 @@ int main(){
hpvm_request_tensor(result, 0);
llvm_hpvm_invokeRtControl(result, labels_pth, start, end);
fifo_write_batch(fp, result);
freeBatchMemory();
}
fclose(fp);
__hpvm__cleanup();
fifo_write_finished("{{fifo_path}}");
}
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