def get_code_template(data: dict) -> Response: """Get code template file for requested Workload.""" workload_id = RequestDataProcessor.get_string_value(data, "workload_id") workdir = Workdir() workload_data = workdir.get_workload_data(workload_id) code_template_path = workload_data.get("code_template_path") if not code_template_path: raise NotFoundException(f"Unable to find code template file for {workload_id}") return ResponseGenerator.serve_from_filesystem( path=code_template_path, mimetype="text/x-python", )
def get_output(data: dict) -> Response: """Get config file for requested Workload.""" workload_id = RequestDataProcessor.get_string_value(data, "workload_id") workdir = Workdir() workload_data = workdir.get_workload_data(workload_id) log_path = workload_data.get("log_path") if not log_path: raise NotFoundException(f"Unable to find output log for {workload_id}") try: response = ResponseGenerator.serve_from_filesystem( path=log_path, mimetype="text/plain", ) except Exception as e: response = ResponseGenerator.from_exception(e) return ResponseGenerator.add_refresh( response=response, refresh_time=3, )
def execute_optimization(data: Dict[str, Any]) -> dict: """Get configuration.""" from lpot.ux.utils.workload.workload import Workload if not str(data.get("id", "")): message = "Missing request id." mq.post_error( "optimization_finish", {"message": message, "code": 404}, ) raise Exception(message) request_id: str = data["id"] workdir: Optional[Workdir] = None try: workdir = Workdir(request_id=request_id, overwrite=False) workload_path: str = workdir.workload_path workload_data = _load_json_as_dict( os.path.join(workload_path, "workload.json"), ) workload = Workload(workload_data) optimization: Optimization = OptimizationFactory.get_optimization( workload, workdir.template_path, ) send_data = { "message": "started", "id": request_id, "size_input_model": get_size(optimization.input_graph), } workdir.clean_logs() workdir.update_data( request_id=request_id, model_path=optimization.input_graph, input_precision=optimization.input_precision, model_output_path=optimization.output_graph, output_precision=optimization.output_precision, status="wip", ) executor = Executor( workspace_path=workload_path, subject="optimization", data=send_data, log_name="output", ) proc = executor.call( optimization.command, ) optimization_time = executor.process_duration if optimization_time: optimization_time = round(optimization_time, 2) log.debug(f"Elapsed time: {optimization_time}") logs = [os.path.join(workload_path, "output.txt")] parser = OptimizationParser(logs) if proc.is_ok: response_data = parser.process() optimized_model_path = response_data.get( "path_optimized_model", optimization.output_graph, ) optimization.output_graph = optimized_model_path if isinstance(response_data, dict): response_data["id"] = request_id response_data["optimization_time"] = optimization_time response_data["size_optimized_model"] = get_size(optimized_model_path) response_data["model_output_path"] = optimized_model_path response_data["size_input_model"] = get_size(optimization.input_graph) response_data["is_custom_dataloader"] = bool(workdir.template_path) workdir.update_data( request_id=request_id, model_path=optimization.input_graph, model_output_path=optimized_model_path, metric=response_data, status="success", execution_details={"optimization": optimization.serialize()}, input_precision=optimization.input_precision, output_precision=optimization.output_precision, ) response_data["execution_details"] = {"optimization": optimization.serialize()} log.debug(f"Parsed data is {json.dumps(response_data)}") mq.post_success("optimization_finish", response_data) return response_data else: log.debug("FAIL") workdir.update_data( request_id=request_id, model_path=optimization.input_graph, input_precision=optimization.input_precision, output_precision=optimization.output_precision, status="error", ) raise ClientErrorException("Optimization failed during execution.") except Exception as err: mq.post_failure("optimization_finish", {"message": str(err), "id": request_id}) if workdir is not None and workdir.get_workload_data(request_id).get("status") != "error": workdir.update_data( request_id=request_id, status="error", ) raise