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.

tensor_util.cc 3.6 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include <cfloat>
  17. #include "src/common/utils.h"
  18. #include "tools/common/tensor_util.h"
  19. #include "tools/common/graph_util.h"
  20. namespace mindspore::lite {
  21. std::unique_ptr<QuantParamT> GetTensorQuantParam(const std::unique_ptr<TensorT> &tensor) {
  22. MS_ASSERT(tensor != nullptr);
  23. auto &quantParams = tensor->quantParams;
  24. if (!quantParams.empty()) {
  25. return CopyQuantParamT(quantParams.front());
  26. } else {
  27. return nullptr;
  28. }
  29. }
  30. std::unique_ptr<schema::QuantParamT> CopyQuantParamT(const std::unique_ptr<schema::QuantParamT> &srcQuantParam) {
  31. MS_ASSERT(srcQuantParam != nullptr);
  32. std::unique_ptr<schema::QuantParamT> dstQuantParam = std::make_unique<schema::QuantParamT>();
  33. dstQuantParam->inited = srcQuantParam->inited;
  34. dstQuantParam->scale = srcQuantParam->scale;
  35. dstQuantParam->zeroPoint = srcQuantParam->zeroPoint;
  36. dstQuantParam->min = srcQuantParam->min;
  37. dstQuantParam->max = srcQuantParam->max;
  38. dstQuantParam->narrowRange = srcQuantParam->narrowRange;
  39. dstQuantParam->numBits = srcQuantParam->numBits;
  40. return dstQuantParam;
  41. }
  42. size_t GetElementSize(const TensorT &tensor) { return GetElementSize(TypeId(tensor.dataType)); }
  43. size_t GetElementSize(const TypeId &dataType) {
  44. switch (dataType) {
  45. case kNumberTypeUInt8:
  46. return sizeof(uint8_t);
  47. case kNumberTypeInt32:
  48. return sizeof(int32_t);
  49. case kNumberTypeFloat:
  50. return sizeof(float);
  51. case kNumberTypeInt16:
  52. return sizeof(int16_t);
  53. case kNumberTypeInt8:
  54. return sizeof(int8_t);
  55. case kNumberTypeUInt32:
  56. return sizeof(uint32_t);
  57. default:
  58. return sizeof(float);
  59. }
  60. }
  61. size_t GetShapeSize(const TensorT &tensor) {
  62. auto shape = tensor.dims;
  63. size_t shapeSize = 1;
  64. for (auto dim : shape) {
  65. shapeSize *= dim;
  66. }
  67. return shapeSize;
  68. }
  69. std::unique_ptr<TensorT> CopyTensorDefT(const std::unique_ptr<TensorT> &oldTensor) {
  70. auto newTensor = std::unique_ptr<TensorT>(new (std::nothrow) TensorT);
  71. if (newTensor == nullptr) {
  72. MS_LOG(ERROR) << "new TensorT failed";
  73. return nullptr;
  74. }
  75. newTensor->dims = oldTensor->dims;
  76. newTensor->format = oldTensor->format;
  77. newTensor->dataType = oldTensor->dataType;
  78. newTensor->refCount = oldTensor->refCount;
  79. newTensor->nodeType = oldTensor->nodeType;
  80. newTensor->data = oldTensor->data;
  81. if (!oldTensor->quantParams.empty()) {
  82. newTensor->quantParams.emplace_back(GetTensorQuantParam(oldTensor));
  83. }
  84. return newTensor;
  85. }
  86. size_t GetRefCount(MetaGraphT *graphT, uint32_t tensorIdx) {
  87. MS_ASSERT(graphT != nullptr);
  88. MS_ASSERT(graphT->allTensors.size() > tensorIdx);
  89. size_t refCount = 0;
  90. for (auto &node : graphT->nodes) {
  91. MS_ASSERT(node != nullptr);
  92. if (IsContain(node->inputIndex, tensorIdx)) {
  93. refCount++;
  94. }
  95. }
  96. return refCount;
  97. }
  98. size_t GetShapeSize(const std::vector<int32_t> &shape) {
  99. size_t shapeSize = 1;
  100. for (auto dim : shape) {
  101. shapeSize *= dim;
  102. }
  103. return shapeSize;
  104. }
  105. } // namespace mindspore::lite