!30009 Add MakeValue related functions in MindAPI

Merge pull request !30009 from hewei/core_api
This commit is contained in:
i-robot 2022-02-15 01:15:40 +00:00 committed by Gitee
commit a8c9db15ea
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 21 additions and 2 deletions

View File

@ -162,7 +162,10 @@ using ValueNodePtr = SharedPtr<ValueNode>;
/// \param[in] value The given value.
///
/// \return The created ValueNode.
inline ValueNodePtr NewValueNode(const ValuePtr &value) { return MakeShared<ValueNode>(value); }
template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<Value, T>, T>>
inline ValueNodePtr NewValueNode(const SharedPtr<T> &value) {
return MakeShared<ValueNode>(value);
}
/// \brief Create a ValueNode with the given primitive type value.
///

View File

@ -240,7 +240,7 @@ template <typename T, typename = typename std::enable_if_t<is_vector<T>::value,
inline ValuePtr MakeValue(const T &values) {
std::vector<ValuePtr> value_vector;
value_vector.reserve(values.size());
for (auto &value : values) {
for (auto value : values) {
value_vector.emplace_back(MakeValue(value));
}
return MakeShared<ValueTuple>(value_vector);

View File

@ -20,6 +20,7 @@
#include "common/common_test.h"
#include "mindapi/base/logging.h"
#include "mindapi/ir/func_graph.h"
#include "mindapi/ir/primitive.h"
#include "mindapi/ir/tensor.h"
#include "mindapi/ir/utils.h"
@ -150,6 +151,21 @@ TEST_F(TestMindApi, test_values) {
ASSERT_EQ(uint8_values[0], 5);
ASSERT_EQ(uint8_values[1], 6);
ASSERT_EQ(uint8_values[2], 7);
auto seq_bool = MakeValue(std::vector<bool>{true, false, true});
auto seq_bool_ptr = seq_bool->cast<ValueSequencePtr>();
ASSERT_TRUE(seq_bool_ptr != nullptr);
ASSERT_EQ(seq_bool_ptr->size(), 3);
ASSERT_EQ(seq_bool_ptr->value().size(), 3);
ASSERT_TRUE(seq_bool_ptr->value()[0]->isa<BoolImm>());
ASSERT_TRUE(GetValue<bool>(seq_bool_ptr->value()[0]));
ASSERT_FALSE(GetValue<bool>(seq_bool_ptr->value()[1]));
ASSERT_TRUE(GetValue<bool>(seq_bool_ptr->value()[2]));
auto prim = MakeShared<Primitive>("myprim");
auto node = NewValueNode(prim);
ASSERT_TRUE(node != nullptr);
ASSERT_EQ(node->value(), prim);
}
/// Feature: MindAPI