Gna plugin more meaningful memory dump (#18119)

* added display of sum of memory usage per operation block and per memory type in GNA dump file

* fixed some clang format issues

* forgot to save file - missing clang fix

* changes according to review - added counting memory usage per layer and names changes

* clang changes

* changes according to review

* clang fixes
This commit is contained in:
Jakub Nowicki 2023-07-26 09:50:11 +02:00 committed by GitHub
parent 1c0c929231
commit 53efed1571
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -440,11 +440,58 @@ inline void DumpCharArray(std::ostream& dumpFile, const char* carray, size_t cou
dumpFile << "\n"; dumpFile << "\n";
} }
class GnaMemStats {
public:
void Add(const std::string memory_tag,
const std::string operand_name,
const std::string operation_name,
const uint32_t size) {
if (size == 0)
return;
const std::pair<std::string, std::string> key = std::make_pair(operation_name, memory_tag + " " + operand_name);
m_stats[key] = size;
}
std::string GetFormattedMemStats() {
std::stringstream output;
output << "Per Layer memory statistics: " << std::endl;
// Assumes that std::map with pair keys is lexicographically ordered which is true in this and previous versions
// of C++.
std::string previous_layer = "";
for (auto pair : m_stats) {
std::string current_layer = pair.first.first;
if (current_layer != previous_layer) {
output << "\t" << current_layer << std::endl;
}
output << "\t\t" << pair.first.second << ": " << pair.second << " bytes." << std::endl;
previous_layer = current_layer;
}
output << "Aggregate memory statistics: " << std::endl;
std::map<std::string, uint32_t> aggr_stats;
for (auto pair : m_stats) {
const std::string key = pair.first.second;
if (aggr_stats.count(key) == 0) {
aggr_stats[key] = pair.second;
} else {
aggr_stats[key] += pair.second;
}
}
for (auto stat : aggr_stats) {
output << "\t" << stat.first << ": " << stat.second << " bytes." << std::endl;
}
return output.str();
}
private:
// uint32_t allows for a little above 4 Gb of size without overflow so it's more than sufficient.
std::map<std::pair<std::string, std::string>, uint32_t> m_stats;
};
void DumpGna2Model(const Gna2Model& gnaModel, void DumpGna2Model(const Gna2Model& gnaModel,
const std::string& dumpFolderNameGNA, const std::string& dumpFolderNameGNA,
bool dumpData, bool dumpData,
const GnaAllocations& allAllocations, const GnaAllocations& allAllocations,
const std::string& modeOfOperation) { const std::string& modeOfOperation) {
GnaMemStats gna_stats;
std::stringstream dumpFileName; std::stringstream dumpFileName;
uint32_t opsNo = gnaModel.NumberOfOperations; uint32_t opsNo = gnaModel.NumberOfOperations;
std::time_t currTime = std::time(nullptr); std::time_t currTime = std::time(nullptr);
@ -490,13 +537,17 @@ void DumpGna2Model(const Gna2Model& gnaModel,
foundName = found->GetTagName(); foundName = found->GetTagName();
offset = found->getOffset(operand.Data).second; offset = found->getOffset(operand.Data).second;
} }
const uint32_t size =
Gna2RoundUp(GetGnaShapeSize(operand.Shape, GetTypeByteSize(operand.Type)),
static_cast<uint32_t>(Limitations::get_instance()->get_memory_alignment()));
dumpFile << "\tOperand " << j << " (" << GetOperandName(operation.Type, j) << ")" dumpFile << "\tOperand " << j << " (" << GetOperandName(operation.Type, j) << ")"
<< " type: " << GetOperandType(operand.Type) << " shape: " << GetSimpleString(operand.Shape) << " type: " << GetOperandType(operand.Type) << " shape: " << GetSimpleString(operand.Shape)
<< " tag: " << foundName << " offset: " << offset << " size: " << " tag: " << foundName << " offset: " << offset << " size: " << size << " data: " << operand.Data
<< Gna2RoundUp(GetGnaShapeSize(operand.Shape, GetTypeByteSize(operand.Type)), << " baseAlloc: " << foundPtr << " layout: ";
static_cast<uint32_t>(Limitations::get_instance()->get_memory_alignment())) gna_stats.Add(foundName,
<< " data: " << operand.Data << " baseAlloc: " << foundPtr << " layout: "; GetOperandName(operation.Type, j),
std::to_string(i) + " " + GetLayerType(operation.Type),
size);
DumpCharArray(dumpFile, operand.Layout, GNA2_SHAPE_MAXIMUM_NUMBER_OF_DIMENSIONS); DumpCharArray(dumpFile, operand.Layout, GNA2_SHAPE_MAXIMUM_NUMBER_OF_DIMENSIONS);
if (operand.Type == Gna2DataTypePwlSegment) { if (operand.Type == Gna2DataTypePwlSegment) {
@ -537,6 +588,8 @@ void DumpGna2Model(const Gna2Model& gnaModel,
} }
} }
} }
dumpFile << "------------------------------------------------------------------------\n\n";
dumpFile << gna_stats.GetFormattedMemStats() << std::endl;
} }
} // namespace dump } // namespace dump