mindspore/include/api/kernel.h

116 lines
4.3 KiB
C
Raw Normal View History

2021-04-17 10:40:06 +08:00
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_INCLUDE_API_KERNEL_H
#define MINDSPORE_INCLUDE_API_KERNEL_H
2021-04-17 10:40:06 +08:00
#include <vector>
#include <string>
#include <utility>
#include "schema/model_generated.h"
#include "include/api/types.h"
#include "include/api/context.h"
2021-04-17 10:40:06 +08:00
namespace mindspore::kernel {
2021-08-10 15:08:17 +08:00
/// \brief The Kernel class is used to define a MindSpore Kernel.
2021-04-17 10:40:06 +08:00
class Kernel {
public:
Kernel() = default;
2021-08-10 15:08:17 +08:00
/// \brief Constructor.
///
/// \param[in] inputs define the input tensors for kernel.
/// \param[in] outputs define the output tensors for kernel.
/// \param[in] primitive define the primitive of kernel generated by flatbuffers.
/// \param[in] ctx define the context for kernel.
Kernel(const std::vector<mindspore::MSTensor> &inputs, const std::vector<mindspore::MSTensor> &outputs,
const schema::Primitive *primitive, const mindspore::Context *ctx)
2021-07-10 10:35:19 +08:00
: context_(ctx), inputs_(std::move(inputs)), outputs_(std::move(outputs)), primitive_(primitive) {
2021-04-17 10:40:06 +08:00
if (primitive != nullptr) {
type_ = primitive->value_type();
}
}
2021-08-10 15:08:17 +08:00
/// \brief Destructor.
2021-04-17 10:40:06 +08:00
virtual ~Kernel() = default;
2021-08-10 15:08:17 +08:00
/// \brief prepare for executing kernel.
///
/// \return result code.
2021-04-17 10:40:06 +08:00
virtual int Prepare() = 0;
2021-08-10 15:08:17 +08:00
/// \brief execute the kernel.
///
/// \return result code.
2021-04-17 10:40:06 +08:00
virtual int Execute() = 0;
2021-08-10 15:08:17 +08:00
/// \brief resize the kernel input shape, memory need to refresh.
///
/// \return result code.
2021-04-17 10:40:06 +08:00
virtual int ReSize() = 0;
2021-08-10 15:08:17 +08:00
/// \brief set kernel's input tensors.
///
/// \param[in] in_tensors define the input tensors.
virtual void set_inputs(const std::vector<mindspore::MSTensor> &in_tensors) { this->inputs_ = in_tensors; }
2021-08-10 15:08:17 +08:00
/// \brief set kernel's input tensor.
///
/// \param[in] in_tensor define the input tensor.
/// \param[in] index define the index of the input tensor.
virtual void set_input(mindspore::MSTensor in_tensor, int index) { this->inputs_[index] = in_tensor; }
2021-08-10 15:08:17 +08:00
/// \brief set kernel's output tensors.
///
/// \param[in] out_tensors define the output tensors.
virtual void set_outputs(const std::vector<mindspore::MSTensor> &out_tensors) { this->outputs_ = out_tensors; }
2021-08-10 15:08:17 +08:00
/// \brief set kernel's output tensor.
///
/// \param[in] out_tensor define the output tensor.
/// \param[in] index define the index of the output tensor.
virtual void set_output(mindspore::MSTensor out_tensor, int index) { this->outputs_[index] = out_tensor; }
2021-08-10 15:08:17 +08:00
/// \brief obtain kernel's input tensors.
///
/// \return input tensors.
virtual const std::vector<mindspore::MSTensor> &inputs() { return this->inputs_; }
2021-08-10 15:08:17 +08:00
/// \brief obtain kernel's output tensors.
///
/// \return output tensors.
virtual const std::vector<mindspore::MSTensor> &outputs() { return this->outputs_; }
2021-08-10 15:08:17 +08:00
/// \brief obtain kernel's name.
///
/// \return kernel's name.
2021-04-17 10:40:06 +08:00
std::string name() const { return this->name_; }
2021-08-10 15:08:17 +08:00
/// \brief set kernel's name.
///
/// \param[in] name define the kernel's name.
2021-04-17 10:40:06 +08:00
void set_name(const std::string &name) { this->name_ = name; }
2021-08-10 15:08:17 +08:00
/// \brief obtain kernel's context.
///
/// \return kernel's context.
const mindspore::Context *context() const { return this->context_; }
2021-08-10 15:08:17 +08:00
/// \brief obtain kernel's type.
///
/// \return kernel's type.
2021-07-10 10:35:19 +08:00
virtual schema::PrimitiveType type() const { return type_; }
2021-08-10 15:08:17 +08:00
/// \brief obtain the primitive of kernel generated by flatbuffers.
///
/// \return the primitive of kernel generated by flatbuffers.
2021-05-28 09:51:03 +08:00
const schema::Primitive *primitive() const { return this->primitive_; }
2021-04-17 10:40:06 +08:00
protected:
2021-07-10 10:35:19 +08:00
std::string name_;
const mindspore::Context *context_ = nullptr;
std::vector<mindspore::MSTensor> inputs_;
std::vector<mindspore::MSTensor> outputs_;
2021-04-17 10:40:06 +08:00
schema::PrimitiveType type_ = schema::PrimitiveType_NONE;
2021-05-28 09:51:03 +08:00
const schema::Primitive *primitive_ = nullptr;
2021-04-17 10:40:06 +08:00
};
} // namespace mindspore::kernel
#endif // MINDSPORE_INCLUDE_API_KERNEL_H