diff --git a/docs/lite/api/source_en/api_cpp/mindspore.rst b/docs/lite/api/source_en/api_cpp/mindspore.rst index 039763dfe51e12b05875ad99a4cfe8f26cd8e672..926b66cb070a35d0f30ad26f8e5791ecb815b8a8 100644 --- a/docs/lite/api/source_en/api_cpp/mindspore.rst +++ b/docs/lite/api/source_en/api_cpp/mindspore.rst @@ -419,6 +419,7 @@ Classes - :doc:`../generate/classmindspore_VectorRef` +- :doc:`../generate/classmindspore_InputAndOutput` Enums ----- diff --git a/docs/lite/api/source_en/api_cpp/mindspore_kernel.rst b/docs/lite/api/source_en/api_cpp/mindspore_kernel.rst index 7532b2d1ba9c78d8fea399aee9a879a8233671c1..8338d2b4c5c71ddad172da9c5b162a2100464920 100644 --- a/docs/lite/api/source_en/api_cpp/mindspore_kernel.rst +++ b/docs/lite/api/source_en/api_cpp/mindspore_kernel.rst @@ -11,3 +11,5 @@ Classes - :doc:`../generate/classmindspore_kernel_Kernel` - :doc:`../generate/classmindspore_kernel_KernelInterface` +- :doc:`../generate/classmindspore_kernel_IKernel` +- :doc:`../generate/classmindspore_kernel_MSKernel` \ No newline at end of file diff --git a/docs/lite/api/source_zh_cn/api_cpp/mindspore.md b/docs/lite/api/source_zh_cn/api_cpp/mindspore.md index 4ddfd3e47d96b4c8a66adaeddf14c8a653350873..41df3b0fb7dc4e05952f478e6faf82edd45eeda8 100644 --- a/docs/lite/api/source_zh_cn/api_cpp/mindspore.md +++ b/docs/lite/api/source_zh_cn/api_cpp/mindspore.md @@ -20,6 +20,8 @@ | [GPUDeviceInfo](#gpudeviceinfo) | 模型运行在GPU上的配置。 | √ | √ | | [KirinNPUDeviceInfo](#kirinnpudeviceinfo) | 模型运行在NPU上的配置。 | ✕ | √ | | [AscendDeviceInfo](#ascenddeviceinfo) | 模型运行在Atlas 200/300/500推理产品、Atlas推理系列产品上的配置。 | √ | √ | +| [DelegateMode](#delegatemode) |模型运行的代理模式。 | √ | √ | +| [Key](#key) | 键 | √ | √ | ### 并发推理 @@ -34,8 +36,8 @@ |--------------------------------------------------|---------------------------------------------------|--------|--------| | [MSTensor](#mstensor) | MindSpore中的张量。 | √ | √ | | [QuantParam](#quantparam) | MSTensor中的一组量化参数。 | √ | √ | -| [mindspore::DataType](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore_datatype.html) | MindSpore MSTensor保存的数据支持的类型。 | √ | √ | -| [mindspore::Format](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore_format.html) | MindSpore MSTensor保存的数据支持的排列格式。 | √ | √ | +| [DataType](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore_datatype.html) | MindSpore MSTensor保存的数据支持的类型。 | √ | √ | +| [Format](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore_format.html) | MindSpore MSTensor保存的数据支持的排列格式。 | √ | √ | | [Allocator](#allocator-1) | 内存管理基类。 | √ | √ | ### 模型分组 @@ -43,12 +45,14 @@ | 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | |-------------------------------------------|-----------------------|--------|--------| | [ModelGroup](#modelgroup) | 模型分组。 | √ | ✕ | +| [ModelGroupFlag](#modelgroupflag) | 模型分组。 | √ | √ | ### 状态 | 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | |--------------------------------------------------|---------------------------------------------------|--------|--------| | [Status](#status) | 返回状态类。 | √ | √ | +| [CompCode](#compcode) | 返回计算类别。 | √ | √ | ### 序列化保存与加载 @@ -98,6 +102,8 @@ | [DelegateModel](#delegatemodel) | MindSpore Lite Delegate机制封装的模型。 | ✕ | √ | | [KernelIter](#kerneliter) | MindSpore Lite 算子列表的迭代器。 | ✕ | √ | | [CoreMLDelegate](#coremldelegate) | MindSpore Lite接入CoreML框架的代理。 | ✕ | √ | +| [AbstractDelegate](#abstractdelegate) | MindSpore Lite接入代理(抽象类)。 | √ | ✕ | +| [IDelegate](#idelegate) | MindSpore Lite接入代理(模板类)。 | √ | ✕ | ### 图容器 @@ -107,8 +113,7 @@ | [Cell](#cell) | 容器类。 | ✕ | √ | | [GraphCell](#graphcell) | 图容器类。 | ✕ | √ | | [Graph](#graph) | 图类。 | ✕ | √ | -| [AbstractDelegate](#abstractdelegate) | MindSpore Lite接入代理(抽象类)。 | √ | ✕ | -| [IDelegate](#idelegate) | MindSpore Lite接入代理(模板类)。 | √ | ✕ | +| [InputAndOutput](#inputandoutput) |输入输出类。 | √ | √ | ## Context @@ -123,6 +128,16 @@ Context() ~Context() = default; ``` +### 公有成员变量 + +#### Data + +```cpp +struct Data; +``` + +Context的数据。 + ### 公有成员函数 | 函数 | 云侧推理是否支持 | 端侧推理是否支持 | @@ -146,6 +161,7 @@ Context() | [void SetMultiModalHW(bool float_mode)](#setmultimodalhw) | ✕ | √ | | [bool GetMultiModalHW() const](#getmultimodalhw) | ✕ | √ | | [std::vector> &MutableDeviceInfo()](#mutabledeviceinfo) | √ | √ | +| [void SetGroupInfoFile(std::string group_info_file)](#setgroupinfofile) | √ | √ | #### SetThreadNum @@ -375,6 +391,31 @@ std::vector> &MutableDeviceInfo() 存储DeviceInfoContext的vector的引用。 +#### SetGroupInfoFile + +```cpp +void SetGroupInfoFile(std::string group_info_file); +``` + +设置组信息文件。 + +- 参数 + + - `group_info_file`: 组信息文件名字。 + +## DelegateMode + +\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/context.h)> + +```cpp +enum DelegateMode { + kNoDelegate = 0, + kCoreML = 1, +}; +``` + +Delegate模式。 + ## DeviceInfoContext \#include <[context.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/context.h)> @@ -388,6 +429,14 @@ DeviceInfoContext() virtual ~DeviceInfoContext() = default; ``` +### 公有成员变量 + +```cpp +struct Data; +``` + +数据。 + ### 公有成员函数 | 函数 | 云侧推理是否支持 | 端侧推理是否支持 | @@ -587,7 +636,22 @@ std::shared_ptr GetAllocator() const; | `void SetFusionSwitchConfigPath(const std::string &cfg_path)` | 配置融合开关

- `cfg_path`: 融合开关配置文件,可指定关闭特定融合规则 | √ | √ | | `std::string GetFusionSwitchConfigPath()` | - 返回值: 已配置的融合开关文件路径 | √ | √ | | `void SetBufferOptimizeMode(const std::string &buffer_optimize_mode)` | 配置缓存优化模式

- `buffer_optimize_mode`: 可选有`l1_optimize`,`l2_optimize`或`off_optimize`,默认为`l2_optimize` | √ | √ | -| `std::string GetBufferOptimizeMode()` | - 返回值: 已配置的缓存优化模式 | √ | √ | +| `std::string GetBufferOptimizeMode()` | - 返回值: 已配置的缓存优化模式 | `void SetRankID(uint32_t rank_id)` |指定模型rank_id。 | √ | √ | +| `uint32_t GetRankID() const` | - 返回值: 获取模型的rank_id | √ | √ | + +## Key + +```cpp +using Key = struct MS_API Key { + size_t max_key_len = 32; + size_t len = 0; + unsigned char key[32] = {0}; + Key() : len(0) {} + explicit Key(const char *dec_key, size_t key_len); +}; +``` + +键结构体。 ## Serialization @@ -881,7 +945,6 @@ Model() | [Status Build(const std::string &model_path, ModelType model_type, const std::shared_ptr &model_context = nullptr)](#build-2) | √ | √ | | [Status Build(const std::string &model_path, ModelType model_type, const std::shared_ptr &model_context, const Key &dec_key, const std::string &dec_mode, const std::string &cropto_lib_path)](#build-3) | √ | √ | | [Status Build(GraphCell graph, const std::shared_ptr &model_context = nullptr, const std::shared_ptr &train_cfg = nullptr)](#build-4) | ✕ | √ | -| [Status Build(const std::string &model_path, ModelType model_type, const std::shared_ptr &model_context, const CryptoInfo &cryptoInfo);](#build-5) | √ | ✕ | | [Status BuildTransferLearning(GraphCell backbone, GraphCell head, const std::shared_ptr &context, const std::shared_ptr &train_cfg = nullptr)](#buildtransferlearning) | ✕ | √ | | [Status Resize(const std::vector &inputs, const std::vector> &dims)](#resize) | √ | √ | | [Status UpdateWeights(const std::vector &new_weights)](#updateweights) | ✕ | √ | @@ -918,6 +981,9 @@ Model() | [Status Train(int epochs, std::shared_ptr< dataset::Dataset> ds, std::vector cbs)](#train) | ✕ | √ | | [Status Evaluate(std::shared_ptr< dataset::Dataset> ds, std::vector cbs)](#evaluate) | ✕ | √ | | [Status Finalize()](#finalize) | √ | √ | +| [Status UpdateWeights(const std::vector> &new_weights)](#updateweights) | √ | √ | +| [const std::shared_ptr impl()](#impl) | √ | √ | +| [inline std::string GetModelInfo(const std::string &key)](#getmodelinfo) | √ | √ | #### Build @@ -1026,26 +1092,6 @@ Status Build(GraphCell graph, const std::shared_ptr &model_context = nu 状态码类`Status`对象,可以使用其公有函数`StatusCode`或`ToString`函数来获取具体错误码及错误信息。 -#### Build - -```cpp -Status Build(const std::string &model_path, ModelType model_type, - const std::shared_ptr &model_context, const CryptoInfo &cryptoInfo); -``` - -根据路径读取加载密文模型,并将模型解密而后编译至可在Device上运行的状态。 - -- 参数 - - - `model_path`: 密文模型文件路径。 - - `model_type`: 模型文件类型,可选有`ModelType::kMindIR_Lite`、`ModelType::kMindIR`,分别对应`ms`模型(`converter_lite`工具导出)和`mindir`模型(MindSpore导出或`converter_lite`工具导出)。MindIR推理支持`ms`和`mindir`模型推理,需要将该参数设置为模型对应的选项值。MindIR推理对`ms`模型的支持,将在未来的迭代中删除,推荐通过`mindir`模型进行推理。 - - `model_context`: 模型[Context](#context)。 - - `cryptoInfo`: 解密相关配置信息。 - -- 返回值 - - 状态码类`Status`对象,可以使用其公有函数`StatusCode`或`ToString`函数来获取具体错误码及错误信息。 - #### Predict ```cpp @@ -1604,6 +1650,50 @@ Status Finalize(); 状态码。 +#### UpdateWeights + +```cpp +Status UpdateWeights(const std::vector> &new_weights); +``` + +更新模型的权重的大小和内容。 + +- 参数 + + - `new_weights`: 要更新的权重Tensor,可同时更新大小和内容。 + +- 返回值 + + 状态码。 + +#### impl + +```cpp +const std::shared_ptr impl() const +``` + +获得模型的实现。 + +- 返回值 + + 获得模型的实现。 + +#### GetModelInfo + +```cpp +inline std::string GetModelInfo(const std::string &key); +``` + +获取模型的信息。 + +- 参数 + + `key`: 模型的key。 + +- 返回值 + + 模型的信息。 + ## MSTensor \#include <[types.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/types.h)> @@ -1803,6 +1893,7 @@ void DestroyTensorPtr(MSTensor *tensor) noexcept; | [MSTensor *Clone() const](#clone) | √ | √ | | [bool operator==(std::nullptr_t) const](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#operator==std-nullptr-t) | √ | √ | | [bool operator!=(std::nullptr_t) const](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#operator!=std-nullptr-t) | √ | √ | +| [bool operator!=(const MSTensor &tensor) const](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#operator!=const-mstensor-tensor) | √ | √ | | [bool operator==(const MSTensor &tensor) const](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#operator==const-mstensor-tensor) | √ | √ | | [void SetShape(const std::vector &shape)](#setshape) | √ | √ | | [void SetDataType(enum DataType data_type)](#setdatatype) | √ | √ | @@ -1998,6 +2089,18 @@ bool operator==(const MSTensor &tensor) const; `MSTensor`是否与另一个MSTensor相等。 +#### operator!=(const MSTensor &tensor) + +```cpp +bool operator!=(const MSTensor &tensor) const; +``` + +判断`MSTensor`是否与另一个MSTensor不相等。 + +- 返回值 + + `MSTensor`是否与另一个MSTensor不相等。 + #### SetShape ```cpp @@ -2267,7 +2370,7 @@ std::shared_ptr CreateKernel(const std::shared_ptr &node) override { return false; } +bool IsDelegateNode(const std::shared_ptr &node) override; ``` 是否是Delegate节点。 @@ -2280,6 +2383,18 @@ bool IsDelegateNode(const std::shared_ptr &node) override { retu bool值。 +#### ReplaceNode + +```cpp +void ReplaceNodes(const std::shared_ptr &graph) override {} +``` + +替换节点。 + +- 参数 + + - `graph`: 要被替换的节点。 + ## CoreMLDelegate \#include <[delegate.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/delegate.h)> @@ -2468,6 +2583,18 @@ KernelIter Replace(KernelIter from, KernelIter end, kernel::Kernel *graph_kernel **KernelIter**,用Delegate子图Kernel替换之后,子图Kernel下一个元素的迭代器,指向下一个未被访问的Kernel。 +#### nodes + +```cpp +std::vector *nodes() +``` + +获取nodes。 + +- 返回值 + + Kernel组成的vector。 + #### inputs ```cpp @@ -2495,7 +2622,7 @@ const std::vector &outputs() #### GetVersion ```cpp -const SchemaVersion GetVersion() { return version_; } +const SchemaVersion GetVersion() ``` 返回当前执行推理的模型文件的版本。 @@ -2529,7 +2656,7 @@ virtual ~AbstractDelegate() = default; #### inputs ```cpp -std::vector &inputs() { return this->inputs_; } +std::vector &inputs() ``` 返回AbstractDelegate的inputTensor。 @@ -2537,7 +2664,7 @@ std::vector &inputs() { return this->inputs_; } #### outputs ```cpp -const std::vector &outputs() { return this->outputs_; } +const std::vector &outputs() ``` 返回AbstractDelegate的outputTensor。 @@ -2611,7 +2738,22 @@ virtual std::shared_ptr CreateKernel(const std::shared_ptr &node) ### 构造函数 ```cpp -TrainCfg() { this->loss_name_ = "_loss_fn"; } +TrainCfg() = default; +TrainCfg(const TrainCfg &rhs) { + this->loss_name_ = rhs.loss_name_; + this->mix_precision_cfg_ = rhs.mix_precision_cfg_; + this->accumulate_gradients_ = rhs.accumulate_gradients_; + } +``` + +- 参数 + + `rhs`: 训练配置。 + +### 析构函数 + +```cpp +~TrainCfg() = default; ``` ### 公有成员变量 @@ -2650,6 +2792,32 @@ bool accumulate_gradients_; 是否累加梯度。 +### 公有成员函数 + +#### GetLossName + +```cpp +inline std::vector GetLossName() const; +``` + +获得损失名称。 + +- 返回值 + + 损失的名称。 + +#### SetLossName + +```cpp +inline void SetLossName(const std::vector &loss_name); +``` + +设置损失名称。 + +- 参数 + + `loss_name`: 损失的名称。 + ## MixPrecisionCfg \#include <[cfg.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/cfg.h)> @@ -2659,11 +2827,28 @@ bool accumulate_gradients_; ### 构造函数 ```cpp - MixPrecisionCfg() { - dynamic_loss_scale_ = false; - loss_scale_ = 128.0f; - num_of_not_nan_iter_th_ = 1000; + MixPrecisionCfg() { + this->dynamic_loss_scale_ = false; + this->loss_scale_ = 128.0f; + this->keep_batchnorm_fp32_ = true; + this->num_of_not_nan_iter_th_ = iter_th; } + MixPrecisionCfg(const MixPrecisionCfg &rhs) { + this->dynamic_loss_scale_ = rhs.dynamic_loss_scale_; + this->loss_scale_ = rhs.loss_scale_; + this->keep_batchnorm_fp32_ = rhs.keep_batchnorm_fp32_; + this->num_of_not_nan_iter_th_ = rhs.num_of_not_nan_iter_th_; + } +``` + +- 参数 + + `rhs`: 混合精度配置。 + +### 析构函数 + +```cpp +~MixPrecisionCfg() = default; ``` ### 共有成员变量 @@ -2692,16 +2877,35 @@ bool is_raw_mix_precision_; 原始模型是否是原生混合精度模型。 +```cpp +bool keep_batchnorm_fp32_ = true; +``` + +原型模型是否保持BatchNorm算子为Fp32格式。 + ## AccuracyMetrics \#include <[accuracy.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/metrics/accuracy.h)> `AccuracyMetrics`MindSpore Lite训练精度类。 -### 构造函数和析构函数 +### 构造函数 ```cpp explicit AccuracyMetrics(int accuracy_metrics = METRICS_CLASSIFICATION, const std::vector &input_indexes = {1}, const std::vector &output_indexes = {0}) +``` + +- 参数 + + `accuracy_metrics`: 精度度量标准。 + + `input_indexes`: 输入的序列号。 + + `output_indexes`: 输出的序列号。 + +### 析构函数 + +```cpp virtual ~AccuracyMetrics() ``` @@ -2752,7 +2956,7 @@ virtual void Clear() {} #### Eval ```cpp -virtual float Eval() { return 0.0; } +virtual float Eval() ``` 模型验证。 @@ -2827,7 +3031,7 @@ virtual void Begin(const TrainCallBackData &cb_data) {} #### EpochEnd ```cpp - virtual CallbackRetValue EpochEnd(const TrainCallBackData &cb_data) { return kContinue; } + virtual CallbackRetValue EpochEnd(const TrainCallBackData &cb_data) ``` 每轮迭代后回调。 @@ -3605,39 +3809,80 @@ static inline std::string CodeAsString(enum StatusCode c) 是否为非空指针。 -## CellBase +## CompCode + +```cpp +enum CompCode : uint32_t { + kCore = 0x00000000u, + kMD = 0x10000000u, + kME = 0x20000000u, + kMC = 0x30000000u, + kLite = 0xF0000000u, +}; +``` + +计算的类型。 + +## InputAndOutput \#include <[cell.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/cell.h)> -### 构造函数和析构函数 +### 构造函数 ```cpp - CellBase() = default; - virtual ~CellBase() = default; + InputAndOutput(); + InputAndOutput(const std::shared_ptr &cell, const std::vector &prev, int32_t index); +``` + +- 参数 + + `cell`: 输入CellBase。 + + `prev`: 之前的InputAndOutput。 + + `index`: 序列号。 + +### 析构函数 + +```cpp +~InputAndOutput() = default; ``` ### 公有成员函数 -#### Clone +#### GetIndex ```cpp - virtual std::shared_ptr Clone() const = 0; +int32_t GetIndex() const ``` -拷贝一份自身的副本。 +返回index。 - 返回值 - 指向副本的指针。 + 返回输入/输出的index。 -## Cell +#### SetIndex + +```cpp +void SetIndex(int32_t index) { index_ = index; } +``` + +设置输入/输出的index。 + +- 参数 + + `index`: 输入/输出的index。 + +## CellBase \#include <[cell.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/cell.h)> -### 析构函数 +### 构造函数和析构函数 ```cpp - virtual ~Cell() = default; + CellBase() = default; + virtual ~CellBase() = default; ``` ### 公有成员函数 @@ -3645,7 +3890,7 @@ static inline std::string CodeAsString(enum StatusCode c) #### Clone ```cpp - std::shared_ptr Clone() const; + virtual std::shared_ptr Clone() const = 0; ``` 拷贝一份自身的副本。 @@ -3657,18 +3902,18 @@ static inline std::string CodeAsString(enum StatusCode c) #### Construct ```cpp -virtual std::vector Construct(const std::vector &inputs) { return {}; } +virtual std::vector Construct(const std::vector &inputs) {return {}; } ``` -构造一份CellBase。 +构建CellBase。 - 参数 - Input组成的vector。 + `inputs`: 输入。 - 返回值 - Output组成的vector。 + 输出。 #### Run @@ -3676,7 +3921,7 @@ virtual std::vector Construct(const std::vector &inputs) { return virtual Status Run(const std::vector &inputs, std::vector *outputs) { return kSuccess; } ``` -运行CellBase。 +运行Cell。 - 参数 @@ -3702,6 +3947,30 @@ std::vector operator()(const std::vector &inputs) const; Output组成的vector。 +## Cell + +\#include <[cell.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/cell.h)> + +### 析构函数 + +```cpp + virtual ~Cell() = default; +``` + +### 公有成员函数 + +#### Clone + +```cpp + std::shared_ptr Clone() const; +``` + +拷贝一份自身的副本。 + +- 返回值 + + 指向副本的指针。 + ## GraphCell \#include <[cell.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/cell.h)> @@ -3721,7 +3990,7 @@ std::vector operator()(const std::vector &inputs) const; #### GetGraph ```cpp - const std::shared_ptr &GetGraph() const { return graph_; } + const std::shared_ptr &GetGraph() ``` 获取Graph指针。 @@ -4139,3 +4408,16 @@ Status CalMaxSizeOfWorkspace(ModelType model_type, const std::shared_ptr GetGraphOutputTensorNames(); 模型的输出名称,默认与原始模型的输出名称一致。 +#### GetConfigInfo + +```cpp +static std::map GetConfigInfo(const std::string §ion) { + return MapVectorCharToString(GetConfigInfo(StringToChar(section))); + } +``` + +获取配置信息。 + +- 参数 + + `section`: 定义了配置的section name。 + +- 返回值 + + 返回配置信息。 + ## NodeParser \#include <[node_parser.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/mindspore/lite/include/registry/node_parser.h)> diff --git a/docs/lite/api/source_zh_cn/api_cpp/mindspore_datatype.md b/docs/lite/api/source_zh_cn/api_cpp/mindspore_datatype.md index 3652a18e22d1d5cac55e939f54e6d80548922bb8..a7374dd59ee8b0f5322d465594208ca2c847de35 100644 --- a/docs/lite/api/source_zh_cn/api_cpp/mindspore_datatype.md +++ b/docs/lite/api/source_zh_cn/api_cpp/mindspore_datatype.md @@ -1,4 +1,4 @@ -# mindspore::DataType +# DataType [![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.7.0rc1/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/r2.7.0rc1/docs/lite/api/source_zh_cn/api_cpp/mindspore_datatype.md) @@ -21,7 +21,6 @@ |kNumberTypeInt16| 33 | 表示int16数据类型。 | |kNumberTypeInt32| 34 | 表示int32数据类型。 | |kNumberTypeInt64| 35 | 表示int64数据类型。 | -|kNumberTypeUInt| 36 | 表示uint数据类型。 | |kNumberTypeUInt8| 37 | 表示uint8数据类型。 | |kNumberTypeUInt16| 38 | 表示uint16数据类型。 | |kNumberTypeUInt32| 39 | 表示uint32数据类型。 | @@ -29,5 +28,6 @@ |kNumberTypeFloat16| 42 | 表示float16数据类型。 | |kNumberTypeFloat32| 43 | 表示float32数据类型。 | |kNumberTypeFloat64| 44 | 表示float64数据类型。| -|kNumberTypeEnd| 46 | 表示number类型的结尾。 | +|kNumberTypeBFloat16| 45 | 表示bfloat16数据类型。| +|kNumberTypeEnd| 52 | 表示number类型的结尾。 | |kInvalidType | INT32_MAX | 表示无效的数据类型。 | diff --git a/docs/lite/api/source_zh_cn/api_cpp/mindspore_format.md b/docs/lite/api/source_zh_cn/api_cpp/mindspore_format.md index 9b957a9dcd0ae2ddfdff4db2af29126b9497e86e..684ba9a9e96eabef7bc7c391e573b7bfa8e6c44a 100644 --- a/docs/lite/api/source_zh_cn/api_cpp/mindspore_format.md +++ b/docs/lite/api/source_zh_cn/api_cpp/mindspore_format.md @@ -10,6 +10,7 @@ | 定义 | 值 | | --- | --- | +| DEFAULT_FORMAT | -1 | | NCHW | 0 | | NHWC | 1 | | NHWC4 | 2 | @@ -24,7 +25,49 @@ | NC | 11 | | NC4 | 12 | | NC4HW4 | 13 | -| NUM_OF_FORMAT | 14 | -| NCDHW | 15 | -| NWC | 16 | -| NCW | 17 | +| NCDHW | 14 | +| NWC | 15 | +| NCW | 16 | +| NDHWC| 17 | +| NC8HW8 | 18 | +| FRACTAL_NZ | 19 | +| ND | 20 | +| NC1HWC0| 21 | +| FRACTAL_Z | 22 | +| NC1C0HWPAD | 23 | +| NHWC1C0 | 24 | +| FSR_NCHW | 25 | +| FRACTAL_DECONV | 26 | +| C1HWNC0 | 27 | +| FRACTAL_DECONV_TRANSPOSE | 28 | +| FRACTAL_DECONV_SP_STRIDE_TRANS | 29 | +| NC1HWC0_C04 | 30 | +| FRACTAL_Z_C04 | 31 | +| CHWN | 32 | +| FRACTAL_DECONV_SP_STRIDE8_TRANS | 33 | +| HWCN | 34 | +| NC1KHKWHWC0 | 35 | +| BN_WEIGHT | 36 | +| FILTER_HWCK | 37 | +| LOOKUP_LOOKUPS | 38 | +| LOOKUP_KEYS | 39 | +| LOOKUP_VALUE | 40 | +| LOOKUP_OUTPUT | 41 | +| LOOKUP_HITS | 42 | +| C1HWNCoC0 | 43 | +| MD | 44 | +| FRACTAL_ZZ | 45 | +| DHWCN | 46 | +| NDC1HWC0 | 47 | +| FRACTAL_Z_3D | 48 | +| CN | 49 | +| DHWNC | 50 | +| FRACTAL_Z_3D_TRANSPOSE | 51 | +| FRACTAL_ZN_LSTM | 52 | +| FRACTAL_Z_G | 53 | +| ND_RNN_BIAS | 54 | +| FRACTAL_ZN_RNN | 55 | +| NYUV | 56 | +| NYUV_A | 57 | +| NCL | 58 | +| NUM_OF_FORMAT | 59 | diff --git a/docs/lite/api/source_zh_cn/api_cpp/mindspore_kernel.md b/docs/lite/api/source_zh_cn/api_cpp/mindspore_kernel.md index d7c38b2efa6ce4d1c474ab707e45f55979307a01..c422dfd1d9acd405f4bdf87456b23b7c4574a4da 100644 --- a/docs/lite/api/source_zh_cn/api_cpp/mindspore_kernel.md +++ b/docs/lite/api/source_zh_cn/api_cpp/mindspore_kernel.md @@ -8,16 +8,18 @@ | --- | --- | | [Kernel](#kernel) | 算子基类。| | [KernelInterface](#kernelinterface) | 算子扩展能力基类。| +| [MSKernel](#mskernel) | Mindspore Kernel Mindspore算子基类。 | +| [IKernel](#ikernel) | IKernel 算子模板类。 | ## Kernel \#include <[kernel.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/kernel.h)> -Kernel是算子实现的基类,定义了几个必须实现的接口。 +Kernel是算子实现的基类,定义了几个必须实现的接口。继承自IKernel。 -## 构造函数 +### 构造函数 -### Kernel +#### Kernel ``` c++ Kernel() @@ -38,9 +40,9 @@ Kernel的默认与带参构造函数,构造Kernel实例。 - `ctx`: 算子的上下文[Context](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#context)。 -## 析构函数 +### 析构函数 -### ~Kernel +#### ~Kernel ``` c++ virtual ~Kernel() @@ -48,34 +50,135 @@ virtual ~Kernel() Kernel的析构函数。 -## 公有成员函数 +### 公有成员函数 -### Prepare +#### InferShape ``` c++ -virtual int Prepare() +virtual int InferShape() ``` -进行算子运行前相关的准备工作,MindSpore Lite 框架运行时会对所有算子执行一遍Prepare后再执行Execute。 +在用户调用`Model::Build`接口时,或是模型推理中需要推理算子形状时,会调用到该接口。 +在自定义算子场景中,用户可以覆写该接口,实现自定义算子的形状推理逻辑。详见[自定义算子章节](https://www.mindspore.cn/lite/docs/zh-CN/r2.7.0rc1/advanced/third_party/register_kernel.html)。 +在`InferShape`函数中,一般需要实现算子的形状、数据类型和数据排布的推理逻辑。 + +- 返回值 + + 是否成功。 -### Execute +#### type ``` c++ -virtual int Execute() +virtual schema::PrimitiveType type() ``` -运行算子。 +返回算子的类型。 -### ReSize +#### quant_type ``` c++ -virtual int ReSize() +virtual schema::QuantType quant_type() ``` -在用户调用`Model::Resize`接口时,或是模型推理中需要重新推理算子形状时,会调用到该接口。 -在`ReSize`函数中,若有必要,根据输入的形状态重新推理输出形状,并分配算子运算中需要的内存。 +返回算子的量化类型。 + +## KernelInterface + +\#include <[kernel_interface.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/mindspore/lite/include/kernel_interface.h)> + +算子扩展能力基类。 + +### ~KernelInterface + +``` c++ +virtual ~KernelInterface() = default; +``` + +析构函数。 + +### KernelInterfaceCreator + +``` c++ +using KernelInterfaceCreator = std::function()> +``` + +创建[KernelInterface](#kernelinterface)的函数原型声明。 + +### 公有成员函数 + +#### Infer + +算子用于推测输出shape的方法。 + +``` c++ +virtual Status Infer(std::vector *inputs, std::vector *outputs, + const schema::Primitive *primitive) +``` + +- 参数 + + - `inputs`: 算子输入[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)。 + + - `outputs`: 算子输出[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)。 + + - `primitive`: 算子经过flatbuffers反序化后的结果,存储算子属性。 + +- 返回值 + + 状态码。 + +#### Infer + +``` c++ +virtual Status Infer(std::vector *inputs, std::vector *outputs, + const schema::Primitive *primitive, const Kernel *kernel) +``` + +- 参数 + + - `inputs`: 算子输入[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)。 + + - `outputs`: 算子输出[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)。 + + - `primitive`: 算子经过flatbuffers反序化后的结果,存储算子属性。 + + - `kernel`: 对应的算子。 + +推测自定义算子的输出shape。 + +## MSKernel + +\#include <[kernel.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/include/api/kernel_api.h)> + +Mindspore Kernel 算子类。是IKernel的父类。 + +### 构造函数 + +```cpp +MSKernel() = default; +MSKernel(std::vector inputs, std::vector outputs, + const mindspore::Context *ctx) + + : context_(ctx), inputs_(std::move(inputs)), outputs_(std::move(outputs)) {} +``` -### InferShape +- 参数 + + `inputs`: 输入。 + + `output`: 输出。 + + `ctx`: 算子对应Context。 + +### 析构函数 + +```cpp +virtual ~MSKernel() = default; +``` + +### 公有成员函数 + +#### InferShape ``` c++ virtual int InferShape() @@ -85,23 +188,48 @@ virtual int InferShape() 在自定义算子场景中,用户可以覆写该接口,实现自定义算子的形状推理逻辑。详见[自定义算子章节](https://www.mindspore.cn/lite/docs/zh-CN/r2.7.0rc1/advanced/third_party/register_kernel.html)。 在`InferShape`函数中,一般需要实现算子的形状、数据类型和数据排布的推理逻辑。 -### type +- 返回值 + + 状态码。 + +#### Prepare ``` c++ -virtual schema::PrimitiveType type() +virtual int Prepare() = 0; ``` -返回算子的类型。 +进行算子运行前相关的准备工作,MindSpore Lite 框架运行时会对所有算子执行一遍Prepare后再执行Execute。 -### quant_type +- 返回值 + + 状态码。 + +#### Execute ``` c++ -virtual schema::QuantType quant_type() +virtual int Execute() = 0; ``` -返回算子的量化类型。 +运行算子。 + +- 返回值 + + 状态码。 -### set_inputs +#### ReSize + +``` c++ +virtual int ReSize() = 0; +``` + +在用户调用`Model::Resize`接口时,或是模型推理中需要重新推理算子形状时,会调用到该接口。 +在`ReSize`函数中,若有必要,根据输入的形状态重新推理输出形状,并分配算子运算中需要的内存。 + +- 返回值 + + 状态码。 + +#### set_inputs ``` c++ virtual void set_inputs(const std::vector &in_tensors) @@ -113,10 +241,10 @@ virtual void set_inputs(const std::vector &in_tensors) - `in_tensors`: 算子的所有输入[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)列表。 -### set_input +#### set_input ``` c++ -virtual set_input(mindspore::MSTensor in_tensor, int index) +virtual void set_input(mindspore::MSTensor in_tensor, int index) ``` 设置算子指定位置的输入。 @@ -127,7 +255,7 @@ virtual set_input(mindspore::MSTensor in_tensor, int index) - `index`: 算子输入在所有输入中的下标,从0开始计数。 -### set_outputs +#### set_outputs ``` c++ virtual void set_outputs(const std::vector &out_tensors) @@ -139,7 +267,7 @@ virtual void set_outputs(const std::vector &out_tensors) - `out_tensor`: 算子的所有输出[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)列表。 -### set_output +#### set_output ``` c++ virtual void set_output(mindspore::MSTensor out_tensor, int index) @@ -153,7 +281,7 @@ virtual void set_output(mindspore::MSTensor out_tensor, int index) - `index`: 算子输出在所有输出中的下标,从0开始计数。 -### inputs +#### inputs ``` c++ virtual const std::vector &inputs() @@ -161,7 +289,11 @@ virtual const std::vector &inputs() 返回算子的所有输入[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)列表。 -### outputs +- 返回值 + + 算子的所有输入。 + +#### outputs ``` c++ virtual const std::vector &outputs() @@ -169,15 +301,23 @@ virtual const std::vector &outputs() 返回算子的所有输出[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)列表。 -### name +- 返回值 + + 算子的所有输出。 + +#### name ``` c++ -std::string name() +std::string name() const ``` 返回算子的名称。 -### set_name +- 返回值 + + 算子名称。 + +#### set_name ``` c++ void set_name(const std::string &name) @@ -189,7 +329,7 @@ void set_name(const std::string &name) - `name`: 算子名称。 -### context +#### context ``` c++ const lite::Context *context() const @@ -197,15 +337,11 @@ const lite::Context *context() const 返回算子对应的[Context](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#context)。 -### primitive - -``` c++ -const schema::Primitive *primitive() const -``` +- 返回值 -返回算子经由flatbuffers反序化为Primitive后的结果。 + 算子的Context。 -### GetAttr +#### GetAttr ``` c++ std::string GetAttr(const std::string &key) const @@ -217,7 +353,7 @@ std::string GetAttr(const std::string &key) const - `key`: 配置名。 -### SetConfig +#### SetConfig ``` c++ void SetConfig(const std::map> *config) @@ -229,7 +365,7 @@ void SetConfig(const std::map> * - `config`: 配置的常量指针。 -### GetConfig +#### GetConfig ``` c++ std::map GetConfig(const std::string §ion) const @@ -241,67 +377,90 @@ std::map GetConfig(const std::string §ion) const - `section`: 配置的章节名称。 -## KernelInterface +### 保护成员变量 -\#include <[kernel_interface.h](https://gitee.com/mindspore/mindspore/blob/v2.7.0-rc1/mindspore/lite/include/kernel_interface.h)> +```cpp +std::string name_; +const mindspore::Context *context_ = nullptr; +std::vector inputs_; +std::vector outputs_; +std::map attrs_; +const std::map> *config_ = nullptr; +``` -算子扩展能力基类。 +name_: 名字 -### ~KernelInterface +context_: 训练context。 -``` c++ -virtual ~KernelInterface() -``` +inputs_: 输入。 -析构函数。 +outputs_: 输出。 -### KernelInterfaceCreator +attrs_: 属性。 -``` c++ -using KernelInterfaceCreator = std::function()> +config_: 配置。 + +### 保护成员函数 + +```cpp +void SetAttr(const std::string &key, const std::string &value) { attrs_[key] = value; } ``` -创建[KernelInterface](#kernelinterface)的函数原型声明。 +设置算子的属性。 -### 公有成员函数 +- 参数 -#### Infer + - `key`: 属性键。 -算子的InferShape能力,用于根据输入推导出输出的形状、数据类型以及format。 + - `value`: 属性值。 -``` c++ -virtual int Infer(std::vector *inputs, std::vector *outputs, const schema::Primitive *primitive, const Kernel *kernel) +## IKernel + +继承自[MSKernel](#mskernel) + +### 构造函数 + +```cpp +IKernel() = default; +IKernel(const std::vector &inputs, const std::vector &outputs, + const Primitive *primitive, const mindspore::Context *ctx) + : MSKernel(inputs, outputs, ctx), primitive_(primitive) {} ``` - 参数 - - `inputs`: 算子输入[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)。 + - `inputs`: 输入Tensor。 - - `outputs`: 算子输出[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)。 + - `outputs`: 输出Tensor。 - `primitive`: 算子经过flatbuffers反序化后的结果,存储算子属性。 - - `kernel`: 算子的基类结构,在Build阶段,kernel是空指针,Build完成后框架传递的kernel才有值,当kernel非空时就不建议去操作primitive了,因为有可能primtive已经无效了。 + - `ctx`: 计算context。 -#### Infer +### 析构函数 -算子的InferShape能力,用于根据输入推导出输出的shape、数据类型以及format。 +```cpp +~IKernel() override = default; +``` -该接口已不推荐使用,建议使用带有kernel参数的Infer接口。因为如果模型通过以下Build接口执行编译,编译后框架会自动释放模型的内存,导致primitive不可用。 +### 公有成员函数 -``` c++ -Status Build(GraphCell graph, const std::shared_ptr &model_context = nullptr, - const std::shared_ptr &train_cfg = nullptr) -``` +#### Primitive -``` c++ -virtual int Infer(std::vector *inputs, std::vector *outputs, const schema::Primitive *primitive) +```cpp +const Primitive *primitive() const { return this->primitive_; } ``` -- 参数 +获取IKernel算子的primitive。 - - `inputs`: 算子输入[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)。 +- 返回值 - - `outputs`: 算子输出[MSTensor](https://www.mindspore.cn/lite/api/zh-CN/r2.7.0rc1/api_cpp/mindspore.html#mstensor)。 + IKernel算子经过flatbuffers反序化后的结果。 - - `primitive`: 算子经过flatbuffers反序化后的结果,存储算子属性。 +### 保护成员函数 + +```cpp +const Primitive *primitive_ = nullptr; +``` + +IKernel算子经过flatbuffers反序化后的结果。 \ No newline at end of file