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.

kernel.h 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright 2021 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 MINDSPORE_INCLUDE_API_KERNEL_H
  17. #define MINDSPORE_INCLUDE_API_KERNEL_H
  18. #include <vector>
  19. #include <string>
  20. #include <utility>
  21. #include "schema/model_generated.h"
  22. #include "include/api/types.h"
  23. #include "include/api/context.h"
  24. namespace mindspore::kernel {
  25. class Kernel {
  26. public:
  27. Kernel() = default;
  28. Kernel(const std::vector<mindspore::MSTensor> &inputs, const std::vector<mindspore::MSTensor> &outputs,
  29. const schema::Primitive *primitive, const mindspore::Context *ctx)
  30. : context_(ctx), inputs_(std::move(inputs)), outputs_(std::move(outputs)), primitive_(primitive) {
  31. if (primitive != nullptr) {
  32. type_ = primitive->value_type();
  33. }
  34. }
  35. virtual ~Kernel() = default;
  36. virtual int Prepare() = 0;
  37. virtual int Execute() = 0;
  38. virtual int ReSize() = 0;
  39. virtual void set_inputs(const std::vector<mindspore::MSTensor> &in_tensors) { this->inputs_ = in_tensors; }
  40. virtual void set_input(mindspore::MSTensor in_tensor, int index) { this->inputs_[index] = in_tensor; }
  41. virtual void set_outputs(const std::vector<mindspore::MSTensor> &out_tensors) { this->outputs_ = out_tensors; }
  42. virtual void set_output(mindspore::MSTensor out_tensor, int index) { this->outputs_[index] = out_tensor; }
  43. virtual const std::vector<mindspore::MSTensor> &inputs() { return this->inputs_; }
  44. virtual const std::vector<mindspore::MSTensor> &outputs() { return this->outputs_; }
  45. std::string name() const { return this->name_; }
  46. void set_name(const std::string &name) { this->name_ = name; }
  47. const mindspore::Context *context() const { return this->context_; }
  48. virtual schema::PrimitiveType type() const { return type_; }
  49. const schema::Primitive *primitive() const { return this->primitive_; }
  50. protected:
  51. std::string name_;
  52. const mindspore::Context *context_ = nullptr;
  53. std::vector<mindspore::MSTensor> inputs_;
  54. std::vector<mindspore::MSTensor> outputs_;
  55. schema::PrimitiveType type_ = schema::PrimitiveType_NONE;
  56. const schema::Primitive *primitive_ = nullptr;
  57. };
  58. } // namespace mindspore::kernel
  59. #endif // MINDSPORE_INCLUDE_API_KERNEL_H