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.

aware_quantizer.h 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Copyright 2019 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 MS_AWARE_QUANTIZER_H
  17. #define MS_AWARE_QUANTIZER_H
  18. #include <array>
  19. #include <string>
  20. #include <memory>
  21. #include "tools/converter/quantizer/quantizer.h"
  22. #include "schema/inner/model_generated.h"
  23. #include "include/errorcode.h"
  24. #include "tools/converter/quantizer/quantize_util.h"
  25. namespace mindspore::lite::quant {
  26. struct InputArray {
  27. std::unique_ptr<schema::QuantParamT> quantParam;
  28. float mMin = 0.0f;
  29. float mMax = 0.0f;
  30. bool narrowRange = false;
  31. int numBits = 8;
  32. TypeId dataType = TypeId::kTypeUnknown;
  33. InputArray(float mean, float stdDev, TypeId dataType = TypeId::kNumberTypeFloat) {
  34. this->dataType = dataType;
  35. constexpr float qmin = -128;
  36. constexpr float qmax = 127;
  37. mMin = (qmin - mean) / stdDev;
  38. mMax = (qmax - mean) / stdDev;
  39. }
  40. STATUS InitQuantParam();
  41. STATUS SetInputArrayQP(schema::MetaGraphT *graph, size_t inputTensorIdx);
  42. };
  43. class AwareQuantizer : public FbQuantizer {
  44. public:
  45. AwareQuantizer(schema::MetaGraphT *graph, const TypeId &inferType, const std::string &stdValues,
  46. const std::string &meanValues);
  47. ~AwareQuantizer() { delete (mInputArray); }
  48. STATUS RemoveFakeQuant() override;
  49. STATUS GenerateQuantParam() override;
  50. STATUS DetermineNodeQuantType() override;
  51. STATUS DoQuantize() override; // override;
  52. private:
  53. // RemoveFakeQuant
  54. STATUS SetAttrToConvolution(const schema::MetaGraphT *subGraph, schema::CNodeT *node);
  55. STATUS GenerateDefaultQuantParam(const schema::MetaGraphT *subGraph);
  56. STATUS QuantAddConstTensor(const schema::MetaGraphT *graph, schema::CNodeT *node);
  57. STATUS QuantDetectionPostProcessConstTensor(const schema::MetaGraphT *subGraph, schema::CNodeT *node);
  58. STATUS QuantConvBias(const schema::MetaGraphT *graph, schema::CNodeT *node);
  59. STATUS QuantConvWeight(const schema::MetaGraphT *subGraph, schema::CNodeT *node);
  60. float inputScale = 0.0f;
  61. InputArray *mInputArray;
  62. static const std::array<schema::PrimitiveType, 7> propagatedOps;
  63. };
  64. } // namespace mindspore::lite::quant
  65. #endif