You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

matrix_mul.h 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * \file dnn/test/common/matrix_mul.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #pragma once
  12. #include <cstddef>
  13. #include <vector>
  14. #include "megdnn/dtype.h"
  15. #include "megdnn/handle.h"
  16. #include "megdnn/opr_param_defs.h"
  17. #include "megdnn/oprs.h"
  18. #include "test/common/checker.h"
  19. namespace megdnn {
  20. namespace test {
  21. namespace matrix_mul {
  22. // mask & 1 denotes transposeA; mask & 2 denotes transposeB
  23. struct TestArg {
  24. constexpr static size_t UNSET_STRIDE_VAL = static_cast<size_t>(-1);
  25. size_t m, n, k, mask;
  26. size_t A_stride, B_stride, C_stride, b;
  27. size_t A_batch_stride, B_batch_stride, C_batch_stride;
  28. // stride = 0 means the default stride, the dim is contiguous, i.e. the
  29. // stride value which makes tensor compact.
  30. TestArg(size_t m, size_t n, size_t k, size_t mask,
  31. size_t A_stride = UNSET_STRIDE_VAL, size_t B_stride = UNSET_STRIDE_VAL,
  32. size_t C_stride = UNSET_STRIDE_VAL, size_t b = 1,
  33. size_t A_batch_stride = UNSET_STRIDE_VAL,
  34. size_t B_batch_stride = UNSET_STRIDE_VAL,
  35. size_t C_batch_stride = UNSET_STRIDE_VAL)
  36. : m{m},
  37. n{n},
  38. k{k},
  39. mask{mask},
  40. A_stride{A_stride},
  41. B_stride{B_stride},
  42. C_stride{C_stride},
  43. b{b},
  44. A_batch_stride{A_batch_stride},
  45. B_batch_stride{B_batch_stride},
  46. C_batch_stride{C_batch_stride} {}
  47. };
  48. std::vector<TestArg> get_matmul_args_no_mask();
  49. std::vector<TestArg> get_matmul_args_mask(uint8_t mask);
  50. std::vector<TestArg> get_matmul_args();
  51. std::vector<TestArg> get_matmul_args_split_k();
  52. std::vector<TestArg> get_batched_matmul_args_mask(uint8_t mask);
  53. std::vector<TestArg> get_batched_matmul_args();
  54. std::vector<TestArg> get_batched_matmul_broadcast_args();
  55. std::vector<TestArg> get_batched_matmul_broadcast_args_mask(uint8_t mask);
  56. std::vector<TestArg> get_matmul_mk_packed_args(size_t nbase);
  57. std::vector<TestArg> get_batched_matmul_args_cublaslt();
  58. std::vector<TestArg> get_batched_matmul_args_int8x8x32();
  59. using TestArgFilterFunc = std::function<bool(const TestArg&)>;
  60. template <typename Opr = megdnn::MatrixMul>
  61. void check_matrix_mul(
  62. DType A_dtype, DType B_dtype, DType C_dtype, Handle* handle,
  63. const ExecutionPolicyAlgoName& algo = {"", {}},
  64. param::MatrixMul::Format format = param::MatrixMul::Format::DEFAULT,
  65. size_t nbase = 8, float eps = 1e-3, std::vector<TestArg>&& args = {},
  66. bool force_deduce_dst = true,
  67. param::MatrixMul::ComputeMode compute_mode =
  68. param::MatrixMul::ComputeMode::DEFAULT);
  69. void check_matrix_mul(
  70. DType A_dtype, DType B_dtype, DType C_dtype, Handle* handle,
  71. const ExecutionPolicyAlgoName& algo = {"", {}},
  72. param::MatrixMul::Format format = param::MatrixMul::Format::DEFAULT,
  73. size_t nbase = 8, float eps = 1e-3, bool force_deduce_dst = true);
  74. void check_batched_matrix_mul(
  75. DType A_dtype, DType B_dtype, DType C_dtype, Handle* handle,
  76. const ExecutionPolicyAlgoName& algo = {"", {}}, float eps = 1e-3,
  77. std::vector<TestArg>&& args = {}, bool force_deduce_dst = true);
  78. #if MEGDNN_WITH_BENCHMARK
  79. std::vector<TestArg> get_benchmark_matmul_args();
  80. std::vector<TestArg> get_benchmark_matmul_mk_packed_args(size_t nbase);
  81. //! benchmark performance with float matmul
  82. void benchmark_with_contrast(
  83. Handle* handle, const std::vector<TestArg>& args, DType A_dtype, DType B_dtype,
  84. DType C_dtype, const char* algo = nullptr,
  85. param::MatrixMul::Format format = param::MatrixMul::Format::DEFAULT,
  86. DType contrast_A_dtype = dtype::Float32{},
  87. DType contrast_B_dtype = dtype::Float32{},
  88. DType contrast_C_dtype = dtype::Float32{}, const char* contrast_algo = nullptr,
  89. param::MatrixMul::Format contrast_format = param::MatrixMul::Format::DEFAULT);
  90. #endif
  91. } // namespace matrix_mul
  92. } // namespace test
  93. } // namespace megdnn
  94. // vim: syntax=cpp.doxygen