Skip to content
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

Target agnostic interface for 8-bit matrix multiply for wasm #49

Merged
merged 27 commits into from
Aug 23, 2021
Merged
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f08b8db
Interface for the integer matrix multiplication for wasm
abhi-agg Aug 4, 2021
5d4ce49
Refactoring
abhi-agg Aug 4, 2021
f55b39c
Removed Int8PrepareBTransposed to leave room for discussions
abhi-agg Aug 4, 2021
bc47163
Small refactoring
abhi-agg Aug 4, 2021
e509979
Removed QuantizedBuffer struct
abhi-agg Aug 5, 2021
0d3e84e
Make Prepared Bias a float instead of int8_t
abhi-agg Aug 5, 2021
06e59cc
Better documentation of each function
abhi-agg Aug 5, 2021
7b7b57d
Small reformatting
abhi-agg Aug 5, 2021
32d317d
Added PrepareB function and removed "Shift" from function names
abhi-agg Aug 9, 2021
faa90cf
Improved documentation
abhi-agg Aug 9, 2021
8d82ae0
Added transpose in PrepareB function, removed Shift from API name
abhi-agg Aug 10, 2021
4b6f02e
Refactor parameter names
abhi-agg Aug 10, 2021
8724a5d
Reformatting: Moved SelectColumnsOfB in the end
abhi-agg Aug 10, 2021
496d17a
Added documentation
abhi-agg Aug 10, 2021
d30a61b
Added changelog
abhi-agg Aug 11, 2021
2f448da
Ran clang format
abhi-agg Aug 11, 2021
e5d45b2
Changed name of Multiply to MultiplyAndAddBias
abhi-agg Aug 11, 2021
e76c8f9
camelCase function names
abhi-agg Aug 11, 2021
e63f119
Better documentation for MultiplyAndAddBias function
abhi-agg Aug 11, 2021
1f7320b
More documentation
abhi-agg Aug 11, 2021
44dea58
Set Index to uint32_t
abhi-agg Aug 11, 2021
8943249
Changed zero point to float
abhi-agg Aug 19, 2021
4e3a129
Consistent naming convention for bias argument
abhi-agg Aug 20, 2021
ed6fbcc
Removed row-major format and shape from documentation of prepared B
abhi-agg Aug 20, 2021
bc38e4a
Final documentation addressing all comments from reviewers
abhi-agg Aug 20, 2021
10661fc
ran clang format
abhi-agg Aug 20, 2021
ae5744e
Improved doc for PrepareA
abhi-agg Aug 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/tensors/cpu/wasm_intgemm_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

/* Main interface for integer matrix multiplication (C = A * B) for wasm.
*
* A is typically activations whose rows should be a multiple of 1 (i.e. no restriction) and
* columns should be a multiple of 64.
*
* B is typically fixed model parameters whose rows should be a multiple of 64 and columns
* should be a multiple of 8.
abhi-agg marked this conversation as resolved.
Show resolved Hide resolved
*
* C is row major.
*/

#include <cstdint>

using Index = unsigned int;
abhi-agg marked this conversation as resolved.
Show resolved Hide resolved

// A structure holding 8-bit quantized values and the associated quantization parameters.
typedef struct QuantizedBuffer {
int8_t* value;
float scale;
int8_t zero_point;
abhi-agg marked this conversation as resolved.
Show resolved Hide resolved
} QuantizedBuffer;


/* Prepare B in a CPU-dependent format from an already quantized, transposed (routine not provided)
* and CPU-independent format of B. The prepared B will be used as an input to Multiply routine.
* This function is useful while using the quantized models that are stored in a CPU-independent
* format on the disk.
*/
void Int8PrepareBQuantizedTransposed(const QuantizedBuffer& B_input, QuantizedBuffer& output, Index inner, Index B_untransposed_cols);


/* Select columns from a prepared B matrix. The number of selected columns must be a multiple of 8.
*/
void Int8SelectColumnsB(const QuantizedBuffer& B_input, QuantizedBuffer& output, Index rows, const Index* cols_begin, const Index* cols_end);


/* Prepare A for the Multiply routine that performs unsigned * signed multiplication. It performs quantization
* on floating values and adds 127 to each number to make sure that all numbers are positive.
*/
void Int8ShiftPrepareA(const float* A_input, QuantizedBuffer& output, Index rows, Index cols);


/* Prepares bias for the Multiply routine that performs unsigned * signed multiplication.
*/
void Int8ShiftPrepareBias(const QuantizedBuffer& B_input, const float* bias_input, QuantizedBuffer& output, Index width, Index B_cols);
abhi-agg marked this conversation as resolved.
Show resolved Hide resolved


/* A multiply routine to perform unsigned * signed multiplication.
* It does C = A * B + Bias, presuming A, B and Bias are quantized inputs prepared using the
* corresponding Prepare* functions.
*/
void Int8ShiftMultiply(const QuantizedBuffer& A_input, const QuantizedBuffer& B_input, const QuantizedBuffer& bias_input, float* output, Index A_rows, Index width, Index B_cols);