Debian Codegraph @

js/src/intgemm/IntegerGemmIntrinsic.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 */
8
9 #include "intgemm/IntegerGemmIntrinsic.h"
10
11 #include "mozilla/CheckedInt.h"
12 #include "mozilla/IntegerPrintfMacros.h"
13 #include "mozilla/TimeStamp.h"
14
15 #include <gemmology_fwd.h>
16 #include "fmt/format.h"
17
18 #include "js/ErrorReport.h"
19 #include "js/HeapAPI.h"
20 #include "vm/ArrayBufferObject.h"
21 #include "vm/GeckoProfiler.h"
22 #include "vm/JSContext.h"
23 #include "wasm/WasmBuiltins.h"
24 #include "wasm/WasmInstance.h"
25 #include "wasm/WasmLog.h"
26
27 #if defined(USE_AVX512BW)
28 # if defined(USE_AVX512VNNI)
29 # if defined(USE_AVXVNNI)
30 # define SUPPORTED_ARCHS \
31 xsimd::arch_list<xsimd::avx512vnni<xsimd::avx512bw>, xsimd::avx512bw, \
32 xsimd::avxvnni, xsimd::avx2, xsimd::ssse3, \
33 xsimd::sse2>
34 # else
35 # define SUPPORTED_ARCHS \
36 xsimd::arch_list<xsimd::avx512vnni<xsimd::avx512bw>, xsimd::avx512bw, \
37 xsimd::avx2, xsimd::ssse3, xsimd::sse2>
38 # endif
39 # elif defined(USE_AVXVNNI)
40 # define SUPPORTED_ARCHS \
41 xsimd::arch_list<xsimd::avx512bw, xsimd::avxvnni, xsimd::avx2, \
42 xsimd::ssse3, xsimd::sse2>
43 # else
44 # define SUPPORTED_ARCHS \
45 xsimd::arch_list<xsimd::avx512bw, xsimd::avx2, xsimd::ssse3, xsimd::sse2>
46 # endif
47 #elif defined(USE_AVXVNNI)
48 # define SUPPORTED_ARCHS \
49 xsimd::arch_list<xsimd::avxvnni, xsimd::avx2, xsimd::ssse3, xsimd::sse2>
50 #elif defined(USE_AVX2)
51 # define SUPPORTED_ARCHS \
52 xsimd::arch_list<xsimd::avx2, xsimd::ssse3, xsimd::sse2>
53 #elif defined(USE_SSSE3)
54 # define SUPPORTED_ARCHS xsimd::arch_list<xsimd::ssse3, xsimd::sse2>
55 #elif defined(USE_SSE2)
56 # define SUPPORTED_ARCHS xsimd::arch_list<xsimd::sse2>
57 #elif defined(USE_NEON) and defined(XSIMD_WITH_NEON64)
58 # if defined(USE_NEON_I8MM)
59 # define SUPPORTED_ARCHS \
60 xsimd::arch_list<xsimd::i8mm<xsimd::neon64>, xsimd::neon64>
61 # else
62 # define SUPPORTED_ARCHS xsimd::arch_list<xsimd::neon64>
63 # endif
64 #else
65 # error no supported architecture
66 #endif
67
68 // Dispatch *at runtime* based on run-time hardware and compile-time
69 // architectures.
70 //
71 // FIXME: Ideally we would not run the dispatch code at each function call.
72 #define GEMMOLOGY_DISPATCH(FUNC) \
73 xsimd::dispatch<SUPPORTED_ARCHS>([](auto arch, auto... args) { \
74 return gemmology::Engine<decltype(arch)>::FUNC(args...); \
75 })
76
77 template <size_t TextLength = 512, typename CharT = char>
78 struct AutoProfilerMarker {
79 AutoProfilerMarker(js::GeckoProfilerRuntime& profiler, const CharT* name)
80 : profiler(profiler), name(name) {
81 if (profiler.enabled()) {
82 startTime = mozilla::TimeStamp::Now();
83 }
84 }
85
86 template <typename... Args>
87 AutoProfilerMarker(js::GeckoProfilerRuntime& profiler, const CharT* name,
88 fmt::format_string<Args...> aFormatStr, Args&&... aArgs)
89 : profiler(profiler), name(name) {
90 if (profiler.enabled()) {
91 startTime = mozilla::TimeStamp::Now();
92 auto [out, size] = fmt::vformat_to_n(
93 text, sizeof(text) - 1, aFormatStr,
94 fmt::make_format_args<fmt::buffered_context<CharT>>(aArgs...));
95
96 MOZ_ASSERT(size > sizeof(text) - 1,
97 "Truncated marker, consider increasing the buffer");
98
99 *out = 0;
100 }
101 }
102 ~AutoProfilerMarker() {
103 if (profiler.enabled()) {
104 profiler.markInterval(name, startTime, text,
105 JS::ProfilingCategoryPair::JS);
106 }
107 }
108 js::GeckoProfilerRuntime& profiler;
109 const char* name;
110 char text[TextLength]{};
111 mozilla::TimeStamp startTime;
112 };
113
114 static constexpr uint32_t ARRAY_ALIGNMENT = 64;
115 static constexpr uint32_t ROWS_A_MULTIPLIER = 1;
116 static constexpr uint32_t COLUMNS_A_MULTIPLIER = 64;
117 static constexpr uint32_t ROWS_B_MULTIPLIER = COLUMNS_A_MULTIPLIER;
118 static constexpr uint32_t COLUMNS_B_MULTIPLIER = 8;
119 static constexpr uint32_t SELECTED_COLUMNS_B_MULTIPLIER = 8;
120
121 void ReportGemmError(JSContext* cx, const unsigned errorNumber) {
122 JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr, errorNumber);
123 }
124
125 size_t GetWasmRawBufferLength(const uint8_t* memBase) {
126 const js::WasmArrayRawBuffer* rawBuf =
127 js::WasmArrayRawBuffer::fromDataPtr(memBase);
128 return rawBuf->byteLength();
129 }
130
131 bool CheckMatrixDimension(JSContext* cx, uint32_t size,
132 uint32_t sizeMultiplier) {
133 // A valid size is a positive integral multiple of Multiplier
134 if ((size == 0) || (size % sizeMultiplier != 0)) {
135 js::wasm::Log(
136 cx, "Invalid dimension value:%" PRIu32 " (should be a multiple of %u)",
137 size, sizeMultiplier);
138 return false;
139 }
140 return true;
141 }
142
143 bool CheckMatrixBound(JSContext* cx, uint32_t input, uint64_t inputSize,
144 size_t wasmBufferSize) {
145 mozilla::CheckedUint64 inputUpperLimit(inputSize);
146 inputUpperLimit += input;
147
148 // Bound check fails if size overflows or it spans outside the wasm memory
149 if (!inputUpperLimit.isValid() ||
150 (inputUpperLimit.value() >= (uint64_t)wasmBufferSize)) {
151 js::wasm::Log(cx, "Memory out of wasm bounds for matrix:%" PRIu32, input);
152 return false;
153 }
154 return true;
155 }
156
157 bool CheckMatrixBoundAndAlignment(JSContext* cx, uint32_t input,
158 uint64_t inputSize, size_t wasmBufferSize) {
159 // Alignment check: It is sufficient to check alignment for the offset rather
160 // than for the actual pointer within wasm memory (as long as following assert
161 // is satisfied)
162 static_assert(js::gc::PageSize >= ARRAY_ALIGNMENT,
163 "PageSize should be bigger than Alignment");
164 if (input % ARRAY_ALIGNMENT != 0) {
165 js::wasm::Log(
166 cx, "Unaligned access for matrix:%" PRIu32 " (should be %u aligned)",
167 input, ARRAY_ALIGNMENT);
168 return false;
169 }
170
171 // Check Bound
172 return CheckMatrixBound(cx, input, inputSize, wasmBufferSize);
173 }
174
175 int32_t js::intgemm::IntrI8PrepareB(wasm::Instance* instance,
176 uint32_t inputMatrixB, float scale,
177 float zeroPoint, uint32_t rowsB,
178 uint32_t colsB, uint32_t outputMatrixB,
179 uint8_t* memBase) {
180 MOZ_ASSERT(wasm::SASigIntrI8PrepareB.failureMode ==
181 wasm::FailureMode::FailOnNegI32);
182 JSContext* cx = instance->cx();
183
184 // Size checks for matricies
185 if (!CheckMatrixDimension(cx, rowsB, ROWS_B_MULTIPLIER) ||
186 !CheckMatrixDimension(cx, colsB, COLUMNS_B_MULTIPLIER)) {
187 wasm::Log(cx, "%s: rowsB:%" PRIu32 " colsB:%" PRIu32, __FUNCTION__, rowsB,
188 colsB);
189 ReportGemmError(cx, JSMSG_WASM_UNREACHABLE);
190 return -1;
191 }
192
193 // Memory Bound and Alignment checks for matricies
194 uint64_t sizeB = (uint64_t)rowsB * (uint64_t)colsB;
195 size_t wasmBufferSize = GetWasmRawBufferLength(memBase);
196 if (!CheckMatrixBoundAndAlignment(cx, inputMatrixB, sizeB, wasmBufferSize) ||
197 !CheckMatrixBoundAndAlignment(cx, outputMatrixB, sizeB, wasmBufferSize)) {
198 wasm::Log(cx,
199 "%s: inputB:%x rowsB:%" PRIu32 " colsB:%" PRIu32
200 " outputB:%x sizeB:%" PRIu64 " wasmBufferSize:%zu",
201 __FUNCTION__, inputMatrixB, rowsB, colsB, outputMatrixB, sizeB,
202 wasmBufferSize);
203 ReportGemmError(cx, JSMSG_WASM_OUT_OF_BOUNDS);
204 return -1;
205 }
206
207 // Actual call to the 3rd party library (intgemm) for PrepareB
208 const float* inputMatrixBPtr =
209 reinterpret_cast<const float*>(&memBase[inputMatrixB]);
210 int8_t* outputMatrixBPtr = reinterpret_cast<int8_t*>(&memBase[outputMatrixB]);
211 AutoProfilerMarker marker(cx->runtime()->geckoProfiler(), "integemm::PreparB",
212 FMT_STRING("rowsB: {} colsB: {} sizeB: {}"), rowsB,
213 colsB, sizeB);
214 GEMMOLOGY_DISPATCH(PrepareB)
215 (inputMatrixBPtr, outputMatrixBPtr,
216 scale, // Quant Mult
217 rowsB, colsB);
218 return 0;
219 }
220
221 int32_t js::intgemm::IntrI8PrepareBFromTransposed(
222 wasm::Instance* instance, uint32_t inputMatrixBTransposed, float scale,
223 float zeroPoint, uint32_t rowsB, uint32_t colsB, uint32_t outputMatrixB,
224 uint8_t* memBase) {
225 MOZ_ASSERT(wasm::SASigIntrI8PrepareBFromTransposed.failureMode ==
226 wasm::FailureMode::FailOnNegI32);
227 JSContext* cx = instance->cx();
228
229 // Size checks for matricies
230 if (!CheckMatrixDimension(cx, rowsB, ROWS_B_MULTIPLIER) ||
231 !CheckMatrixDimension(cx, colsB, COLUMNS_B_MULTIPLIER)) {
232 wasm::Log(cx, "%s: rowsB:%" PRIu32 " colsB:%" PRIu32, __FUNCTION__, rowsB,
233 colsB);
234 ReportGemmError(cx, JSMSG_WASM_UNREACHABLE);
235 return -1;
236 }
237
238 // Memory Bound checks for all matricies
239 uint64_t sizeB = (uint64_t)rowsB * (uint64_t)colsB;
240 size_t wasmBufferSize = GetWasmRawBufferLength(memBase);
241 if (!CheckMatrixBoundAndAlignment(cx, inputMatrixBTransposed, sizeB,
242 wasmBufferSize) ||
243 !CheckMatrixBoundAndAlignment(cx, outputMatrixB, sizeB, wasmBufferSize)) {
244 wasm::Log(cx,
245 "%s: inputBT:%x rowsB:%" PRIu32 " colsB:%" PRIu32
246 " outputB:%x sizeB:%" PRIu64 " wasmBufferSize:%zu",
247 __FUNCTION__, inputMatrixBTransposed, rowsB, colsB, outputMatrixB,
248 sizeB, wasmBufferSize);
249 ReportGemmError(cx, JSMSG_WASM_OUT_OF_BOUNDS);
250 return -1;
251 }
252
253 // Actual call to the 3rd party library (intgemm) for PrepareBTransposed
254 const float* inputMatrixBTransposedPtr =
255 reinterpret_cast<const float*>(&memBase[inputMatrixBTransposed]);
256 int8_t* outputMatrixBPtr = reinterpret_cast<int8_t*>(&memBase[outputMatrixB]);
257 AutoProfilerMarker marker(
258 cx->runtime()->geckoProfiler(), "intgemm::PreparBTransposed",
259 FMT_STRING("rowsB: {} colsB: {} sizeB: {}"), rowsB, colsB, sizeB);
260 GEMMOLOGY_DISPATCH(PrepareBTransposed)
261 (inputMatrixBTransposedPtr, outputMatrixBPtr,
262 scale, // Quant Mult
263 rowsB, colsB);
264 return 0;
265 }
266
267 int32_t js::intgemm::IntrI8PrepareBFromQuantizedTransposed(
268 wasm::Instance* instance, uint32_t inputMatrixBQuantizedTransposed,
269 uint32_t rowsB, uint32_t colsB, uint32_t outputMatrixB, uint8_t* memBase) {
270 MOZ_ASSERT(wasm::SASigIntrI8PrepareBFromQuantizedTransposed.failureMode ==
271 wasm::FailureMode::FailOnNegI32);
272 JSContext* cx = instance->cx();
273
274 // Size checks for matricies
275 if (!CheckMatrixDimension(cx, rowsB, ROWS_B_MULTIPLIER) ||
276 !CheckMatrixDimension(cx, colsB, COLUMNS_B_MULTIPLIER)) {
277 wasm::Log(cx, "%s: rowsB:%" PRIu32 " colsB:%" PRIu32, __FUNCTION__, rowsB,
278 colsB);
279 ReportGemmError(cx, JSMSG_WASM_UNREACHABLE);
280 return -1;
281 }
282
283 // Memory Bound checks for all matricies
284 uint64_t sizeB = (uint64_t)rowsB * (uint64_t)colsB;
285 size_t wasmBufferSize = GetWasmRawBufferLength(memBase);
286 if (!CheckMatrixBoundAndAlignment(cx, inputMatrixBQuantizedTransposed, sizeB,
287 wasmBufferSize) ||
288 !CheckMatrixBoundAndAlignment(cx, outputMatrixB, sizeB, wasmBufferSize)) {
289 wasm::Log(cx,
290 "%s: inputBQT:%x rowsB:%" PRIu32 " colsB:%" PRIu32
291 " outputB:%x sizeA:%" PRIu64 " wasmBufferSize:%zu",
292 __FUNCTION__, inputMatrixBQuantizedTransposed, rowsB, colsB,
293 outputMatrixB, sizeB, wasmBufferSize);
294 ReportGemmError(cx, JSMSG_WASM_OUT_OF_BOUNDS);
295 return -1;
296 }
297
298 // Actual call to the 3rd party library (intgemm)
299 const int8_t* inputMatrixBQuantizedTransposedPtr =
300 reinterpret_cast<const int8_t*>(
301 &memBase[inputMatrixBQuantizedTransposed]);
302 int8_t* outputMatrixBPtr = reinterpret_cast<int8_t*>(&memBase[outputMatrixB]);
303 AutoProfilerMarker marker(cx->runtime()->geckoProfiler(),
304 "intgemm::PrepareBQuantizedTransposed",
305 FMT_STRING("rowsB: {}, colsB: {}"), rowsB, colsB);
306 GEMMOLOGY_DISPATCH(PrepareBQuantizedTransposed)
307 (inputMatrixBQuantizedTransposedPtr, outputMatrixBPtr, rowsB, colsB);
308 return 0;
309 }
310
311 int32_t js::intgemm::IntrI8PrepareA(wasm::Instance* instance,
312 uint32_t inputMatrixA, float scale,
313 float zeroPoint, uint32_t rowsA,
314 uint32_t colsA, uint32_t outputMatrixA,
315 uint8_t* memBase) {
316 MOZ_ASSERT(wasm::SASigIntrI8PrepareA.failureMode ==
317 wasm::FailureMode::FailOnNegI32);
318 JSContext* cx = instance->cx();
319 // Size checks for matricies
320 if (!CheckMatrixDimension(cx, rowsA, ROWS_A_MULTIPLIER) ||
321 !CheckMatrixDimension(cx, colsA, COLUMNS_A_MULTIPLIER)) {
322 wasm::Log(cx, "%s: rowsA:%" PRIu32 " colsA:%" PRIu32, __FUNCTION__, rowsA,
323 colsA);
324 ReportGemmError(cx, JSMSG_WASM_UNREACHABLE);
325 return -1;
326 }
327
328 // Memory Bound checks for all matricies
329 uint64_t sizeA = (uint64_t)rowsA * (uint64_t)colsA;
330 size_t wasmBufferSize = GetWasmRawBufferLength(memBase);
331 if (!CheckMatrixBoundAndAlignment(cx, inputMatrixA, sizeA, wasmBufferSize) ||
332 !CheckMatrixBoundAndAlignment(cx, outputMatrixA, sizeA, wasmBufferSize)) {
333 wasm::Log(cx,
334 "%s: inputA:%x rowsA:%" PRIu32 " colsA:%" PRIu32
335 " outputA:%x sizeA:%" PRIu64 " wasmBufferSize:%zu",
336 __FUNCTION__, inputMatrixA, rowsA, colsA, outputMatrixA, sizeA,
337 wasmBufferSize);
338 ReportGemmError(cx, JSMSG_WASM_OUT_OF_BOUNDS);
339 return -1;
340 }
341
342 // Actual call to the 3rd party library (intgemm)
343 const float* inputMatrixAPtr =
344 reinterpret_cast<const float*>(&memBase[inputMatrixA]);
345 uint8_t* outputMatrixAPtr = &memBase[outputMatrixA];
346 AutoProfilerMarker marker(cx->runtime()->geckoProfiler(), "intgemm::PrepareA",
347 FMT_STRING("rowsA: {}, colsA: {}"), rowsA, colsA);
348 GEMMOLOGY_DISPATCH(Shift::PrepareA)
349 (inputMatrixAPtr, outputMatrixAPtr, scale, rowsA, colsA);
350 return 0;
351 }
352
353 int32_t js::intgemm::IntrI8PrepareBias(
354 wasm::Instance* instance, uint32_t inputMatrixBPrepared, float scaleA,
355 float zeroPointA, float scaleB, float zeroPointB, uint32_t rowsB,
356 uint32_t colsB, uint32_t inputBias, uint32_t output, uint8_t* memBase) {
357 MOZ_ASSERT(wasm::SASigIntrI8PrepareBias.failureMode ==
358 wasm::FailureMode::FailOnNegI32);
359 JSContext* cx = instance->cx();
360
361 // Size checks for matricies
362 if (!CheckMatrixDimension(cx, rowsB, ROWS_B_MULTIPLIER) ||
363 !CheckMatrixDimension(cx, colsB, COLUMNS_B_MULTIPLIER)) {
364 wasm::Log(cx, "%s: rowsB:%" PRIu32 " colsB:%" PRIu32, __FUNCTION__, rowsB,
365 colsB);
366 ReportGemmError(cx, JSMSG_WASM_UNREACHABLE);
367 return -1;
368 }
369
370 // Memory Bound checks for all matrices
371 uint64_t sizeB = (uint64_t)rowsB * (uint64_t)colsB;
372 uint64_t sizeBias = colsB;
373 size_t wasmBufferSize = GetWasmRawBufferLength(memBase);
374 if (!CheckMatrixBoundAndAlignment(cx, inputMatrixBPrepared, sizeB,
375 wasmBufferSize) ||
376 !CheckMatrixBound(cx, output, sizeBias, wasmBufferSize)) {
377 wasm::Log(cx,
378 "%s: preparedB:%x rowsB:%" PRIu32 " colsB:%" PRIu32
379 " outputBias:%x sizeB:%" PRIu64 " wasmBufferSize:%zu",
380 __FUNCTION__, inputMatrixBPrepared, rowsB, colsB, output, sizeB,
381 wasmBufferSize);
382 ReportGemmError(cx, JSMSG_WASM_OUT_OF_BOUNDS);
383 return -1;
384 }
385
386 // Actual call to the 3rd party library (intgemm)
387 const int8_t* inputMatrixBPreparedPtr =
388 (const int8_t*)&memBase[inputMatrixBPrepared];
389 float* outputPtr = (float*)&memBase[output];
390 float unquantFactor =
391 (-1) * ((127.0f / scaleA) * (127.0f / scaleB)) / (127.0f);
392
393 if (inputBias) {
394 if (!CheckMatrixBound(cx, inputBias, sizeBias, wasmBufferSize)) {
395 wasm::Log(cx, "%s: inputBias:%x wasmBufferSize:%zu", __FUNCTION__,
396 inputBias, wasmBufferSize);
397 ReportGemmError(cx, JSMSG_WASM_OUT_OF_BOUNDS);
398 return -1;
399 }
400 const float* inputBiasPtr = reinterpret_cast<float*>(&memBase[inputBias]);
401
402 AutoProfilerMarker marker(
403 cx->runtime()->geckoProfiler(), "intgemm::PrepareBias w/ input bias",
404 FMT_STRING("rowsB: {} colsB: {} sizeB: {}"), rowsB, colsB, sizeB);
405 GEMMOLOGY_DISPATCH(Shift::PrepareBias)
406 (inputMatrixBPreparedPtr, rowsB, colsB,
407 gemmology::callbacks::UnquantizeAndAddBiasAndWrite(
408 unquantFactor, inputBiasPtr, outputPtr));
409 } else {
410 AutoProfilerMarker marker(
411 cx->runtime()->geckoProfiler(), "intgemm::PrepareBias",
412 FMT_STRING("rowsB: {} colsB: {} sizeB: {}"), rowsB, colsB, sizeB);
413 GEMMOLOGY_DISPATCH(Shift::PrepareBias)
414 (inputMatrixBPreparedPtr, rowsB, colsB,
415 gemmology::callbacks::UnquantizeAndWrite(unquantFactor, outputPtr));
416 }
417 return 0;
418 }
419
420 int32_t js::intgemm::IntrI8MultiplyAndAddBias(
421 wasm::Instance* instance, uint32_t inputMatrixAPrepared, float scaleA,
422 float zeroPointA, uint32_t inputMatrixBPrepared, float scaleB,
423 float zeroPointB, uint32_t inputBiasPrepared, float unquantMultiplier,
424 uint32_t rowsA, uint32_t width, uint32_t colsB, uint32_t output,
425 uint8_t* memBase) {
426 MOZ_ASSERT(wasm::SASigIntrI8MultiplyAndAddBias.failureMode ==
427 wasm::FailureMode::FailOnNegI32);
428 JSContext* cx = instance->cx();
429
430 // Size checks for matricies
431 if (!CheckMatrixDimension(cx, rowsA, ROWS_A_MULTIPLIER) ||
432 !CheckMatrixDimension(cx, width, COLUMNS_A_MULTIPLIER) ||
433 !CheckMatrixDimension(cx, colsB, COLUMNS_B_MULTIPLIER)) {
434 wasm::Log(cx, "%s: rowsA:%" PRIu32 " width:%" PRIu32 " colsB:%" PRIu32,
435 __FUNCTION__, rowsA, width, colsB);
436 ReportGemmError(cx, JSMSG_WASM_UNREACHABLE);
437 return -1;
438 }
439
440 // Memory Bound checks for all matricies
441 uint64_t sizeA = (uint64_t)rowsA * (uint64_t)width;
442 uint64_t sizeB = (uint64_t)width * (uint64_t)colsB;
443 uint64_t sizeBias = (uint64_t)colsB;
444 uint64_t sizeOutput = (uint64_t)rowsA * (uint64_t)colsB;
445 size_t wasmBufferSize = GetWasmRawBufferLength(memBase);
446 if (!CheckMatrixBoundAndAlignment(cx, inputMatrixAPrepared, sizeA,
447 wasmBufferSize) ||
448 !CheckMatrixBoundAndAlignment(cx, inputMatrixBPrepared, sizeB,
449 wasmBufferSize) ||
450 !CheckMatrixBound(cx, inputBiasPrepared, sizeBias, wasmBufferSize) ||
451 !CheckMatrixBound(cx, output, sizeOutput, wasmBufferSize)) {
452 wasm::Log(cx,
453 "%s: preparedA:%x preparedB:%x preparedBias:%x rowsA:%" PRIu32
454 " width:%" PRIu32 " colsB:%" PRIu32
455 " output:%x sizeA:%" PRIu64 " sizeB:%" PRIu64
456 " sizeBias:%" PRIu64 " sizeOutput:%" PRIu64,
457 __FUNCTION__, inputMatrixAPrepared, inputMatrixBPrepared,
458 inputBiasPrepared, rowsA, width, colsB, output, sizeA, sizeB,
459 sizeBias, sizeOutput);
460 ReportGemmError(cx, JSMSG_WASM_OUT_OF_BOUNDS);
461 return -1;
462 }
463
464 // Actual call to the 3rd party library (intgemm)
465 const uint8_t* inputMatrixAPreparedPtr = &memBase[inputMatrixAPrepared];
466 const int8_t* inputMatrixBPreparedPtr =
467 reinterpret_cast<const int8_t*>(&memBase[inputMatrixBPrepared]);
468 const float* inputBiasPreparedPtr =
469 reinterpret_cast<const float*>(&memBase[inputBiasPrepared]);
470 float* outputPtr = reinterpret_cast<float*>(&memBase[output]);
471 float unquantFactor = unquantMultiplier / (scaleA * scaleB);
472
473 AutoProfilerMarker marker(
474 cx->runtime()->geckoProfiler(), "intgemm::Shift::Multiply",
475 FMT_STRING("rowsA: {}, width: {}, colsA: {}"), rowsA, width, colsB);
476 GEMMOLOGY_DISPATCH(Shift::Multiply)
477 (inputMatrixAPreparedPtr, inputMatrixBPreparedPtr, rowsA, width, colsB,
478 gemmology::callbacks::UnquantizeAndAddBiasAndWrite(
479 unquantFactor, inputBiasPreparedPtr, outputPtr));
480 return 0;
481 }
482
483 int32_t js::intgemm::IntrI8SelectColumnsOfB(wasm::Instance* instance,
484 uint32_t inputMatrixBPrepared,
485 uint32_t rowsB, uint32_t colsB,
486 uint32_t colIndexList,
487 uint32_t sizeColIndexList,
488 uint32_t output, uint8_t* memBase) {
489 MOZ_ASSERT(wasm::SASigIntrI8SelectColumnsOfB.failureMode ==
490 wasm::FailureMode::FailOnNegI32);
491 JSContext* cx = instance->cx();
492
493 // Size checks for matricies
494 if (!CheckMatrixDimension(cx, rowsB, ROWS_B_MULTIPLIER) ||
495 !CheckMatrixDimension(cx, colsB, COLUMNS_B_MULTIPLIER) ||
496 !CheckMatrixDimension(cx, sizeColIndexList,
497 SELECTED_COLUMNS_B_MULTIPLIER)) {
498 wasm::Log(cx,
499 "%s: rowsB:%" PRIu32 " colsB:%" PRIu32
500 " sizeColIndexList:%" PRIu32,
501 __FUNCTION__, rowsB, colsB, sizeColIndexList);
502 ReportGemmError(cx, JSMSG_WASM_UNREACHABLE);
503 return -1;
504 }
505
506 // Memory Bound checks for all matricies
507 uint64_t sizeB = (uint64_t)rowsB * (uint64_t)colsB;
508 uint64_t sizeOutput = (uint64_t)rowsB * (uint64_t)sizeColIndexList;
509 size_t wasmBufferSize = GetWasmRawBufferLength(memBase);
510 if (!CheckMatrixBoundAndAlignment(cx, inputMatrixBPrepared, sizeB,
511 wasmBufferSize) ||
512 !CheckMatrixBound(cx, colIndexList, sizeColIndexList, wasmBufferSize) ||
513 !CheckMatrixBound(cx, output, sizeOutput, wasmBufferSize)) {
514 wasm::Log(cx,
515 "%s: preparedB:%x rowsB:%" PRIu32 " colsB:%" PRIu32
516 " colList:%x sizeColList:%" PRIu32 " output:%x sizeB:%" PRIu64
517 " sizeOutput:%" PRIu64,
518 __FUNCTION__, inputMatrixBPrepared, rowsB, colsB, colIndexList,
519 sizeColIndexList, output, sizeB, sizeOutput);
520 ReportGemmError(cx, JSMSG_WASM_OUT_OF_BOUNDS);
521 return -1;
522 }
523
524 // Actual call to the 3rd party library (intgemm)
525 const int8_t* inputMatrixBPreparedPtr =
526 reinterpret_cast<const int8_t*>(&memBase[inputMatrixBPrepared]);
527 const uint32_t* colIndexListPtr =
528 reinterpret_cast<const uint32_t*>(&memBase[colIndexList]);
529 int8_t* outputPtr = reinterpret_cast<int8_t*>(&memBase[output]);
530 AutoProfilerMarker marker(
531 cx->runtime()->geckoProfiler(), "integemm::SelectColumnsB",
532 FMT_STRING("rowsB: {} colsB: {} sizecolList: {}, sizeB: {}"), rowsB,
533 colsB, sizeColIndexList, sizeB);
534 GEMMOLOGY_DISPATCH(SelectColumnsB)
535 (inputMatrixBPreparedPtr, outputPtr, rowsB, colIndexListPtr,
536 colIndexListPtr + sizeColIndexList);
537 return 0;
538 }
539
540 #undef GEMMOLOGY_DISPATCH
541 #undef SUPPORTED_ARCHS