transform/op_adapter_util.h

73 lines
3.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Copyright 2019 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_ADAPTER_UTIL_H_
#define MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_OP_ADAPTER_UTIL_H_
#include <string>
#include <vector>
#include "transform/graph_ir/op_adapter_base.h"
namespace mindspore {
namespace transform {
template <typename P, typename Q>
static Q ConvertAnyUtil(const ValuePtr &value, const AnyTraits<P> &, const AnyTraits<Q> &) {
return static_cast<Q>(GetValue<P>(value));
}
GeTensor ConvertAnyUtil(const ValuePtr &value, const AnyTraits<mindspore::tensor::Tensor> &traits);
std::vector<int64_t> ConvertAnyUtil(const ValuePtr &value, const std::string &name,
const AnyTraits<std::vector<int64_t>>);
std::string ConvertAnyUtil(const ValuePtr &value, const AnyTraits<std::vector<int64_t>>, const AnyTraits<std::string>);
std::vector<float> ConvertAnyUtil(const ValuePtr &value, const AnyTraits<std::vector<float>>, const AnyTraits<float>);
std::vector<int64_t> ConvertAnyUtil(const ValuePtr &value, const std::string &format,
const AnyTraits<std::vector<int64_t>>, const AnyTraits<int64_t>);
GeDataType ConvertAnyUtil(const ValuePtr &value, const AnyTraits<GEType>);
template <typename P, typename Q>
// ConvertAnyUtil 函数用于将给定的 ValuePtr 转换为具有类型 P 的元素的 std::vector<Q>。
// 这里 P 和 Q 可以是不同的类型。
std::vector<Q> ConvertAnyUtil(const ValuePtr &value, AnyTraits<P>, const AnyTraits<std::vector<Q>>) {
MS_EXCEPTION_IF_NULL(value); // 检查给定的 ValuePtr 是否为空指针,如果是,抛出异常。
// 检查给定的 ValuePtr 是否为 ValueTuple 或 ValueList如果不是抛出异常。
if (!value->isa<ValueTuple>() && !value->isa<ValueList>()) {
MS_LOG(EXCEPTION) << "error convert Value to vector for value: " << value->ToString()
<< ", type: " << value->type_name() << ", value should be a tuple or list";
}
// 获取 ValuePtr 中的数据集合,可以是 ValueTuple 或 ValueList。
auto vec = value->isa<ValueTuple>() ? value->cast<ValueTuplePtr>()->value() : value->cast<ValueListPtr>()->value();
std::vector<Q> data; // 创建 std::vector<Q>,用于存储转换后的结果。
for (auto &it : vec) { // 遍历集合中的每个元素,对每个元素调用 ConvertAnyUtil 进行转换,并将结果添加到 data 中。
data.push_back(ConvertAnyUtil(it, AnyTraits<P>(), AnyTraits<Q>()));
}
return data; // 返回转换后的 std::vector<Q>。
}
GeTensor ConvertAnyUtil(const ValuePtr &value, const AnyTraits<AnyValue>);
bool IsCustomPrim(const PrimitivePtr &prim);
bool IsCustomCNode(const AnfNodePtr &node);
std::string GetOpIOFormat(const AnfNodePtr &node);
} // namespace transform
} // namespace mindspore
#endif // MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_OP_ADAPTER_UTIL_H_