Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@repository_configuration//:repository_config.bzl", "PROFILER_REQUIREMENTS_FILE")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")

Expand All @@ -15,6 +16,18 @@ exports_files([
"rollup.config.js",
])

bool_flag(
name = "enable_module_summary",
build_setting_default = False,
visibility = ["//visibility:public"],
)

config_setting(
name = "module_summary_enabled",
flag_values = {":enable_module_summary": "True"},
visibility = ["//visibility:public"],
)

py_library(
name = "expect_tensorflow_installed",
# This is a dummy rule used as a tensorflow dependency in open-source.
Expand Down
11 changes: 10 additions & 1 deletion xprof/pywrap/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ cc_binary(
] + select({
":dbg_build": ["-g"],
"//conditions:default": [],
}) + select({
"//xprof:module_summary_enabled": ["-DENABLE_MODULE_SUMMARY"],
"//conditions:default": [],
}),
linkopts = select({
"@xla//xla/tsl:windows": [
Expand All @@ -43,7 +46,13 @@ cc_binary(
"@org_xprof//xprof/convert:tool_options",
"@rules_python//python/cc:current_py_cc_headers",
"@xla//xla/tsl/profiler/rpc/client:capture_profile",
],
] + select({
"//xprof:module_summary_enabled": [
"@org_xprof//internal/module_summary:module_summary_c_api",
"@org_xprof//internal/module_summary:refactored_tool_lib",
],
"//conditions:default": [],
}),
)

py_library(
Expand Down
57 changes: 57 additions & 0 deletions xprof/pywrap/_pywrap_profiler_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,60 @@ def initialize_stubs(worker_service_addresses: str) -> None:
_lib.InitializeStubs(
worker_service_addresses.encode() if worker_service_addresses else None
)


if hasattr(_lib, "CreateModuleSummary"):
_lib.CreateModuleSummary.argtypes = [ctypes.c_char_p]
_lib.CreateModuleSummary.restype = ctypes.c_void_p

_lib.GetTotalCount.argtypes = [ctypes.c_void_p]
_lib.GetTotalCount.restype = ctypes.c_int

_lib.GetUniqueResourceCount.argtypes = [ctypes.c_void_p]
_lib.GetUniqueResourceCount.restype = ctypes.c_int

_lib.GetHistogramBuckets.argtypes = [ctypes.c_void_p]
_lib.GetHistogramBuckets.restype = ctypes.c_int

_lib.GetBucketKey.argtypes = [ctypes.c_void_p, ctypes.c_int]
_lib.GetBucketKey.restype = ctypes.c_int

_lib.GetBucketValue.argtypes = [ctypes.c_void_p, ctypes.c_int]
_lib.GetBucketValue.restype = ctypes.c_int

_lib.FreeModuleSummary.argtypes = [ctypes.c_void_p]
_lib.FreeModuleSummary.restype = None

def get_module_digest(filename: str) -> dict[str, Any]:
"""Gets a module digest."""
_ensure_initialized()
handle = _lib.CreateModuleSummary(filename.encode("utf-8"))
if not handle:
return {"success": False}

try:
result = {
"success": True,
"total_count": _lib.GetTotalCount(handle),
"unique_resource_count": _lib.GetUniqueResourceCount(handle),
}

resources = {}
num_resources = _lib.GetHistogramBuckets(handle)
for i in range(num_resources):
resource = _lib.GetBucketKey(handle, i)
count = _lib.GetBucketValue(handle, i)
resources[resource] = count

result["resource_histogram"] = resources
return result
finally:
_lib.FreeModuleSummary(handle)

else:

def get_module_digest(filename: str) -> dict[str, Any]:
del filename
raise NotImplementedError(
"get_module_digest is not supported in this build"
)
6 changes: 6 additions & 0 deletions xprof/pywrap/profiler_wrapper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def test_check_error_raises_runtime_error(self):
with self.assertRaisesRegex(RuntimeError, r"^INVALID_ARGUMENT"):
profiler_wrapper_plugin._check_error(err)

def test_get_module_digest(self):
if hasattr(profiler_wrapper_plugin, "get_module_digest"):
test_file = self.create_tempfile().full_path
result = profiler_wrapper_plugin.get_module_digest(test_file)
self.assertFalse(result["success"])


if __name__ == "__main__":
absltest.main()
Loading