Loading...
Searching...
No Matches
SchemeII.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5// Ozaki Scheme II matrix products on the CPU (Ozaki, Uchino and Imamura,
6// "Ozaki Scheme II: A GEMM-oriented emulation of floating-point matrix
7// multiplication using an integer modular technique", arXiv:2504.08009).
8//
9// C = alpha op(A) op(B) + beta C is evaluated as follows.
10//
11// 1. Row i of op(A) is scaled by 2^s_i and column j of op(B) by 2^t_j and
12// both are rounded to integers A', B' with |A'|, |B'| <= 2^(bits-1).
13// s_i depends only on max_l |op(A)_il| and t_j only on max_l |op(B)_lj|
14// (or on exponents the caller supplies from global maxima), so the
15// integer of an output entry does not depend on which other rows or
16// columns are in the same call.
17// 2. For pairwise coprime moduli p_1..p_N with product M, the residues of
18// A' and B' modulo p_t are multiplied by an ordinary GEMM whose
19// arithmetic is exact: FP64 with chunk (p_t/2)^2 <= 2^53 (Section 3.2
20// of the paper) or INT8 with INT32 accumulation (Section 3.1). An exact
21// GEMM returns the same integers for any blocking, summation order or
22// thread count.
23// 3. The residues of A'B' are combined with the Chinese Remainder Theorem
24// into the integer A'B' itself, which is unique because
25// 2 k 2^(2 bits - 2) (1 + 2^-16) < M. The reconstruction is exact
26// integer arithmetic carried in binary64 limbs.
27// 4. The integer is rounded to binary64 (faithfully: the limbs are summed
28// in double-word arithmetic) and unscaled by 2^-(s_i + t_j). Then
29// C = alpha P + beta C, elementwise.
30//
31// The result of an entry is a function of row i of op(A), column j of op(B)
32// and bits alone. Two engines that use the same bits return identical
33// bits, whatever their moduli; a split of m or n across calls, ranks or
34// threads returns identical bits as well.
35//
36// Error. With a_i = max_l |op(A)_il|, b_j = max_l |op(B)_lj| the rounding
37// to integers moves each operand entry by at most a_i 2^(1-bits) (resp.
38// b_j 2^(1-bits)), so before the final rounding
39// |P_ij - (op(A) op(B))_ij| <= k a_i b_j (2^(2-bits) + 2^(2-2 bits)).
40// For the default bits = 56 this is k a_i b_j 2^-54 (1 + 2^-54), half of
41// the worst case k u a_i b_j (u = 2^-53) of the bound gamma_k (|A||B|)_ij
42// for an FP64 GEMM. The final rounding adds at most 2u |P_ij|.
43
44#include <cstddef>
45#include <cstdint>
46#include <memory>
47#include <span>
48#include <string>
49#include <string_view>
50#include <vector>
51
52namespace gpr::linalg::schemeii {
53
54enum class Engine { Fp64, Int8 };
56
57inline constexpr int kDefaultBits = 56;
58inline constexpr int kMaxBits = 62;
59
60struct Options {
64 int bits = 0;
66 int moduli = 0;
69 std::string library;
72 int device = -1;
74 int threads = 0;
77 std::int64_t block_rows = 512;
78 std::int64_t block_cols = 256;
81 std::size_t panel_bytes = std::size_t{2} << 30;
86 double device_min = 16777216.0;
87};
88
97Options parseOptions(std::string_view spec);
98bool isSpec(std::string_view spec) noexcept;
99
101struct Plan {
102 int bits = 0;
103 std::int64_t k = 0;
104 std::int64_t chunk = 0; // inner length of one exact GEMM
105 std::vector<std::uint32_t> moduli;
107};
108
112int scaleExponent(double max_abs, int bits) noexcept;
113
114class Context;
115
118class Operand {
119public:
121 Operand(Operand&&) noexcept;
122 Operand& operator=(Operand&&) noexcept;
124 [[nodiscard]] std::int64_t extent() const noexcept; // m or n
125 [[nodiscard]] std::int64_t depth() const noexcept; // k
126 [[nodiscard]] const std::vector<int>& exponents() const noexcept;
127 [[nodiscard]] std::size_t bytes() const noexcept;
128
129 struct Data;
130
131private:
132 friend class Context;
133 std::unique_ptr<Data> data_;
134};
135
138struct Task {
139 const Operand* rows = nullptr;
140 std::int64_t row0 = 0;
141 std::int64_t m = 0;
142 const Operand* cols = nullptr;
143 std::int64_t col0 = 0;
144 std::int64_t n = 0;
145 double alpha = 1.0;
146 double beta = 0.0;
147 double* c = nullptr;
148 std::int64_t ldc = 0;
149};
150
151class Context {
152public:
153 explicit Context(Options options);
155 Context(const Context&) = delete;
156 Context& operator=(const Context&) = delete;
157
158 [[nodiscard]] const Options& options() const noexcept;
160 [[nodiscard]] std::string describe(std::int64_t k) const;
161 [[nodiscard]] Plan plan(std::int64_t k) const;
162
164 void setThreadSource(int (*source)());
165
167 Operand prepareRows(const double* a, std::int64_t lda, bool transpose,
168 std::int64_t m, std::int64_t k, const Plan& plan,
169 const int* exponents = nullptr) const;
171 Operand prepareColumns(const double* b, std::int64_t ldb, bool transpose,
172 std::int64_t k, std::int64_t n, const Plan& plan,
173 const int* exponents = nullptr) const;
174
177 void run(std::span<const Task> tasks) const;
178
181 void gemm(bool transpose_a, bool transpose_b, std::int64_t m,
182 std::int64_t n, std::int64_t k, double alpha, const double* a,
183 std::int64_t lda, const double* b, std::int64_t ldb, double beta,
184 double* c, std::int64_t ldc) const;
185
191 void residues(const Task& task, std::vector<std::uint32_t>& out) const;
193 void reconstruct(const Plan& plan, std::span<const std::uint32_t> residues,
194 std::span<const int> row_exponents,
195 std::span<const int> column_exponents, std::int64_t m,
196 std::int64_t n, double alpha, double beta, double* c,
197 std::int64_t ldc) const;
198
199 struct Impl;
200
201private:
202 std::unique_ptr<Impl> impl_;
203};
204
206std::vector<std::uint32_t> engineModuli(Engine engine, std::int64_t chunk);
208std::int64_t engineChunk(Engine engine, std::int64_t k);
209
210} // namespace gpr::linalg::schemeii
void residues(const Task &task, std::vector< std::uint32_t > &out) const
Plan plan(std::int64_t k) const
Context & operator=(const Context &)=delete
void gemm(bool transpose_a, bool transpose_b, std::int64_t m, std::int64_t n, std::int64_t k, double alpha, const double *a, std::int64_t lda, const double *b, std::int64_t ldb, double beta, double *c, std::int64_t ldc) const
Operand prepareRows(const double *a, std::int64_t lda, bool transpose, std::int64_t m, std::int64_t k, const Plan &plan, const int *exponents=nullptr) const
Row operand from op(A) (m x k). Exponents override the row maxima.
void setThreadSource(int(*source)())
Thread count used when Options::threads is zero.
const Options & options() const noexcept
void run(std::span< const Task > tasks) const
void reconstruct(const Plan &plan, std::span< const std::uint32_t > residues, std::span< const int > row_exponents, std::span< const int > column_exponents, std::int64_t m, std::int64_t n, double alpha, double beta, double *c, std::int64_t ldc) const
C block from summed residues (see residues()).
std::string describe(std::int64_t k) const
One line naming engine, provider, bits and moduli for k.
Operand prepareColumns(const double *b, std::int64_t ldb, bool transpose, std::int64_t k, std::int64_t n, const Plan &plan, const int *exponents=nullptr) const
Column operand from op(B) (k x n).
Context(const Context &)=delete
std::int64_t depth() const noexcept
std::size_t bytes() const noexcept
Operand(Operand &&) noexcept
const std::vector< int > & exponents() const noexcept
std::int64_t extent() const noexcept
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
int scaleExponent(double max_abs, int bits) noexcept
Definition SchemeII.cpp:447
constexpr int kDefaultBits
Definition SchemeII.h:57
std::vector< std::uint32_t > engineModuli(Engine engine, std::int64_t chunk)
Moduli of an engine for an inner chunk length, largest first.
Definition SchemeII.cpp:442
std::int64_t engineChunk(Engine engine, std::int64_t k)
Inner chunk length of one exact GEMM for an engine.
Definition SchemeII.cpp:436
bool isSpec(std::string_view spec) noexcept
Definition SchemeII.cpp:455
Options parseOptions(std::string_view spec)
Definition SchemeII.cpp:460
constexpr int kMaxBits
Definition SchemeII.h:58
STL namespace.
int threads
Worker threads. Zero asks the thread source at each product.
Definition SchemeII.h:74
int moduli
Number of moduli. Zero selects the fewest that support bits.
Definition SchemeII.h:66
std::string library
Shared library of the INT8 provider. Empty searches the defaults.
Definition SchemeII.h:69
Moduli for one inner dimension k.
Definition SchemeII.h:101
std::vector< std::uint32_t > moduli
Definition SchemeII.h:105