From 9bb9190c4e84feb3adbb88f4ee121599ef2dc827 Mon Sep 17 00:00:00 2001 From: sangameshdavey Date: Sun, 4 Feb 2024 16:45:59 +0530 Subject: [PATCH 1/2] LoopDist Training Flow for Pipe-( Bytes and Json) fixed --- .../Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp | 24 +- .../LoopDistributionServer.cpp | 50 +-- .../src/LoopDistribution_pb2.py | 144 --------- .../src/LoopDistribution_pb2_grpc.py | 66 ---- model/LoopDistribution/src/experiment.py | 31 +- model/LoopDistribution/src/log_reader.py | 162 ---------- model/LoopDistribution/src/multiagentEnv.py | 295 ++++-------------- 7 files changed, 94 insertions(+), 678 deletions(-) delete mode 100644 model/LoopDistribution/src/LoopDistribution_pb2.py delete mode 100644 model/LoopDistribution/src/LoopDistribution_pb2_grpc.py delete mode 100644 model/LoopDistribution/src/log_reader.py diff --git a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp index afa379bc6712..8208569ae924 100644 --- a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp +++ b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp @@ -93,7 +93,6 @@ bool LoopCost::runOnFunction(Function &F) { if (F.getName() != funcName) return false; - errs() << "Function Name: " << F.getName() << "\n"; // auto *AA = &getAnalysis().getAAResults(); auto *LI = &getAnalysis(F).getLoopInfo(); auto *LAA = &getAnalysis(F); @@ -183,11 +182,10 @@ bool LoopCost::runOnFunction(Function &F) { } } - errs() << "loop ID: " << L->getLoopID() << "\n"; L->dump(); - dbgs() << "VF: " << VF << "\n"; - dbgs() << "IF: " << IF << "\n"; - dbgs() << "TC: " << TripCount << "\n"; + // dbgs() << "VF: " << VF << "\n"; + // dbgs() << "IF: " << IF << "\n"; + // dbgs() << "TC: " << TripCount << "\n"; Loop *InstCostLoop = nullptr; if (VecLoop) @@ -206,7 +204,7 @@ bool LoopCost::runOnFunction(Function &F) { } LoopCost = LoopCost * (TripCount / (VF * IF)); - dbgs() << "Loop cost with only instructions: " << LoopCost << "\n"; + // dbgs() << "Loop cost with only instructions: " << LoopCost << "\n"; for (auto BB : L->getBlocks()) { for (auto &I : *BB) { @@ -219,21 +217,20 @@ bool LoopCost::runOnFunction(Function &F) { // const LoopAccessInfo &LAI_WR = LAA->getInfo(L); // const LoopAccessInfo &LAI_RAR = LAA->getInfo(L, 1); - errs() << "aaaaaaaaaaaaaaaaaaaaa\n"; Locality CL(TTI); int64_t CacheMisses = CL.computeLocalityCost(L, TripCount, SE, DI); LoopCost = LoopCost * (TripCount / (VF * IF)); - dbgs() << "Loop cost with only instructions: " << LoopCost << "\n"; + // dbgs() << "Loop cost with only instructions: " << LoopCost << "\n"; uint64_t TotalMemAccess = NumMemInsts * ((TripCount == 1000) ? TripCount : TripCount * (VF * IF)); - dbgs() << "Total memory accesses : " << NumMemInsts << " * " << TripCount - << " = " << TotalMemAccess << "\n"; + // dbgs() << "Total memory accesses : " << NumMemInsts << " * " << TripCount + // << " = " << TotalMemAccess << "\n"; uint64_t CacheCost = CacheMisses * (0.7 * MemoryInstCost) + (TotalMemAccess - CacheMisses) * (0.3 * MemoryInstCost); - dbgs() << "Cache cost: " << CacheCost << "\n"; + // dbgs() << "Cache cost: " << CacheCost << "\n"; uint64_t TotalLoopCost = LoopCost + CacheCost; - dbgs() << "TotalLoopCost for Loop: " << TotalLoopCost << "\n"; + // dbgs() << "TotalLoopCost for Loop: " << TotalLoopCost << "\n"; this->loop_cost= TotalLoopCost; } @@ -297,7 +294,6 @@ void LoopCost::GetInnerLoops(Loop *L, SmallVectorImpl &InnerLoops) { } uint64_t LoopCost::getLoopCost() { - errs() << "function Name: " << funcName << " loop ID: " << loopID << " loop Cost: " << this->loop_cost << "\n"; return this->loop_cost; } // Registering the pass @@ -381,7 +377,6 @@ int64_t Locality::computeLocalityCost(Loop *L, unsigned TripCount, } } - // errs() << "Src:" << *SrcI << " --> Dst:" << *DstI << "\n"; } } } @@ -451,7 +446,6 @@ int64_t Locality::computeLocalityCost(Loop *L, unsigned TripCount, } } } - errs() << "Total cache misses : " << Locality_Cost << "\n"; return Locality_Cost; } diff --git a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopDistributionServer/LoopDistributionServer.cpp b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopDistributionServer/LoopDistributionServer.cpp index dd8636694c98..286cb86607f3 100644 --- a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopDistributionServer/LoopDistributionServer.cpp +++ b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopDistributionServer/LoopDistributionServer.cpp @@ -68,7 +68,6 @@ struct LoopDistributionServerPass return false; this->F = &F; - errs() << F.getName() << "\n"; this->M = F.getParent(); dist_helper.setLid(loopID); @@ -81,7 +80,6 @@ struct LoopDistributionServerPass ORE = &getAnalysis().getORE(); LAA = &getAnalysis(); OriginalLoopCost = getAnalysis().getLoopCost(); - errs() << "OriginalLoopCost: " << OriginalLoopCost << "\n"; GetLAA = [&](Loop &L) -> const LoopAccessInfo & { return LAA->getInfo(&L); }; @@ -91,14 +89,13 @@ struct LoopDistributionServerPass auto DI = DependenceInfo(&F, AA, SE, LI); this->DI = &DI; FileModified = false; - errs() << "Before Communicate \n"; - if (use_pipe) + if (use_pipe) { initPipeCommunication(); - else if (use_grpc) { - errs() << "came here\n"; + } else if (use_grpc) { + /****** - ISSUE: Advice during training and inference is different + // ISSUE: Advice during training and inference is different FIX: 1. use `Message Advice` present in LoopDistribution.proto for training same as inference instead of `Message LoopDistributionResponse` @@ -118,15 +115,12 @@ struct LoopDistributionServerPass const ::loopdistribution::LoopDistributionRequest *request, ::loopdistribution::LoopDistributionResponse *response) override { - errs() << "came here\n"; std::string partition = request->partitionpattern(); if (partition == "Exit") { - errs() << "server exit requested\n"; - AOTRunner->requestExit(); + // AOTRunner->requestExit(); HAVE TO UNCOMMENT LATER return grpc::Status::OK; } - errs() << "distribution request received for pattern " << partition << "\n"; DistributedLoopCost = this->distributeLoopAndGetLoopCostHelper(partition); @@ -137,7 +131,6 @@ struct LoopDistributionServerPass } uint64_t distributeLoopAndGetLoopCostHelper(std::string partition) { - errs() << "Entered Distribute Func\n"; dist_helper.setPartition(partition); @@ -177,32 +170,19 @@ struct LoopDistributionServerPass } else if (data_format == "protobuf") { SerDesType = BaseSerDes::Kind::Protobuf; } else { - errs() << "Invalid data format\n"; return; } - - std::unique_ptr MLRunner = std::make_unique( + MLRunner = std::make_unique( basename + ".out", basename + ".in", SerDesType, &M->getContext()); - std::pair p1("loopcost", (long)OriginalLoopCost); MLRunner->populateFeatures(p1); - errs() << "Value populated (original cost):" << (long)OriginalLoopCost - << "\n"; int cnt = 1; - // for (auto rdg : RDG_List) { - // std::pair p1("RDG", rdg); - // MLRunner->populateFeatures(p1); - - // Obtain partition sequence + int *out; size_t size; MLRunner->evaluate(out, size); - errs() << "Func name: " << this->F << " : " << cnt++ << "\n"; - std::vector distSequence; - for (int i = 0; i < size; i++) { - distSequence.push_back(out[i]); - } + std::vector distSequence(out, out + size); std::string partition; for (int i = 0; i < 100; i++) { int element = distSequence[i]; @@ -217,21 +197,15 @@ struct LoopDistributionServerPass } // Calculate and return costs - errs() << "Received partition:" << partition << "\n"; if (partition == "Exit") { - errs() << "server exit requested\n"; - AOTRunner->requestExit(); return; - // return grpc::Status::OK; + } DistributedLoopCost = this->distributeLoopAndGetLoopCostHelper(partition); std::pair p2("loopcost", (long)DistributedLoopCost); MLRunner->populateFeatures(p2); - errs() << "Features populated (distributed cost): " - << (long)DistributedLoopCost << "\n"; - int *status_out; MLRunner->evaluate(status_out, size); std::string final_status; @@ -239,9 +213,9 @@ struct LoopDistributionServerPass final_status.push_back((char)status_out[i]); } if (final_status == "Exit") { - errs() << "Costs sent and acknowledged\n"; + MLBRIDGE_DEBUG(errs() << "Costs sent and acknowledged\n"); } else { - errs() << "Costs sent NOT acknowledged!\n"; + MLBRIDGE_DEBUG(errs() << "Costs sent NOT acknowledged!\n"); } } @@ -270,7 +244,7 @@ struct LoopDistributionServerPass Module *M; uint64_t OriginalLoopCost; uint64_t DistributedLoopCost; - std::unique_ptr AOTRunner; + std::unique_ptr MLRunner; }; } // namespace char LoopDistributionServerPass::ID = 0; diff --git a/model/LoopDistribution/src/LoopDistribution_pb2.py b/model/LoopDistribution/src/LoopDistribution_pb2.py deleted file mode 100644 index ca08215d3f21..000000000000 --- a/model/LoopDistribution/src/LoopDistribution_pb2.py +++ /dev/null @@ -1,144 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: LoopDistribution.proto -"""Generated protocol buffer code.""" -from google.protobuf import descriptor as _descriptor -from google.protobuf import message as _message -from google.protobuf import reflection as _reflection -from google.protobuf import symbol_database as _symbol_database -# @@protoc_insertion_point(imports) - -_sym_db = _symbol_database.Default() - - - - -DESCRIPTOR = _descriptor.FileDescriptor( - name='LoopDistribution.proto', - package='loopdistribution', - syntax='proto3', - serialized_options=b'\n!io.grpc.examples.loopdistributionB\025LoopDistributionProtoP\001\242\002\002LD', - create_key=_descriptor._internal_create_key, - serialized_pb=b'\n\x16LoopDistribution.proto\x12\x10loopdistribution\"3\n\x17LoopDistributionRequest\x12\x18\n\x10partitionPattern\x18\x03 \x01(\t\"Q\n\x18LoopDistributionResponse\x12\x18\n\x10OrignialloopCost\x18\x01 \x01(\x03\x12\x1b\n\x13\x64istributedLoopCost\x18\x02 \x01(\x03\x32\x8b\x01\n\x10LoopDistribution\x12w\n\x1c\x64istributeLoopAndGetLoopCost\x12).loopdistribution.LoopDistributionRequest\x1a*.loopdistribution.LoopDistributionResponse\"\x00\x42\x41\n!io.grpc.examples.loopdistributionB\x15LoopDistributionProtoP\x01\xa2\x02\x02LDb\x06proto3' -) - - - - -_LOOPDISTRIBUTIONREQUEST = _descriptor.Descriptor( - name='LoopDistributionRequest', - full_name='loopdistribution.LoopDistributionRequest', - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name='partitionPattern', full_name='loopdistribution.LoopDistributionRequest.partitionPattern', index=0, - number=3, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=b"".decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), - ], - extensions=[ - ], - nested_types=[], - enum_types=[ - ], - serialized_options=None, - is_extendable=False, - syntax='proto3', - extension_ranges=[], - oneofs=[ - ], - serialized_start=44, - serialized_end=95, -) - - -_LOOPDISTRIBUTIONRESPONSE = _descriptor.Descriptor( - name='LoopDistributionResponse', - full_name='loopdistribution.LoopDistributionResponse', - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name='OrignialloopCost', full_name='loopdistribution.LoopDistributionResponse.OrignialloopCost', index=0, - number=1, type=3, cpp_type=2, label=1, - has_default_value=False, default_value=0, - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), - _descriptor.FieldDescriptor( - name='distributedLoopCost', full_name='loopdistribution.LoopDistributionResponse.distributedLoopCost', index=1, - number=2, type=3, cpp_type=2, label=1, - has_default_value=False, default_value=0, - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), - ], - extensions=[ - ], - nested_types=[], - enum_types=[ - ], - serialized_options=None, - is_extendable=False, - syntax='proto3', - extension_ranges=[], - oneofs=[ - ], - serialized_start=97, - serialized_end=178, -) - -DESCRIPTOR.message_types_by_name['LoopDistributionRequest'] = _LOOPDISTRIBUTIONREQUEST -DESCRIPTOR.message_types_by_name['LoopDistributionResponse'] = _LOOPDISTRIBUTIONRESPONSE -_sym_db.RegisterFileDescriptor(DESCRIPTOR) - -LoopDistributionRequest = _reflection.GeneratedProtocolMessageType('LoopDistributionRequest', (_message.Message,), { - 'DESCRIPTOR' : _LOOPDISTRIBUTIONREQUEST, - '__module__' : 'LoopDistribution_pb2' - # @@protoc_insertion_point(class_scope:loopdistribution.LoopDistributionRequest) - }) -_sym_db.RegisterMessage(LoopDistributionRequest) - -LoopDistributionResponse = _reflection.GeneratedProtocolMessageType('LoopDistributionResponse', (_message.Message,), { - 'DESCRIPTOR' : _LOOPDISTRIBUTIONRESPONSE, - '__module__' : 'LoopDistribution_pb2' - # @@protoc_insertion_point(class_scope:loopdistribution.LoopDistributionResponse) - }) -_sym_db.RegisterMessage(LoopDistributionResponse) - - -DESCRIPTOR._options = None - -_LOOPDISTRIBUTION = _descriptor.ServiceDescriptor( - name='LoopDistribution', - full_name='loopdistribution.LoopDistribution', - file=DESCRIPTOR, - index=0, - serialized_options=None, - create_key=_descriptor._internal_create_key, - serialized_start=181, - serialized_end=320, - methods=[ - _descriptor.MethodDescriptor( - name='distributeLoopAndGetLoopCost', - full_name='loopdistribution.LoopDistribution.distributeLoopAndGetLoopCost', - index=0, - containing_service=None, - input_type=_LOOPDISTRIBUTIONREQUEST, - output_type=_LOOPDISTRIBUTIONRESPONSE, - serialized_options=None, - create_key=_descriptor._internal_create_key, - ), -]) -_sym_db.RegisterServiceDescriptor(_LOOPDISTRIBUTION) - -DESCRIPTOR.services_by_name['LoopDistribution'] = _LOOPDISTRIBUTION - -# @@protoc_insertion_point(module_scope) diff --git a/model/LoopDistribution/src/LoopDistribution_pb2_grpc.py b/model/LoopDistribution/src/LoopDistribution_pb2_grpc.py deleted file mode 100644 index 73cda87fd890..000000000000 --- a/model/LoopDistribution/src/LoopDistribution_pb2_grpc.py +++ /dev/null @@ -1,66 +0,0 @@ -# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! -"""Client and server classes corresponding to protobuf-defined services.""" -import grpc - -import LoopDistribution_pb2 as LoopDistribution__pb2 - - -class LoopDistributionStub(object): - """Missing associated documentation comment in .proto file.""" - - def __init__(self, channel): - """Constructor. - - Args: - channel: A grpc.Channel. - """ - self.distributeLoopAndGetLoopCost = channel.unary_unary( - '/loopdistribution.LoopDistribution/distributeLoopAndGetLoopCost', - request_serializer=LoopDistribution__pb2.LoopDistributionRequest.SerializeToString, - response_deserializer=LoopDistribution__pb2.LoopDistributionResponse.FromString, - ) - - -class LoopDistributionServicer(object): - """Missing associated documentation comment in .proto file.""" - - def distributeLoopAndGetLoopCost(self, request, context): - """Missing associated documentation comment in .proto file.""" - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details('Method not implemented!') - raise NotImplementedError('Method not implemented!') - - -def add_LoopDistributionServicer_to_server(servicer, server): - rpc_method_handlers = { - 'distributeLoopAndGetLoopCost': grpc.unary_unary_rpc_method_handler( - servicer.distributeLoopAndGetLoopCost, - request_deserializer=LoopDistribution__pb2.LoopDistributionRequest.FromString, - response_serializer=LoopDistribution__pb2.LoopDistributionResponse.SerializeToString, - ), - } - generic_handler = grpc.method_handlers_generic_handler( - 'loopdistribution.LoopDistribution', rpc_method_handlers) - server.add_generic_rpc_handlers((generic_handler,)) - - - # This class is part of an EXPERIMENTAL API. -class LoopDistribution(object): - """Missing associated documentation comment in .proto file.""" - - @staticmethod - def distributeLoopAndGetLoopCost(request, - target, - options=(), - channel_credentials=None, - call_credentials=None, - insecure=False, - compression=None, - wait_for_ready=None, - timeout=None, - metadata=None): - return grpc.experimental.unary_unary(request, target, '/loopdistribution.LoopDistribution/distributeLoopAndGetLoopCost', - LoopDistribution__pb2.LoopDistributionRequest.SerializeToString, - LoopDistribution__pb2.LoopDistributionResponse.FromString, - options, channel_credentials, - insecure, call_credentials, compression, wait_for_ready, timeout, metadata) diff --git a/model/LoopDistribution/src/experiment.py b/model/LoopDistribution/src/experiment.py index 474ed1af72a8..5cb0965ea906 100644 --- a/model/LoopDistribution/src/experiment.py +++ b/model/LoopDistribution/src/experiment.py @@ -3,12 +3,11 @@ from importlib.resources import path from posixpath import normpath from tabnanny import check -import os +import os, sys import time import numpy as np from typing import Dict as type_dict import torch -import numpy as np import ray from ray import tune from ray.tune import function @@ -49,11 +48,9 @@ def experiment(config): train_results = {} checkpoint = MODEL_PATH train_agent = SimpleQTrainer(config=config, env=DistributeLoopEnv) - print('------------------------ aegent --------------------------------- ', train_agent) if checkpoint is not None: train_agent.restore(checkpoint) - print("Checkpoint restored") # # export model using torch.onnx SELECT_NODE_MODEL_PATH = "/home/cs20btech11024/onnx/select_node.onnx" @@ -62,7 +59,6 @@ def experiment(config): last_checkpoint = 0 for i in range(iterations): - print("Training iteration: ", i) train_results = train_agent.train() # auto_garbage_collect() if i == iterations - 1 or (train_results['episodes_total'] - last_checkpoint) > 99: @@ -82,8 +78,12 @@ def experiment(config): # torch.onnx.export(train_agent.get_policy("distribution_policy").model, ({"obs": torch.randn(1, 603)}, {}), f=DISTRIBUTION_MODEL_PATH, verbose=True, input_names=["obs"], output_names=["output"]) +def blockPrint(): + sys.stdout = open(os.devnull, 'w') + if __name__ == "__main__": + args = parser.parse_args() logger = logging.getLogger(__file__) log_level=logging.DEBUG @@ -104,22 +104,18 @@ def experiment(config): config["env_config"]["data_format"] = args.data_format config["env_config"]["use_grpc"] = args.use_grpc - # utils.get_parse_args() config["env"] = DistributeLoopEnv config["env_config"]["mode"] = "train" config["env_config"]["loop_cost"] = "LC" config["env_config"]["EPOCHS"] = 100 config["env_config"]["dataset"] = DATA_DIR - Curr_Dir = os.path.basename(normpath(config["env_config"]["dataset"])) POST_DIS_PASSES_ARG = 1 config["env_config"]["distributed_data"] = config["env_config"]["trained_model"] + Curr_Dir + "/PDP_" + str(POST_DIS_PASSES_ARG) + "_EP" + str(config["env_config"]["EPOCHS"]) + "/" + config["env_config"]["mode"] - ModelCatalog.register_custom_model("select_node_model", SelectNodeNetwork) ModelCatalog.register_custom_model("distribution_model", DistributionTask) - # ModelCatalog.register_custom_model("vectorization_model", VectorizationTask) box_obs = Box( FLOAT_MIN, FLOAT_MAX, shape=(config["env_config"]["state_size"], ), dtype=np.float32) @@ -136,22 +132,17 @@ def experiment(config): "curr_Node": box_obs, "dist_flag": Box(0, 1, shape=(1,)), "action_mask": Box(0, 1, shape=(2,)), - # "state": box_obs + }) - # obs_vectorization_node = Dict({ - # "action_mask": Box() - # "state": box_obs - # }) + def policy_mapping_fn(agent_id, episode=None, **kwargs): if agent_id.startswith("select_node_agent"): return "select_node_policy" elif agent_id.startswith("distribution_agent"): return "distribution_policy" - # else: - # return "vectorization_policy" - + policies = { "select_node_policy": (None, obs_select_node, Discrete(config["env_config"]["max_number_nodes"]), { @@ -177,13 +168,13 @@ def policy_mapping_fn(agent_id, episode=None, **kwargs): }, }, }), - # "vectorization_policy": (None, obs_vectorization_node, - # ) + } config["multiagent"] = { "policies" : policies, - "policy_mapping_fn": function(policy_mapping_fn) + + "policy_mapping_fn": policy_mapping_fn } start_time = time.time() diff --git a/model/LoopDistribution/src/log_reader.py b/model/LoopDistribution/src/log_reader.py deleted file mode 100644 index fdb8eb4e2d1d..000000000000 --- a/model/LoopDistribution/src/log_reader.py +++ /dev/null @@ -1,162 +0,0 @@ -"""Reader for training log. - -See lib/Analysis/TrainingLogger.cpp for a description of the format. -""" -import ctypes -import dataclasses -import io -import json -import math -import sys -from typing import List, Optional -from functools import reduce -import operator -import numpy - -_element_types = { - "float": ctypes.c_float, - "double": ctypes.c_double, - "int8_t": ctypes.c_int8, - "uint8_t": ctypes.c_uint8, - "int16_t": ctypes.c_int16, - "uint16_t": ctypes.c_uint16, - "int32_t": ctypes.c_int32, - "uint32_t": ctypes.c_uint32, - "int64_t": ctypes.c_int64, - "uint64_t": ctypes.c_uint64, -} - - -@dataclasses.dataclass(frozen=True) -class TensorSpec: - name: str - port: int - shape: List[int] - element_type: type - - @staticmethod - def from_dict(d: dict): - name = d["name"] - port = d["port"] - shape = [int(e) for e in d["shape"]] - element_type_str = d["type"] - if element_type_str not in _element_types: - raise ValueError(f"uknown type: {element_type_str}") - return TensorSpec( - name=name, - port=port, - shape=shape, - element_type=_element_types[element_type_str], - ) - - -class TensorValue: - def __init__(self, spec: TensorSpec, buffer: bytes): - self._spec = spec - self._buffer = buffer - self._view = ctypes.cast(self._buffer, ctypes.POINTER(self._spec.element_type)) - # self._len = math.prod(self._spec.shape) - self._len = reduce(operator.mul, self._spec.shape, 1) - # self._view = numpy.frombuffer(self._buffer, float) - # print("Value of", self._spec.name, "is:", self._view) - - def spec(self) -> TensorSpec: - return self._spec - - def __len__(self) -> int: - return self._len - - def __getitem__(self, index): - if index < 0 or index >= self._len: - raise IndexError(f"Index {index} out of range [0..{self._len})") - return self._view[index] - - -def read_tensor(fs: io.BufferedReader, ts: TensorSpec) -> TensorValue: - size = reduce(operator.mul, ts.shape, 1) * ctypes.sizeof(ts.element_type) - # size = math.prod(ts.shape) * ctypes.sizeof(ts.element_type) - data = fs.read(size) - return TensorValue(ts, data) - - -def pretty_print_tensor_value(tv: TensorValue): - print(f'{tv.spec().name}: {",".join([str(v) for v in tv])}') - - -def read_header(f: io.BufferedReader): - header = json.loads(f.readline()) - tensor_specs = [TensorSpec.from_dict(ts) for ts in header["features"]] - score_spec = TensorSpec.from_dict(header["score"]) if "score" in header else None - advice_spec = TensorSpec.from_dict(header["advice"]) if "advice" in header else None - return tensor_specs, score_spec, advice_spec - - -def read_one_observation( - context: Optional[str], - event_str: str, - f: io.BufferedReader, - tensor_specs: List[TensorSpec], - score_spec: Optional[TensorSpec], -): - # event = json.loads(event_str) - # print('JSON: ', event) #DEBUG - # if "context" in event: - # context = event["context"] - # # print('CTX', context) # DEBUG - # event = json.loads(f.readline()) - # observation_id = int(event["observation"]) - features = [] - for ts in tensor_specs: - features.append(read_tensor(f, ts)) - f.readline() - # score = None - # if score_spec is not None: - # score_header = json.loads(f.readline()) - # assert int(score_header["outcome"]) == observation_id - # score = read_tensor(f, score_spec) - # f.readline() - return context, None, features, None - - -def read_stream(fname: str): - with io.BufferedReader(io.FileIO(fname, "rb")) as f: - tensor_specs, score_spec, _ = read_header(f) - context = None - while True: - event_str = f.readline() - if not event_str: - break - context, observation_id, features, score = read_one_observation( - context, event_str, f, tensor_specs, score_spec - ) - yield context, observation_id, features, score - -def read_stream2(f: io.BufferedReader): - context = None - while True: - tensor_specs, score_spec, _ = read_header(f) - # event_str = f.readline() - # print("Event: ", event_str) - # if not event_str: - # break - print("Read header", tensor_specs, score_spec) - context, observation_id, features, score = read_one_observation( - context, '', f, tensor_specs, score_spec - ) - yield context, observation_id, features, score - -def main(args): - last_context = None - for ctx, obs_id, features, score in read_stream(args[1]): - if last_context != ctx: - print(f"context: {ctx}") - last_context = ctx - print(f"observation: {obs_id}") - for fv in features: - pretty_print_tensor_value(fv) - if score: - pretty_print_tensor_value(score) - - -if __name__ == "__main__": - main(sys.argv) diff --git a/model/LoopDistribution/src/multiagentEnv.py b/model/LoopDistribution/src/multiagentEnv.py index ba2a8184ccc9..ce70cd73b8b2 100644 --- a/model/LoopDistribution/src/multiagentEnv.py +++ b/model/LoopDistribution/src/multiagentEnv.py @@ -7,13 +7,11 @@ """ import argparse from doctest import testfile -# from importlib.metadata import distribution import gym import numpy as np import os import random import utils -# import utils_1 import logging import json import math @@ -22,7 +20,7 @@ from gym.spaces import Discrete, Box from ld_config import BUILD_DIR, CONFIG_DIR from ggnn_1 import GatedGraphNeuralNetwork, AdjacencyList, get_observations - +import time import ray from ray import tune from ray.tune import grid_search @@ -43,18 +41,19 @@ import subprocess import time -#import grpc related modules from google.protobuf.json_format import MessageToJson import grpc -import LoopDistribution_pb2_grpc, LoopDistribution_pb2 -# pipe related imports import io import re -import log_reader + import ctypes from functools import reduce import operator +sys.path.append(f"{BUILD_DIR}/../MLCompilerBridge/CompilerInterface") +from PipeCompilerInterface import PipeCompilerInterface +from GrpcCompilerInterface import GrpcCompilerInterface +import log_reader sys.path.extend([ f"{BUILD_DIR}/MLCompilerBridge/MLModelRunner/gRPCModelRunner/Python-Utilities" @@ -92,7 +91,6 @@ def __init__(self, env_config): if self.mode != 'inference': dataset = env_config["dataset"] self.graphs_num = env_config["graphs_num"] - print("dataset path: {}".format(dataset)) self.training_graphs=glob.glob(os.path.join(dataset, 'graphs/loops/json/*.json')) assert len(self.training_graphs) > 0, 'training set is empty' if len(self.training_graphs) > self.graphs_num: @@ -106,19 +104,17 @@ def __init__(self, env_config): else: filename = 'mcacost_p{}.csv'.format(self.config["post_pass_key"]) filepath = os.path.join(self.config["dataset"], filename) - print("filepath {}".format(filepath)) self.loopcost_cache = utils.load_precomputed_cost(filepath) self.distributed_data = self.config["distributed_data"] self.second_loopcost_cache = set() - print("self.distributed_data: {}".format(self.distributed_data)) self.GraphList = [] self.doneList = [] self.grpc_rtt = 0 + self.is_init = True self.speedup = 0 self.use_pipe = env_config['use_pipe'] - print("use pipe --------------", self.use_pipe) self.use_grpc = not self.use_pipe if self.use_pipe: self.data_format = env_config['data_format'] @@ -126,23 +122,14 @@ def __init__(self, env_config): if self.use_pipe: - # pipes opening - self.temp_rootname = "/tmp/loopdistppipe" - to_compiler = self.temp_rootname + ".in" - from_compiler = self.temp_rootname + ".out" - print("to_compiler", to_compiler) - print("from_compiler", from_compiler) - if os.path.exists(to_compiler): - os.remove(to_compiler) - if os.path.exists(from_compiler): - os.remove(from_compiler) - os.mkfifo(to_compiler, 0o666) - os.mkfifo(from_compiler, 0o666) - self.tc = None - self.fc = None + self.temp_rootname="/tmp/loopdistppipe" + self.compiler_interface=PipeCompilerInterface(data_format=self.data_format, pipe_name=self.temp_rootname) + self.compiler_interface.init_pipes() + + self.tensor_specs = None self.advice_spec = None - self.partition_seq = None + self.partition_seq = None def reward_formula(self, distributedLoopCost, OriginalLoopCost): reward = 0 @@ -153,22 +140,18 @@ def reward_formula(self, distributedLoopCost, OriginalLoopCost): else: logging.warning('distributedLoopCost or Original LoopCost is Zero ....., reward={}'.format(reward)) - return reward #, self.speedup + return reward def getDistributionReward(self, action=None): if self.mode != 'inference': home_dir = self.home_dir - print(home_dir) method_name = self.functionName loop_id = self.loopId ll_file_name = self.fileName fun_id = self.fun_id - # hloop_id = self.hloop_id reward=0 - # key = "{filename}_{function_name}_{loopid}_{disSeq}".format(filename=ll_file_name, function_name=method_name, loopid=loop_id, disSeq=self.distribution) - # key = (ll_file_name, method_name, loop_id, '|'.join([ ','.join(sorted(seqdis.split(','))) for seqdis in self.distribution.split('|')])) key = (ll_file_name, method_name, loop_id, '|'.join([ ','.join(list(map(lambda x: 'S{}'.format(x), sorted(seqdis.replace('S','').split(','))))) for seqdis in self.distribution.split('|')])) isFound = False @@ -192,33 +175,16 @@ def getDistributionReward(self, action=None): meta_ssa_file_path = os.path.join(meta_ssa_dir, ll_file_name) input_file_path = meta_ssa_file_path - # if self.mode == 'test': - # LL_DIR_CONST='llfiles' - # dist_ll_dir=os.path.join(self.distributed_data, LL_DIR_CONST) - # if not os.path.exists(dist_ll_dir): - # os.makedirs(dist_ll_dir) - # dist_llfile = os.path.join(dist_ll_dir, ll_file_name) - - # Is_exist=os.path.exists(dist_llfile) - # if Is_exist: - # input_file_path = dist_llfile - # vectorfactor = None - # if isVectorizationtask: - # vectorfactor = action - # distributed_llfile = utils.call_distributionPass( input_file_path, self.distribution, method_name, loop_id, fun_id, loop_id, self.distributed_data, vecfactor=vectorfactor) + if self.use_grpc or self.use_pipe: OriginalLoopCost,distributedLoopCost = self.doLoopDistributionGetLoopCost(self.distribution) else: - # distributed_llfile = input_file_path distributed_llfile = utils.call_distributionPass( input_file_path, self.distribution, method_name, loop_id, fun_id, loop_id, self.distributed_data) - # print("distribution pattern: {}".format(self.distribution)) - # self.speedup=0 if distributed_llfile is None: logging.warning('distributed file not generated...., reward={}'.format(reward)) else: if self.config["loop_cost"]: pass - # logging.info('Get the loop_cost metric for original loop') OriginalLoopCost = utils.getLoopCost(meta_ssa_file_path, loop_id, method_name) logging.info('Get the loop_cost metric for distributed loop') distributedLoopCost = utils.getLoopCost(distributed_llfile, loop_id, method_name) @@ -230,17 +196,11 @@ def getDistributionReward(self, action=None): if self.mode != 'test': os.remove(distributed_llfile) reward = self.reward_formula(distributedLoopCost, OriginalLoopCost) - # Remove, it is occupies a lot of space - - # update the cache - # if not isVectorizationtask: + self.second_loopcost_cache.add(key + (distributedLoopCost, OriginalLoopCost,)) logging.info('***Key added to the cache***') logging.info('ll_filename|OriginalLoopCost|TransformLoopCost|reward|distributeSeq|task {} {} {} {} {} {}'.format(ll_file_name, OriginalLoopCost, distributedLoopCost, reward, self.distribution, 'Distribution')) - # else: - # logging.info('ll_filename|OriginalLoopCost|TransformLoopCost|reward|distributeSeq|task|predVecfactor {} {} {} {} {} {} {}'.format(ll_file_name, OriginalLoopCost, distributedLoopCost, reward, self.distribution, 'Vectorization', vectorfactor)) - # costly operation - # self.loopcost_cache.loc[key,['Distributed cost', 'Undsitributed Cost']] = [ distributedLoopCost, OriginalLoopCost] + return reward @@ -250,20 +210,14 @@ def getReward(self): def _select_distribution_step(self, action): dist_flag = [1] * 1 if(action == 0): - # print("DO NOT DISTRIBUTE (MERGE)") self.obs.annotations[self.cur_node][0] = 1 - # print(self.obs.annotations) - # self.ggnn.mpAfterDisplacement(self.cur_node) self.node_id = self.obs.idx_nid[self.cur_node] self.distribution = "{},{}".format(self.distribution, self.node_id) self.obs.unique_type_map['pair'].append((self.prev_node, self.cur_node)) - # self.obs.addPairEdge(self.prev_node, self.cur_node) logging.info('DLOOP merge {prev_node} with {cur_node}'.format(prev_node=self.prev_node, cur_node=self.cur_node)) else: - # print("DISTRIBUTE") dist_flag = [0] * 1 self.obs.annotations[self.cur_node][0] = 1 - # self.obs.mpAfterDisplacement(self.cur_node) self.node_id = self.obs.idx_nid[self.cur_node] self.distribution = "{}|{}".format(self.distribution, self.node_id) logging.info('DLOOP distribute {prev_node} with {cur_node}'.format(prev_node=self.prev_node, cur_node=self.cur_node)) @@ -279,7 +233,6 @@ def _select_distribution_step(self, action): if len(possibleStartNodes) == 0 : if self.mode != 'inference': reward_final = self.getDistributionReward() - print(reward_final) if select_node_mask is None: select_node_mask = [0]*self.max_number_nodes done_all = True @@ -321,51 +274,28 @@ def _select_distribution_step(self, action): self.distribution_agent_id: True, "__all__": True } - # else: - # self.agent_count += 1 - # self.select_node_agent_id += "select_node_agent_{}".format(self.agent_count) - # self.distribution_agent_id += "distribution_node_agent_{}".format(self.agent_count) - - # print("action mask: {}".format(np.array(select_node_mask).shape)) - # print("state: {}".format(cur_obs.shape)) - # obs[self.select_node_agent_id] = { - # self.select_node_agent_id: { 'action_mask': np.array(select_node_mask), 'state': cur_obs} - # } - # reward[self.select_node_agent_id] = 0 - # done[self.select_node_agent_id] = done_all - print("self.distribution: {}".format(self.distribution)) + if self.mode != 'inference': return obs, reward, done, {} else: - # if self.use_pipe: - # self.parseSequenceAndSend(self.distribution) + distribution = self.parseSeq(self.distribution) return obs, reward, done, distribution - # def step_via_vectorization(self, action): - # next_state =None - # done=True - # reward = 0 - # if self.mode != 'inference': - # reward = self.getDistributionReward(action, isVectorizationtask=True) - # return next_state, reward, done + def createNodeSelectMask(self): mask = [0]*self.max_number_nodes - # eligibleNodes = self.obs.graph_topology.get_eligibleNodes() eligibleNodes = self.obs.graph_topology.findAllVertaxWithZeroWeights() - print("len(eligibleNodes): {}".format(len(eligibleNodes))) assert len(eligibleNodes) < self.max_number_nodes, "Graph has more then maximum nodes allowed" for inx, x in enumerate(eligibleNodes): if x in eligibleNodes: mask[x] = 1 if all(v == 0 for v in mask): - print("eligibleNodes", eligibleNodes) return None - # assert False, "No node elegible to select" return mask @@ -375,13 +305,10 @@ def _select_node_step(self, action): self.prev_node = self.cur_node self.cur_node = action - print("current node: {}".format(self.cur_node)) update_status = self.obs.graph_topology.UpdateVisitList(self.cur_node) if not update_status: - print("UpdateVisitList failed for {} graph at {} node".format(self.path, self.cur_node)) assert False, 'discovered node visited.' self.node_id = self.obs.idx_nid[self.cur_node] - print("Node selected = {}, corresponding register id = {}".format(action, self.node_id)) logging.info("Node selected = {}, corresponding register id = {}".format(action, self.node_id)) if (self.prev_node == None): @@ -389,7 +316,6 @@ def _select_node_step(self, action): state = self.obs self.hidden_state = self.ggnn(initial_node_representation=state. initial_node_representation, annotations=state.annotations, adjacency_lists=state.adjacency_lists) - # self.obs.initial_node_representation = self.hidden_state self.current_obs = self.hidden_state[self.cur_node][0:self.emb_size] if self.current_obs is not None and not isinstance(self.current_obs, np.ndarray): self.current_obs = self.current_obs.detach().numpy() @@ -434,7 +360,6 @@ def _select_node_step(self, action): self.distribution_agent_id: { 'action_mask': np.array(distribution_mask, dtype=np.float32), 'prev_Node': np.array(self.prev_node_obs, dtype=np.float32), 'curr_Node' : np.array(self.cur_node_obs, dtype=np.float32), 'dist_flag':np.array(dist_flag, dtype=np.float32)}, } - # self.cur_obs = hidden_state[node_index] logging.debug("Exit _select_node_step") return obs, reward, done, {} @@ -456,35 +381,19 @@ def reset(self, graph=None): if (len(self.GraphList) == 0): self.GraphList = self.doneList.copy() self.doneList.clear() - # self.iteration_counter += 1 - # self.rename_Dir = True + else: - # self.GraphList = graph - # index = np.random.random_integers(0, len(self.GraphList) - 1) - # path = self.config["test_dir"] + graph - # with open(path) as f: - # graph = json.load(f) - # self.reset_env(graph, path) + self.reset_env(graph) self.agent_count = 0 self.select_node_agent_id = "select_node_agent_{}".format(self.agent_count) self.distribution_agent_id = "distribution_agent_{}".format(self.agent_count) - # self.vectorization_agent_id = "vectorization_agent_{}".format(self.agent_count) select_node_mask = self.createNodeSelectMask() state = self.obs - # import math - # for i, vec in enumerate(state.initial_node_representation): - # for v in vec: - # if math.isnan(v): - # print('multiagentEnv state.initial_node_representation ****NAN****', v, i) - - # for i, vec in enumerate(state.annotations): - # for v in vec: - # if math.isnan(v): - # print('multiagentEnv state.annotation ****NAN****', v, i) + self.hidden_state = self.ggnn(initial_node_representation=state.initial_node_representation, annotations=state.annotations, adjacency_lists=state.adjacency_lists) node_mat = self.hidden_state.detach().numpy() @@ -499,15 +408,11 @@ def reset(self, graph=None): return obs - # input graph : jsonnx - # return the state of the graph, all the possible starting nodes + def reset_env(self, graph=None, path=None): self.ggnn = GatedGraphNeuralNetwork(hidden_size=self.emb_size, annotation_size=2, num_edge_types=1, layer_timesteps=[1], residual_connections={}, nodelevel=True) - # if graph is None: - # print("graph is None") - # else: - # print("graph is present") + self.topology = None # Have the graph formed from adjency list using dependence edges only self.distribution = "" @@ -515,14 +420,12 @@ def reset_env(self, graph=None, path=None): self.cur_node = None self.graph = graph - # if self.mode != 'inference': if(self.mode != 'inference'): fileinfo = utils.getllFileAttributes_old(path) self.home_dir = fileinfo['HOME_DIR'] self.loop_id = fileinfo['LOOP_ID'] self.fun_id = fileinfo['FUN_ID'] self.path = path - # print("type(graph): {}".format(type(graph))) self.fileName = graph['graph'][1][1]['FileName'].strip('\"') self.functionName = graph['graph'][1][1]['Function'].strip('\"') @@ -536,78 +439,29 @@ def reset_env(self, graph=None, path=None): input_file_path = os.path.join(meta_ssa_dir, self.fileName) if self.use_grpc or self.use_pipe: self.serverAddress = "127.0.0.1:5005" - print("loopId: ",self.loopId," loop_id: ",self.loop_id) self.process = self.startServer(input_file_path,self.functionName,self.loop_id,self.serverAddress) if self.use_grpc: self.channel = grpc.insecure_channel( self.serverAddress) self.stub = LoopDistribution_pb2_grpc.LoopDistributionStub(self.channel) - if self.use_pipe: + if self.use_pipe and self.is_init == True: # Opening pipe files - to_compiler = self.temp_rootname + ".in" - from_compiler = self.temp_rootname + ".out" - print("Creating pipe files", to_compiler, from_compiler) - self.tc = io.BufferedWriter(io.FileIO(to_compiler, "wb")) - print("Opened the write pipe") - self.fc = io.BufferedReader(io.FileIO(from_compiler, "rb")) - print("Opened the read pipe") - # self.tensor_specs, _, self.advice_spec = log_reader.read_header(self.fc) - # print("Tensor and Advice spec", self.tensor_specs, self.advice_spec) - # result = self.readObservation() - # element = result[0].__getitem__(0) - # print("Pipe init result:", element) - # exit(0) - + self.compiler_interface.reset_pipes() + self.is_init = False + def readObservation(self): - - next_event = self.fc.readline() - # if not next_event: - # break - print("Reading next line:", next_event) - context = None - last_context, observation_id, features,_ = log_reader.read_one_observation( - context, next_event, self.fc, self.tensor_specs, None - ) - if last_context != context: - print(f"context: {last_context}") - context = last_context - print(f"observation: {observation_id}") - tensor_values = [] - for fv in features: - # log_reader.pretty_print_tensor_value(fv) - tensor_values.append(fv) - # return tensor_values - + + features=self.compiler_interface.evaluate() loopcost = None if self.data_format == "bytes": - #read first 8 bytes to be compatible with protobuf - # if self.read_stream_iter is None: - self.read_stream_iter = log_reader.read_stream2(self.fc) - print("Before reading cost") - hdr = self.fc.read(8) - print("Header", hdr) - context, observation_id, features, score = next(self.read_stream_iter) - print("Obtained feature value is", features[0].__getitem__(0)) - # assert len(features[0]) == 1 loopcost = features[0].__getitem__(0) - # loopcost = np.empty([1]) - # for i in range(len(features[0])): - # loopcost[i] = features[0][i] + + elif self.data_format == "json": - print("reading json...") - #read first 8 bytes to be compatible with protobuf - hdr = self.fc.read(8) - print("hdr: ",hdr) - size = int.from_bytes(hdr, "little") - print("size: ", size) - msg = self.fc.read(size) - loopcost = json.loads(msg.decode('utf-8'))["loopcost"] - # assert len(embedding) == 300 - # embedding = np.array(embedding) - + loopcost=features['loopcost'] return loopcost def sendResponseV1(self, f: io.BufferedWriter, value, spec: log_reader.TensorSpec): @@ -615,12 +469,10 @@ def sendResponseV1(self, f: io.BufferedWriter, value, spec: log_reader.TensorSpe """Send the `value` - currently just a scalar - formatted as per `spec`.""" # just int64 for now assert spec.element_type == ctypes.c_int64 - # to_send = ctypes.c_int64(int(value)) + to_send = (ctypes.c_int64 * len(value))(*value) assert f.write(bytes(to_send)) == ctypes.sizeof(spec.element_type) * reduce(operator.mul, spec.shape, 1) - # assert f.write(bytes(to_send)) == ctypes.sizeof(spec.element_type) * math.prod( - # spec.shape - # ) + f.flush() except (BrokenPipeError, IOError): pass @@ -638,24 +490,17 @@ def sendResponse(self, data): elif self.data_format == "protobuf": pass out = hdr + msg - # print("out: ", out) self.tc.write(out) self.tc.flush() def step(self, action_dict): - # assert(self.ggnn is not None, 'ggnn is None') if self.select_node_agent_id in action_dict: return self._select_node_step(action_dict[self.select_node_agent_id]) if self.distribution_agent_id in action_dict: return self._select_distribution_step(action_dict[self.distribution_agent_id]) - # if task == 'Distribution': - # return self.step_via_distribution(action) - # elif task == 'Vectorization': - # return self.step_via_vectorization(action) - # else: - # assert(False, 'Not valid task selected') + def startServer(self,filePath, functionName, loopId, serverAddress): optPath = f"{BUILD_DIR}/bin/opt" @@ -663,50 +508,49 @@ def startServer(self,filePath, functionName, loopId, serverAddress): # edit server, path if(self.use_pipe): - cmd = optPath + " -LoopDistributionServer -loopID " + loopId + " -funcName " + functionName + " -lc-function " + functionName + " -lc-lID " + loopId + " --ml-config-path " + CONFIG_DIR + " -S " + filePath + " --use-pipe-loop-dist " + " --loop-dist-data-format " + self.config["data_format"] + " -o /dev/null" # f" --ml-config-path {REPO_DIR}/build_all/config " + + + cmd = optPath + " -LoopDistributionServer -loopID " + loopId + " -funcName " + functionName + " -lc-function " + functionName + " -lc-lID " + loopId + " --ml-config-path " + CONFIG_DIR + " -S " + filePath + " --use-pipe-loop-dist " + " --loop-dist-data-format " + self.config["data_format"] + " -o /dev/null " # f" --ml-config-path {REPO_DIR}/build_all/config " + elif(self.use_grpc): # old cmd : - cmd = optPath + " -LoopDistributionServer -loopID " + loopId + " -funcName " + functionName + " -lc-function " + functionName + " -lc-lID " + loopId + " --server_address_loop_dist "+ serverAddress + " --ml-config-path " + CONFIG_DIR + " -S " + filePath + " --use-grpc-loop-dist " + " -o /dev/null" + cmd = optPath + " -LoopDistributionServer -loopID " + loopId + " -funcName " + functionName + " -lc-function " + functionName + " -lc-lID " + loopId + " --server_address_loop_dist "+ serverAddress + " --ml-config-path " + CONFIG_DIR + " -S " + filePath + " --use-grpc-loop-dist " + " -o /dev/null " - print("server start command: ",cmd) pid = subprocess.Popen(cmd, executable='/bin/bash', shell=True, preexec_fn= os.setsid) - + time.sleep(2) #VERY IMPORTANT + + return pid def doLoopDistributionGetLoopCost(self,partition: str): - print('use_pipe in multiagentEnv', self.use_pipe) if self.use_grpc: response = self.stable_grpc(partition) jsonObj = MessageToJson(response) response = json.loads(jsonObj) - print("gRPC response: ",response) self.stable_grpc("Exit") OrignialloopCost = int(response['OrignialloopCost']) distributedLoopCost = int(response['distributedLoopCost']) elif self.use_pipe: - - print("Before reading original loop cost") + #first c++ sends original cost + #then python sends updated seq + #then c++ runs that and gives back cost(distributed) + #then py sends exit status result = self.readObservation() - OrignialloopCost = result # .__getitem__(0) - print("Python received original cost", OrignialloopCost) + OrignialloopCost = result self.parseSequenceAndSend(partition) - print("Partition Sequence sent") result = self.readObservation() - distributedLoopCost = result # .__getitem__(0) - # send acknowledgement out:status + distributedLoopCost = result + exitStatus = [ ord(c) for c in "Exit" ] - self.sendResponse(exitStatus) - print("Recieved costs: ", OrignialloopCost, distributedLoopCost) - - self.killServer() + self.compiler_interface.populate_buffer(exitStatus) + self.compiler_interface.evaluate(mode="exit") + time.sleep(2) #MAKES SURE THAT THE c++ reads it first ,and then only we kill the sever #VERY IMPORTANT LINE OF CODE + + self.killServer() return OrignialloopCost, distributedLoopCost def parseSeq(self, partition): seq = partition.replace('|', '-') - # seq = re.split(r',|-', seq) seq = re.split('(\W)', seq) - print("Partition Sequence before: ",partition) partition_seq = [-1]*100 for i in range(len(seq)): if 'S' in seq[i]: @@ -716,27 +560,23 @@ def parseSeq(self, partition): partition_seq[i] = 101 else: partition_seq[i] = 102 - print("Partition Sequence after: ", partition, partition_seq) self.partition_seq = partition_seq - print("In parseSeq func: self.partition_seq: ", self.partition_seq) return partition_seq def parseSequenceAndSend(self, partition): self.parseSeq(partition) - self.sendResponse(self.partition_seq) - # self.sendResponse(self.tc, partition_seq, self.advice_spec) + self.compiler_interface.populate_buffer(self.partition_seq) def killServer(self): - # self.process.kill() - # self.process.communicate() + os.killpg(os.getpgid(self.process.pid), signal.SIGKILL) - if self.process.poll() is not None: - print("Server Closed Sucessfully") + stdout, stderr = self.process.communicate() + def stable_grpc(self, pattern): attempt = 0 - max_retries = 5 + max_retries = 10 #chaning it to 10 as it says Max tries completed (it was 5 before) retry_wait_seconds = 0.1 retry_wait_backoff_exponent = 1.5 @@ -744,12 +584,10 @@ def stable_grpc(self, pattern): result = None while True: try: - print("LLVM grpc called") t1 = time.time() if pattern != "Exit": result = self.stub.distributeLoopAndGetLoopCost(request) t2 = time.time() - print("LLVM grpc response came in {} sec".format(t2 - t1)) self.grpc_rtt += t2-t1 # time.sleep(.1) break @@ -758,24 +596,15 @@ def stable_grpc(self, pattern): if e.code() == grpc.StatusCode.UNAVAILABLE: attempt += 1 if attempt > max_retries: - print("Maximum attempts completed") return None - # raise #ServiceTransportError( f"{self.url} {e.details()} ({max_retries} retries)") from None remaining = max_retries - attempt - # logging.warning( - # "%s %s (%d %s remaining)", - # self.url, - # e.details(), - # remaining, - # plural(remaining, "attempt", "attempts"), - # ) + time.sleep(retry_wait_seconds) retry_wait_seconds *= retry_wait_backoff_exponent else: if self.mode != 'inference': - print("Unknown error", e.code()) return None else: raise return result - \ No newline at end of file + From e53be8026a6e381a3298a924ed97a1ad9aa22cc2 Mon Sep 17 00:00:00 2001 From: Kaivalya Date: Sat, 17 Feb 2024 17:11:11 +0530 Subject: [PATCH 2/2] LPD Code cleanup cpp files Clean up of all the cpp files in the /llvm/lib/Transforms/Scalar/IR2Vec-LOF/ folder --- .../DependeneDistanceMutation.cpp | 25 +---------- .../IR2Vec-LOF/IR2Vec-SCC/IR2Vec-SCC.cpp | 44 ++----------------- .../Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp | 11 +---- .../LoopDistribution/LoopDistribution.cpp | 6 --- .../Transforms/Scalar/IR2Vec-LOF/RDG/RDG.cpp | 42 ++---------------- .../custom_loop_distribution.cpp | 25 ----------- .../inference/multi_agent_env.cpp | 24 +--------- 7 files changed, 9 insertions(+), 168 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/DependeneDistanceMutation/DependeneDistanceMutation.cpp b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/DependeneDistanceMutation/DependeneDistanceMutation.cpp index c0aec75585d5..faf9df163567 100644 --- a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/DependeneDistanceMutation/DependeneDistanceMutation.cpp +++ b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/DependeneDistanceMutation/DependeneDistanceMutation.cpp @@ -22,17 +22,13 @@ using namespace llvm; void DependenceDistanceMutation::Mutate_InstList( InstructionListType StoreInstList, LLVMContext &Context) { for (auto S : StoreInstList) { - // errs() << "StoreInstList: " << *S << "\n"; for (auto i = S->op_begin(), e = S->op_end(); i != e; ++i) { if (dyn_cast(&(**i))) { Instruction *OP = dyn_cast(&(**i)); - // errs() << "OP: " << *OP << "\n"; if (isa(OP)) { auto array_OP = dyn_cast(OP); - // errs() << "Number of indices: " << array_OP->getNumOperands() << "\n"; - // errs() << "Name: " << OP->getName() << "\n"; - + bool flag_Name = 0; for (auto n : NameList) { if (n == OP->getName()) { @@ -43,27 +39,19 @@ void DependenceDistanceMutation::Mutate_InstList( if (flag_Name == 0) { NameList.push_back(OP->getName()); - // errs() << "Name3: " << OP->getName() << "\n"; unsigned op_i = array_OP->getNumOperands() - 1; - // for (unsigned op_i = 1; op_i < array_OP->getNumOperands(); ++op_i) - // { auto array_index = dyn_cast(array_OP->getOperand(op_i)); - // errs() << "op_i: " << *array_index << "\n"; // Create new "add" instruction IRBuilder<> builder(array_OP); auto *OP_type = array_OP->getOperand(op_i)->getType(); Value *Right = ConstantInt::get(Type::getInt64Ty(Context), mutate); - // errs() << "aaaaaaaaaaa: " << *OP_type << " : " << *Right->getType() - // << "\n"; Value *Result = builder.CreateAdd(array_OP->getOperand(op_i), Right); - errs() << "bbbbbbbbbbbbbbbbbb: " << *Result << "\n"; array_OP->setOperand(op_i, Result); } - // } } } // errs() << "\n"; @@ -221,27 +209,16 @@ bool DependenceDistanceMutation::runOnFunction(Function &F) { break; } if (flag_MergedList == 0) { - // errs() << "StoreInstList: " << *S << "\n"; MergedInstList.push_back(S); } } else { - // errs() << "StoreInstList: " << *S << "\n"; MergedInstList.push_back(S); } } } Mutate_InstList(MergedInstList, Context); - - - // Mutate all instruction inside StoreInstList - // errs() << "aaaaaaaaaaaaaaaaaaaa\n"; - // Mutate_InstList(StoreInstList, Context); - - // Mutate all instruction inside WAWInstList - // errs() << "bbbbbbbbbbbbbbbbbbbbb\n"; - // Mutate_InstList(WAWInstList, Context); } } return false; diff --git a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/IR2Vec-SCC/IR2Vec-SCC.cpp b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/IR2Vec-SCC/IR2Vec-SCC.cpp index 4555c84dbf68..5ef77601d06e 100644 --- a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/IR2Vec-SCC/IR2Vec-SCC.cpp +++ b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/IR2Vec-SCC/IR2Vec-SCC.cpp @@ -132,12 +132,6 @@ void RDGWrapperPass::Print_IR2Vec_File(DataDependenceGraph &G, if (tgtNode->NodeLabel != "") { if ((*E).isMemoryDependence()) { md++; - // errs() << NodeNumber.find(N)->second << " -> " - // << NodeNumber.find(&E->getTargetNode())->second << " : - // " - // << (*E).getKind() << " : " << (*E).getEdgeWeight() << - // "\n"; - File << NodeNumber.find(N)->second << " -> " << NodeNumber.find(&E->getTargetNode())->second << "[label=\"" << (*E).getKind() << ": " @@ -200,9 +194,6 @@ void RDGWrapperPass::addMCACalls(Loop *L, int loopID) const { bool RDGWrapperPass::runOnFunction(Function &F) { return computeRDG(F); } bool RDGWrapperPass::computeRDG(Function &F) { - // errs()<<"Inside RDGWrapperPass:\n"; - // F.dump(); - DT = &getAnalysis().getDomTree(); LI = &getAnalysis().getLoopInfo(); @@ -289,17 +280,11 @@ void RDGWrapperPass::canonicalizeLoopsWithLoads( auto dest = tuples[1]; auto insv = tuples[2]; auto inst = dyn_cast(insv); - // errs() << "inst: "; - // inst->dump(); auto ldInst = new LoadInst(dest, ""); ldInst->insertBefore(inst); for (unsigned i = 0; i < inst->getNumOperands(); i++) { if (inst->getOperand(i) == src) { inst->setOperand(i, ldInst); - // errs() << "Operand set successfully - "; - // inst->dump(); - // errs() << "===\n"; - // inst->getParent()->dump(); break; } } @@ -342,11 +327,6 @@ void RDGWrapperPass::populateDOTData(DataDependenceGraph &G, continue; auto Vec = instVecMap.find(II)->second; - // errs() << "Vec[" << II << "] = "; - // for (unsigned I = 0; I < DIM; ++I) - // errs() << Vec[I] << " "; - // errs() << "\n"; - // errs() << "End\n"; if (!Found) { NodeVec = Vec; Found = true; @@ -356,13 +336,7 @@ void RDGWrapperPass::populateDOTData(DataDependenceGraph &G, } } // rdg.NodeRepresentations[stoi(N->NodeLabel.substr(1)) - 1] = NodeVec; - // errs() << "Nodevecd : ---\n"; - // for(unsigned I = 0; I < DIM; ++I) - // { - // errs() << NodeVec[I] << " "; - // } - // errs() << "\n"; - // errs() << "End\n"; + NodeNumber.insert(std::make_pair(N, N->NodeLabel)); } LLVM_DEBUG(errs() << "**before adjlist filling\n"); @@ -412,26 +386,18 @@ RDGData RDGWrapperPass::computeRDGForFunction(Function &F) { int loopNum = 0; for (LoopInfo::iterator i = LI->begin(), e = LI->end(); i != e; ++i) { Loop *L = *i; - // L->dump(); - // errs() << " came lo line 376\n"; for (auto il = df_begin(L), el = df_end(L); il != el; ++il) { if (il->getSubLoops().size() > 0) { - // errs() << "IR2Vec-SCC.cpp : line 377\n"; continue; } - // for (auto i : il->blocks()) { - // i->dump(); - // } - + // Append Memory Dependence Edges with weights into Graph loopNum++; auto *LAA = &getAnalysis(); const LoopAccessInfo &LAI = LAA->getInfo(*il); auto RDGraph = RDG(*AA, *SE, *LI, DI, LAI, ORE); - // errs() << "Function Name: " << F.getName() <<"\n"; - // errs() << "**** LOOP No. :" << loopNum << "\n"; - + auto SCC_Graph = RDGraph.computeRDGForInnerLoop(**il); if (SCC_Graph == nullptr) { @@ -458,7 +424,6 @@ RDGData RDGWrapperPass::computeRDGForFunction(Function &F) { std::string SCC_Filename = "S_" + s2 + "_F" + std::to_string(FunctionNumber) + "_L" + std::to_string(loopNum) + ".dot"; - // "S_" + s3 + "_F_" + s4 + "_L" + std::to_string(loopNum) + ".dot"; LLVM_DEBUG(errs() << "Writing " + SCC_Filename + "\n"); RDGraph.PrintDotFile_LAI(SCCGraph, SCC_Filename, s1); @@ -468,13 +433,10 @@ RDGData RDGWrapperPass::computeRDGForFunction(Function &F) { content.assign((std::istreambuf_iterator(ifs)), (std::istreambuf_iterator())); - // errs() << "String: " << content << "\n"; - // Print Input File std::string Input_Filename = "I_" + s2 + "_F" + std::to_string(FunctionNumber) + "_L" + std::to_string(loopNum) + ".dot"; - // "I_" + s3 + "_F_" + s4 + "_L" + std::to_string(loopNum) + ".dot"; LLVM_DEBUG(errs() << "Writing " + Input_Filename + "\n"); Print_IR2Vec_File(SCCGraph, Input_Filename, s2, loopNum); diff --git a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp index 8208569ae924..1dda3e3cd2f8 100644 --- a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp +++ b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopCost/LoopCost.cpp @@ -183,10 +183,7 @@ bool LoopCost::runOnFunction(Function &F) { } L->dump(); - // dbgs() << "VF: " << VF << "\n"; - // dbgs() << "IF: " << IF << "\n"; - // dbgs() << "TC: " << TripCount << "\n"; - + Loop *InstCostLoop = nullptr; if (VecLoop) InstCostLoop = VecLoop; @@ -204,7 +201,6 @@ bool LoopCost::runOnFunction(Function &F) { } LoopCost = LoopCost * (TripCount / (VF * IF)); - // dbgs() << "Loop cost with only instructions: " << LoopCost << "\n"; for (auto BB : L->getBlocks()) { for (auto &I : *BB) { @@ -220,17 +216,12 @@ bool LoopCost::runOnFunction(Function &F) { Locality CL(TTI); int64_t CacheMisses = CL.computeLocalityCost(L, TripCount, SE, DI); LoopCost = LoopCost * (TripCount / (VF * IF)); - // dbgs() << "Loop cost with only instructions: " << LoopCost << "\n"; uint64_t TotalMemAccess = NumMemInsts * ((TripCount == 1000) ? TripCount : TripCount * (VF * IF)); - // dbgs() << "Total memory accesses : " << NumMemInsts << " * " << TripCount - // << " = " << TotalMemAccess << "\n"; uint64_t CacheCost = CacheMisses * (0.7 * MemoryInstCost) + (TotalMemAccess - CacheMisses) * (0.3 * MemoryInstCost); - // dbgs() << "Cache cost: " << CacheCost << "\n"; uint64_t TotalLoopCost = LoopCost + CacheCost; - // dbgs() << "TotalLoopCost for Loop: " << TotalLoopCost << "\n"; this->loop_cost= TotalLoopCost; } diff --git a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopDistribution/LoopDistribution.cpp b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopDistribution/LoopDistribution.cpp index 4f3e10e64035..760f80f9f710 100644 --- a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopDistribution/LoopDistribution.cpp +++ b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/LoopDistribution/LoopDistribution.cpp @@ -6,9 +6,6 @@ #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" -// #include -// #include "llvm/IR/PassManager.h" -// #include "llvm/Analysis/CFG.h" #include "llvm/Analysis/LoopAccessAnalysis.h" #include "llvm/Analysis/LoopAnalysisManager.h" #include "llvm/InitializePasses.h" @@ -239,12 +236,9 @@ Loop *LoopDistribution::cloneLoop(Loop *L, LoopInfo *LI, DominatorTree *DT, // it was empty. assert(&*oldPreheader->begin() == oldPreheader->getTerminator() && "preheader not empty"); - // errs() << "here1- before cloning\n"; Loop *newLoop = cloneLoopWithPreheader(L->getLoopPreheader(), L->getLoopPreheader(), L, VMap, Twine("new-"), LI, DT, newLoopBlocks); - // errs() << "here2- after cloning\n"; - // VMap can contain mappings at instruction level or BB level // The code below obtains VMap at instruction level from BB level diff --git a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/RDG/RDG.cpp b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/RDG/RDG.cpp index 84349ea9befd..d0695a1fa25b 100644 --- a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/RDG/RDG.cpp +++ b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/RDG/RDG.cpp @@ -101,10 +101,7 @@ void RDG::createMemoryEdgeMergedNode(DataDependenceGraph &G, DependenceInfo &DI, using EdgeKind = typename EdgeType::EdgeKind; // Merging Node Outgoing edges => Final Node Outgoing edges - // Connect target of Merging Node as a target of Final Node - - // errs() << FinalNode << "\n" << MergingNode << "\n\n"; - + // Connect target of Merging Node as a target of Final Node for (EdgeType *oldEdge : MergingNode) { NodeType *tgt = &oldEdge->getTargetNode(); DDGEdge &EdgeDel = MergingNode.back(); @@ -135,11 +132,9 @@ void RDG::createMemoryEdgeMergedNode(DataDependenceGraph &G, DependenceInfo &DI, } } else if (EdgeDel.getKind() == EdgeKind::MemoryDependence) { int i = EdgeDel.getEdgeWeight(); - // errs() << "outgoing edge: " << i << "\n"; bool MSL_flag = 0; for (NodeType *MSL : NodeDeletionList) { if (MSL == tgt) { - // errs() << "Same Node:"; MSL_flag = 1; break; } @@ -166,13 +161,11 @@ void RDG::createMemoryEdgeMergedNode(DataDependenceGraph &G, DependenceInfo &DI, for (NodeType *N : G) { int MSL_flag = 0; if (*N != FinalNode) { - // errs() << "Not a FinalNode: " << *N << "\n"; for (EdgeType *e : *N) { NodeType *tgt = &e->getTargetNode(); if (*tgt == MergingNode) { for (NodeType *MSL : NodeDeletionList) { if (MSL == N) { - // errs() << "Same Node:"; MSL_flag = 1; break; } @@ -204,7 +197,6 @@ void RDG::createMemoryEdgeMergedNode(DataDependenceGraph &G, DependenceInfo &DI, if (*tgt == MergingNode) { if (e->getKind() == EdgeKind::MemoryDependence) { int i = e->getEdgeWeight(); - // errs() << "incoming edge: " << "\n"; bool nt_flag = 0; for (EdgeType *eF : *N) { NodeType *Ftgt = &eF->getTargetNode(); @@ -237,19 +229,11 @@ void RDG::CreateSCC(DataDependenceGraph &G, DependenceInfo &DI) { ListOfSCCs.emplace_back(SCC.begin(), SCC.end()); } - // for (NodeListType &NL : ListOfSCCs) { - // for (NodeType *n : NL){ - // errs() << *n << "\n"; - // } - // } for (NodeListType &NL : ListOfSCCs) { LLVM_DEBUG(dbgs() << "Creating pi-block node with " << NL.size() << " nodes in it.\n"); - // for (NodeType *n : NL){ - // errs() << *n << "\n"; - // } for (NodeType *Source : NL) { for (NodeType *Target : NL) { // No Merging Possible: if Source and Target are same node @@ -268,9 +252,7 @@ void RDG::CreateSCC(DataDependenceGraph &G, DependenceInfo &DI) { InstList); - createMemoryEdgeMergedNode(G, DI, *Source, *Target, NodeDeletionList); - // errs() << "bbbbbbbbbbbbb Source: " << *Source << "\n"; - + createMemoryEdgeMergedNode(G, DI, *Source, *Target, NodeDeletionList); // Apepnd the node (merged into Store node) into NodeDeletionList bool ni = 0; @@ -282,7 +264,6 @@ void RDG::CreateSCC(DataDependenceGraph &G, DependenceInfo &DI) { } if (ni == 0) { NodeDeletionList.push_back(Target); - // errs() << "nodeList: " << *Target << "\n"; } } break; @@ -301,7 +282,6 @@ void RDG::SelectOnlyStoreNode(DataDependenceGraph &G) { int label = 0; for (auto *N : G) { - // errs() << *N << "\n"; InstructionListType InstList; N->collectInstructions([](const Instruction *I) { return true; }, InstList); @@ -387,23 +367,19 @@ void RDG::Merge_NonLabel_Nodes(DataDependenceGraph &G, DependenceInfo &DI) { bool RDG::BuildRDG_DA(raw_ostream &OS, DataDependenceGraph &G, DependenceInfo *DI, Loop &IL) { for (auto b : IL.blocks()) { - // b->dump(); for (BasicBlock::iterator SrcI = b->begin(), SrcE = b->end(); SrcI != SrcE; ++SrcI) { - // errs() << "instruction: " << *SrcI << "\n"; if (SrcI->mayReadOrWriteMemory()) { for (BasicBlock::iterator DstI = SrcI, DstE = b->end(); DstI != DstE; ++DstI) { if (DstI->mayReadOrWriteMemory()) { if (SrcI != DstI) { if (auto D = DI->depends(&*SrcI, &*DstI, true)) { if (!D->isInput()) { - // OS << "Src:" << *SrcI << " --> Dst:" << *DstI << "\n"; Instruction &si = *SrcI; Instruction &di = *DstI; NodeType *SrcNode, *DstNode; for (NodeType *N : G) { - // errs() << "Node: " << *N << "\n"; InstructionListType InstList; N->collectInstructions([](const Instruction *I) { return true; }, InstList); @@ -417,30 +393,18 @@ bool RDG::BuildRDG_DA(raw_ostream &OS, DataDependenceGraph &G, DependenceInfo *D } } - // OS << " da analyze - "; - // D->dump(OS); - // errs() << D->getLevels() << "\n"; unsigned Level = D->getLevels(); const SCEV *Distance = D->getDistance(Level); if (Distance != nullptr) if (auto x = dyn_cast(D->getDistance(Level))) { int DepDist = x->getValue()->getSExtValue(); - // OS << "Src:" << *SrcI << " --> Dst:" << *DstI << "\n"; - // errs() << *SrcNode << "\n" << *DstNode << "\n"; - // errs() << "Distance: " << DepDist << "\n"; if (DepDist < 0){ DDGBuilder(G, *DI, BBList, ReductionPHIList).createMemoryWeightedEdge(*DstNode, *SrcNode, -DepDist); } else { DDGBuilder(G, *DI, BBList, ReductionPHIList).createMemoryWeightedEdge(*SrcNode, *DstNode, DepDist); } - - // errs() << "aaaaaaaaaaa: " << DepDist << "\n"; - // errs() << *SrcNode << "\n"; - // errs() << *DstNode << "\n"; + } - // if (Distance) { - // OS << *Distance << "\n"; - // } } } } diff --git a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/custom_loop_distribution/custom_loop_distribution.cpp b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/custom_loop_distribution/custom_loop_distribution.cpp index 94078b9a6591..e947833a7bad 100644 --- a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/custom_loop_distribution/custom_loop_distribution.cpp +++ b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/custom_loop_distribution/custom_loop_distribution.cpp @@ -46,8 +46,6 @@ static cl::opt usePipe("cld-use-pipe-inf", cl::Optional, cl::init(false)); static cl::opt useOnnx("cld-use-onnx", cl::desc("Use onnx for inference"), cl::Hidden, cl::Optional, cl::init(false)); -// static cl::opt useOrg("cld-use-org", cl::desc("Use org for inference"), -// cl::Hidden, cl::Optional, cl::init(false)); static cl::opt pipe_name("cld-pipe-name", cl::Hidden, cl::init("loopdistppipe")); static cl::opt data_format("cld-data-format", cl::Hidden, @@ -127,17 +125,11 @@ void custom_loop_distribution::canonicalizeLoopsWithLoads() { auto dest = tuples[1]; auto insv = tuples[2]; auto inst = dyn_cast(insv); - // errs() << "inst: "; - // inst->dump(); auto ldInst = new LoadInst(dest, ""); ldInst->insertBefore(inst); for (unsigned i = 0; i < inst->getNumOperands(); i++) { if (inst->getOperand(i) == src) { inst->setOperand(i, ldInst); - // errs() << "Operand set successfully - "; - // inst->dump(); - // errs() << "===\n"; - // inst->getParent()->dump(); break; } } @@ -150,11 +142,9 @@ void custom_loop_distribution::initPipeCommunication( for (auto rdg : RDG_List) { std::pair p1("RDG", rdg); MLRunner->populateFeatures(p1); - errs() << "Features populated END...\n"; int *out; size_t size; MLRunner->evaluate(out, size); - errs() << "Func name: " << this->FName << " : " << cnt++ << "\n"; std::vector distSequence; for (int i = 0; i < size; i++) { distSequence.push_back(out[i]); @@ -171,12 +161,9 @@ void custom_loop_distribution::initPipeCommunication( else partition.append("S" + std::to_string(element)); } - errs() << "Reseved partition:" << partition << "\n"; distributed_seqs.push_back(partition); outfile << partition << "\n"; - errs() << "Dist_seqs vec size: " << distSequence.size() << "\n"; } - errs() << "Covered all RDGs\n"; std::pair p1("RDG", "Exit"); MLRunner->populateFeatures(p1); int *out; @@ -187,9 +174,6 @@ void custom_loop_distribution::initPipeCommunication( bool custom_loop_distribution::runOnFunction(Function &F) { assert(MLConfig::mlconfig != "" && "ml-config-path required"); - // if (F.getName() != "s222") - // return false; - // F.dump(); errs() << "Entered custom_loop_distribution pass\n"; this->M = F.getParent(); this->FName = F.getName(); @@ -202,8 +186,6 @@ bool custom_loop_distribution::runOnFunction(Function &F) { R.computeRDG(F); RDGData data = R.getRDGInfo(); - // RDG_List.insert(RDG_List.end(), data.input_rdgs.begin(), - // data.input_rdgs.end()); SCCGraphs.insert(SCCGraphs.end(), data.SCCGraphs.begin(), data.SCCGraphs.end()); loops.insert(loops.end(), data.loops.begin(), data.loops.end()); @@ -275,15 +257,12 @@ bool custom_loop_distribution::runOnFunction(Function &F) { agents[DISTRIBUTION_AGENT] = &distribution_agent; MLRunner = std::make_unique(this, agents, &M->getContext()); - // runInference(); MLRunner->evaluate(); errs() << this->DistributionSeq << "\n"; outfile << this->DistributionSeq << "\n"; distributed_seqs.push_back(this->DistributionSeq); } outfile.close(); - // errs() << "Code is Commented\n"; - // exit(0); } else { loopdistribution::RDGData request; loopdistribution::Advice response; @@ -327,10 +306,6 @@ bool custom_loop_distribution::runOnFunction(Function &F) { bool isdis = dist_helper.runwithAnalysis(SCCGraphs, loops, distributed_seqs, SE, LI, DT, AA, ORE, GetLAA, DI); - // bool isdis = - // dist_helper.runwithAnalysis(SCCGraphs, loops, distributed_seqs, - // vf_seqs, - // SE, LI, DT, AA, ORE, GetLAA, DI); distributed_seqs.clear(); LLVM_DEBUG(if (isdis) { errs() << "Code is distributed..\n"; }); return isdis; diff --git a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/custom_loop_distribution/inference/multi_agent_env.cpp b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/custom_loop_distribution/inference/multi_agent_env.cpp index ca02794cfd5f..566d3632607e 100644 --- a/llvm/lib/Transforms/Scalar/IR2Vec-LOF/custom_loop_distribution/inference/multi_agent_env.cpp +++ b/llvm/lib/Transforms/Scalar/IR2Vec-LOF/custom_loop_distribution/inference/multi_agent_env.cpp @@ -15,8 +15,7 @@ Observation &LDEnv::reset() { this->CurrentNode = -1; this->resetDone(); int idx = 0; - errs() << "NodeRepresentation size = " << currRDG.NodeRepresentations.size() - << "\n"; + this->NodeRepresentation.clear(); for (auto &E : currRDG.NodeRepresentations) { this->NodeRepresentation.push_back(E.second); @@ -25,16 +24,13 @@ Observation &LDEnv::reset() { this->idx_nid[idx] = E.first; idx++; } - // this->resetDone(); - // delete this->GraphTopology; this->GraphTopology = new Graph(currRDG.AdjList, currRDG.NodeRepresentations.size()); CurrObs.resize(SELECT_NODE_OBS_SIZE); this->select_node_obs_constructor(CurrObs); this->setNextAgent(SELECT_NODE_AGENT); - // this->setCurrentObservation(Obs, SELECT_NODE_AGENT); return CurrObs; } @@ -58,11 +54,6 @@ void LDEnv::create_node_select_mask(SmallVector &Mask) { SmallVector EligibleNodes; this->GraphTopology->getEligibleNodes(EligibleNodes); - (errs() << "EligibleNodes = "); - for (auto Node : EligibleNodes) { - (errs() << Node << " "); - } - (errs() << "\n"); LLVM_DEBUG(errs() << "\n"); for (auto Node : EligibleNodes) { if (Node >= MAX_NODES_COUNT) { @@ -91,7 +82,6 @@ void LDEnv::select_node_obs_constructor(Observation &Obs) { } errs() << "]\n\n"; }); - errs() << "\n\n"; for (auto V : this->NodeRepresentation) { for (auto e : V) { Obs[CurrIdx++] = e; @@ -102,7 +92,6 @@ void LDEnv::select_node_obs_constructor(Observation &Obs) { Observation LDEnv::select_node_step(Action Action) { this->PrevNode = this->CurrentNode; this->CurrentNode = Action; - errs() << "current node: " << this->CurrentNode << "\n"; this->GraphTopology->updateVisitList(this->CurrentNode); LLVM_DEBUG(errs() << "select_node_step: line: " << 53 << "\n"); int CurrIdx = 0; @@ -113,12 +102,10 @@ Observation LDEnv::select_node_step(Action Action) { // fill the Obs vector LLVM_DEBUG(errs() << "prevnode = " << this->PrevNode << "\n"); if (this->PrevNode < 0) { - printIdx(59); Observation Obs(SELECT_NODE_OBS_SIZE); this->select_node_obs_constructor(Obs); - // setCurrentObservation(Obs, SELECT_NODE_AGENT); this->DistributionSeq = "S" + std::to_string(this->CurrentNode + 1); return Obs; } else { @@ -126,7 +113,6 @@ Observation LDEnv::select_node_step(Action Action) { // Action mask Obs[CurrIdx++] = 1; Obs[CurrIdx++] = 1; - printIdx(95); // CurrentNode for (auto e : this->NodeRepresentation[this->CurrentNode]) { Obs[CurrIdx++] = e; @@ -134,16 +120,13 @@ Observation LDEnv::select_node_step(Action Action) { // dist_flag Obs[CurrIdx++] = 0; - printIdx(103); // PrevNode for (auto e : this->NodeRepresentation[this->PrevNode]) { Obs[CurrIdx++] = e; } - // setCurrentObservation(Obs, DISTRIBUTION_AGENT); setNextAgent(DISTRIBUTION_AGENT); return Obs; } - // printIdx(119); } Observation LDEnv::select_distribution_step(Action Action) { @@ -156,8 +139,6 @@ Observation LDEnv::select_distribution_step(Action Action) { this->DistributionSeq = this->DistributionSeq + "|S" + std::to_string(this->CurrentNode + 1); } - errs() << "previous node: " << this->PrevNode - << " and current node: " << this->CurrentNode << "\n"; Observation Obs(SELECT_NODE_OBS_SIZE); // Action mask SmallVector ActionMask(MAX_NODES_COUNT, 0); @@ -167,11 +148,9 @@ Observation LDEnv::select_distribution_step(Action Action) { << "\n"); }; this->create_node_select_mask(ActionMask); - printIdx(138); for (auto e : ActionMask) { Obs[CurrIdx++] = e; } - printIdx(141); // state for (auto V : this->NodeRepresentation) { for (auto e : V) { @@ -180,6 +159,5 @@ Observation LDEnv::select_distribution_step(Action Action) { } // setCurrentObservation(Obs, SELECT_NODE_AGENT); setNextAgent(SELECT_NODE_AGENT); - printIdx(148); return Obs; }