|
| 1 | +#ifndef DEEPX_TF_MATMUL_HPP |
| 2 | +#define DEEPX_TF_MATMUL_HPP |
| 3 | + |
| 4 | +#include <cuda_fp16.h> |
| 5 | +#include <cuda_bf16.h> |
| 6 | + |
| 7 | +#include "deepx/tf/tf.hpp" |
| 8 | +#include "deepx/dtype.hpp" |
| 9 | +#include "deepx/dtype_cuda.hpp" |
| 10 | +#include "deepx/tensorfunc/matmul_cublas.hpp" |
| 11 | + |
| 12 | +namespace deepx::tf |
| 13 | +{ |
| 14 | + template <typename Author> |
| 15 | + class MatMul : public TF |
| 16 | + { |
| 17 | + public: |
| 18 | + MatMul(const vector<Param> &args, const vector<Param> &returns) |
| 19 | + { |
| 20 | + this->name = "matmul"; |
| 21 | + this->author = Author::name(); |
| 22 | + this->args = args; |
| 23 | + this->returns = returns; |
| 24 | + } |
| 25 | + |
| 26 | + MatMul(string text) |
| 27 | + { |
| 28 | + this->parse(text); |
| 29 | + this->author = Author::name(); |
| 30 | + if (this->name != "matmul") |
| 31 | + { |
| 32 | + throw std::runtime_error("Invalid name: " + this->name); |
| 33 | + } |
| 34 | + } |
| 35 | + string math_formula() const override |
| 36 | + { |
| 37 | + return "T3=T1 @ T2"; |
| 38 | + } |
| 39 | + shared_ptr<TF> clone() const override |
| 40 | + { |
| 41 | + return make_shared<MatMul<Author>>(*this); |
| 42 | + } |
| 43 | + int run(shared_ptr<MemBase> mem, string &error) override |
| 44 | + { |
| 45 | + Precision a_type = mem->gettensor(this->args[0].textvalue).get()->shape.dtype; |
| 46 | + Precision b_type = mem->gettensor(this->args[1].textvalue).get()->shape.dtype; |
| 47 | + Precision c_type = mem->gettensor(this->returns[0].textvalue).get()->shape.dtype; |
| 48 | + if (a_type != b_type || a_type != c_type) |
| 49 | + { |
| 50 | + error = "Type mismatch: " + precision_str(a_type) + " != " + precision_str(b_type) + " != " + precision_str(c_type); |
| 51 | + return 1; |
| 52 | + } |
| 53 | + switch (a_type) |
| 54 | + { |
| 55 | + case Precision::Float64: |
| 56 | + tensorfunc::matmul<Author, double>(*mem->gettensor<double>(this->args[0].textvalue), *mem->gettensor<double>(this->args[1].textvalue), *mem->gettensor<double>(this->returns[0].textvalue)); |
| 57 | + break; |
| 58 | + case Precision::Float32: |
| 59 | + tensorfunc::matmul<Author, float>(*mem->gettensor<float>(this->args[0].textvalue), *mem->gettensor<float>(this->args[1].textvalue), *mem->gettensor<float>(this->returns[0].textvalue)); |
| 60 | + break; |
| 61 | + case Precision::Float16: |
| 62 | + tensorfunc::matmul<Author, half>(*mem->gettensor<half>(this->args[0].textvalue), *mem->gettensor<half>(this->args[1].textvalue), *mem->gettensor<half>(this->returns[0].textvalue)); |
| 63 | + break; |
| 64 | + case Precision::BFloat16: |
| 65 | + tensorfunc::matmul<Author, nv_bfloat16>(*mem->gettensor<nv_bfloat16>(this->args[0].textvalue), *mem->gettensor<nv_bfloat16>(this->args[1].textvalue), *mem->gettensor<nv_bfloat16>(this->returns[0].textvalue)); |
| 66 | + break; |
| 67 | + case Precision::Int64: |
| 68 | + tensorfunc::matmul<Author, int64_t>(*mem->gettensor<int64_t>(this->args[0].textvalue), *mem->gettensor<int64_t>(this->args[1].textvalue), *mem->gettensor<int64_t>(this->returns[0].textvalue)); |
| 69 | + break; |
| 70 | + case Precision::Int32: |
| 71 | + tensorfunc::matmul<Author, int32_t>(*mem->gettensor<int32_t>(this->args[0].textvalue), *mem->gettensor<int32_t>(this->args[1].textvalue), *mem->gettensor<int32_t>(this->returns[0].textvalue)); |
| 72 | + break; |
| 73 | + case Precision::Int16: |
| 74 | + tensorfunc::matmul<Author, int16_t>(*mem->gettensor<int16_t>(this->args[0].textvalue), *mem->gettensor<int16_t>(this->args[1].textvalue), *mem->gettensor<int16_t>(this->returns[0].textvalue)); |
| 75 | + break; |
| 76 | + case Precision::Int8: |
| 77 | + tensorfunc::matmul<Author, int8_t>(*mem->gettensor<int8_t>(this->args[0].textvalue), *mem->gettensor<int8_t>(this->args[1].textvalue), *mem->gettensor<int8_t>(this->returns[0].textvalue)); |
| 78 | + break; |
| 79 | + default: |
| 80 | + error = "Unsupported dtype: " + precision_str(a_type); |
| 81 | + return 1; |
| 82 | + } |
| 83 | + return 0; |
| 84 | + } |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +#endif |
0 commit comments