Skip to content

Commit a66f64b

Browse files
authored
Merge branch 'main' into peter/sc-443/test-sampling-in-simulation
2 parents adbc361 + 6b795cc commit a66f64b

File tree

7 files changed

+322
-166
lines changed

7 files changed

+322
-166
lines changed

tools/src/tensil/tools/Compiler.scala

Lines changed: 114 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,15 @@ object Compiler {
200200
new TfFrontend(
201201
graphDef = util.protoFromStream(GraphDef, modelStream),
202202
arch = options.arch,
203-
inputBatchSize = options.inputBatchSize,
204203
graphStream = graphStream,
205-
printSchedulerSummary = options.printSchedulerSummary,
206-
printLayersSummary = options.printLayersSummary,
207-
printProgress = options.printProgress
204+
options = options
208205
)
209206
} else if (modelSourceType == CompilerSourceType.ONNX) {
210207
new OnnxFrontend(
211208
modelProto = util.protoFromStream(ModelProto, modelStream),
212209
arch = options.arch,
213-
inputBatchSize = options.inputBatchSize,
214210
graphStream = graphStream,
215-
printSchedulerSummary = options.printSchedulerSummary,
216-
printLayersSummary = options.printLayersSummary,
217-
printProgress = options.printProgress
211+
options = options
218212
)
219213
} else
220214
throw new CompilerException(
@@ -257,11 +251,17 @@ object Compiler {
257251

258252
val flowNodeNames = frontend.traverse(outputNames)
259253

260-
if (options.printProgress)
254+
if (options.printProgress) {
255+
println(s"Found ${flowNodeNames.size} node(s)")
261256
println(s"Rewriting emitters ...")
257+
}
262258

263259
val flowEmitters = frontend.rewrite(flowNodeNames)
264260

261+
if (options.printProgress) {
262+
println(s"Rewritten to ${flowEmitters.size} emitter(s)")
263+
}
264+
265265
val context = EmitContext(backend, backendStats, mm, outputNames)
266266

267267
val emitResults = for (emitter <- flowEmitters) yield {
@@ -272,11 +272,11 @@ object Compiler {
272272

273273
layerSchedulerResults = emitResults.filter(_.isDefined).map(_.get).toList
274274
macs = layerSchedulerResults.map(_.macs).sum
275-
macEfficiency = backendStats
276-
.map(stats =>
277-
macs.toFloat / (options.arch.arraySize * options.arch.arraySize).toFloat / stats.totalCycles.toFloat
278-
)
279-
.getOrElse(0f)
275+
macEfficiency =
276+
if (backendStats.isDefined)
277+
BackendStats.macEfficiency(backendStats.get, options.arch, macs)
278+
else
279+
0f
280280

281281
// TODO: fix leaks
282282
// mm.reportObjects()
@@ -354,93 +354,110 @@ object Compiler {
354354
)
355355
tb.addNamedLine("True consts scalar size", mm.constsScalarSize)
356356
tb.addNamedLine("Consts utilization (%)", mm.constsUtilization * 100f)
357-
tb.addNamedLine("True MACs (M)", macs.toFloat / 1e6f)
358-
tb.addNamedLine("MAC efficiency (%)", macEfficiency * 100f)
357+
val (macsLetter, macsDivisor) =
358+
BackendStats.getUnitsLetterAndDivisor(macs)
359+
tb.addNamedLine(
360+
s"True MACs (${macsLetter}MAC)",
361+
macs.toFloat / macsDivisor
362+
)
363+
if (backendStats.isDefined)
364+
tb.addNamedLine("MAC efficiency (%)", macEfficiency * 100f)
359365
print(tb)
366+
}
360367

361-
if (options.printLayersSummary) {
362-
val layerSchedulerResultsWithIndex =
363-
layerSchedulerResults.zipWithIndex
364-
365-
for (
366-
groupResultsWithIndex <- layerSchedulerResultsWithIndex.grouped(32)
367-
) {
368-
val tb = new TablePrinter(Some("LAYERS SUMMARY"), true)
369-
tb.addLine(
370-
new TableLine(
371-
List("Layer:") ++ groupResultsWithIndex.map(_._2)
372-
)
368+
if (options.printLayersSummary) {
369+
val layerSchedulerResultsWithIndex =
370+
layerSchedulerResults.zipWithIndex
371+
372+
for (
373+
groupResultsWithIndex <- layerSchedulerResultsWithIndex.grouped(32)
374+
) {
375+
val tb = new TablePrinter(Some("LAYERS SUMMARY"), true)
376+
tb.addLine(
377+
new TableLine(
378+
List("Layer:") ++ groupResultsWithIndex.map(_._2)
373379
)
374-
tb.addLine(
375-
new TableLine(
376-
List(
377-
"Number of stages:"
378-
) ++ groupResultsWithIndex
379-
.map(_._1.numberOfStages)
380-
)
380+
)
381+
tb.addLine(
382+
new TableLine(
383+
List(
384+
"Number of stages:"
385+
) ++ groupResultsWithIndex
386+
.map(_._1.numberOfStages)
381387
)
382-
tb.addLine(
383-
new TableLine(
384-
List(
385-
"Number of combined stages:"
386-
) ++ groupResultsWithIndex
387-
.map(_._1.numberOfCombinedStages)
388-
)
388+
)
389+
tb.addLine(
390+
new TableLine(
391+
List(
392+
"Number of combined stages:"
393+
) ++ groupResultsWithIndex
394+
.map(_._1.numberOfCombinedStages)
389395
)
390-
tb.addLine(
391-
new TableLine(
392-
List(
393-
"Number of partitions:"
394-
) ++ groupResultsWithIndex.map(_._1.numberOfPartitions)
395-
)
396+
)
397+
tb.addLine(
398+
new TableLine(
399+
List(
400+
"Number of partitions:"
401+
) ++ groupResultsWithIndex.map(_._1.numberOfPartitions)
396402
)
397-
tb.addLine(
398-
new TableLine(
399-
List(
400-
"True MACs (M):"
401-
) ++ groupResultsWithIndex
402-
.map(_._1.macs.toFloat)
403-
.map(_ / 1e6f)
404-
.map(f => f"$f%.3f")
405-
)
403+
)
404+
val (macsLetter, macsDivisor) =
405+
BackendStats.getUnitsLetterAndDivisor(
406+
groupResultsWithIndex
407+
.map(_._1.macs)
408+
.filter(v => v > 0)
409+
.min
406410
)
407-
tb.addLine(
408-
new TableLine(
409-
List(
410-
"MAC efficiency (%):"
411-
) ++ groupResultsWithIndex
412-
.map(_._1.macEfficiency)
413-
.map(_ * 100f)
414-
.map(f => f"$f%.1f")
415-
)
411+
tb.addLine(
412+
new TableLine(
413+
List(
414+
s"True MACs (${macsLetter}MAC):"
415+
) ++ groupResultsWithIndex
416+
.map(_._1.macs.toFloat)
417+
.map(_ / macsDivisor)
418+
.map(f => f"$f%.3f")
416419
)
417-
tb.addLine(
418-
new TableLine(
419-
List(
420-
"Accumulator utilization (%):"
421-
) ++ groupResultsWithIndex
422-
.map(_._1.accumulatorUtilization)
423-
.map(_ * 100f)
424-
.map(f => f"$f%.1f")
425-
)
420+
)
421+
tb.addLine(
422+
new TableLine(
423+
List(
424+
"MAC efficiency (%):"
425+
) ++ groupResultsWithIndex
426+
.map(_._1.macEfficiency)
427+
.map(_ * 100f)
428+
.map(f => f"$f%.1f")
426429
)
427-
tb.addLine(
428-
new TableLine(
429-
List("Local utilization (%):") ++ groupResultsWithIndex
430-
.map(_._1.localUtilization)
431-
.map(_ * 100f)
432-
.map(f => f"$f%.1f")
433-
)
430+
)
431+
tb.addLine(
432+
new TableLine(
433+
List(
434+
"Accumulator utilization (%):"
435+
) ++ groupResultsWithIndex
436+
.map(_._1.accumulatorUtilization)
437+
.map(_ * 100f)
438+
.map(f => f"$f%.1f")
434439
)
435-
print(tb)
436-
}
440+
)
441+
tb.addLine(
442+
new TableLine(
443+
List("Local utilization (%):") ++ groupResultsWithIndex
444+
.map(_._1.localUtilization)
445+
.map(_ * 100f)
446+
.map(f => f"$f%.1f")
447+
)
448+
)
449+
print(tb)
437450
}
451+
}
438452

439-
if (backendStats.isDefined) {
440-
BackendStats.printCompositionSummary(backendStats.get)
441-
BackendStats.printCyclesSummary(backendStats.get)
442-
BackendStats.printEnergySummary(backendStats.get)
453+
if (backendStats.isDefined) {
454+
if (options.printInstructionsSummary) {
455+
BackendStats.printCompositionSummary("TOTAL", backendStats.get)
456+
BackendStats.printCyclesSummary("TOTAL", backendStats.get)
457+
BackendStats.printEnergySummary("TOTAL", backendStats.get)
458+
}
443459

460+
if (options.printStridesSummary) {
444461
def printStrideStats(
445462
title: String,
446463
select: StrideStats => Any
@@ -456,10 +473,16 @@ object Compiler {
456473
print(tb)
457474
}
458475

459-
printStrideStats("STRIDES COUNT SUMMARY", stats => stats.count)
460-
printStrideStats("STRIDES MAX SIZE SUMMARY", stats => stats.maxSize)
461476
printStrideStats(
462-
"STRIDES AVERAGE SIZE SUMMARY",
477+
"TOTAL STRIDES COUNT SUMMARY",
478+
stats => stats.count
479+
)
480+
printStrideStats(
481+
"TOTAL STRIDES MAX SIZE SUMMARY",
482+
stats => stats.maxSize
483+
)
484+
printStrideStats(
485+
"TOTAL STRIDES AVERAGE SIZE SUMMARY",
463486
stats => Math.round(stats.totalSize.toFloat / stats.count.toFloat)
464487
)
465488
}

tools/src/tensil/tools/CompilerOptions.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ case class CompilerOptions(
1616
printSummary: Boolean = false,
1717
printLayersSummary: Boolean = false,
1818
printSchedulerSummary: Boolean = false,
19+
printPartitionsSummary: Boolean = false,
20+
printStridesSummary: Boolean = false,
21+
printInstructionsSummary: Boolean = false,
1922
printProgress: Boolean = true,
2023
printProgramWithComments: Boolean = false,
2124
printProgramFileName: Option[String] = None,

tools/src/tensil/tools/Main.scala

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ case class Args(
1616
inputBatchSize: Int = 1,
1717
verbose: Boolean = false,
1818
summary: Boolean = false,
19+
layersSummary: Boolean = false,
20+
schedulerSummary: Boolean = false,
21+
partitionsSummary: Boolean = false,
22+
stridesSummary: Boolean = false,
23+
instructionsSummary: Boolean = false,
24+
backendStats: Boolean = true,
1925
)
2026

2127
object Main extends App {
@@ -50,7 +56,38 @@ object Main extends App {
5056

5157
opt[Boolean]('s', "summary")
5258
.valueName("true|false")
53-
.action((x, c) => c.copy(summary = x)),
59+
.action((x, c) => c.copy(summary = x))
60+
.text("Print summary, defaults to false")
61+
62+
opt[Boolean]("layers-summary")
63+
.valueName("true|false")
64+
.action((x, c) => c.copy(layersSummary = x))
65+
.text("Print layer summary, defaults to false")
66+
67+
opt[Boolean]("scheduler-summary")
68+
.valueName("true|false")
69+
.action((x, c) => c.copy(schedulerSummary = x))
70+
.text("Print scheduler summary, defaults to false")
71+
72+
opt[Boolean]("partitions-summary")
73+
.valueName("true|false")
74+
.action((x, c) => c.copy(partitionsSummary = x))
75+
.text("Print partitions summary, defaults to false")
76+
77+
opt[Boolean]("strides-summary")
78+
.valueName("true|false")
79+
.action((x, c) => c.copy(stridesSummary = x))
80+
.text("Print strides summary, defaults to false")
81+
82+
opt[Boolean]("instructions-summary")
83+
.valueName("true|false")
84+
.action((x, c) => c.copy(instructionsSummary = x))
85+
.text("Print instructions summary, defaults to false")
86+
87+
opt[Boolean]("backend-stats")
88+
.valueName("true|false")
89+
.action((x, c) => c.copy(backendStats = x))
90+
.text("Collect backend stats, defaults to true")
5491
}
5592

5693
argParser.parse(args, Args()) match {
@@ -62,6 +99,12 @@ object Main extends App {
6299
inputBatchSize = args.inputBatchSize,
63100
printProgress = args.verbose,
64101
printSummary = args.summary,
102+
printLayersSummary = args.layersSummary,
103+
printSchedulerSummary = args.schedulerSummary,
104+
printPartitionsSummary = args.partitionsSummary,
105+
printStridesSummary = args.stridesSummary,
106+
printInstructionsSummary = args.instructionsSummary,
107+
collectBackendStats = args.backendStats
65108
)
66109

67110
val archName = args.archFile.getName().split("\\.")(0)

0 commit comments

Comments
 (0)