Commit 2466bacd authored by Robert Kimball's avatar Robert Kimball Committed by Avijit

Bob/nan (#335)

* nan unit test

* fix NAN issue

* add INFINITY support
parent 556fda0a
......@@ -21,6 +21,32 @@
using namespace ngraph;
using namespace std;
template <typename T>
std::string to_cpp_string(T value)
{
string rc;
if (isnan(value))
{
rc = "NAN";
}
else if (isinf(value))
{
if (value > 0)
{
rc = "INFINITY";
}
else
{
rc = "-INFINITY";
}
}
else
{
rc = to_string(value);
}
return rc;
}
op::Constant::~Constant()
{
if (m_data)
......@@ -44,14 +70,14 @@ std::vector<std::string> op::Constant::get_value_strings() const
{
for (float value : get_vector<float>())
{
rc.push_back(to_string(value));
rc.push_back(to_cpp_string(value));
}
}
else if (m_element_type == element::f64)
{
for (double value : get_vector<double>())
{
rc.push_back(to_string(value));
rc.push_back(to_cpp_string(value));
}
}
else if (m_element_type == element::i8)
......@@ -72,7 +98,6 @@ std::vector<std::string> op::Constant::get_value_strings() const
{
for (int32_t value : get_vector<int32_t>())
{
NGRAPH_INFO << value;
rc.push_back(to_string(value));
}
}
......
......@@ -4431,3 +4431,79 @@ TEST(${BACKEND_NAME}, not)
cf->call({a}, {result});
EXPECT_EQ((vector<char>{0, 1, 0, 1}), result->get_vector<char>());
}
TEST(${BACKEND_NAME}, numeric_float_nan)
{
auto shape = Shape{5};
auto A = op::Constant::create(element::f32, shape, {-2.5f, 25.5f, 2.25f, NAN, 6.0f});
auto B = op::Constant::create(element::f32, shape, {10.0f, 5.0f, 2.25f, 10.0f, NAN});
auto rt = make_shared<TensorViewType>(element::boolean, shape);
auto f = make_shared<Function>(make_shared<op::Equal>(A, B), rt, op::Parameters{});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
auto result = backend->make_primary_tensor_view(element::boolean, shape);
cf->call({}, {result});
EXPECT_EQ((vector<char>{false, false, true, false, false}), result->get_vector<char>());
}
TEST(${BACKEND_NAME}, numeric_double_nan)
{
auto shape = Shape{5};
auto A = op::Constant::create(element::f64, shape, {-2.5f, 25.5f, 2.25f, NAN, 6.0f});
auto B = op::Constant::create(element::f64, shape, {10.0f, 5.0f, 2.25f, 10.0f, NAN});
auto rt = make_shared<TensorViewType>(element::boolean, shape);
auto f = make_shared<Function>(make_shared<op::Equal>(A, B), rt, op::Parameters{});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
auto result = backend->make_primary_tensor_view(element::boolean, shape);
cf->call({}, {result});
EXPECT_EQ((vector<char>{false, false, true, false, false}), result->get_vector<char>());
}
TEST(${BACKEND_NAME}, numeric_float_inf)
{
auto shape = Shape{5};
auto A = op::Constant::create(element::f32, shape, {-2.5f, 25.5f, 2.25f, INFINITY, 6.0f});
auto B = op::Constant::create(element::f32, shape, {10.0f, 5.0f, 2.25f, 10.0f, -INFINITY});
auto rt = make_shared<TensorViewType>(element::boolean, shape);
auto f = make_shared<Function>(make_shared<op::Equal>(A, B), rt, op::Parameters{});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
auto result = backend->make_primary_tensor_view(element::boolean, shape);
cf->call({}, {result});
EXPECT_EQ((vector<char>{false, false, true, false, false}), result->get_vector<char>());
}
TEST(${BACKEND_NAME}, numeric_double_inf)
{
auto shape = Shape{5};
auto A = op::Constant::create(element::f64, shape, {-2.5f, 25.5f, 2.25f, INFINITY, 6.0f});
auto B = op::Constant::create(element::f64, shape, {10.0f, 5.0f, 2.25f, 10.0f, -INFINITY});
auto rt = make_shared<TensorViewType>(element::boolean, shape);
auto f = make_shared<Function>(make_shared<op::Equal>(A, B), rt, op::Parameters{});
auto manager = runtime::Manager::get("${BACKEND_NAME}");
auto external = manager->compile(f);
auto backend = manager->allocate_backend();
auto cf = backend->make_call_frame(external);
// Create some tensors for input/output
auto result = backend->make_primary_tensor_view(element::boolean, shape);
cf->call({}, {result});
EXPECT_EQ((vector<char>{false, false, true, false, false}), result->get_vector<char>());
}
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