|
| 1 | +/* |
| 2 | + * (C) Copyright 1996- ECMWF. |
| 3 | + * |
| 4 | + * This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 | + * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 | + * In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 | + * granted to it by virtue of its status as an intergovernmental organisation |
| 8 | + * nor does it submit to any jurisdiction. |
| 9 | + */ |
| 10 | + |
| 11 | + |
| 12 | +#include "eckit/linalg/sparse/LinearAlgebraTorch.h" |
| 13 | + |
| 14 | +#include <cstring> |
| 15 | +#include <ostream> |
| 16 | + |
| 17 | +#include "torch/torch.h" |
| 18 | + |
| 19 | +#include "eckit/config/Resource.h" |
| 20 | +#include "eckit/exception/Exceptions.h" |
| 21 | +#include "eckit/linalg/Matrix.h" |
| 22 | +#include "eckit/linalg/SparseMatrix.h" |
| 23 | +#include "eckit/linalg/Vector.h" |
| 24 | +#include "eckit/linalg/sparse/LinearAlgebraGeneric.h" |
| 25 | + |
| 26 | + |
| 27 | +namespace eckit::linalg::sparse { |
| 28 | + |
| 29 | + |
| 30 | +static_assert(std::is_same<int32_t, Index>::value, "Index type mismatch"); |
| 31 | +static_assert(std::is_same<double, Scalar>::value, "Scalar type mismatch"); |
| 32 | + |
| 33 | +static const LinearAlgebraTorch __la_torch("torch"); |
| 34 | + |
| 35 | + |
| 36 | +namespace { |
| 37 | + |
| 38 | + |
| 39 | +torch::TensorOptions make_options(torch::ScalarType _dtype) { |
| 40 | + static const auto _device = [](const std::string& dev) { |
| 41 | + return dev == "cpu" ? torch::DeviceType::CPU |
| 42 | + : dev == "cuda" ? torch::DeviceType::CUDA |
| 43 | + : dev == "hip" ? torch::DeviceType::HIP |
| 44 | + : dev == "fpga" ? torch::DeviceType::FPGA |
| 45 | + : dev == "maia" ? torch::DeviceType::MAIA |
| 46 | + : dev == "xla" ? torch::DeviceType::XLA |
| 47 | + : dev == "mps" ? torch::DeviceType::MPS |
| 48 | + : dev == "meta" ? torch::DeviceType::Meta |
| 49 | + : dev == "vulkan" ? torch::DeviceType::Vulkan |
| 50 | + : dev == "metal" ? torch::DeviceType::Metal |
| 51 | + : dev == "xpu" ? torch::DeviceType::XPU |
| 52 | + : dev == "hpu" ? torch::DeviceType::HPU |
| 53 | + : dev == "ve" ? torch::DeviceType::VE |
| 54 | + : dev == "lazy" ? torch::DeviceType::Lazy |
| 55 | + : dev == "ipu" ? torch::DeviceType::IPU |
| 56 | + : dev == "mtia" ? torch::DeviceType::MTIA |
| 57 | + : NOTIMP; |
| 58 | + }(eckit::Resource<std::string>("$ECKIT_LINALG_TORCH_DEVICE;eckitLinalgTorchDevice;-eckitLinalgTorchDevice", "cpu")); |
| 59 | + |
| 60 | + return torch::TensorOptions().dtype(_dtype).device(_device); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +} // namespace |
| 65 | + |
| 66 | + |
| 67 | +void LinearAlgebraTorch::spmv(const SparseMatrix& A, const Vector& x, Vector& y) const { |
| 68 | + const auto options_int = make_options(torch::kInt32); |
| 69 | + const auto options_float = make_options(torch::kFloat64); |
| 70 | + |
| 71 | + const auto Ni = static_cast<int32_t>(A.rows()); |
| 72 | + const auto Nj = static_cast<int32_t>(A.cols()); |
| 73 | + const auto Nz = static_cast<int32_t>(A.nonZeros()); |
| 74 | + ASSERT(Ni == y.rows()); |
| 75 | + ASSERT(Nj == x.rows()); |
| 76 | + |
| 77 | + // torch tensors |
| 78 | + const auto ia = torch::from_blob(const_cast<int32_t*>(A.outer()), Ni + 1, options_int); |
| 79 | + const auto ja = torch::from_blob(const_cast<int32_t*>(A.inner()), Nz, options_int); |
| 80 | + const auto a = torch::from_blob(const_cast<double*>(A.data()), Nz, options_float); |
| 81 | + |
| 82 | + const auto A_tensor = torch::sparse_csr_tensor(ia, ja, a, {Ni, Nj}, options_float.layout(torch::kSparseCsr)); |
| 83 | + |
| 84 | + // multiplication |
| 85 | + const auto x_tensor = torch::from_blob(const_cast<double*>(x.data()), Nj, options_float); |
| 86 | + const auto y_tensor = torch::matmul(A_tensor, x_tensor); |
| 87 | + |
| 88 | + // assignment |
| 89 | + std::memcpy(y.data(), y_tensor.data_ptr<double>(), Ni * sizeof(double)); |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +void LinearAlgebraTorch::spmm(const SparseMatrix& A, const Matrix& X, Matrix& Y) const { |
| 94 | + const auto options_int = make_options(torch::kInt32); |
| 95 | + const auto options_float = make_options(torch::kFloat64); |
| 96 | + |
| 97 | + const auto Ni = static_cast<int32_t>(A.rows()); |
| 98 | + const auto Nj = static_cast<int32_t>(A.cols()); |
| 99 | + const auto Nk = static_cast<int32_t>(X.cols()); |
| 100 | + const auto Nz = static_cast<int32_t>(A.nonZeros()); |
| 101 | + ASSERT(Ni == Y.rows()); |
| 102 | + ASSERT(Nj == X.rows()); |
| 103 | + ASSERT(Nk == Y.cols()); |
| 104 | + |
| 105 | + // torch tensors |
| 106 | + const auto ia = torch::from_blob(const_cast<int32_t*>(A.outer()), Ni + 1, options_int); |
| 107 | + const auto ja = torch::from_blob(const_cast<int32_t*>(A.inner()), Nz, options_int); |
| 108 | + const auto a = torch::from_blob(const_cast<double*>(A.data()), Nz, options_float); |
| 109 | + |
| 110 | + const auto A_tensor = torch::sparse_csr_tensor(ia, ja, a, {Ni, Nj}, options_float.layout(torch::kSparseCsr)); |
| 111 | + |
| 112 | + // multiplication and conversion from column-major to row-major (and back) |
| 113 | + auto t = [](auto&& tensor) { return tensor.transpose(0, 1).contiguous(); }; |
| 114 | + |
| 115 | + const auto X_tensor = t(torch::from_blob(const_cast<double*>(X.data()), {Nk, Nj}, options_float)); |
| 116 | + const auto Y_tensor = t(torch::matmul(A_tensor, X_tensor)); |
| 117 | + |
| 118 | + // assignment |
| 119 | + std::memcpy(Y.data(), Y_tensor.data_ptr<double>(), Y.size() * sizeof(double)); |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +void LinearAlgebraTorch::dsptd(const Vector& x, const SparseMatrix& A, const Vector& y, SparseMatrix& B) const { |
| 124 | + static const sparse::LinearAlgebraGeneric generic; |
| 125 | + generic.dsptd(x, A, y, B); |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +void LinearAlgebraTorch::print(std::ostream& out) const { |
| 130 | + out << "LinearAlgebraTorch[]"; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +} // namespace eckit::linalg::sparse |
0 commit comments