Skip to content

Commit e90d5a3

Browse files
authored
[PROTON] Unify python frame representation (#8241)
Previous pc sampling and cupti uses different format, now that they use the same format as "file_name:line_number@function_name"
1 parent 15cefc9 commit e90d5a3

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

third_party/proton/csrc/include/Utility/String.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ inline std::vector<std::string> split(const std::string &str,
5959
return result;
6060
}
6161

62+
inline std::string formatFileLineFunction(const std::string &file, int line,
63+
const std::string &function) {
64+
return file + ":" + std::to_string(line) + "@" + function;
65+
}
66+
6267
} // namespace proton
6368

6469
#endif // PROTON_UTILITY_STRING_H_

third_party/proton/csrc/lib/Context/Python.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Context/Python.h"
2+
#include "Utility/String.h"
23
#include "pybind11/pybind11.h"
34
#include <algorithm>
45
#include <string>
@@ -67,7 +68,7 @@ std::vector<Context> PythonContextSource::getContextsImpl() {
6768
size_t firstLineNo = f_code->co_firstlineno;
6869
std::string file = unpackPyobject(f_code->co_filename);
6970
std::string function = unpackPyobject(f_code->co_name);
70-
auto pythonFrame = file + ":" + function + "@" + std::to_string(lineno);
71+
auto pythonFrame = formatFileLineFunction(file, lineno, function);
7172
contexts.push_back(Context(pythonFrame));
7273
auto newFrame = getFrameBack(frame);
7374
Py_DECREF(frame);

third_party/proton/csrc/lib/Profiler/Cupti/CuptiPCSampling.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ void CuptiPCSampling::processPCSamplingData(ConfigureData *configureData,
387387
scopeId = data->addOp(externId, lineInfo.functionName);
388388
if (lineInfo.fileName.size())
389389
scopeId = data->addOp(
390-
scopeId, lineInfo.dirName + "/" + lineInfo.fileName + ":" +
391-
std::to_string(lineInfo.lineNumber) + "@" +
392-
lineInfo.functionName);
390+
scopeId, formatFileLineFunction(
391+
lineInfo.dirName + "/" + lineInfo.fileName,
392+
lineInfo.lineNumber, lineInfo.functionName));
393393
auto metricKind = static_cast<PCSamplingMetric::PCSamplingMetricKind>(
394394
configureData->stallReasonIndexToMetricIndex
395395
[stallReason->pcSamplingStallReasonIndex]);

third_party/proton/test/test_profile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ def test_torch(context, tmp_path: pathlib.Path):
3636
assert len(data[0]["children"]) == 1
3737
# bfs search until find the "elementwise_kernel" and then check its children
3838
queue = [data[0]]
39+
import re
3940
while len(queue) > 0:
4041
parent_frame = queue.pop(0)
4142
for child in parent_frame["children"]:
4243
if "elementwise_kernel" in child["frame"]["name"]:
4344
assert len(child["children"]) == 0
45+
# check the regex of the parent name matches
46+
# file_name:line_number@function_name
47+
regex = r".+:\d+@.+"
48+
assert re.match(regex, parent_frame["frame"]["name"])
4449
return
4550
queue.append(child)
4651

0 commit comments

Comments
 (0)