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.

optimized_kernel.h 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_LITE_NNACL_OPTIMIZED_KERNEL_H_
  17. #define MINDSPORE_LITE_NNACL_OPTIMIZED_KERNEL_H_
  18. #ifndef _WIN32
  19. #include <dlfcn.h>
  20. #endif
  21. #ifdef __ANDROID__
  22. #include <asm/hwcap.h>
  23. #include "nnacl/nnacl_utils.h"
  24. #endif
  25. #include "src/common/log_adapter.h"
  26. #define OPTIMIZE_SHARED_LIBRARY_PATH "libmindspore-lite-optimize.so"
  27. #define FLOAT16_SHARED_LIBRARY_PATH "libmindspore-lite-fp16.so"
  28. class OptimizeModule {
  29. public:
  30. OptimizeModule() {
  31. bool support_optimize_ops = false;
  32. #ifdef ENABLE_ARM64
  33. int hwcap_type = 16;
  34. uint32_t hwcap = getHwCap(hwcap_type);
  35. if (hwcap & HWCAP_ASIMDDP) {
  36. MS_LOG(INFO) << "Hw cap support SMID Dot Product, hwcap: 0x" << hwcap;
  37. support_optimize_ops = true;
  38. } else {
  39. MS_LOG(INFO) << "Hw cap NOT support SIMD Dot Product, hwcap: 0x" << hwcap;
  40. }
  41. #endif
  42. if (support_optimize_ops == false) {
  43. return;
  44. }
  45. #ifdef ENABLE_ARM64
  46. optimized_op_handler_ = dlopen(OPTIMIZE_SHARED_LIBRARY_PATH, RTLD_LAZY);
  47. if (optimized_op_handler_ == nullptr) {
  48. MS_LOG(INFO) << "Open optimize shared library failed: " << dlerror();
  49. }
  50. #endif
  51. }
  52. ~OptimizeModule() = default;
  53. static OptimizeModule *GetInstance() {
  54. static OptimizeModule opt_module;
  55. return &opt_module;
  56. }
  57. void *optimized_op_handler_ = nullptr;
  58. };
  59. class Float16Module {
  60. public:
  61. Float16Module() {
  62. bool support_fp16 = false;
  63. #ifdef ENABLE_ARM64
  64. int hwcap_type = 16;
  65. uint32_t hwcap = getHwCap(hwcap_type);
  66. if (hwcap & HWCAP_FPHP) {
  67. MS_LOG(INFO) << "Hw cap support FP16, hwcap: 0x" << hwcap;
  68. support_fp16 = true;
  69. }
  70. #endif
  71. if (support_fp16 == false) {
  72. return;
  73. }
  74. #ifdef ENABLE_ARM64
  75. float16_op_handler_ = dlopen(FLOAT16_SHARED_LIBRARY_PATH, RTLD_LAZY);
  76. if (float16_op_handler_ == nullptr) {
  77. MS_LOG(INFO) << "Open optimize shared library failed: " << dlerror();
  78. }
  79. #endif
  80. }
  81. ~Float16Module() = default;
  82. static Float16Module *GetInstance() {
  83. static Float16Module fp16_module;
  84. return &fp16_module;
  85. }
  86. void *float16_op_handler_ = nullptr;
  87. };
  88. #endif // MINDSPORE_LITE_NNACL_OPTIMIZED_KERNEL_H_