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.

expr_builder.h 3.5 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifndef UT_BASE_EXPR_BUILDER_H_
  17. #define UT_BASE_EXPR_BUILDER_H_
  18. #include <string>
  19. #include <vector>
  20. #include "tvm/expr.h"
  21. #include "tvm/operation.h"
  22. #include "tvm/tensor.h"
  23. namespace akg {
  24. class UTExprBuilder {
  25. public:
  26. UTExprBuilder() = default;
  27. ~UTExprBuilder() = default;
  28. static air::Expr IntImm(int64_t value, air::DataType dtype = air::Int(32));
  29. static air::Expr UIntImm(uint64_t value, air::DataType dtype = air::UInt(32));
  30. static air::Expr BoolImm(bool value);
  31. static air::Array<air::Expr> CreateShape(const std::vector<int32_t> &shapes);
  32. static air::Var CreateVar(const std::string &name);
  33. static air::Array<air::Expr> CreateVars(const std::vector<std::string> &names);
  34. static air::Range CreateRange(int32_t min, int32_t max);
  35. static air::Region CreateRegion(const std::vector<int32_t> &shapes);
  36. static air::Region CreateRegion(const air::Array<air::Expr> &shapes);
  37. static air::Operation PlaceholderOpNode(
  38. const std::string &name,
  39. const std::vector<int32_t> &shapes,
  40. air::DataType dtype = air::Float(16));
  41. static air::Expr TensorElement(
  42. const std::string &name,
  43. const std::vector<int32_t> &shapes,
  44. const air::Array<air::Expr> &axis_vars,
  45. air::DataType dtype = air::Float(16));
  46. static air::Expr ElementOf(
  47. const air::Operation &op,
  48. const air::Array<air::Expr> &axis_vars);
  49. static air::Expr ElementOfPlaceholderOp(
  50. const air::Operation &op,
  51. const air::Array<air::Expr> &axis_vars);
  52. static air::Expr CreateCall(
  53. const air::ir::FunctionRef func,
  54. air::Array<air::Expr> args,
  55. air::ir::Call::CallType call_type = air::ir::Call::Halide,
  56. int value_index = 0);
  57. static air::Tensor CreateTensorByPlaceholder(const air::Operation op);
  58. }; // UTExprBuilder
  59. class UTVariablePool {
  60. public:
  61. UTVariablePool() = default;
  62. ~UTVariablePool() = default;
  63. void AddVar(const std::string &name);
  64. void AddVars(const std::vector<std::string> &names);
  65. air::Var GetVar(const std::string &name) const;
  66. air::Array<air::Expr> GetVars(const std::vector<std::string> &names) const;
  67. void Reset();
  68. private:
  69. std::map<std::string, air::Var> map_name_var_;
  70. }; // class UTVariablePool
  71. class UTTensorElementHelper {
  72. public:
  73. UTTensorElementHelper(const std::vector<int32_t> &shapes,
  74. const std::string &axis_name_prefix = "ax");
  75. ~UTTensorElementHelper() = default;
  76. air::Expr Elem(const std::string &name,
  77. uint32_t dim,
  78. air::DataType dtype = air::Float(16)) const;
  79. const UTVariablePool &GetVarPool() const {
  80. return var_pool_;
  81. }
  82. private:
  83. std::vector<int32_t> shapes_;
  84. std::string axis_name_prefix_;
  85. std::vector<std::string> axis_names_;
  86. UTVariablePool var_pool_;
  87. }; // class UTTensorElementHelper
  88. } // namespace akg
  89. #endif // UT_BASE_EXPR_BUILDER_H_