214 lines
11 KiB
C++
214 lines
11 KiB
C++
/**
|
||
* Copyright 2019-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_CCSRC_TRANSFORM_GRAPH_IR_OP_DECLARE_MACRO_H_
|
||
#define MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_OP_DECLARE_MACRO_H_
|
||
|
||
#include <string>
|
||
#include <memory>
|
||
#include "utils/hash_map.h"
|
||
#include "transform/graph_ir/op_adapter.h"
|
||
#include "transform/graph_ir/op_adapter_desc.h"
|
||
#include "include/transform/graph_ir/op_adapter_map.h"
|
||
#include "mindspore/core/base/core_ops.h"
|
||
|
||
namespace mindspore::transform {
|
||
//定义了一个宏 DECLARE_OP_ADAPTER(T),用于声明对应的Op Adapter
|
||
#define DECLARE_OP_ADAPTER(T) \
|
||
using T = ge::op::T; \//定义一个别名 T,表示对应GE框架中的操作ge::op::T
|
||
template <> \
|
||
const mindspore::HashMap<int, InputDesc> OpAdapter<T>::input_map_; \//一个模板特化,它声明了一个静态成员变量 input_map_,用于存储 T 类型的操作的输入映射信息。
|
||
// InputDesc 是一个自定义的结构体,用于描述输入的信息,例如输入的名称和相应的处理函数。
|
||
template <> \
|
||
const mindspore::HashMap<std::string, AttrDesc> OpAdapter<T>::attr_map_; //一个模板特化,它声明了一个静态成员变量 attr_map_,用于存储 T 类型的操作的属性映射信息。
|
||
//AttrDesc 是一个自定义的结构体,用于描述属性的信息,例如属性的名称和相应的处理函数。
|
||
|
||
#define DECLARE_OP_USE_OUTPUT(T) \
|
||
template <> \
|
||
const mindspore::HashMap<int, OutputDesc> OpAdapter<T>::output_map_;// 声明了 OpAdapter 类的一个模板特化,使用模板参数 T来定义 output_map_。
|
||
// 这个宏用于为 OpAdapter 类声明一个 output_map_ 的模板特化。它将整数键与 OutputDesc 值关联起来。 output_map_ 模板用于将整数标识符映射到 OutputDesc
|
||
|
||
|
||
#define DECLARE_OP_USE_ENUM(T) \
|
||
template <> \
|
||
const mindspore::HashMap<std::string, int> OpAdapter<T>::enum_map_{};// 声明了 OpAdapter 类的一个模板特化,使用模板参数 T,来定义了一个空的 enum_map_。
|
||
// 这个宏用于为 OpAdapter 类声明一个 enum_map_ 的模板特化。它将字符串键与整数值关联起来。模板参数 T 表示数据类型。
|
||
|
||
#define DECLARE_OP_USE_INPUT_ATTR(T) \
|
||
template <> \
|
||
const mindspore::HashMap<unsigned int, AttrDesc> OpAdapter<T>::input_attr_map_;// 声明了 OpAdapter 类的一个模板特化,使用模板参数 T,来定义 input_attr_map_。
|
||
// 这个宏用于为 OpAdapter 类声明一个 input_attr_map_ 的模板特化。它将无符号整数键与 AttrDesc 值关联起来。
|
||
|
||
|
||
#define DECLARE_OP_USE_DYN_INPUT(T) \
|
||
template <> \
|
||
const mindspore::HashMap<int, DynInputDesc> OpAdapter<T>::dyn_input_map_;//声明了 OpAdapter 类的一个模板特化,使用模板参数 T,来定义 dyn_input_map_。
|
||
// 这个宏用于为 OpAdapter 类声明一个 dyn_input_map_ 的模板特化。它将整数键与 DynInputDesc 值关联起来。
|
||
|
||
#define DECLARE_OP_USE_DYN_SUBGRAPH(T) \
|
||
template <> \
|
||
const mindspore::HashMap<int, DynSubGraphDesc> OpAdapter<T>::dyn_subgraph_map_;//声明了 OpAdapter 类的一个模板特化,使用模板参数 T,来定义dyn_subgraph_map_。
|
||
// 这个宏用于为 OpAdapter 类声明一个 dyn_input_map_ 的模板特化。它将整数键与 DynInputDesc值关联起来。
|
||
|
||
#define DECLARE_OP_USE_DYN_OUTPUT(T) \
|
||
template <> \
|
||
const mindspore::HashMap<int, DynOutputDesc> OpAdapter<T>::dyn_output_map_;//声明了 OpAdapter 类的一个模板特化,使用模板参数 T,来定义dyn_output_map_。
|
||
// 这个宏用于为 OpAdapter 类声明一个 dyn_input_map_ 的模板特化。它将整数键与 DynInputDesc值关联起来。
|
||
|
||
#define INPUT_MAP(T) \
|
||
template <> \
|
||
const mindspore::HashMap<int, InputDesc> OpAdapter<T>::input_map_
|
||
// 定义宏 EMPTY_INPUT_MAP,表示一个空的输入映射,使用 mindspore::HashMap<int, InputDesc>() 初始化。
|
||
|
||
// 定义宏 INPUT_DESC(name),用于为输入描述创建一个匿名函数对象。
|
||
#define EMPTY_INPUT_MAP mindspore::HashMap<int, InputDesc>()
|
||
#define INPUT_DESC(name) \
|
||
{ \
|
||
#name, \
|
||
[](const OperatorPtr op, const OperatorPtr input) { \//设置输入
|
||
auto p = std::static_pointer_cast<OpType>(op); \
|
||
(void)p->set_input_##name(*input); \
|
||
}, \
|
||
[](const OperatorPtr op, const OutHandler& handle) { \//处理输出
|
||
auto p = std::static_pointer_cast<OpType>(op); \
|
||
(void)p->set_input_##name(*(handle.op), handle.out); \
|
||
}, \
|
||
[](const OperatorPtr op, const GeTensorDesc desc) { \//更新描述
|
||
auto p = std::static_pointer_cast<OpType>(op); \
|
||
(void)p->update_input_desc_##name(desc); \
|
||
} \
|
||
}//为输入描述提供注释和操作
|
||
|
||
|
||
// 定义宏 DYN_INPUT_MAP(T),用于为 OpAdapter 类声明动态输入映射特化。
|
||
#define DYN_INPUT_MAP(T) \
|
||
template <> \
|
||
const mindspore::HashMap<int, DynInputDesc> OpAdapter<T>::dyn_input_map_
|
||
|
||
// 定义宏 DYN_INPUT_DESC(name),用于为动态输入描述创建一个匿名函数对象。
|
||
#define DYN_INPUT_DESC(name) \
|
||
{ \
|
||
#name, \
|
||
[](const OperatorPtr op, unsigned int num) { \//创建
|
||
auto p = std::static_pointer_cast<OpType>(op); \
|
||
(void)p->create_dynamic_input_##name(num); \
|
||
}, \
|
||
[](const OperatorPtr op, unsigned int index, const OperatorPtr input) { \//设置输入
|
||
auto p = std::static_pointer_cast<OpType>(op); \
|
||
(void)p->set_dynamic_input_##name(index, *input); \
|
||
}, \
|
||
[](const OperatorPtr op, unsigned int index, const OutHandler& handle) { \//处理输出
|
||
auto p = std::static_pointer_cast<OpType>(op); \
|
||
(void)p->set_dynamic_input_##name(index, *(handle.op), handle.out); \
|
||
} \
|
||
}//为输入描述提供注释和操作
|
||
|
||
// 定义宏 DYN_SUBGRAPH_MAP(T),用于为 OpAdapter 类声明动态子图映射特化。
|
||
#define DYN_SUBGRAPH_MAP(T) \
|
||
template <> \
|
||
const mindspore::HashMap<int, DynSubGraphDesc> OpAdapter<T>::dyn_subgraph_map_
|
||
|
||
// 定义宏 DYN_SUBGRAPH_DESC(name),用于为动态子图描述创建一个匿名函数对象。
|
||
#define DYN_SUBGRAPH_DESC(name) \
|
||
{ \
|
||
#name, \
|
||
[](const OperatorPtr op, unsigned int num) { \//创建动态子图
|
||
auto p = std::static_pointer_cast<OpType>(op); \
|
||
(void)p->create_dynamic_subgraph_##name(num); \
|
||
}, \
|
||
[](const OperatorPtr op, unsigned int index, const DfGraphPtr graph) { \//设置子图构建器
|
||
auto p = std::static_pointer_cast<OpType>(op); \
|
||
(void)p->set_dynamic_subgraph_builder_##name(index, [graph](){return *graph;}); \
|
||
} \
|
||
}// 为动态子图描述提供注释和操作
|
||
|
||
// 定义宏 ATTR_MAP(T),用于为 OpAdapter 类声明属性映射特化。
|
||
#define ATTR_MAP(T) \
|
||
template <> \
|
||
const mindspore::HashMap<std::string, AttrDesc> OpAdapter<T>::attr_map_
|
||
#define EMPTY_ATTR_MAP mindspore::HashMap<std::string, AttrDesc>()
|
||
// 定义宏 EMPTY_ATTR_MAP,表示一个空的属性映射,使用 mindspore::HashMap<std::string, AttrDesc>() 初始化。
|
||
#define ATTR_DESC(name, ...) \
|
||
// 定义宏 ATTR_DESC(name, ...),用于为属性描述创建一个匿名函数对象。
|
||
{ \
|
||
#name, \
|
||
[](const OperatorPtr op, const ValuePtr& value) { \ //设置属性值
|
||
auto p = std::static_pointer_cast<OpType>(op); \
|
||
(void)p->set_attr_##name(ConvertAny(value, __VA_ARGS__)); \
|
||
} \
|
||
}// 为属性描述提供注释和操作
|
||
|
||
// 定义宏 INPUT_ATTR_MAP(T),用于为 OpAdapter 类声明输入属性映射特化。
|
||
|
||
//INPUT_ATTR_MAP 宏定义了一个针对类型 T 的模板特化。在这个特化中,有一个名为 input_attr_map_ 的常量哈希映射,它将无符号整数映射到 AttrDesc 对象
|
||
#define INPUT_ATTR_MAP(T) \
|
||
template <> \
|
||
const mindspore::HashMap<unsigned int, AttrDesc> OpAdapter<T>::input_attr_map_ //输入属性映射表
|
||
|
||
//OUTPUT_MAP 宏定义了一个针对类型 T 的模板特化。在这个特化中,有一个名为 output_map_ 的常量哈希映射,它将整数映射到 OutputDesc 对象
|
||
#define OUTPUT_MAP(T) \
|
||
template <> \
|
||
const mindspore::HashMap<int, OutputDesc> OpAdapter<T>::output_map_ //输出属性映射表
|
||
#define OUTPUT_DESC(name) \
|
||
{ \
|
||
#name, \
|
||
[](const OperatorPtr op, const GeTensorDesc desc) { \ //定义一个 Lambda 表达式,接收 OperatorPtr 和 GeTensorDesc 参数
|
||
auto p = std::static_pointer_cast<OpType>(op); \ //将 OperatorPtr 转换为 OpType 的智能指针
|
||
(void)p->update_output_desc_##name(desc); \ //调用 OpType 类的成员函数 update_output_desc_name,其中 name 是宏展开的参数
|
||
} \
|
||
}
|
||
|
||
//DYN_OUTPUT_MAP 宏定义了一个针对类型 T 的模板特化。在这个特化中,有一个名为 dyn_output_map_ 的常量哈希映射,它将整数映射到 DynOutputDesc 对象
|
||
#define DYN_OUTPUT_MAP(T) \
|
||
template <> \
|
||
const mindspore::HashMap<int, DynOutputDesc> OpAdapter<T>::dyn_output_map_ //动态输出映射表
|
||
|
||
#define DYN_OUTPUT_DESC(name) \
|
||
{ \
|
||
#name, \
|
||
[](const OperatorPtr op, unsigned int num) { \ //定义一个 Lambda 表达式,接收 OperatorPtr 和 unsigned int 参数
|
||
auto p = std::static_pointer_cast<OpType>(op); \ //将 OperatorPtr 转换为 OpType 的智能指针
|
||
(void)p->create_dynamic_output_##name(num); \//调用 OpType 类的成员函数 create_dynamic_output_name,其中 name 是宏展开的参数,num 是传递给函数的 unsigned int 参数
|
||
} \
|
||
}
|
||
|
||
#define ADPT_DESC_ONE(T) std::make_shared<OpAdapterDesc>(std::make_shared<OpAdapter<T>>())
|
||
//使用 std::make_shared<OpAdapter<T>>() 创建了一个 OpAdapter<T> 类型的智能指针,并将其作为参数传递给 std::make_shared<OpAdapterDesc>() 来创建 OpAdapterDesc 类型的智能指针。
|
||
#define ADPT_DESC_TWO(T, I) \
|
||
std::make_shared<OpAdapterDesc>(std::make_shared<OpAdapter<T>>(), std::make_shared<OpAdapter<I>>())
|
||
//这个宏定义返回一个 std::shared_ptr<OpAdapterDesc> 对象。
|
||
//它使用 std::make_shared<OpAdapter<T>>()`` 和 std::make_shared<OpAdapter<I>>()创建了两个不同类型的智能指针,然后将它们作为参数传递给std::make_shared<OpAdapterDesc>()来创建OpAdapterDesc类型的智能指针。
|
||
//这个宏表示有两个模板参数T和I` 的情况。
|
||
#define GET_MACRO(_1, _2, DESC, ...) DESC
|
||
//这个宏定义是一个辅助宏,用于根据参数的数量来选择不同的宏定义。在这里,它根据传入的参数数量选择 ADPT_DESC_TWO 或 ADPT_DESC_ONE。
|
||
#define ADPT_DESC(...) GET_MACRO(__VA_ARGS__, ADPT_DESC_TWO, ADPT_DESC_ONE, ...)(__VA_ARGS__)
|
||
//这个宏定义是根据传入的参数数量选择调用 ADPT_DESC_TWO 或 ADPT_DESC_ONE 宏。它将传入的参数原样传递给 GET_MACRO 宏,然后根据参数的数量选择正确的宏。
|
||
#define REG_ADPT_DESC(name, name_str, adpt_desc) \
|
||
static struct RegAdptDesc##name { \
|
||
public: \
|
||
RegAdptDesc##name() { OpAdapterMap::get()[name_str] = adpt_desc; } \
|
||
\
|
||
private: \
|
||
int ph_{0}; \// ph_{0} 是一个无用的成员,用于确保结构体有独一无二的实例化。
|
||
} g_reg_adpt_desc_##name;
|
||
|
||
//这个宏定义用于注册适配器描述。它在静态存储区定义了一个结构体 RegAdptDesc##name,其中 name 是传入的参数。
|
||
//然后,在结构体的构造函数中,将适配器描述 adpt_desc 添加到 OpAdapterMap 的映射中,映射的键是 name_str。
|
||
//这个宏允许在程序运行时自动注册适配器描述。
|
||
|
||
} // namespace mindspore::transform
|
||
#endif // MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_OP_DECLARE_MACRO_H_
|