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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright 2019-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 INC_EXTERNAL_GRAPH_TENSOR_H_
  17. #define INC_EXTERNAL_GRAPH_TENSOR_H_
  18. #include <atomic>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include <utility>
  23. #include "./ge_error_codes.h"
  24. #include "./types.h"
  25. namespace ge {
  26. class ShapeImpl;
  27. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Shape {
  28. public:
  29. Shape();
  30. ~Shape() = default;
  31. explicit Shape(const std::vector<int64_t> &dims);
  32. size_t GetDimNum() const;
  33. // If the idx is invalid, return 0
  34. int64_t GetDim(size_t idx) const;
  35. graphStatus SetDim(size_t idx, int64_t value);
  36. std::vector<int64_t> GetDims() const;
  37. int64_t GetShapeSize() const;
  38. private:
  39. std::shared_ptr<ShapeImpl> impl_;
  40. };
  41. class TensorDescImpl;
  42. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY TensorDesc {
  43. public:
  44. TensorDesc();
  45. ~TensorDesc() = default;
  46. explicit TensorDesc(Shape shape, Format format = FORMAT_ND, DataType dt = DT_FLOAT);
  47. // Copy
  48. TensorDesc(const TensorDesc &desc);
  49. // Move
  50. TensorDesc(TensorDesc &&desc);
  51. // Copy
  52. TensorDesc &operator=(const TensorDesc &desc);
  53. // Move
  54. TensorDesc &operator=(TensorDesc &&desc);
  55. void Update(const Shape &shape, Format format = FORMAT_ND, DataType dt = DT_FLOAT);
  56. Shape GetShape() const;
  57. void SetShape(const Shape &shape);
  58. // set shape with -2, it stand for unknown shape
  59. graphStatus SetUnknownDimNumShape();
  60. // for unknown shape
  61. graphStatus SetShapeRange(const std::vector<std::pair<int64_t, int64_t>> &range);
  62. graphStatus GetShapeRange(std::vector<std::pair<int64_t, int64_t>> &range) const;
  63. Format GetFormat() const;
  64. void SetFormat(Format format);
  65. Shape GetOriginShape() const;
  66. void SetOriginShape(const Shape &originShape);
  67. Format GetOriginFormat() const;
  68. void SetOriginFormat(Format originFormat);
  69. DataType GetDataType() const;
  70. void SetDataType(DataType dt);
  71. std::string GetName() const;
  72. void SetName(const std::string &name);
  73. // Attr acess
  74. void SetSize(int64_t size);
  75. int64_t GetSize() const;
  76. int64_t GetRealDimCnt() const;
  77. void SetRealDimCnt(const int64_t realDimCnt);
  78. private:
  79. std::shared_ptr<TensorDescImpl> impl;
  80. };
  81. class TensorImpl;
  82. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Tensor {
  83. public:
  84. Tensor();
  85. ~Tensor() = default;
  86. explicit Tensor(const TensorDesc &tensorDesc);
  87. Tensor(const TensorDesc &tensorDesc, const std::vector<uint8_t> &data);
  88. Tensor(const TensorDesc &tensorDesc, const uint8_t *data, size_t size);
  89. Tensor(TensorDesc &&tensorDesc, std::vector<uint8_t> &&data);
  90. TensorDesc GetTensorDesc() const;
  91. graphStatus SetTensorDesc(const TensorDesc &tensorDesc);
  92. const uint8_t *GetData() const;
  93. uint8_t *GetData();
  94. size_t GetSize() const;
  95. graphStatus SetData(std::vector<uint8_t> &&data);
  96. graphStatus SetData(const std::vector<uint8_t> &data);
  97. graphStatus SetData(const uint8_t *data, size_t size);
  98. graphStatus SetData(const std::string &data);
  99. graphStatus SetData(const std::vector<std::string> &data);
  100. graphStatus IsValid();
  101. Tensor Clone() const;
  102. private:
  103. std::shared_ptr<TensorImpl> impl;
  104. friend class TensorAdapter;
  105. };
  106. } // namespace ge
  107. #endif // INC_EXTERNAL_GRAPH_TENSOR_H_

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示