From bb87ab0b5a97863f9a1104d84abf7dcfc6f36389 Mon Sep 17 00:00:00 2001 From: tkaruturi Date: Thu, 27 Jul 2023 14:41:49 -0700 Subject: [PATCH 1/2] Use python_binary and python_library from build wrappers in profiler targets Differential Revision: D47851150 fbshipit-source-id: a8ab56c67498a2ebd01026bd678fd5e3f206023c --- profiler/TARGETS | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/profiler/TARGETS b/profiler/TARGETS index 34d40b795d3..095cb7c0c4b 100644 --- a/profiler/TARGETS +++ b/profiler/TARGETS @@ -1,15 +1,15 @@ # Any targets that should be shared between fbcode and xplat must be defined in # targets.bzl. This file can contain fbcode-only targets. -load("@fbcode_macros//build_defs:python_binary.bzl", "python_binary") -load("@fbcode_macros//build_defs:python_library.bzl", "python_library") +load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime") -python_library( +runtime.python_library( name = "parse_profiler_library", srcs = [ "parse_profiler_results.py", + "profiler_results_cli.py", ], - visibility = ["PUBLIC"], + visibility = ["@EXECUTORCH_CLIENTS"], deps = [ "fbsource//third-party/pypi/prettytable:prettytable", "//executorch/exir:schema", @@ -17,11 +17,12 @@ python_library( ], ) -python_library( +runtime.python_library( name = "profiler_results_scuba", srcs = [ "profiler_results_scuba.py", ], + visibility = ["@EXECUTORCH_CLIENTS"], deps = [ ":parse_profiler_library", "//executorch/exir:schema", @@ -32,15 +33,22 @@ python_library( ], ) -python_binary( - name = "profiler_results_cli", +runtime.python_library( + name = "profiler_results_cli_lib", srcs = [ "profiler_results_cli.py", ], - main_module = "executorch.profiler.profiler_results_cli", deps = [ ":parse_profiler_library", ":profiler_results_scuba", "//executorch/exir/serialize:lib", ], ) + +runtime.python_binary( + name = "profiler_results_cli", + main_module = "executorch.profiler.profiler_results_cli", + deps = [ + ":profiler_results_cli_lib", + ], +) From 3f517f82904e73d2da7a7797f5f9c33cd448ed1b Mon Sep 17 00:00:00 2001 From: Tarun Karuturi Date: Thu, 27 Jul 2023 14:42:03 -0700 Subject: [PATCH 2/2] Move profiler scuba logging to fb specific folder Differential Revision: D47851149 fbshipit-source-id: ad998f10bc7bddaf6a2e82ed5df5c28e0b6a0a51 --- profiler/TARGETS | 17 ----- profiler/profiler_results_cli.py | 3 - profiler/profiler_results_scuba.py | 110 ----------------------------- 3 files changed, 130 deletions(-) delete mode 100644 profiler/profiler_results_scuba.py diff --git a/profiler/TARGETS b/profiler/TARGETS index 095cb7c0c4b..f55c9381259 100644 --- a/profiler/TARGETS +++ b/profiler/TARGETS @@ -17,22 +17,6 @@ runtime.python_library( ], ) -runtime.python_library( - name = "profiler_results_scuba", - srcs = [ - "profiler_results_scuba.py", - ], - visibility = ["@EXECUTORCH_CLIENTS"], - deps = [ - ":parse_profiler_library", - "//executorch/exir:schema", - "//executorch/exir/serialize:lib", - "//libfb/py:fburl", - "//libfb/py:scuba_url", - "//rfe/scubadata:scubadata_py3", - ], -) - runtime.python_library( name = "profiler_results_cli_lib", srcs = [ @@ -40,7 +24,6 @@ runtime.python_library( ], deps = [ ":parse_profiler_library", - ":profiler_results_scuba", "//executorch/exir/serialize:lib", ], ) diff --git a/profiler/profiler_results_cli.py b/profiler/profiler_results_cli.py index aa7b4d2bad3..fdc66f5e43f 100644 --- a/profiler/profiler_results_cli.py +++ b/profiler/profiler_results_cli.py @@ -14,9 +14,6 @@ ProfileData, ) -from executorch.profiler.profiler_results_scuba import upload_to_scuba - - def gen_chrome_traceevents_json( profile_results_path: str, model_ff_path: str, json_out_path: str ): diff --git a/profiler/profiler_results_scuba.py b/profiler/profiler_results_scuba.py deleted file mode 100644 index b98e1154e27..00000000000 --- a/profiler/profiler_results_scuba.py +++ /dev/null @@ -1,110 +0,0 @@ -import getpass -import hashlib -import uuid - -from typing import Dict, List, Optional - -from executorch.exir.schema import FrameList -from executorch.exir.serialize import deserialize_from_flatbuffer - -from executorch.profiler.parse_profiler_results import ( - frame_list_normal_vector_short, - frame_list_short_repr, - get_frame_list, - ProfileEvent, -) - -from libfb.py.fburl import FBUrlError, get_fburl, resolve_fburl -from libfb.py.scuba_url import ScubaDrillstate, ScubaURL -from rfe.scubadata.scubadata_py3 import Sample, ScubaData - - -def generate_scuba_fburl(scuba_url): - scuba_url = str(scuba_url) - key = hashlib.md5(scuba_url.encode()).hexdigest()[:20] - try: - fb_url = "https://fburl.com/{}".format(key) - resolve_fburl(fb_url) - return fb_url - except FBUrlError: - return get_fburl(scuba_url, string_key=key) - - -def upload_to_scuba(profile_data: Dict[str, List[ProfileEvent]], model_path: str): - scuba_table = "executorch_profile" - scuba_data = ScubaData(scuba_table) - run_id = str(uuid.uuid4()) - - program = None - model_bytes = None - if model_path is not None: - with open(model_path, "rb") as model_file: - model_bytes = model_file.read() - program = deserialize_from_flatbuffer(model_bytes) - - def frame_list_normal_vector(frame_list: FrameList) -> Optional[List[str]]: - if frame_list is None: - return None - return [ - f'File "{frame.filename}", line {frame.lineno}, in {frame.name}\n{frame.context}\n' - for frame in frame_list.items - ] - - execution_plan_idx = 0 - samples = [] - - for _, profile_data_list in profile_data.items(): - for d in profile_data_list: - sample = Sample() - sample.setTimeColumnNow() - sample.addNormalValue("run_id", run_id) - sample.addNormalValue("unixname", getpass.getuser()) - - sample.addNormalValue("profile_event_name", d.name) - - sample.addIntValue("execution_plan_idx", execution_plan_idx) - sample.addIntValue("chain_idx", d.chain_idx) - sample.addIntValue("instruction_idx", d.instruction_idx) - sample.addFloatValue("duration", d.duration[0]) - - if model_bytes is not None: - sample.addNormalValue("model_file_path", model_path) - sample.addIntValue("model_file_size_bytes", len(model_bytes)) - - frame_list = get_frame_list( - program, execution_plan_idx, d.chain_idx, d.instruction_idx - ) - stacktrace = frame_list_normal_vector(frame_list) - stacktrace_short = frame_list_normal_vector_short(frame_list) - - sample.addNormVectorValue("model_stacktrace", stacktrace) - sample.addNormVectorValue("model_stacktrace_short", stacktrace_short) - sample.addNormalValue("model_frame", frame_list_short_repr(frame_list)) - - samples.append(sample) - - start_time = samples[0].time() - 1 - end_time = samples[-1].time() + 1 - - try: - print(f"Uploading to scuba with run_id: {run_id}") - scuba_data.add_samples(samples) - drillstate = ( - ScubaDrillstate() - .setStartTime(str(start_time)) - .setEndTime(str(end_time)) - .setGroupBy(["profile_event_name"]) - .setAggregationField("sum") - .setMetric("sum") - .setLimit("20") - .setOrderColumn("duration") - .setOrderDesc(True) - .addEqConstraint("run_id", run_id) - .addMatchRegexConstraint("profile_event_name", "^native_call.*") - ) - scuba_url = generate_scuba_fburl(ScubaURL(scuba_table, drillstate, view="Pie")) - print(f"Successfully uploaded to scuba: {scuba_url}") - print("It may take up to 5 minutes to see it in scuba") - except Exception as e: - print(f"Error logging result to Scuba table: {e}") - raise