-
Notifications
You must be signed in to change notification settings - Fork 26
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
AMX Extension #93
base: master
Are you sure you want to change the base?
AMX Extension #93
Changes from 40 commits
95f1c83
1fdf36f
2b9adf9
a91ea44
e2a8731
529bb7f
b3b94f8
8e8e82d
892f234
5c5f935
edf806c
af302d6
7caf6cb
38fe61d
0427834
9fc69b6
b5fa958
fb60acd
5576797
ee707cf
6e66d26
824520c
87d6af9
b775b8c
ce86848
3a789bf
83e91eb
fdf01db
ce35b28
b1577ce
15822f2
31226ad
e73d1ef
27ce53e
98efa54
345ed8e
0396c64
c4e8ca6
287d87a
997565d
872ffa1
a30f850
b4618e5
13202bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/****************************************************************************** | ||
* FIRESTARTER - A Processor Stress Test Utility | ||
* Copyright (C) 2024 TU Dresden, Center for Information Services and High | ||
* Performance Computing | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/\>. | ||
* | ||
* Contact: [email protected] | ||
*****************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include "firestarter/Environment/X86/Payload/AVX512Payload.hpp" | ||
|
||
#include <asmjit/asmjit.h> | ||
|
||
namespace firestarter::environment::x86::payload { | ||
|
||
/// This payload is designed for the AVX512 foundation CPU extension specialized for AMX. | ||
class AVX512WithAMXPayload : public AVX512Payload { | ||
public: | ||
AVX512WithAMXPayload() noexcept { | ||
// Enable the AMX instruction in the AVX512 Payload and request AMX_TILE and AMX_BF16 feature. | ||
addInstructionFlops("AMX", 512); | ||
addFeatureRequest(asmjit::CpuFeatures::X86::kAMX_TILE); | ||
addFeatureRequest(asmjit::CpuFeatures::X86::kAMX_BF16); | ||
} | ||
Comment on lines
+33
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this wrapper to the |
||
}; | ||
} // namespace firestarter::environment::x86::payload |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/****************************************************************************** | ||
* FIRESTARTER - A Processor Stress Test Utility | ||
* Copyright (C) 2024 TU Dresden, Center for Information Services and High | ||
* Performance Computing | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/\>. | ||
* | ||
* Contact: [email protected] | ||
*****************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include "firestarter/Environment/X86/Payload/AVX512WithAMXPayload.hpp" | ||
#include "firestarter/Environment/X86/Platform/X86PlatformConfig.hpp" | ||
|
||
namespace firestarter::environment::x86::platform { | ||
class SapphireRapidsConfig final : public X86PlatformConfig { | ||
public: | ||
SapphireRapidsConfig() noexcept | ||
: X86PlatformConfig(/*Name=*/"SPR_XEONEP", /*Family=*/6, /*Models=*/{143}, | ||
/*Settings=*/ | ||
environment::payload::PayloadSettings(/*Threads=*/{1, 2}, | ||
/*DataCacheBufferSize=*/{32768, 1048576, 1441792}, | ||
/*RamBufferSize=*/1048576000, /*Lines=*/1536, | ||
/*InstructionGroups=*/ | ||
{{"RAM_S", 3}, | ||
{"RAM_P", 1}, | ||
{"L3_S", 1}, | ||
{"L3_P", 1}, | ||
{"L2_S", 4}, | ||
{"L2_L", 70}, | ||
{"L1_S", 0}, | ||
{"L1_L", 40}, | ||
{"REG", 140}, | ||
{"AMX", 1}}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These values should be updated. E.g. the L1 data cache size changed from SkylakeSP to Sapphire Rapids |
||
/*Payload=*/std::make_shared<const payload::AVX512WithAMXPayload>()) {} | ||
}; | ||
} // namespace firestarter::environment::x86::platform |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,34 @@ | |
#include "firestarter/Environment/X86/Payload/AVX512Payload.hpp" | ||
#include "firestarter/Environment/X86/Payload/CompiledX86Payload.hpp" | ||
|
||
#include <asm/prctl.h> /* Definition of ARCH_* constants */ | ||
#include <immintrin.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be able to remove these includes |
||
#include <sys/syscall.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also include guard for this header. See the comment in the |
||
|
||
#define XFEATURE_XTILECFG 17 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use a |
||
#define XFEATURE_XTILEDATA 18 | ||
#define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG) | ||
#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA) | ||
#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA) | ||
|
||
#define ARCH_GET_XCOMP_PERM 0x1022 | ||
#define ARCH_REQ_XCOMP_PERM 0x1023 | ||
|
||
#define MAX 1024 | ||
#define MAX_ROWS 16 | ||
#define MAX_COLS 64 | ||
|
||
namespace firestarter::environment::x86::payload { | ||
|
||
auto AVX512Payload::compilePayload(const environment::payload::PayloadSettings& Settings, bool DumpRegisters, | ||
bool ErrorDetection) const -> environment::payload::CompiledPayload::UniquePtr { | ||
using Imm = asmjit::Imm; | ||
using Tmm = asmjit::x86::Tmm; | ||
using Zmm = asmjit::x86::Zmm; | ||
// NOLINTBEGIN(readability-identifier-naming) | ||
constexpr auto tmm6 = asmjit::x86::tmm6; | ||
constexpr auto tmm7 = asmjit::x86::tmm7; | ||
|
||
constexpr asmjit::x86::Mem (*zmmword_ptr)(const asmjit::x86::Gp&, int32_t) = asmjit::x86::zmmword_ptr; | ||
constexpr auto zmm0 = asmjit::x86::zmm0; | ||
constexpr auto zmm1 = asmjit::x86::zmm1; | ||
|
@@ -158,6 +179,42 @@ auto AVX512Payload::compilePayload(const environment::payload::PayloadSettings& | |
for (auto const& Reg : ShiftReg32) { | ||
Cb.mov(Reg, Imm(0xAAAAAAAA)); | ||
} | ||
|
||
// Init AMX registers and config | ||
TileConfig tile_data = {0}; | ||
request_permission(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should be called on the start of the assembler kernel. You should be able to provide the pointer to a static function in asmjit and call it |
||
create_AMX_config(&tile_data); // Create tilecfg and fill it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for this one |
||
|
||
static bool init = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused variable |
||
uintptr_t src1, src2; | ||
uint64_t src3; | ||
unsigned int aligned_alloc_size = static_cast<unsigned int>(MAX * sizeof(__bfloat16)); | ||
if (aligned_alloc_size % 1024) { // aligned_alloc expects size to be multiple of alignment (aka 1024) | ||
aligned_alloc_size = aligned_alloc_size + (1024 - (aligned_alloc_size % 1024)); | ||
} | ||
src1 = (uintptr_t)aligned_alloc(1024, aligned_alloc_size); | ||
src2 = (uintptr_t)aligned_alloc(1024, aligned_alloc_size); | ||
src3 = (uint64_t)aligned_alloc(1024, aligned_alloc_size); | ||
if (((void*)src1 == nullptr) || (void*)src2 == nullptr || | ||
(void*)src3 == nullptr) { // uintptr_t garantuees we can cast it to void* and back | ||
std::cout << "[ERROR]: Allocation of source and target buffer for AMX failed. Aborting...\n"; | ||
exit(1); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This memory should be allocated in the |
||
|
||
// Init buffers | ||
init_buffer_rand(src1, src2); | ||
memset((void*)src3, 0, aligned_alloc_size); | ||
|
||
Cb.tileloaddt1(tmm6, asmjit::x86::ptr(src1)); | ||
Cb.tileloaddt1(tmm7, asmjit::x86::ptr(src2)); // Ensure no overflows through loading x and -x in src2 | ||
|
||
Cb.tileloaddt1(asmjit::x86::tmm0, asmjit::x86::ptr(src3)); // Preload with 0 | ||
Cb.tileloaddt1(asmjit::x86::tmm1, asmjit::x86::ptr(src3)); | ||
Cb.tileloaddt1(asmjit::x86::tmm2, asmjit::x86::ptr(src3)); | ||
Cb.tileloaddt1(asmjit::x86::tmm3, asmjit::x86::ptr(src3)); | ||
Cb.tileloaddt1(asmjit::x86::tmm4, asmjit::x86::ptr(src3)); | ||
Cb.tileloaddt1(asmjit::x86::tmm5, asmjit::x86::ptr(src3)); | ||
|
||
// Initialize AVX512-Registers for FMA Operations | ||
Cb.vmovapd(zmm0, zmmword_ptr(PointerReg, 0)); | ||
Cb.vmovapd(zmm1, zmmword_ptr(PointerReg, 64)); | ||
|
@@ -196,6 +253,7 @@ auto AVX512Payload::compilePayload(const environment::payload::PayloadSettings& | |
auto AddDest = AddStart + 1; | ||
auto MovDst = TransStart; | ||
unsigned L1Offset = 0; | ||
unsigned AmxRegisterCounter = 0; | ||
|
||
const auto L1Increment = [&Cb, &L1Offset, &L1Size, &L1Addr, &OffsetReg, &PointerReg]() { | ||
L1Offset += 64; | ||
|
@@ -212,7 +270,9 @@ auto AVX512Payload::compilePayload(const environment::payload::PayloadSettings& | |
|
||
for (auto Count = 0U; Count < Repetitions; Count++) { | ||
for (const auto& Item : Sequence) { | ||
if (Item == "REG") { | ||
if (Item == "AMX") { | ||
Cb.tdpbf16ps(Tmm(AmxRegisterCounter++ % 6), tmm6, tmm7); | ||
} else if (Item == "REG") { | ||
Cb.vfmadd231pd(Zmm(AddDest), zmm0, zmm2); | ||
Cb.vfmadd231pd(Zmm(MovDst), zmm2, zmm1); | ||
Cb.xor_(ShiftReg[(ShiftPos + NrShiftRegs - 1) % NrShiftRegs], TempReg); | ||
|
@@ -388,4 +448,65 @@ void AVX512Payload::init(double* MemoryAddr, uint64_t BufferSize) const { | |
X86Payload::initMemory(MemoryAddr, BufferSize, 0.27948995982e-4, 0.27948995982e-4); | ||
} | ||
|
||
} // namespace firestarter::environment::x86::payload | ||
void AVX512Payload::create_AMX_config(TileConfig* tileinfo) { | ||
// Create tile_cfg, fill it and return | ||
int i; | ||
tileinfo->palette_id = 1; | ||
tileinfo->start_row = 0; | ||
|
||
for (i = 0; i < 8; ++i) { | ||
tileinfo->colsb[i] = MAX_COLS; | ||
tileinfo->rows[i] = MAX_ROWS; | ||
} | ||
|
||
_tile_loadconfig(tileinfo); | ||
} | ||
|
||
void AVX512Payload::request_permission() { | ||
|
||
long rc; | ||
unsigned long bitmask; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume that the syscall is required to enable AMX on the OS level. This however will cause to not compile on Windows and MacOS. Please guard this code with an linux ifdef and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taking a look at this PR microsoft/onnxruntime#14042 it seems that AMX is just supported on Windows. Only question is if the compiler sets some flag in the binary for the operating system or if it just works with Jit generated assembler code. |
||
rc = syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_PERM, XFEATURE_XTILEDATA); | ||
|
||
if (rc) { | ||
workerLog::error() << "XTILE_DATA request failed: " << rc; | ||
} | ||
|
||
rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_PERM, &bitmask); | ||
if (rc) { | ||
workerLog::error() << "prctl(ARCH_GET_XCOMP_PERM) error: " << rc; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this check if |
||
if (bitmask & XFEATURE_MASK_XTILE) { | ||
workerLog::trace() << "ARCH_REQ_XCOMP_PERM XTILE_DATA successful."; | ||
} else { | ||
workerLog::error() << "[ERROR] ARCH_REQ_XCOMP_PERM XTILE_DATA unsuccessful!"; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pointers to the function should be |
||
void AVX512Payload::init_buffer_rand(uintptr_t src1, uintptr_t src2) { | ||
|
||
// Initialize buffer with random values | ||
// Multiplication always produces either 1 or -1 | ||
// Accumulation operation always on (1 + -1) = 0 ensures stable values | ||
|
||
__bfloat16* buf1 = (__bfloat16*)src1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not use c-style casts |
||
__bfloat16* buf2 = (__bfloat16*)src2; | ||
|
||
// TODO: Change MAX_ROWS/MAXC_COLS from constant to maximum size check by asmJit | ||
// Currently not supported by asmJit | ||
// Alternative: Manually parse CPUID | ||
|
||
for (int i = 0; i < MAX_ROWS; i++) { | ||
__bfloat16 random_init = (__bfloat16)(rand() % 65536); // Limit maximum size as 1/x needs to fit bfloat16 | ||
for (int j = 0; j < MAX_COLS; j++) { | ||
buf1[i * MAX_COLS + j] = (__bfloat16)(random_init); | ||
if (!(j % 2)) { | ||
buf2[i * MAX_COLS + j] = (__bfloat16)((-1) / random_init); | ||
} else if (j % 2) { | ||
buf2[i * MAX_COLS + j] = (__bfloat16)(1 / random_init); | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should be called in the AVX512WithAMXPayload::init function. It should override and call the |
||
} | ||
|
||
} // namespace firestarter::environment::x86::payload |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change will not be necessary when the ldtilecfg instruction is integrated into the assembler kernel