OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[TF FE] Support bf16 constants stored in half_val (#39097)
Gilad Zamoscinski36ba7864363e
diff --git a/src/frontends/tensorflow/src/tf_utils.cpp b/src/frontends/tensorflow/src/tf_utils.cppindex 3a96ba86ed6..207c6f86965 100644--- a/src/frontends/tensorflow/src/tf_utils.cpp+++ b/src/frontends/tensorflow/src/tf_utils.cpp@@ -105,7 +105,11 @@ void extract_compressed_tensor_content(const ::tensorflow::TensorProto& tensor_pval_i = tensor_proto.int64_val()[i];break;case f16:- val_i = float16::from_bits(tensor_proto.half_val()[i]);+ val_i = static_cast<SRC_T>(float16::from_bits(static_cast<uint16_t>(tensor_proto.half_val()[i])));+ break;+ case bf16:+ // TensorFlow stores bfloat16 values as raw 16-bit patterns in half_val, the same field as DT_HALF+ val_i = static_cast<SRC_T>(bfloat16::from_bits(static_cast<uint16_t>(tensor_proto.half_val()[i])));break;case f32:val_i = tensor_proto.float_val()[i];@@ -329,6 +333,10 @@ Any unpack_tensor_proto(const ::tensorflow::TensorProto& tensor_proto,val_size = tensor_proto.half_val_size();extract_compressed_tensor_content<float16>(tensor_proto, val_size, &res);break;+ case bf16:+ val_size = tensor_proto.half_val_size();+ extract_compressed_tensor_content<bfloat16>(tensor_proto, val_size, &res);+ break;case element::string: {auto string_val_size = static_cast<size_t>(tensor_proto.string_val_size());FRONT_END_GENERAL_CHECK(diff --git a/src/frontends/tensorflow/tests/convert_tricky_models.cpp b/src/frontends/tensorflow/tests/convert_tricky_models.cppindex 6e7e4dc3c3f..dcdc3ba0366 100644--- a/src/frontends/tensorflow/tests/convert_tricky_models.cpp+++ b/src/frontends/tensorflow/tests/convert_tricky_models.cpp@@ -335,6 +335,22 @@ TEST_F(FrontEndConversionWithReferenceTestsF, ModelWithConstResultSubgraphs) {}}+TEST_F(FrontEndConversionWithReferenceTestsF, ModelWithBF16ConstantsInHalfVal) {+ // bfloat16 Const values stored in half_val as raw 16-bit patterns: a scalar (0x4000 = 2.0)+ // and a splat with a single value for a [2, 3] shape (0x3FC0 = 1.5)+ { model = convert_model("model_bf16_const_half_val/model_bf16_const_half_val.pbtxt"); }+ {+ // create a reference graph+ auto x = make_shared<v0::Parameter>(element::bf16, Shape{2, 3});+ auto scalar_const = make_shared<v0::Constant>(element::bf16, Shape{}, vector<bfloat16>{bfloat16(2.0f)});+ auto mul = make_shared<v1::Multiply>(x, scalar_const);+ auto splat_const = make_shared<v0::Constant>(element::bf16, Shape{2, 3}, vector<bfloat16>(6, bfloat16(1.5f)));+ auto add = make_shared<v1::Add>(mul, splat_const);++ model_ref = make_shared<Model>(OutputVector{add}, ParameterVector{x});+ }+}+TEST_F(FrontEndConversionWithReferenceTestsF, ModelWithIteratorGetNext) {{ model = convert_model("model_with_iterator_get_next/model_with_iterator_get_next.pbtxt"); }{diff --git a/src/frontends/tensorflow/tests/sources.cmake b/src/frontends/tensorflow/tests/sources.cmakeindex 08f68e2add4..4e96525bd31 100644--- a/src/frontends/tensorflow/tests/sources.cmake+++ b/src/frontends/tensorflow/tests/sources.cmake@@ -61,6 +61,7 @@ set(TENSORFLOW_MODELS_PBTXT${CMAKE_CURRENT_LIST_DIR}/test_models/models_pbtxt/forward_edge_model_unsorted.pbtxt${CMAKE_CURRENT_LIST_DIR}/test_models/models_pbtxt/gather_tree_model.pbtxt${CMAKE_CURRENT_LIST_DIR}/test_models/models_pbtxt/injected_body_and_if.pbtxt+ ${CMAKE_CURRENT_LIST_DIR}/test_models/models_pbtxt/model_bf16_const_half_val.pbtxt${CMAKE_CURRENT_LIST_DIR}/test_models/models_pbtxt/model_ngram.pbtxt${CMAKE_CURRENT_LIST_DIR}/test_models/models_pbtxt/model_savev2.pbtxt${CMAKE_CURRENT_LIST_DIR}/test_models/models_pbtxt/model_switch_merge_several_cond_flows.pbtxtdiff --git a/src/frontends/tensorflow/tests/test_models/models_pbtxt/model_bf16_const_half_val.pbtxt b/src/frontends/tensorflow/tests/test_models/models_pbtxt/model_bf16_const_half_val.pbtxtnew file mode 100644index 00000000000..bb4dfebca3e--- /dev/null+++ b/src/frontends/tensorflow/tests/test_models/models_pbtxt/model_bf16_const_half_val.pbtxt@@ -0,0 +1,95 @@+node {+ name: "x"+ op: "Placeholder"+ attr {+ key: "dtype"+ value {+ type: DT_BFLOAT16+ }+ }+ attr {+ key: "shape"+ value {+ shape {+ dim {+ size: 2+ }+ dim {+ size: 3+ }+ }+ }+ }+}+node {+ name: "scalar_const"+ op: "Const"+ attr {+ key: "dtype"+ value {+ type: DT_BFLOAT16+ }+ }+ attr {+ key: "value"+ value {+ tensor {+ dtype: DT_BFLOAT16+ tensor_shape {+ }+ half_val: 16384+ }+ }+ }+}+node {+ name: "splat_const"+ op: "Const"+ attr {+ key: "dtype"+ value {+ type: DT_BFLOAT16+ }+ }+ attr {+ key: "value"+ value {+ tensor {+ dtype: DT_BFLOAT16+ tensor_shape {+ dim {+ size: 2+ }+ dim {+ size: 3+ }+ }+ half_val: 16320+ }+ }+ }+}+node {+ name: "mul"+ op: "Mul"+ input: "x"+ input: "scalar_const"+ attr {+ key: "T"+ value {+ type: DT_BFLOAT16+ }+ }+}+node {+ name: "add"+ op: "AddV2"+ input: "mul"+ input: "splat_const"+ attr {+ key: "T"+ value {+ type: DT_BFLOAT16+ }+ }+}