Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
linalg/ops/gemm.hpp
Go to the documentation of this file.
1#pragma once
2
17#if UNI20_HAS_MPLAPACK_FLOAT80
19#endif
20
21#if UNI20_BACKEND_BLAS || UNI20_HAS_MPLAPACK_MPREAL
23#endif
24
25#if UNI20_BACKEND_CUBLAS
27#endif
28
29#include <utility>
30
31namespace uni20::linalg
32{
46template <class BackendSelector, uni20::MutableRankedTensorView<2> OutputTensor, class Alpha, class Beta,
47 uni20::RankedTensorView<2> LhsTensor, uni20::RankedTensorView<2> RhsTensor>
48 requires(OperationScalar<Alpha, tensor_element_t<OutputTensor>> &&
49 OperationScalar<Beta, tensor_element_t<OutputTensor>>)
50void gemm(BackendSelector&& selector, OutputTensor&& output, Alpha alpha, LhsTensor const& lhs, RhsTensor const& rhs,
51 Beta beta, std::optional<Precision> requested = {})
52{
54 auto output_span = uni20::mdspec_of(output);
55 auto lhs_span = uni20::mdspec_of(lhs);
56 auto rhs_span = uni20::mdspec_of(rhs);
57 auto p = requested ? *requested : common_default_precision(lhs, rhs);
58 scalar_type const beta_scalar(beta);
59 if (!requested && beta_scalar != scalar_type{}) p = common_precision(p, precision_of(output));
60 detail::dispatch_at_precision<scalar_type>(std::forward<BackendSelector>(selector), gemm_op{}, p, output_span,
61 scalar_type(alpha), lhs_span, rhs_span, beta_scalar);
62 uni20::detail::record_output_precision(output, p);
63}
64
66template <uni20::MutableRankedTensorView<2> OutputTensor, class Alpha, class Beta, uni20::RankedTensorView<2> LhsTensor,
67 uni20::RankedTensorView<2> RhsTensor>
68 requires(OperationScalar<Alpha, tensor_element_t<OutputTensor>> &&
69 OperationScalar<Beta, tensor_element_t<OutputTensor>>)
70void gemm(OutputTensor&& output, Alpha alpha, LhsTensor const& lhs, RhsTensor const& rhs, Beta beta,
71 std::optional<Precision> p = {})
72{
73 auto selector = select_backend(gemm_op{}, output, lhs, rhs);
74 gemm(selector, std::forward<OutputTensor>(output), alpha, lhs, rhs, beta, p);
75}
76
77} // namespace uni20::linalg
Operation-tag backend dispatch over tensor-view operands.
BLAS backend adapter for operation-tag GEMM dispatch.
Reference CPU GEMM backend for tensor-view operands.
cuBLAS backend adapter for provider-ready GEMM dispatch.
Definition linalg/async.hpp:28
void gemm(BackendSelector selector, async::Async< OutputTensor > &output, Alpha &&alpha, async::Async< LhsTensor > const &lhs, async::Async< RhsTensor > const &rhs, Beta &&beta, std::optional< Precision > p={})
Schedule fixed-output GEMM with an explicit backend selector.
Definition async/matrix_product.hpp:162
constexpr auto select_backend(Operation const &operation, FirstTensor const &, RestTensors const &...)
Select the default backend list for an operation on Tensor operands.
Definition backend_selector.hpp:252
Definition data_table.cpp:8
constexpr Precision precision_of(T const &value)
Read scalar precision without changing any scalar or provider state.
Definition runtime_precision.hpp:53
constexpr Precision common_precision(Precision a, Precision b)
Combine precisions, treating exact values as neutral and rejecting unequal finite precisions.
Definition core/precision.hpp:57
constexpr Precision common_default_precision(A const &a, B const &b)
Select a binary operation's default without inspecting element precisions.
Definition tensor/precision.hpp:43
constexpr decltype(auto) mdspec_of(Tensor &tensor) noexcept(noexcept(tensor.mdspec()))
Return a tensor's immediate or descriptor-backed multidimensional metadata.
Definition tensor/concepts.hpp:50
std::remove_cv_t< typename tensor_mdspec_t< T >::element_type > tensor_element_t
Scalar value type exposed by a tensor-level object.
Definition tensor/concepts.hpp:126
mpreal beta(mpreal const &a, mpreal const &x)
Real Beta function; positive rational/integer pairs retain exact values.
Definition mpreal_special.hpp:164
Numeric coefficient sources shared by synchronous and asynchronous operations.
Backend-independent operation values used with normalized kernel operands.
Trait to extract the scalar type from a type T.
Definition scalar_traits.hpp:93
Concepts for tensor-level objects that resolve mdspan kernel operands.
Shape preparation contracts for tensor operation outputs.