diff --git a/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/hpvm-rt-controller.h b/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/hpvm-rt-controller.h index 4c8c3156a2e4f8677e634074da8420b1228e2385..7f81069c78aae8873c1b7ea3961408cbc05cb832 100644 --- a/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/hpvm-rt-controller.h +++ b/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/hpvm-rt-controller.h @@ -487,8 +487,100 @@ void llvm_hpvm_clearRuntimeController() { return; } +//*** Methods to compute accuracy of a tensor by the runtime controller ***// +uint32_t* labels_from_file = NULL; + +uint32_t* hpvm_rt_readLabelsBatch_cached(const char* labels_file, int start, int end) { + + // Initialize buffer + if (!labels_from_file) { + FILE* file = fopen(labels_file, "rb"); + if (file == NULL) { + ERROR("Data file %s is not found. Aborting...\n", labels_file); + abort(); + } + // Get number of labels + fseek(file, 0, SEEK_END); + long size = ftell(file); + fseek(file, 0, SEEK_SET); // return file pointer to beginning + + // Allocate memory for labels + labels_from_file = (uint32_t*) malloc(sizeof(uint32_t) * size); + if (labels_from_file == NULL) { + ERROR("Memory allocation for labels unsucessfull. Aborting...\n"); + abort(); + } + + // Copy the labels file into the allocated buffer + size_t result = fread(labels_from_file, 1, sizeof(uint32_t) * size, file); + if (result != size) { + // We did not read as many elemets as there are in the file + ERROR("Reading labels file unsucessfull. Aborting...\n"); + abort(); + } + + fclose(file); + } + +// int num_labels = end - start; +// uint32_t* labels = (uint32_t*) malloc(sizeof(uint32_t) * num_labels); +// for (unsigned i = start; i < end; i++) { +// labels[i-start] = labels_from_file[i]; +// } +// return labels; + + // Return pointer to labels + return &labels_from_file[start]; + +} + +//*** Copied from dnn_sources/include/utils.h ***// +float hpvm_rt_computeAccuracy3(uint32_t* labels, void* result_ptr) { + + struct Tensor* result = (struct Tensor*) result_ptr; + + size_t batch_dim = result->dims.dim_sizes[0]; + size_t num_classes = result->dims.dim_sizes[1]; + float* data = (float*) result->host_data; + int num_errors = 0; + + printf("batch_dim = %lu, num_classes = %lu \n", batch_dim, num_classes); + + for(int i = 0; i < batch_dim; i++){ + + int chosen = 0; + for (int id = 1; id < num_classes; ++id){ + if (data[i * num_classes + chosen] < data[i * num_classes + id]) chosen = id; + } + + if(chosen != labels[i]) + num_errors++; + } + + float accuracy = ((batch_dim - num_errors) * 1.0 / batch_dim * 1.0) * 100.0; + printf("****** Accuracy = %f \n\n", accuracy); + + FILE* fp = fopen("final_accuracy", "w+"); + if(fp != NULL){ + + std::ostringstream ss; + ss << std::fixed << accuracy; + std::string print_str = ss.str(); + + fwrite(print_str.c_str(), 1, print_str.length(), fp); + } + + fclose(fp); + + return accuracy; +} + +void llvm_hpvm_invokeRtControl(void* result, const char* str, int start, int end) { + + uint32_t* labels_cached = hpvm_rt_readLabelsBatch_cached(str, start, end); + + hpvm_rt_computeAccuracy3(labels_cached, result); -void llvm_hpvm_invokeRtControl(void* result, uint8_t* labels){ RC->findNextConfiguration(); // Still use findNext configuration, to update the configurationIdx, // to point to next location @@ -497,8 +589,5 @@ void llvm_hpvm_invokeRtControl(void* result, uint8_t* labels){ RC->findTargetConfiguration(goalVal, k); } -void llvm_hpvm_invokeRtControl2(void* result, uint32_t* labels){ - -} #endif diff --git a/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/rt-controller-api.h b/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/rt-controller-api.h index 903c9674bdcd679d2252f4ce6469f4979cf7dab8..b9fe649325b7da11d3e03db6e21cad386d56747b 100644 --- a/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/rt-controller-api.h +++ b/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/rt-controller-api.h @@ -5,8 +5,7 @@ extern "C"{ void llvm_hpvm_initializeRuntimeController(const char *, const char *); void llvm_hpvm_clearRuntimeController(); - void llvm_hpvm_invokeRtControl(void* result, uint8_t* labels); - void llvm_hpvm_invokeRtControl2(void* result, uint32_t* labels); + void llvm_hpvm_invokeRtControl(void* result, const char* str, int start, int end); }