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.7 kB

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