Skip to content

version for profiling function #1280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 51 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
db0fab3
init some class definition
Dec 11, 2024
0e8a997
change to snake naming
Dec 12, 2024
51f8a35
add starttime & endtime
Dec 12, 2024
cb2b519
add topology for profiler
Dec 12, 2024
669b12e
init opt to json
Dec 13, 2024
2c4770b
change for gramma of trace json
Dec 13, 2024
4410f08
fix typo
Dec 13, 2024
339ab6e
change interface for profiling
Dec 13, 2024
c3a7428
add some code for profiling
Dec 17, 2024
29ffc71
simplify some implementation
Dec 17, 2024
dc8075b
remove build error mac
Dec 17, 2024
886452f
change string to string_view
Dec 17, 2024
1f67622
seal the interface
Dec 17, 2024
c7c0674
Merge branch 'dev/3.0' into feature/profiling
Dec 18, 2024
e055c29
seal api for invoke form python
Dec 18, 2024
6051d9f
change for interface
Dec 19, 2024
7af7b9a
change option from string to string_view
Dec 19, 2024
3418eaa
remove const for string_view
Dec 19, 2024
a939439
change inner class name
Dec 19, 2024
74ecfee
revise for cpu
Dec 19, 2024
678ba58
transfer the buffer to runtime
Dec 20, 2024
8994f22
revise bug for macos
Dec 20, 2024
1c2499e
change order for defination
Dec 21, 2024
4d88b27
change pid tid tag from num to str
Dec 23, 2024
9ab0292
add abstract for profiling
Dec 24, 2024
fd71b41
add head file for x86 build error
Dec 24, 2024
169cddf
extend profiling to other func
Dec 24, 2024
7d3da9b
trim end of string for build error
Dec 24, 2024
ae0b58f
add some function for Ops
Dec 25, 2024
5e58f89
fallback for build error
Dec 25, 2024
a54a10f
Revert "add abstract for profiling"
Dec 25, 2024
1299e20
add abstract for profiling
Dec 25, 2024
51fc137
Merge branch 'feature/profiling' of https://github.com/kendryte/nncas…
Dec 25, 2024
8a551d1
restore some changes
Dec 25, 2024
dac8cc5
add profiling to matmul cshtml
Dec 25, 2024
e9d6196
expand profiling to more function
Dec 26, 2024
8775e20
add Ops level profiling function
Dec 26, 2024
caf7bf6
change for chrome json
Dec 26, 2024
e19d535
fix bug
Dec 26, 2024
a17ce06
expand Kernel test to other Kernel
Dec 26, 2024
3dec40c
active ci test
Dec 27, 2024
6760e68
Merge branch 'dev/3.0' into feature/profiling
Dec 30, 2024
7f3e45a
remove chinese comments
Mar 10, 2025
b1ecfae
Merge branch 'dev/3.0' into feature/profiling
Mar 10, 2025
fe82603
revise some conflict
Mar 10, 2025
35d41ba
Apply code-format changes
uranus0515 Mar 10, 2025
3ef63af
remove warnings
Mar 10, 2025
f0522bb
Merge branch 'feature/profiling' of https://github.com/kendryte/nncas…
Mar 10, 2025
b0376c7
change default profiling option
Mar 10, 2025
f01a1cf
revise bug for perm of transpose
Mar 10, 2025
45e8436
revert transpose changes
Mar 11, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,46 @@ public DeviceCSourceConvertVisitor()

public PrimFunction VisitEntry => (TIR.PrimFunction)VisitRoot!;

public static void WriteWithProfiler(string functionName, string tagName = "")
{
functionName = functionName.TrimEnd(new char[] { ';', '\n' });
if (tagName == string.Empty)
{
int index = functionName.IndexOf('(', StringComparison.Ordinal);
if (index != -1)
{
tagName = functionName.Substring(0, index);
}
}

tagName = tagName == string.Empty ? functionName : tagName;
IndentScope.Writer.IndWrite("{\n");
IndentScope.Writer.Write($"constexpr std::string_view function_name = \"{tagName}\";\n");
IndentScope.Writer.Write($"auto_profiler profiler(function_name, runtime::profiling_level::device);\n");
IndentScope.Writer.Write($"{functionName};\n");
IndentScope.Writer.IndWrite("}\n");
}

public static void WriteIndWithProfiler(string functionName, string tagName = "")
{
functionName = functionName.TrimEnd(new char[] { ';', '\n' });
if (tagName == string.Empty)
{
int index = functionName.IndexOf('(', StringComparison.Ordinal);
if (index != -1)
{
tagName = functionName.Substring(0, index);
}
}

tagName = tagName == string.Empty ? functionName : tagName;
IndentScope.Writer.IndWrite("{\n");
IndentScope.Writer.IndWrite($"constexpr std::string_view function_name = \"{tagName}\";\n");
IndentScope.Writer.IndWrite($"auto_profiler profiler(function_name, runtime::profiling_level::device);\n");
IndentScope.Writer.IndWrite($"{functionName};\n");
IndentScope.Writer.IndWrite("}\n");
}

public string GetHeader()
{
return _deviceBuilder.ToString();
Expand Down Expand Up @@ -210,7 +250,7 @@ protected override CSymbol VisitCall(Call expr)
switch (expr.Target)
{
case PrimFunction deviceFunc:
IndentScope.Writer.IndWrite($"{deviceFunc.Name}({string.Join(",", arguments.Select(arg => arg.Name))});\n");
WriteIndWithProfiler($"{deviceFunc.Name}({string.Join(",", arguments.Select(arg => arg.Name))});\n");
break;
case IR.Math.Binary op:
str = CSourceUtilities.ContertBinary(op, arguments);
Expand Down Expand Up @@ -291,24 +331,24 @@ protected override CSymbol VisitCall(Call expr)
str = $"(({op.NewType.ToC()}){arguments[0].Name})";
break;
case TIR.Memcopy op:
IndentScope.Writer.IndWrite($"tensor_copy({arguments[1].Name}, {arguments[0].Name});\n");
WriteIndWithProfiler($"tensor_copy({arguments[1].Name}, {arguments[0].Name});\n");
break;
case TIR.CPU.Unary op:
IndentScope.Writer.IndWrite(RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/Kernels/Unary.cshtml", new UnaryKernelTemplateModel
WriteIndWithProfiler(RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/Kernels/Unary.cshtml", new UnaryKernelTemplateModel
{
Arguments = arguments.Select(x => new KernelArgument { Symbol = x }).ToArray(),
UnaryOp = op.UnaryOp,
}).Result);
break;
case TIR.CPU.Binary op:
IndentScope.Writer.IndWrite(RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/Kernels/Binary.cshtml", new BinaryKernelTemplateModel
WriteIndWithProfiler(RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/Kernels/Binary.cshtml", new BinaryKernelTemplateModel
{
Arguments = arguments.Select(x => new KernelArgument { Symbol = x }).ToArray(),
BinaryOp = op.BinaryOp,
}).Result);
break;
case TIR.CPU.PackedBinary op:
IndentScope.Writer.IndWrite(RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/Kernels/Binary.cshtml", new BinaryKernelTemplateModel
WriteIndWithProfiler(RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/Kernels/Binary.cshtml", new BinaryKernelTemplateModel
{
Arguments = arguments.Select(x => new KernelArgument { Symbol = x }).ToArray(),
BinaryOp = op.BinaryOp,
Expand All @@ -317,13 +357,13 @@ protected override CSymbol VisitCall(Call expr)
case TIR.CPU.Swish swish:
if (swish.Beta == 1.0f)
{
IndentScope.Writer.IndWrite($"unary<ops::swish>({arguments[0].Name}, {arguments[1].Name});\n");
WriteIndWithProfiler($"unary<ops::swish>({arguments[0].Name}, {arguments[1].Name});\n");
}
else
{
IndentScope.Writer.IndWrite($"float beta[1] = {{{swish.Beta}}};\n");
IndentScope.Writer.IndWrite($"tensor_view<float, fixed_shape<1>> tb(std::span<float, 1>(beta, beta + 1));\n");
IndentScope.Writer.IndWrite($"binary<ops::swishb>({arguments[0].Name}, tb, {arguments[1].Name});\n");
WriteIndWithProfiler($"binary<ops::swishb>({arguments[0].Name}, tb, {arguments[1].Name});\n");
}

break;
Expand All @@ -336,7 +376,7 @@ protected override CSymbol VisitCall(Call expr)

break;
case TIR.CPU.Pack pack:
IndentScope.Writer.Write(RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/Kernels/Pack.cshtml", new TypedKernelTemplateModel<TIR.CPU.Pack>(pack)
WriteWithProfiler(RazorTemplateEngine.RenderAsync("~/CodeGen/CPU/Templates/Kernels/Pack.cshtml", new TypedKernelTemplateModel<TIR.CPU.Pack>(pack)
{
Arguments = arguments.Select(x => new KernelArgument { Symbol = x }).ToArray(),
Indent = new string(' ', IndentScope.Writer.Indent),
Expand Down
Loading
Loading