Loading...
Searching...
No Matches
gpr::linalg::schemeii Namespace Reference

Classes

class  Context
class  Operand
struct  Options
struct  Plan
 Moduli for one inner dimension k. More...
struct  Task

Enumerations

enum class  Engine { Fp64 , Int8 }
enum class  Int8Provider {
  Auto , Builtin , Mkl , Onednn ,
  Cuda
}

Functions

std::int64_t engineChunk (Engine engine, std::int64_t k)
 Inner chunk length of one exact GEMM for an engine.
std::vector< std::uint32_t > engineModuli (Engine engine, std::int64_t chunk)
 Moduli of an engine for an inner chunk length, largest first.
int scaleExponent (double max_abs, int bits) noexcept
bool isSpec (std::string_view spec) noexcept
Options parseOptions (std::string_view spec)

Variables

constexpr int kDefaultBits = 56
constexpr int kMaxBits = 62

Enumeration Type Documentation

◆ Engine

enum class gpr::linalg::schemeii::Engine
strong
Enumerator
Fp64 
Int8 

Definition at line 54 of file SchemeII.h.

◆ Int8Provider

Function Documentation

◆ engineChunk()

std::int64_t gpr::linalg::schemeii::engineChunk ( Engine engine,
std::int64_t k )

Inner chunk length of one exact GEMM for an engine.

Definition at line 436 of file SchemeII.cpp.

437{
438 return std::max<std::int64_t>(
439 1, std::min(k, engine == Engine::Int8 ? kInt8Chunk : kFp64MaxChunk));
440}

◆ engineModuli()

std::vector< std::uint32_t > gpr::linalg::schemeii::engineModuli ( Engine engine,
std::int64_t chunk )

Moduli of an engine for an inner chunk length, largest first.

Definition at line 442 of file SchemeII.cpp.

443{
444 return engine == Engine::Int8 ? int8Moduli() : fp64Moduli(chunk);
445}

◆ isSpec()

bool gpr::linalg::schemeii::isSpec ( std::string_view spec)
noexcept

Definition at line 455 of file SchemeII.cpp.

456{
457 return spec.substr(0, 7) == "scheme2";
458}

◆ parseOptions()

Options gpr::linalg::schemeii::parseOptions ( std::string_view spec)

Parses "scheme2:<fp64|int8>[:bits=B][:moduli=N][:provider=P] [:library=PATH][:device=D][:device_min=W][:threads=T][:block_rows=R] [:block_cols=C]". provider=cuda multiplies on INT8 tensor cores through libgpr_scheme2_cuda.so (CudaEngine.h) and returns the bits of the CPU providers: residues are prepared on the host, every product and reconstruction of a run happens on the device. Throws std::invalid_argument.

Definition at line 460 of file SchemeII.cpp.

461{
462 if (!isSpec(spec))
463 throw std::invalid_argument("not a Scheme II specification");
464 Options options;
465 std::vector<std::string> parts;
466 std::size_t start = 0;
467 while (start <= spec.size()) {
468 const std::size_t end = std::min(spec.find(':', start), spec.size());
469 parts.emplace_back(spec.substr(start, end - start));
470 start = end + 1;
471 }
472 if (parts.front() != "scheme2")
473 throw std::invalid_argument(
474 "Scheme II specification must start with scheme2");
475 bool engine_seen = false;
476 const auto integer = [](const std::string& text, const char* what) {
477 std::size_t used = 0;
478 const long value = std::stol(text, &used);
479 if (used != text.size() || value < 0)
480 throw std::invalid_argument(std::string("invalid Scheme II ") +
481 what);
482 return value;
483 };
484 for (std::size_t i = 1; i < parts.size(); ++i) {
485 const std::string& part = parts[i];
486 const std::size_t eq = part.find('=');
487 if (eq == std::string::npos) {
488 if (part == "fp64")
489 options.engine = Engine::Fp64;
490 else if (part == "int8")
491 options.engine = Engine::Int8;
492 else
493 throw std::invalid_argument("unknown Scheme II engine " + part);
494 engine_seen = true;
495 continue;
496 }
497 const std::string key = part.substr(0, eq), value = part.substr(eq + 1);
498 if (key == "bits")
499 options.bits = static_cast<int>(integer(value, "bits"));
500 else if (key == "moduli")
501 options.moduli = static_cast<int>(integer(value, "moduli"));
502 else if (key == "threads")
503 options.threads = static_cast<int>(integer(value, "threads"));
504 else if (key == "library")
505 options.library = value;
506 else if (key == "device")
507 options.device = static_cast<int>(integer(value, "device"));
508 else if (key == "device_min")
509 options.device_min =
510 static_cast<double>(integer(value, "device_min"));
511 else if (key == "block_rows")
512 options.block_rows = integer(value, "block_rows");
513 else if (key == "block_cols")
514 options.block_cols = integer(value, "block_cols");
515 else if (key == "provider") {
516 if (value == "auto")
518 else if (value == "builtin")
520 else if (value == "mkl")
521 options.provider = Int8Provider::Mkl;
522 else if (value == "onednn")
524 else if (value == "cuda")
526 else
527 throw std::invalid_argument("unknown INT8 provider " + value);
528 } else
529 throw std::invalid_argument("unknown Scheme II option " + key);
530 }
531 if (!engine_seen && parts.size() > 1 &&
532 parts[1].find('=') != std::string::npos)
533 options.engine = Engine::Fp64;
534 if (options.bits != 0 && (options.bits < 2 || options.bits > kMaxBits))
535 throw std::invalid_argument("Scheme II bits must lie in [2, 62]");
536 if (options.moduli > kMaxModuli)
537 throw std::invalid_argument("too many Scheme II moduli");
538 if (options.block_rows < 1 || options.block_cols < 1)
539 throw std::invalid_argument("invalid Scheme II block size");
540 return options;
541}
bool isSpec(std::string_view spec) noexcept
Definition SchemeII.cpp:455
constexpr int kMaxBits
Definition SchemeII.h:58
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

◆ scaleExponent()

int gpr::linalg::schemeii::scaleExponent ( double max_abs,
int bits )
noexcept

s with max_abs * 2^s < 2^(bits-1) <= 2 max_abs * 2^s; zero for zero. Computed from a global maximum it is the exponent to supply when the rows or columns of one product are split across calls along k.

Definition at line 447 of file SchemeII.cpp.

448{
449 if (!(max_abs > 0.0) || !std::isfinite(max_abs)) return 0;
450 // max_abs in [2^(E-1), 2^E); then max_abs 2^(bits-1-E) < 2^(bits-1).
451 const int e = std::ilogb(max_abs) + 1;
452 return bits - 1 - e;
453}

Variable Documentation

◆ kDefaultBits

int gpr::linalg::schemeii::kDefaultBits = 56
inlineconstexpr

Definition at line 57 of file SchemeII.h.

◆ kMaxBits

int gpr::linalg::schemeii::kMaxBits = 62
inlineconstexpr

Definition at line 58 of file SchemeII.h.