def main(): parser = argparse.ArgumentParser( prog="megengine.tools.svg_viewer", description="View SVG Graph produced bt megengine profiler", ) parser.add_argument("-p", "--port", type=int, default=8000, help="server port") parser.add_argument( "-a", "--address", type=str, default="localhost", help="server address" ) args = parser.parse_args() address = args.address port = args.port src_filename = "svg_viewer.html" dst_filename = "index.html" src_path = os.path.join(os.path.dirname(__file__), src_filename) url = "http://{}:{}/{}".format("localhost", port, dst_filename) ssh_fwd_cmd = "ssh -L {}:localhost:{} <remote ip>".format(port, port) with tempfile.TemporaryDirectory() as serve_dir: dst_path = os.path.join(serve_dir, dst_filename) os.symlink(src_path, dst_path) os.chdir(serve_dir) get_logger().info("cd to serve directory: {}, starting".format(serve_dir)) server = http.server.HTTPServer( (address, port), http.server.SimpleHTTPRequestHandler ) get_logger().info( "server started, please visit '{}' to watch profiling result".format(url) ) get_logger().info( "if you are in remote environment, use '{}' to forward port to local".format( ssh_fwd_cmd ) ) try: server.serve_forever() except KeyboardInterrupt: get_logger().info("server exiting")
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. import argparse import logging import time from collections import OrderedDict import numpy as np import megengine as mge from megengine.core.tensor import megbrain_graph as G from megengine.device import get_device_count, set_default_device from megengine.functional.debug_param import set_execution_strategy from megengine.logger import enable_debug_log, get_logger, set_log_file from megengine.utils import comp_graph_tools as tools logger = get_logger(__name__) def make_data_given_desc(args, inputs, shape0_multiply=1): if args.load_input_data: logger.info("load data from {}".format(args.load_input_data)) data = mge.load(args.load_input_data) data_names = [inp.name for inp in inputs] if isinstance(data, np.ndarray): assert len(data_names) == 1, ( "data is given as a single numpy array, so there should be " "exactly one input in the graph; got: {}".format(data_names) ) data = {data_names[0]: data}
def main(): parser = argparse.ArgumentParser(prog="megengine.tools.profiler", description="Profiling megengine program") parser.add_argument("-m", "--module", action="store_true", help="whether launch program as module") parser.add_argument("-o", "--output", type=str, help="output file location") parser.add_argument( "-f", "--format", action="append", type=str, help="output file format", choices=Profiler.valid_formats, ) parser.add_argument( "--merge_trace_events", action="store_true", ) parser.add_argument( "--clean", action="store_true", ) for opt in Profiler.valid_options: parser.add_argument("--" + opt, type=int, default=None) args, extras = parser.parse_known_args(sys.argv[1:]) prof_args = {} for opt in Profiler.valid_options: optval = getattr(args, opt, None) if optval is not None: prof_args[opt] = optval if args.output is not None: prof_args["path"] = args.output if args.format: prof_args["formats"] = args.format if len(extras) == 0: if not args.merge_trace_events: parser.print_usage() exit(1) else: filename = extras[0] if not args.module: if not os.path.exists(filename): get_logger().fatal("cannot find file {}".format(filename)) exit(1) filename = os.path.realpath(filename) # Replace profiler's dir with script's dir in front of module search path. sys.path[0] = os.path.dirname(filename) sys.argv[:] = [filename, *extras[1:]] profiler = Profiler(**prof_args) if args.clean: for file in os.listdir(profiler.directory): os.remove(os.path.join(profiler.directory, file)) with profiler: if args.module: run_module(filename) else: run_script(filename) profiler.dump() if args.merge_trace_events: merge_trace_events(profiler.directory)