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

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