Handle f32/bf16 conversion in UT (#5330)
This commit is contained in:
1 parent
82e2b4188b
commit
3e8bc2db69
5 files changed
+55
-54
No files matched your search
@@ -9,7 +9,9 @@
|
||||
#include "runtime/backend.hpp"
|
||||
#include "util/all_close.hpp"
|
||||
#include "util/all_close_f.hpp"
|
||||
#include "util/engine/test_engines.hpp"
|
||||
#include "util/ndarray.hpp"
|
||||
#include "util/test_case.hpp"
|
||||
#include "util/test_control.hpp"
|
||||
#include "util/test_tools.hpp"
|
||||
|
||||
@@ -18,6 +20,8 @@ using namespace ngraph;
|
||||
|
||||
static string s_manifest = "${MANIFEST}";
|
||||
|
||||
using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME});
|
||||
|
||||
NGRAPH_TEST(${BACKEND_NAME}, convert_int32_float32)
|
||||
{
|
||||
Shape shape{2, 2};
|
||||
@@ -104,61 +108,35 @@ NGRAPH_TEST(${BACKEND_NAME}, convert_float32_bool)
|
||||
|
||||
NGRAPH_TEST(${BACKEND_NAME}, convert_float32_bf16)
|
||||
{
|
||||
Shape shape_a{1, 1, 3, 5};
|
||||
|
||||
// input data
|
||||
vector<float> a_data = {
|
||||
const vector<float> a_data = {
|
||||
0.5f, 1.5f, 0.5f, 2.5f, 1.5f, 0.5f, 3.5f, 2.5f, 0.5f, 0.5f, 2.5f, 0.5f, 0.5f, 0.5f, 1.5f};
|
||||
|
||||
auto A = make_shared<op::Parameter>(element::f32, shape_a);
|
||||
auto convert = make_shared<op::Convert>(A, element::bf16);
|
||||
auto f = make_shared<Function>(NodeVector{convert}, ParameterVector{A});
|
||||
auto backend = runtime::Backend::create("${BACKEND_NAME}");
|
||||
// Create some tensors for input/output
|
||||
auto a = backend->create_tensor(element::f32, shape_a);
|
||||
copy_data(a, a_data);
|
||||
auto result = backend->create_tensor(element::bf16, shape_a);
|
||||
auto handle = backend->compile(f);
|
||||
handle->call_with_validate({result}, {a});
|
||||
EXPECT_EQ((vector<bfloat16>{
|
||||
0.5, 1.5, 0.5, 2.5, 1.5, 0.5, 3.5, 2.5, 0.5, 0.5, 2.5, 0.5, 0.5, 0.5, 1.5}),
|
||||
read_vector<bfloat16>(result));
|
||||
const auto A = make_shared<op::Parameter>(element::f32, Shape{1, 1, 3, 5});
|
||||
const auto convert = make_shared<op::Convert>(A, element::bf16);
|
||||
const auto f = make_shared<Function>(NodeVector{convert}, ParameterVector{A});
|
||||
|
||||
auto test_case = test::TestCase<TestEngine>(f);
|
||||
test_case.add_input<float>(a_data);
|
||||
test_case.add_expected_output<bfloat16>(
|
||||
std::vector<bfloat16>(std::begin(a_data), std::end(a_data)));
|
||||
|
||||
test_case.run();
|
||||
}
|
||||
|
||||
NGRAPH_TEST(${BACKEND_NAME}, convert_bf16_float32)
|
||||
{
|
||||
Shape shape_a{1, 1, 3, 5};
|
||||
|
||||
// input data
|
||||
vector<bfloat16> a_data = {
|
||||
const vector<bfloat16> a_data = {
|
||||
0.5, 1.5, 0.5, 2.5, 1.5, 0.5, 3.5, 2.5, 0.5, 0.5, 2.5, 0.5, 0.5, 0.5, 1.5};
|
||||
|
||||
auto A = make_shared<op::Parameter>(element::bf16, shape_a);
|
||||
auto convert = make_shared<op::Convert>(A, element::f32);
|
||||
auto f = make_shared<Function>(NodeVector{convert}, ParameterVector{A});
|
||||
auto backend = runtime::Backend::create("${BACKEND_NAME}");
|
||||
// Create some tensors for input/output
|
||||
auto a = backend->create_tensor(element::bf16, shape_a);
|
||||
copy_data(a, a_data);
|
||||
auto result = backend->create_tensor(element::f32, shape_a);
|
||||
auto handle = backend->compile(f);
|
||||
handle->call_with_validate({result}, {a});
|
||||
EXPECT_EQ((vector<float>{0.5f,
|
||||
1.5f,
|
||||
0.5f,
|
||||
2.5f,
|
||||
1.5f,
|
||||
0.5f,
|
||||
3.5f,
|
||||
2.5f,
|
||||
0.5f,
|
||||
0.5f,
|
||||
2.5f,
|
||||
0.5f,
|
||||
0.5f,
|
||||
0.5f,
|
||||
1.5f}),
|
||||
read_vector<float>(result));
|
||||
const auto A = make_shared<op::Parameter>(element::bf16, Shape{1, 1, 3, 5});
|
||||
const auto convert = make_shared<op::Convert>(A, element::f32);
|
||||
const auto f = make_shared<Function>(NodeVector{convert}, ParameterVector{A});
|
||||
|
||||
auto test_case = test::TestCase<TestEngine>(f);
|
||||
test_case.add_input<bfloat16>(a_data);
|
||||
test_case.add_expected_output<float>(std::vector<float>(std::begin(a_data), std::end(a_data)));
|
||||
|
||||
test_case.run();
|
||||
}
|
||||
|
||||
NGRAPH_TEST(${BACKEND_NAME}, convert_fp16_float32)
|
||||
|
||||
@@ -565,10 +565,6 @@ quantize_clamp_int32
|
||||
max_3d_to_scalar_double
|
||||
argmin_trivial_in_double
|
||||
|
||||
# Incorrect precision bf16!
|
||||
convert_float32_bf16
|
||||
convert_bf16_float32
|
||||
|
||||
# [nGraph upgrade pass] Unable to convert Softmax:0 to Softmax:1 with zero or more than one axis.
|
||||
softmax_dynamic_axes
|
||||
softmax_all
|
||||
|
||||
@@ -1545,6 +1545,9 @@ namespace
|
||||
case element::Type_t::f64:
|
||||
convert_v0::evaluate<element::Type_t::f64, OUT_ET>(op, outputs, inputs);
|
||||
break;
|
||||
case element::Type_t::bf16:
|
||||
convert_v0::evaluate<element::Type_t::bf16, OUT_ET>(op, outputs, inputs);
|
||||
break;
|
||||
default: return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
fake_quantize_pdpd
|
||||
convert_float32_bf16
|
||||
convert_bf16_float32
|
||||
|
||||
INTERPRETER.onnx_model_quant_conv_linear
|
||||
INTERPRETER.onnx_top_k_opset_10
|
||||
|
||||
@@ -59,6 +59,29 @@ namespace
|
||||
return ngraph::test::all_close<T>(test_results.first, test_results.second);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_class<T>::value, testing::AssertionResult>::type
|
||||
compare_blobs(InferenceEngine::MemoryBlob::CPtr computed,
|
||||
InferenceEngine::MemoryBlob::CPtr expected,
|
||||
const size_t tolerance_bits)
|
||||
{
|
||||
const auto test_results = extract_test_results<T>(computed, expected);
|
||||
|
||||
NGRAPH_CHECK(test_results.first.size() == test_results.second.size(),
|
||||
"Number of expected and computed results don't match");
|
||||
|
||||
std::vector<double> expected_double(test_results.first.size());
|
||||
std::vector<double> result_double(test_results.second.size());
|
||||
|
||||
for (size_t i = 0; i < test_results.first.size(); ++i)
|
||||
{
|
||||
expected_double[i] = static_cast<double>(test_results.first[i]);
|
||||
result_double[i] = static_cast<double>(test_results.second[i]);
|
||||
}
|
||||
|
||||
return ngraph::test::all_close_f(expected_double, result_double, tolerance_bits);
|
||||
}
|
||||
|
||||
/// Compares two blobs elementwise
|
||||
inline testing::AssertionResult compare_blobs(InferenceEngine::MemoryBlob::CPtr computed,
|
||||
InferenceEngine::MemoryBlob::CPtr expected,
|
||||
@@ -107,7 +130,10 @@ namespace
|
||||
case InferenceEngine::Precision::BOOL:
|
||||
return compare_blobs<uint8_t>(computed, expected, tolerance_bits);
|
||||
break;
|
||||
default: IE_THROW() << "Not implemented yet";
|
||||
case InferenceEngine::Precision::BF16:
|
||||
return compare_blobs<bfloat16>(computed, expected, tolerance_bits);
|
||||
break;
|
||||
default: THROW_IE_EXCEPTION << "Not implemented yet";
|
||||
}
|
||||
}
|
||||
}; // namespace
|
||||
|
||||
Reference in new issue
Block a user