Commit cf09e68d authored by Jaikrishnan Menon's avatar Jaikrishnan Menon

CPU: Implement support for scalar and tensor constants

via op::ParameterizedConstant
parent 9f507559
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "ngraph/node.hpp" #include "ngraph/node.hpp"
#include "ngraph/descriptor/layout/dense_tensor_view_layout.hpp" #include "ngraph/descriptor/layout/dense_tensor_view_layout.hpp"
#include "ngraph/ops/concatenate.hpp" #include "ngraph/ops/concatenate.hpp"
#include "ngraph/ops/constant.hpp"
#include "ngraph/ops/get_tuple_element.hpp" #include "ngraph/ops/get_tuple_element.hpp"
#include "ngraph/runtime/tensor_view_info.hpp" #include "ngraph/runtime/tensor_view_info.hpp"
#include "ngraph/runtime/cpu/external_function.hpp" #include "ngraph/runtime/cpu/external_function.hpp"
...@@ -551,3 +552,162 @@ void Emitter::EMITTER_DECL(EmitSubtract) ...@@ -551,3 +552,162 @@ void Emitter::EMITTER_DECL(EmitSubtract)
EIGEN_VECTOR_FORMAT(inputs[1].get_layout<DenseTensorViewLayout>()->get_size()) ");\n" EIGEN_VECTOR_FORMAT(inputs[1].get_layout<DenseTensorViewLayout>()->get_size()) ");\n"
" }\n"; " }\n";
} }
void Emitter::EMITTER_DECL(EmitParameterizedConstantBool)
{
auto value = dynamic_cast<const op::ParameterizedConstant<ngraph::element::Bool>*>(n)->get_value()->get_vector();
TU += " {\n"
" call_frame->get_parameterized_tensor_view<" + element_type_names[TI(ngraph::element::Bool)] +
">(" + to_string(outputs[0].get_index()) + ")->get_vector() = std::vector<" + element_type_names[TI(ngraph::element::Bool)] +
"::type>{";
for (size_t i = 0; i < value.size(); i++)
{
if (i)
TU += ", ";
if (value[i])
{
TU += "true";
}
else
{
TU += "false";
}
}
TU += "};\n }\n";
}
void Emitter::EMITTER_DECL(EmitParameterizedConstantFloat32)
{
auto value = dynamic_cast<const op::ParameterizedConstant<ngraph::element::Float32>*>(n)->get_value()->get_vector();
TU += " {\n"
" call_frame->get_parameterized_tensor_view<" + element_type_names[TI(ngraph::element::Float32)] +
">(" + to_string(outputs[0].get_index()) + ")->get_vector() = std::vector<" + element_type_names[TI(ngraph::element::Float32)] +
"::type>{";
for (size_t i = 0; i < value.size(); i++)
{
if (i)
TU += ", ";
TU += to_string(value[i]) + "f";
}
TU += "};\n }\n";
}
void Emitter::EMITTER_DECL(EmitParameterizedConstantInt8)
{
auto value = dynamic_cast<const op::ParameterizedConstant<ngraph::element::Int8>*>(n)->get_value()->get_vector();
TU += " {\n"
" call_frame->get_parameterized_tensor_view<" + element_type_names[TI(ngraph::element::Int8)] +
">(" + to_string(outputs[0].get_index()) + ")->get_vector() = std::vector<" + element_type_names[TI(ngraph::element::Int8)] +
"::type>{";
for (size_t i = 0; i < value.size(); i++)
{
if (i)
TU += ", ";
TU += to_string(value[i]);
}
TU += "};\n }\n";
}
void Emitter::EMITTER_DECL(EmitParameterizedConstantInt32)
{
auto value = dynamic_cast<const op::ParameterizedConstant<ngraph::element::Int32>*>(n)->get_value()->get_vector();
TU += " {\n"
" call_frame->get_parameterized_tensor_view<" + element_type_names[TI(ngraph::element::Int32)] +
">(" + to_string(outputs[0].get_index()) + ")->get_vector() = std::vector<" + element_type_names[TI(ngraph::element::Int32)] +
"::type>{";
for (size_t i = 0; i < value.size(); i++)
{
if (i)
TU += ", ";
TU += to_string(value[i]);
}
TU += "};\n }\n";
}
void Emitter::EMITTER_DECL(EmitParameterizedConstantInt64)
{
auto value = dynamic_cast<const op::ParameterizedConstant<ngraph::element::Int64>*>(n)->get_value()->get_vector();
TU += " {\n"
" call_frame->get_parameterized_tensor_view<" + element_type_names[TI(ngraph::element::Int64)] +
">(" + to_string(outputs[0].get_index()) + ")->get_vector() = std::vector<" + element_type_names[TI(ngraph::element::Int64)] +
"::type>{";
for (size_t i = 0; i < value.size(); i++)
{
if (i)
TU += ", ";
TU += to_string(value[i]);
}
TU += "};\n }\n";
}
void Emitter::EMITTER_DECL(EmitParameterizedConstantUInt8)
{
auto value = dynamic_cast<const op::ParameterizedConstant<ngraph::element::UInt8>*>(n)->get_value()->get_vector();
TU += " {\n"
" call_frame->get_parameterized_tensor_view<" + element_type_names[TI(ngraph::element::UInt8)] +
">(" + to_string(outputs[0].get_index()) + ")->get_vector() = std::vector<" + element_type_names[TI(ngraph::element::UInt8)] +
"::type>{";
for (size_t i = 0; i < value.size(); i++)
{
if (i)
TU += ", ";
TU += to_string(value[i]);
}
TU += "};\n }\n";
}
void Emitter::EMITTER_DECL(EmitParameterizedConstantUInt32)
{
auto value = dynamic_cast<const op::ParameterizedConstant<ngraph::element::UInt32>*>(n)->get_value()->get_vector();
TU += " {\n"
" call_frame->get_parameterized_tensor_view<" + element_type_names[TI(ngraph::element::UInt32)] +
">(" + to_string(outputs[0].get_index()) + ")->get_vector() = std::vector<" + element_type_names[TI(ngraph::element::UInt32)] +
"::type>{";
for (size_t i = 0; i < value.size(); i++)
{
if (i)
TU += ", ";
TU += to_string(value[i]);
}
TU += "};\n }\n";
}
void Emitter::EMITTER_DECL(EmitParameterizedConstantUInt64)
{
auto value = dynamic_cast<const op::ParameterizedConstant<ngraph::element::UInt64>*>(n)->get_value()->get_vector();
TU += " {\n"
" call_frame->get_parameterized_tensor_view<" + element_type_names[TI(ngraph::element::UInt64)] +
">(" + to_string(outputs[0].get_index()) + ")->get_vector() = std::vector<" + element_type_names[TI(ngraph::element::UInt64)] +
"::type>{";
for (size_t i = 0; i < value.size(); i++)
{
if (i)
TU += ", ";
TU += to_string(value[i]);
}
TU += "};\n }\n";
}
...@@ -63,6 +63,14 @@ namespace ngraph ...@@ -63,6 +63,14 @@ namespace ngraph
void EMITTER_DECL(EmitNotEqual); void EMITTER_DECL(EmitNotEqual);
void EMITTER_DECL(EmitSelect); void EMITTER_DECL(EmitSelect);
void EMITTER_DECL(EmitSubtract); void EMITTER_DECL(EmitSubtract);
void EMITTER_DECL(EmitParameterizedConstantBool);
void EMITTER_DECL(EmitParameterizedConstantFloat32);
void EMITTER_DECL(EmitParameterizedConstantInt8);
void EMITTER_DECL(EmitParameterizedConstantInt32);
void EMITTER_DECL(EmitParameterizedConstantInt64);
void EMITTER_DECL(EmitParameterizedConstantUInt8);
void EMITTER_DECL(EmitParameterizedConstantUInt32);
void EMITTER_DECL(EmitParameterizedConstantUInt64);
}; };
} }
......
...@@ -89,7 +89,15 @@ static const OpMap dispatcher{{TI(ngraph::op::Add), &Emitter::EmitAdd}, ...@@ -89,7 +89,15 @@ static const OpMap dispatcher{{TI(ngraph::op::Add), &Emitter::EmitAdd},
{TI(ngraph::op::Negative), &Emitter::EmitNegative}, {TI(ngraph::op::Negative), &Emitter::EmitNegative},
{TI(ngraph::op::NotEqual), &Emitter::EmitNotEqual}, {TI(ngraph::op::NotEqual), &Emitter::EmitNotEqual},
{TI(ngraph::op::Select), &Emitter::EmitSelect}, {TI(ngraph::op::Select), &Emitter::EmitSelect},
{TI(ngraph::op::Subtract), &Emitter::EmitSubtract} {TI(ngraph::op::Subtract), &Emitter::EmitSubtract},
{TI(ngraph::op::ParameterizedConstant<ngraph::element::Bool>), &Emitter::EmitParameterizedConstantBool},
{TI(ngraph::op::ParameterizedConstant<ngraph::element::Float32>), &Emitter::EmitParameterizedConstantFloat32},
{TI(ngraph::op::ParameterizedConstant<ngraph::element::Int8>), &Emitter::EmitParameterizedConstantInt8},
{TI(ngraph::op::ParameterizedConstant<ngraph::element::Int32>), &Emitter::EmitParameterizedConstantInt32},
{TI(ngraph::op::ParameterizedConstant<ngraph::element::Int64>), &Emitter::EmitParameterizedConstantInt64},
{TI(ngraph::op::ParameterizedConstant<ngraph::element::UInt8>), &Emitter::EmitParameterizedConstantUInt8},
{TI(ngraph::op::ParameterizedConstant<ngraph::element::UInt32>), &Emitter::EmitParameterizedConstantUInt32},
{TI(ngraph::op::ParameterizedConstant<ngraph::element::UInt64>), &Emitter::EmitParameterizedConstantUInt64},
}; };
......
...@@ -1022,8 +1022,7 @@ TEST(cpu, subtract) ...@@ -1022,8 +1022,7 @@ TEST(cpu, subtract)
ASSERT_EQ((vector<float>{1, 2, 4, 8}), result->get_vector()); ASSERT_EQ((vector<float>{1, 2, 4, 8}), result->get_vector());
} }
/* TEST(cpu, scalar_constant)
TEST(execute, scalar_constant)
{ {
auto shape = Shape{}; auto shape = Shape{};
auto t = ngraph::runtime::make_tensor<element::Float32>(shape); auto t = ngraph::runtime::make_tensor<element::Float32>(shape);
...@@ -1032,7 +1031,7 @@ TEST(execute, scalar_constant) ...@@ -1032,7 +1031,7 @@ TEST(execute, scalar_constant)
auto rt = make_shared<TensorViewType>(element::Float32::element_type(), shape); auto rt = make_shared<TensorViewType>(element::Float32::element_type(), shape);
auto f = make_shared<Function>(A, rt, op::Parameters{}); auto f = make_shared<Function>(A, rt, op::Parameters{});
auto manager = runtime::Manager::get("NGVM"); auto manager = runtime::Manager::get("CPU");
auto external = manager->compile(f); auto external = manager->compile(f);
auto backend = manager->allocate_backend(); auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external); auto cf = backend->make_call_frame(external);
...@@ -1044,7 +1043,7 @@ TEST(execute, scalar_constant) ...@@ -1044,7 +1043,7 @@ TEST(execute, scalar_constant)
ASSERT_EQ((vector<float>{-3.0f}), result->get_vector()); ASSERT_EQ((vector<float>{-3.0f}), result->get_vector());
} }
TEST(execute, tensor_constant) TEST(cpu, tensor_constant)
{ {
auto shape = Shape{2, 2, 2}; auto shape = Shape{2, 2, 2};
auto t = ngraph::runtime::make_tensor<element::Float32>(shape); auto t = ngraph::runtime::make_tensor<element::Float32>(shape);
...@@ -1053,7 +1052,7 @@ TEST(execute, tensor_constant) ...@@ -1053,7 +1052,7 @@ TEST(execute, tensor_constant)
auto rt = make_shared<TensorViewType>(element::Float32::element_type(), shape); auto rt = make_shared<TensorViewType>(element::Float32::element_type(), shape);
auto f = make_shared<Function>(A, rt, op::Parameters{}); auto f = make_shared<Function>(A, rt, op::Parameters{});
auto manager = runtime::Manager::get("NGVM"); auto manager = runtime::Manager::get("CPU");
auto external = manager->compile(f); auto external = manager->compile(f);
auto backend = manager->allocate_backend(); auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external); auto cf = backend->make_call_frame(external);
...@@ -1065,7 +1064,7 @@ TEST(execute, tensor_constant) ...@@ -1065,7 +1064,7 @@ TEST(execute, tensor_constant)
ASSERT_EQ((vector<float>{1, 2, 3, 4, 5, 6, 7, 8}), result->get_vector()); ASSERT_EQ((vector<float>{1, 2, 3, 4, 5, 6, 7, 8}), result->get_vector());
} }
TEST(execute, tensor_constant_with_op) TEST(cpu, tensor_constant_with_op)
{ {
auto shape = Shape{2, 2, 2}; auto shape = Shape{2, 2, 2};
auto t = ngraph::runtime::make_tensor<element::Float32>(shape); auto t = ngraph::runtime::make_tensor<element::Float32>(shape);
...@@ -1074,7 +1073,7 @@ TEST(execute, tensor_constant_with_op) ...@@ -1074,7 +1073,7 @@ TEST(execute, tensor_constant_with_op)
auto rt = make_shared<TensorViewType>(element::Float32::element_type(), shape); auto rt = make_shared<TensorViewType>(element::Float32::element_type(), shape);
auto f = make_shared<Function>(make_shared<op::Abs>(A), rt, op::Parameters{}); auto f = make_shared<Function>(make_shared<op::Abs>(A), rt, op::Parameters{});
auto manager = runtime::Manager::get("NGVM"); auto manager = runtime::Manager::get("CPU");
auto external = manager->compile(f); auto external = manager->compile(f);
auto backend = manager->allocate_backend(); auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external); auto cf = backend->make_call_frame(external);
...@@ -1086,6 +1085,7 @@ TEST(execute, tensor_constant_with_op) ...@@ -1086,6 +1085,7 @@ TEST(execute, tensor_constant_with_op)
ASSERT_EQ((vector<float>{1, 2, 3, 4, 5, 6, 7, 8}), result->get_vector()); ASSERT_EQ((vector<float>{1, 2, 3, 4, 5, 6, 7, 8}), result->get_vector());
} }
/*
TEST(execute, function_call) TEST(execute, function_call)
{ {
// First create "f(A,B,C) = (A+B)*C". // First create "f(A,B,C) = (A+B)*C".
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment