Loading...
Searching...
No Matches
TrainingProduct.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#include "TrainingProduct.h"
4
5#include <dlfcn.h>
6
7#include <algorithm>
8#include <cstdio>
9#include <memory>
10#include <mutex>
11#include <stdexcept>
12
13#include "LinalgThreads.h"
14#include "TrainingProductApi.h"
15#include "schemeii/SchemeII.h"
16
17namespace gpr::linalg {
18namespace {
19struct ProductLibrary {
20 void* library = nullptr;
21 void* workspace = nullptr;
22 decltype(&gpr_training_product_destroy_v1) destroy = nullptr;
23 decltype(&gpr_training_product_gemm_v1) gemm = nullptr;
24 std::mutex execution;
25
26 ~ProductLibrary()
27 {
28 if (workspace) destroy(workspace);
29 if (library) dlclose(library);
30 }
31};
32std::mutex configuration;
33std::shared_ptr<ProductLibrary> configured;
34std::shared_ptr<const schemeii::Context> configured_scheme;
35
36std::shared_ptr<ProductLibrary> current()
37{
38 std::lock_guard lock(configuration);
39 return configured;
40}
41
42int linalgThreadSource()
43{
45}
46} // namespace
47
48std::shared_ptr<const schemeii::Context> schemeIIContext()
49{
50 std::lock_guard lock(configuration);
51 return configured_scheme;
52}
53
54void configureTrainingProductLibrary(const std::string& library)
55{
56 std::shared_ptr<ProductLibrary> replacement;
57 std::shared_ptr<schemeii::Context> scheme;
58 if (schemeii::isSpec(library)) {
59 // In-process Scheme II engine; no device library is loaded.
60 scheme = std::make_shared<schemeii::Context>(
61 schemeii::parseOptions(library));
62 scheme->setThreadSource(&linalgThreadSource);
63 std::fprintf(stderr, "[training-product] scheme2 %s\n",
64 scheme->describe(256).c_str());
65 } else if (!library.empty()) {
66 replacement = std::make_shared<ProductLibrary>();
67 auto& state = *replacement;
68 state.library = dlopen(library.c_str(), RTLD_NOW | RTLD_LOCAL);
69 if (!state.library) throw std::runtime_error(dlerror());
70 const auto create =
71 reinterpret_cast<decltype(&gpr_training_product_create_v1)>(
72 dlsym(state.library, "gpr_training_product_create_v1"));
73 state.destroy =
74 reinterpret_cast<decltype(&gpr_training_product_destroy_v1)>(
75 dlsym(state.library, "gpr_training_product_destroy_v1"));
76 state.gemm = reinterpret_cast<decltype(&gpr_training_product_gemm_v1)>(
77 dlsym(state.library, "gpr_training_product_gemm_v1"));
78 if (!create || !state.destroy || !state.gemm)
79 throw std::runtime_error("incompatible training product library");
80 char error[1024]{};
81 state.workspace = create(error, sizeof(error));
82 if (!state.workspace) throw std::runtime_error(error);
83 }
84 std::lock_guard lock(configuration);
85 configured = std::move(replacement);
86 configured_scheme = std::move(scheme);
87}
88
90{
91 std::lock_guard lock(configuration);
92 return configured != nullptr || configured_scheme != nullptr;
93}
94
96{
97 return current() != nullptr;
98}
99
101 ProductMatrix c, bool transpose_a, bool transpose_b,
102 double alpha, double beta, bool partitioned)
103{
104 const auto state = current();
105 const auto scheme = schemeIIContext();
106 if (!state && !scheme) return false;
107 const Eigen::Index m = transpose_a ? a.cols() : a.rows();
108 const Eigen::Index k = transpose_a ? a.rows() : a.cols();
109 const Eigen::Index n = transpose_b ? b.rows() : b.cols();
110 if (k != (transpose_b ? b.cols() : b.rows()) || c.rows() != m ||
111 c.cols() != n)
112 throw std::invalid_argument("invalid training product dimensions");
113 // Small matrices do not amortize device transfer and modular GEMM launch
114 // costs. This floor also keeps scalar and vector operations on the host.
115 if (std::min({m, n, k}) == 0 || (!partitioned && std::min({m, n, k}) < 128))
116 return false;
117 if (scheme) {
118 scheme->gemm(transpose_a, transpose_b, m, n, k, alpha, a.data(),
119 a.outerStride(), b.data(), b.outerStride(), beta, c.data(),
120 c.outerStride());
121 return true;
122 }
123 std::lock_guard lock(state->execution);
124 char error[1024]{};
125 if (state->gemm(state->workspace, transpose_a, transpose_b, m, n, k, alpha,
126 a.data(), a.outerStride(), b.data(), b.outerStride(), beta,
127 c.data(), c.outerStride(), error, sizeof(error)))
128 throw std::runtime_error(error);
129 return true;
130}
131
133 bool transpose_a, bool transpose_b)
134{
135 const Eigen::Index m = transpose_a ? a.cols() : a.rows();
136 const Eigen::Index k = transpose_a ? a.rows() : a.cols();
137 const Eigen::Index n = transpose_b ? b.rows() : b.cols();
138 if (k != (transpose_b ? b.cols() : b.rows()))
139 throw std::invalid_argument("invalid training product dimensions");
140 Eigen::MatrixXd c(m, n);
141 if (!tryTrainingProduct(a, b, c, transpose_a, transpose_b)) {
142 if (transpose_a && transpose_b)
143 c.noalias() = a.transpose() * b.transpose();
144 else if (transpose_a)
145 c.noalias() = a.transpose() * b;
146 else if (transpose_b)
147 c.noalias() = a * b.transpose();
148 else
149 c.noalias() = a * b;
150 }
151 return c;
152}
153} // namespace gpr::linalg
void * gpr_training_product_create_v1(char *error, size_t capacity)
int gpr_training_product_gemm_v1(void *workspace, int transpose_a, int transpose_b, int64_t m, int64_t n, int64_t k, double alpha, const double *a, int64_t lda, const double *b, int64_t ldb, double beta, double *c, int64_t ldc, char *error, size_t capacity)
void gpr_training_product_destroy_v1(void *workspace)
bool isSpec(std::string_view spec) noexcept
Definition SchemeII.cpp:455
Options parseOptions(std::string_view spec)
Definition SchemeII.cpp:460
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Eigen::Ref< const Eigen::MatrixXd, 0, Eigen::OuterStride<> > ConstProductMatrix
Eigen::MatrixXd trainingProduct(ConstProductMatrix a, ConstProductMatrix b, bool transpose_a, bool transpose_b)
Allocating form with a native Eigen product when no device product runs.
Eigen::Ref< Eigen::MatrixXd, 0, Eigen::OuterStride<> > ProductMatrix
bool trainingProductLibraryLoaded()
A device library (not an in-process engine) is configured.
void configureTrainingProductLibrary(const std::string &library)
bool tryTrainingProduct(ConstProductMatrix a, ConstProductMatrix b, ProductMatrix c, bool transpose_a, bool transpose_b, double alpha, double beta, bool partitioned)
int configuredLinalgThreads() noexcept
Requested dense numerical budget, independent of nested BLAS controls.
std::shared_ptr< const schemeii::Context > schemeIIContext()
bool trainingProductConfigured()
A device library or an in-process engine is configured.