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.

op_imp.cpp 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <cstdint>
  17. #include <functional>
  18. #include <algorithm>
  19. #include <vector>
  20. #include "debug/ge_log.h"
  21. #include "debug/ge_util.h"
  22. using namespace std;
  23. namespace ge {
  24. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus
  25. BroadCastInfer(const function<vector<int64_t>()>& get_in1_shape, const function<vector<int64_t>()>& get_in2_shape,
  26. const function<void(const vector<int64_t>& outShape)>& set_out_shape) {
  27. auto x1_shape = get_in1_shape();
  28. auto x2_shape = get_in2_shape();
  29. vector<int64_t> y_shape;
  30. if (x1_shape.empty()) {
  31. y_shape = x2_shape;
  32. set_out_shape(y_shape);
  33. return GRAPH_SUCCESS;
  34. }
  35. if (x2_shape.empty()) {
  36. y_shape = x1_shape;
  37. set_out_shape(y_shape);
  38. return GRAPH_SUCCESS;
  39. }
  40. int len_diff = static_cast<int>(x1_shape.size() - x2_shape.size());
  41. if (len_diff >= 0) {
  42. for (int i = 0; i < len_diff; i++) {
  43. y_shape.push_back(x1_shape[i]);
  44. }
  45. int x2_shape_size = static_cast<int>(x2_shape.size());
  46. for (int i = 0; i < x2_shape_size; i++) {
  47. bool shapeFlag =
  48. ((x1_shape[i + len_diff] != x2_shape[i]) && (std::min(x1_shape[i + len_diff], x2_shape[i]) != 1));
  49. if (shapeFlag) {
  50. GE_LOGE("operands could not be broadcast together");
  51. return GRAPH_FAILED;
  52. }
  53. y_shape.push_back(std::max(x1_shape[i + len_diff], x2_shape[i]));
  54. }
  55. } else {
  56. for (int i = 0; i < -len_diff; i++) {
  57. y_shape.push_back(x2_shape[i]);
  58. }
  59. int x1_shape_size = static_cast<int>(x1_shape.size());
  60. for (int i = 0; i < x1_shape_size; i++) {
  61. bool shapeFlag =
  62. ((x1_shape[i] != x2_shape[i - len_diff]) && (std::min(x1_shape[i], x2_shape[i - len_diff]) != 1));
  63. if (shapeFlag) {
  64. GE_LOGE("operands could not be broadcast together");
  65. return GRAPH_FAILED;
  66. }
  67. y_shape.push_back(std::max(x1_shape[i], x2_shape[i - len_diff]));
  68. }
  69. }
  70. set_out_shape(y_shape);
  71. return GRAPH_SUCCESS;
  72. }
  73. } // namespace ge

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