-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perf improvement for Intel MTL CPUs (#19524)
### Description See the comments inside of the changed files for more detailed information. The file onnxruntime/core/platform/windows/hardware_core_enumerator.cc and onnxruntime/core/platform/windows/hardware_core_enumerator.h were copied from WinML source folder in this repo, with minor coding style changes. I had an offline discussion with Sheil. We agree that given the lack of a future proof solution, we may check-in this temp fix first, and rework it later. I will have a meeting with @ivberg for discussing the issue deeply, and seeking for a long term solution. Thanks for offering help, @ivberg ! ### Motivation and Context With this change, we will see about 2x perf improvement on some Intel CPUs.
- Loading branch information
Showing
5 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
onnxruntime/core/platform/windows/hardware_core_enumerator.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "hardware_core_enumerator.h" | ||
#include <memory> | ||
#include <Windows.h> | ||
#include <assert.h> | ||
|
||
namespace onnxruntime { | ||
|
||
struct LogicalProcessorInformation { | ||
std::unique_ptr<char[]> Buffer; | ||
size_t Length; | ||
}; | ||
|
||
struct CoreCounter { | ||
uint32_t PhysicalCores = 0; | ||
uint32_t SocDieCores = 0; | ||
}; | ||
|
||
static LogicalProcessorInformation GetLogicalProcessorInfos(LOGICAL_PROCESSOR_RELATIONSHIP relationship) { | ||
DWORD length = 0; | ||
DWORD rc = GetLogicalProcessorInformationEx(relationship, nullptr, &length); | ||
|
||
assert(rc == FALSE); | ||
|
||
auto processorInformationBytes = std::make_unique<char[]>(length); | ||
|
||
rc = GetLogicalProcessorInformationEx( | ||
relationship, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(processorInformationBytes.get()), &length); | ||
|
||
assert(rc == TRUE); | ||
|
||
return {std::move(processorInformationBytes), length}; | ||
} | ||
|
||
uint32_t CountSetBits(DWORD input) { | ||
uint32_t c; | ||
for (c = 0; input; c++) { | ||
input &= input - 1; | ||
} | ||
return c; | ||
} | ||
|
||
static CoreCounter GetNumberOPhysicalAndEngineeringCores() { | ||
auto logicalProcessorInformation = GetLogicalProcessorInfos(RelationAll); | ||
|
||
CoreCounter cores; | ||
DWORD dwLevel2GroupMask = 0; | ||
DWORD dwLevel3GroupMask = 0; | ||
size_t read = 0; | ||
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX currentProcessorInfo = NULL; | ||
|
||
while ((read + FIELD_OFFSET(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, Processor)) < logicalProcessorInformation.Length) { | ||
currentProcessorInfo = | ||
reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(logicalProcessorInformation.Buffer.get() + read); | ||
if ((read + currentProcessorInfo->Size) > logicalProcessorInformation.Length) { | ||
break; | ||
} | ||
|
||
switch (currentProcessorInfo->Relationship) { | ||
case RelationProcessorCore: | ||
cores.PhysicalCores++; | ||
break; | ||
case RelationCache: | ||
if (currentProcessorInfo->Cache.Level == 2) { | ||
dwLevel2GroupMask |= currentProcessorInfo->Cache.GroupMask.Mask; | ||
} else if (currentProcessorInfo->Cache.Level == 3) { | ||
dwLevel3GroupMask |= currentProcessorInfo->Cache.GroupMask.Mask; | ||
} | ||
break; | ||
} | ||
|
||
read += currentProcessorInfo->Size; | ||
} | ||
|
||
cores.SocDieCores = CountSetBits(dwLevel2GroupMask & ~dwLevel3GroupMask); | ||
return cores; | ||
} | ||
|
||
uint32_t HardwareCoreEnumerator::DefaultIntraOpNumThreads() { | ||
// # of physical cores = # of P cores + # of E Cores + # of Soc Cores. | ||
// # of logical cores = # of P cores x 2 (if hyper threading is enabled) + # of E cores + # of Soc Cores. | ||
auto cores = GetNumberOPhysicalAndEngineeringCores(); | ||
// We want to use the number of physical cores, but exclude soc cores | ||
return cores.PhysicalCores - cores.SocDieCores; | ||
} | ||
|
||
} // namespace onnxruntime |
12 changes: 12 additions & 0 deletions
12
onnxruntime/core/platform/windows/hardware_core_enumerator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
#include <stdint.h> | ||
|
||
namespace onnxruntime { | ||
struct HardwareCoreEnumerator { | ||
HardwareCoreEnumerator() = delete; | ||
static uint32_t DefaultIntraOpNumThreads(); | ||
}; | ||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters