chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
+182
View File
@@ -0,0 +1,182 @@
load(
"//tensorflow:tensorflow.bzl",
"tf_cc_test",
"tf_copts",
)
load("//tensorflow:tensorflow.default.bzl", "filegroup")
load(
"//tensorflow/core/platform:build_config.bzl",
"tf_jspb_proto_library",
"tf_proto_library",
"tf_pyclif_proto_library",
)
load("//tensorflow/core/platform:rules_cc.bzl", "cc_library")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
default_visibility = [
"//visibility:public",
],
licenses = ["notice"],
)
# TODO(bmzhao): Refactor this target to use granular dependencies
# after stage 4 of the TF build refactor is complete:
# https://github.com/tensorflow/community/pull/179
cc_library(
name = "example_parser_configuration",
srcs = ["example_parser_configuration.cc"],
hdrs = ["example_parser_configuration.h"],
copts = tf_copts(),
linkstatic = 1,
deps = [
"//tensorflow/core:core_cpu",
"//tensorflow/core:core_cpu_internal",
"//tensorflow/core:framework",
"//tensorflow/core:lib",
"//tensorflow/core:lib_internal",
"//tensorflow/core:protos_all_cc",
],
alwayslink = 1,
)
cc_library(
name = "feature_util",
srcs = ["feature_util.cc"],
hdrs = ["feature_util.h"],
deps = [
":example_protos_cc",
"//tensorflow/core/platform:protobuf",
"//tensorflow/core/platform:stringpiece",
"@com_google_absl//absl/strings",
],
alwayslink = 1,
)
tf_cc_test(
name = "feature_util_test",
size = "small",
srcs = ["feature_util_test.cc"],
deps = [
":example_protos_cc",
":feature_util",
"//tensorflow/core:test",
"//tensorflow/core:test_main",
"//tensorflow/core/platform:types",
"@com_google_absl//absl/strings",
],
)
# TODO(bmzhao): Refactor this target to use granular dependencies
# after stage 4 of the TF build refactor is complete:
# https://github.com/tensorflow/community/pull/179
tf_cc_test(
name = "example_parser_configuration_test",
size = "small",
srcs = ["example_parser_configuration_test.cc"],
data = [":example_parser_configuration_testdata"],
deps = [
"//tensorflow/cc:cc_ops",
"//tensorflow/core:core_cpu",
"//tensorflow/core:core_cpu_internal",
"//tensorflow/core:example_parser_configuration",
"//tensorflow/core:framework",
"//tensorflow/core:framework_internal",
"//tensorflow/core:lib",
"//tensorflow/core:lib_internal",
"//tensorflow/core:ops",
"//tensorflow/core:protos_all_cc",
"//tensorflow/core:test",
"//tensorflow/core:test_main",
"//tensorflow/core:testlib",
"//tensorflow/core/common_runtime:direct_session_internal",
"//tensorflow/core/kernels:example_parsing_ops",
],
)
alias(
name = "example_parser_configuration_testdata",
actual = "//tensorflow/core/example/testdata:example_parser_configuration_testdata",
)
tf_proto_library(
name = "example_protos",
srcs = [
"example.proto",
"feature.proto",
],
make_default_target_header_only = True,
tags = ["alt_dep=//third_party/tensorflow/core:protos_all"],
)
tf_proto_library(
name = "example_parser_configuration_proto",
srcs = ["example_parser_configuration.proto"],
make_default_target_header_only = True,
protodeps = [
"//tensorflow/core/framework:tensor_shape_proto",
"//tensorflow/core/framework:tensor_proto",
"//tensorflow/core/framework:types_proto",
],
)
tf_proto_library(
name = "protos_all",
make_default_target_header_only = True,
protodeps = [
":example_protos",
":example_parser_configuration_proto",
],
tags = [
"alt_dep=//third_party/tensorflow/core:protos_all",
],
)
tf_pyclif_proto_library(
name = "example_pyclif",
proto_lib = "//tensorflow/core:protos_all",
proto_srcfile = "example.proto",
)
tf_pyclif_proto_library(
name = "feature_pyclif",
proto_lib = "//tensorflow/core:protos_all",
proto_srcfile = "feature.proto",
)
tf_jspb_proto_library(
name = "example_jspb_proto",
deps = [":example_protos"],
)
filegroup(
name = "mobile_srcs_no_runtime",
srcs = [
"feature_util.cc",
],
)
filegroup(
name = "mobile_hdrs_no_runtime",
srcs = [
"feature_util.h",
":example_protos_cc",
],
)
# TODO(bmzhao): These files are exported since they are directly referenced
# from targets in tensorflow/core/BUILD. Remove these after phase 4 of the
# TF refactor: https://github.com/tensorflow/community/pull/179
exports_files(
srcs = [
"example.proto",
"example_parser_configuration_test.cc",
"feature.proto",
"feature_util.cc",
"feature_util.h",
"feature_util_test.cc",
],
visibility = [
"//tensorflow/core:__pkg__",
],
)
+50
View File
@@ -0,0 +1,50 @@
#
# Copyright 2024 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if (NOT TARGET protobuf::libprotobuf)
find_package(Protobuf REQUIRED)
endif()
set(GEN_PROTO_DIR ${CMAKE_CURRENT_BINARY_DIR}/tensorflow/core/example)
# Generate feature proto .h, .cc and lib.
list(APPEND feature_generated_files ${GEN_PROTO_DIR}/feature.pb.h ${GEN_PROTO_DIR}/feature.pb.cc)
add_custom_command(
OUTPUT ${feature_generated_files}
COMMAND ${Protobuf_PROTOC_EXECUTABLE}
ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} --proto_path=${TENSORFLOW_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/feature.proto
DEPENDS ${Protobuf_PROTOC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/feature.proto
)
set_source_files_properties(${feature_generated_files} PROPERTIES GENERATED TRUE)
add_library(feature_proto ${feature_generated_files})
target_link_libraries(feature_proto protobuf::libprotobuf)
target_include_directories(feature_proto PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
# Generate example proto .h, .cc and lib.
list(APPEND example_generated_files ${GEN_PROTO_DIR}/example.pb.h ${GEN_PROTO_DIR}/example.pb.cc)
add_custom_command(
OUTPUT ${example_generated_files}
COMMAND ${Protobuf_PROTOC_EXECUTABLE}
ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} --proto_path=${TENSORFLOW_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/example.proto
DEPENDS ${Protobuf_PROTOC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example.proto ${feature_generated_files}
)
set_source_files_properties(${example_generated_files} PROPERTIES GENERATED TRUE)
add_library(example_proto ${example_generated_files})
target_link_libraries(example_proto feature_proto protobuf::libprotobuf)
target_include_directories(example_proto PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
+316
View File
@@ -0,0 +1,316 @@
// Copyright 2026 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ==============================================================================
// Protocol messages for describing input data Examples for machine learning
// model training or inference.
syntax = "proto3";
package tensorflow;
import "tensorflow/core/example/feature.proto";
option cc_enable_arenas = true;
option java_outer_classname = "ExampleProtos";
option java_multiple_files = true;
option java_package = "org.tensorflow.example";
option go_package = "github.com/tensorflow/tensorflow/tensorflow/go/core/example/example_protos_go_proto";
// An Example is a mostly-normalized data format for storing data for
// training and inference. It contains a key-value store (features); where
// each key (string) maps to a Feature message (which is oneof packed BytesList,
// FloatList, or Int64List). This flexible and compact format allows the
// storage of large amounts of typed data, but requires that the data shape
// and use be determined by the configuration files and parsers that are used to
// read and write this format. That is, the Example is mostly *not* a
// self-describing format. In TensorFlow, Examples are read in row-major
// format, so any configuration that describes data with rank-2 or above
// should keep this in mind. If you flatten a matrix into a FloatList it should
// be stored as [ row 0 ... row 1 ... row M-1 ]
//
// An Example for a movie recommendation application:
// features {
// feature {
// key: "age"
// value { float_list {
// value: 29.0
// }}
// }
// feature {
// key: "movie"
// value { bytes_list {
// value: "The Shawshank Redemption"
// value: "Fight Club"
// }}
// }
// feature {
// key: "movie_ratings"
// value { float_list {
// value: 9.0
// value: 9.7
// }}
// }
// feature {
// key: "suggestion"
// value { bytes_list {
// value: "Inception"
// }}
// }
// # Note that this feature exists to be used as a label in training.
// # E.g., if training a logistic regression model to predict purchase
// # probability in our learning tool we would set the label feature to
// # "suggestion_purchased".
// feature {
// key: "suggestion_purchased"
// value { float_list {
// value: 1.0
// }}
// }
// # Similar to "suggestion_purchased" above this feature exists to be used
// # as a label in training.
// # E.g., if training a linear regression model to predict purchase
// # price in our learning tool we would set the label feature to
// # "purchase_price".
// feature {
// key: "purchase_price"
// value { float_list {
// value: 9.99
// }}
// }
// }
//
// A conformant Example data set obeys the following conventions:
// - If a Feature K exists in one example with data type T, it must be of
// type T in all other examples when present. It may be omitted.
// - The number of instances of Feature K list data may vary across examples,
// depending on the requirements of the model.
// - If a Feature K doesn't exist in an example, a K-specific default will be
// used, if configured.
// - If a Feature K exists in an example but contains no items, the intent
// is considered to be an empty tensor and no default will be used.
message Example {
Features features = 1;
}
// A SequenceExample is an Example representing one or more sequences, and
// some context. The context contains features which apply to the entire
// example. The feature_lists contain a key, value map where each key is
// associated with a repeated set of Features (a FeatureList).
// A FeatureList thus represents the values of a feature identified by its key
// over time / frames.
//
// Below is a SequenceExample for a movie recommendation application recording a
// sequence of ratings by a user. The time-independent features ("locale",
// "age", "favorites") describing the user are part of the context. The sequence
// of movies the user rated are part of the feature_lists. For each movie in the
// sequence we have information on its name and actors and the user's rating.
// This information is recorded in three separate feature_list(s).
// In the example below there are only two movies. All three feature_list(s),
// namely "movie_ratings", "movie_names", and "actors" have a feature value for
// both movies. Note, that "actors" is itself a bytes_list with multiple
// strings per movie.
//
// context: {
// feature: {
// key : "locale"
// value: {
// bytes_list: {
// value: [ "pt_BR" ]
// }
// }
// }
// feature: {
// key : "age"
// value: {
// float_list: {
// value: [ 19.0 ]
// }
// }
// }
// feature: {
// key : "favorites"
// value: {
// bytes_list: {
// value: [ "Majesty Rose", "Savannah Outen", "One Direction" ]
// }
// }
// }
// }
// feature_lists: {
// feature_list: {
// key : "movie_ratings"
// value: {
// feature: {
// float_list: {
// value: [ 4.5 ]
// }
// }
// feature: {
// float_list: {
// value: [ 5.0 ]
// }
// }
// }
// }
// feature_list: {
// key : "movie_names"
// value: {
// feature: {
// bytes_list: {
// value: [ "The Shawshank Redemption" ]
// }
// }
// feature: {
// bytes_list: {
// value: [ "Fight Club" ]
// }
// }
// }
// }
// feature_list: {
// key : "actors"
// value: {
// feature: {
// bytes_list: {
// value: [ "Tim Robbins", "Morgan Freeman" ]
// }
// }
// feature: {
// bytes_list: {
// value: [ "Brad Pitt", "Edward Norton", "Helena Bonham Carter" ]
// }
// }
// }
// }
// }
//
// A conformant SequenceExample data set obeys the following conventions:
//
// Context:
// - All conformant context features K must obey the same conventions as
// a conformant Example's features (see above).
// Feature lists:
// - A FeatureList L may be missing in an example; it is up to the
// parser configuration to determine if this is allowed or considered
// an empty list (zero length).
// - If a FeatureList L exists, it may be empty (zero length).
// - If a FeatureList L is non-empty, all features within the FeatureList
// must have the same data type T. Even across SequenceExamples, the type T
// of the FeatureList identified by the same key must be the same. An entry
// without any values may serve as an empty feature.
// - If a FeatureList L is non-empty, it is up to the parser configuration
// to determine if all features within the FeatureList must
// have the same size. The same holds for this FeatureList across multiple
// examples.
// - For sequence modeling, e.g.:
// http://colah.github.io/posts/2015-08-Understanding-LSTMs/
// https://github.com/tensorflow/nmt
// the feature lists represent a sequence of frames.
// In this scenario, all FeatureLists in a SequenceExample have the same
// number of Feature messages, so that the ith element in each FeatureList
// is part of the ith frame (or time step).
// Examples of conformant and non-conformant examples' FeatureLists:
//
// Conformant FeatureLists:
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { float_list: { value: [ 5.0 ] } } }
// } }
//
// Non-conformant FeatureLists (mismatched types):
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { int64_list: { value: [ 5 ] } } }
// } }
//
// Conditionally conformant FeatureLists, the parser configuration determines
// if the feature sizes must match:
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { float_list: { value: [ 5.0, 6.0 ] } } }
// } }
//
// Conformant pair of SequenceExample
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { float_list: { value: [ 5.0 ] } } }
// } }
// and:
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { float_list: { value: [ 5.0 ] } }
// feature: { float_list: { value: [ 2.0 ] } } }
// } }
//
// Conformant pair of SequenceExample
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { float_list: { value: [ 5.0 ] } } }
// } }
// and:
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { }
// } }
//
// Conditionally conformant pair of SequenceExample, the parser configuration
// determines if the second feature_lists is consistent (zero-length) or
// invalid (missing "movie_ratings"):
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { float_list: { value: [ 5.0 ] } } }
// } }
// and:
// feature_lists: { }
//
// Non-conformant pair of SequenceExample (mismatched types)
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { float_list: { value: [ 5.0 ] } } }
// } }
// and:
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { int64_list: { value: [ 4 ] } }
// feature: { int64_list: { value: [ 5 ] } }
// feature: { int64_list: { value: [ 2 ] } } }
// } }
//
// Conditionally conformant pair of SequenceExample; the parser configuration
// determines if the feature sizes must match:
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.5 ] } }
// feature: { float_list: { value: [ 5.0 ] } } }
// } }
// and:
// feature_lists: { feature_list: {
// key: "movie_ratings"
// value: { feature: { float_list: { value: [ 4.0 ] } }
// feature: { float_list: { value: [ 5.0, 3.0 ] } }
// } }
message SequenceExample {
Features context = 1;
FeatureLists feature_lists = 2;
}
@@ -0,0 +1,203 @@
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/core/example/example_parser_configuration.h"
#include <vector>
#include "tensorflow/core/example/feature.pb.h"
#include "tensorflow/core/framework/attr_value.pb.h"
#include "tensorflow/core/framework/node_def.pb.h"
#include "tensorflow/core/framework/numeric_op.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.pb.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/protobuf.h"
namespace tensorflow {
absl::Status FindNodeIndexByName(const tensorflow::GraphDef& graph,
const std::string& node_name, int* node_idx) {
for (int i = 0; i < graph.node_size(); ++i) {
const auto& node = graph.node(i);
if (node.name() == node_name) {
*node_idx = i;
return absl::OkStatus();
}
}
return absl::InvalidArgumentError(
absl::StrCat(node_name, " not found in GraphDef"));
}
absl::Status ExtractExampleParserConfiguration(
const tensorflow::GraphDef& graph, const std::string& node_name,
tensorflow::Session* session,
std::vector<FixedLenFeature>* fixed_len_features,
std::vector<VarLenFeature>* var_len_features) {
int node_idx;
TF_RETURN_IF_ERROR(FindNodeIndexByName(graph, node_name, &node_idx));
const auto& node = graph.node(node_idx);
if (node.op() != "ParseExample") {
return absl::InvalidArgumentError(
absl::StrCat(node_name, " node is not a ParseExample op"));
}
auto& attr_map = node.attr();
auto num_sparse = attr_map.at("Nsparse").i();
auto num_dense = attr_map.at("Ndense").i();
fixed_len_features->resize(num_dense);
var_len_features->resize(num_sparse);
auto tdense = attr_map.at("Tdense");
auto dense_shapes = attr_map.at("dense_shapes");
auto sparse_types = attr_map.at("sparse_types");
// Consistency check attributes.
if (tdense.list().type_size() != num_dense) {
return absl::InvalidArgumentError(
absl::StrCat("Node attr Tdense has ", tdense.list().type_size(),
" elements != Ndense attr: ", num_dense));
}
if (dense_shapes.list().shape_size() != num_dense) {
return absl::InvalidArgumentError(absl::StrCat(
"Node attr dense_shapes has ", dense_shapes.list().shape_size(),
" elements != Ndense attr: ", num_dense));
}
if (sparse_types.list().type_size() != num_sparse) {
return absl::InvalidArgumentError(absl::StrCat(
"Node attr sparse_types has ", sparse_types.list().type_size(),
" elements != NSparse attr: ", num_sparse));
}
for (int i = 0; i < tdense.list().type_size(); ++i) {
(*fixed_len_features)[i].dtype = tdense.list().type(i);
// Convert TensorShapeProto to TensorShape.
(*fixed_len_features)[i].shape = TensorShape(dense_shapes.list().shape(i));
}
for (int i = 0; i < sparse_types.list().type_size(); ++i) {
(*var_len_features)[i].dtype = sparse_types.list().type(i);
}
// We must fetch the configuration input tensors to the ParseExample op.
// Skipping index = 0, which is the serialized proto input.
std::vector<std::string> fetch_names(node.input_size() - 1);
for (int i = 1; i < node.input_size(); ++i) {
fetch_names[i - 1] = node.input(i);
}
std::vector<Tensor> op_input_tensors;
TF_RETURN_IF_ERROR(session->Run({}, // no_inputs,
fetch_names, {}, // no target_node_names,
&op_input_tensors));
// The input tensors are laid out sequentially in a flat manner.
// Here are the various start offsets.
int sparse_keys_start = 1;
int dense_keys_start = sparse_keys_start + num_sparse;
int dense_defaults_start = dense_keys_start + num_dense;
for (int i = 0; i < num_sparse; ++i) {
int input_idx = sparse_keys_start + i;
(*var_len_features)[i].key =
op_input_tensors[input_idx].scalar<tstring>()();
}
for (int i = 0; i < num_dense; ++i) {
FixedLenFeature& config = (*fixed_len_features)[i];
int dense_keys_offset = dense_keys_start + i;
config.key = op_input_tensors[dense_keys_offset].scalar<tstring>()();
int defaults_offset = dense_defaults_start + i;
config.default_value = op_input_tensors[defaults_offset];
}
// The output tensors are laid out sequentially in a flat manner.
// Here are the various start offsets.
int sparse_indices_output_start = 0;
int sparse_values_output_start = sparse_indices_output_start + num_sparse;
int sparse_shapes_output_start = sparse_values_output_start + num_sparse;
int dense_values_output_start = sparse_shapes_output_start + num_sparse;
std::string node_output_prefix = absl::StrCat(node_name, ":");
for (int i = 0; i < num_sparse; ++i) {
VarLenFeature& config = (*var_len_features)[i];
int indices_offset = sparse_indices_output_start + i;
config.indices_output_tensor_name =
absl::StrCat(node_output_prefix, indices_offset);
int values_offset = sparse_values_output_start + i;
config.values_output_tensor_name =
absl::StrCat(node_output_prefix, values_offset);
int shapes_offset = sparse_shapes_output_start + i;
config.shapes_output_tensor_name =
absl::StrCat(node_output_prefix, shapes_offset);
}
for (int i = 0; i < num_dense; ++i) {
int output_idx = dense_values_output_start + i;
(*fixed_len_features)[i].values_output_tensor_name =
absl::StrCat(node_output_prefix, output_idx);
}
return absl::OkStatus();
}
absl::Status ExampleParserConfigurationProtoToFeatureVectors(
const ExampleParserConfiguration& config_proto,
std::vector<FixedLenFeature>* fixed_len_features,
std::vector<VarLenFeature>* var_len_features) {
const auto& feature_map = config_proto.feature_map();
for (auto it = feature_map.cbegin(); it != feature_map.cend(); ++it) {
std::string key = it->first;
const auto& config = it->second;
if (config.has_fixed_len_feature()) {
const auto& fixed_config = config.fixed_len_feature();
FixedLenFeature f;
f.key = key;
f.dtype = fixed_config.dtype();
f.shape = TensorShape(fixed_config.shape());
Tensor default_value(f.dtype, f.shape);
if (!default_value.FromProto(fixed_config.default_value())) {
return absl::InvalidArgumentError(
absl::StrCat("Invalid default_value in config proto ",
fixed_config.default_value().DebugString()));
}
f.default_value = default_value;
f.values_output_tensor_name = fixed_config.values_output_tensor_name();
fixed_len_features->push_back(f);
} else {
const auto& var_len_config = config.var_len_feature();
VarLenFeature v;
v.key = key;
v.dtype = var_len_config.dtype();
v.values_output_tensor_name = var_len_config.values_output_tensor_name();
v.indices_output_tensor_name =
var_len_config.indices_output_tensor_name();
v.shapes_output_tensor_name = var_len_config.shapes_output_tensor_name();
var_len_features->push_back(v);
}
}
return absl::OkStatus();
}
} // namespace tensorflow
@@ -0,0 +1,56 @@
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_CORE_EXAMPLE_EXAMPLE_PARSER_CONFIGURATION_H_
#define TENSORFLOW_CORE_EXAMPLE_EXAMPLE_PARSER_CONFIGURATION_H_
#include <string>
#include <vector>
#include "tensorflow/core/example/example.pb.h"
#include "tensorflow/core/example/example_parser_configuration.pb.h"
#include "tensorflow/core/framework/allocator.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/util/example_proto_helper.h"
#include "tensorflow/core/util/sparse/sparse_tensor.h"
// This is a set of helper methods that will make it possible to share
// tensorflow::Example proto Tensor conversion code inside the ExampleParserOp
// OpKernel as well as in external code.
namespace tensorflow {
// Given a graph and the node_name of a ParseExample op,
// extract the FixedLenFeature/VarLenFeature configurations.
absl::Status ExtractExampleParserConfiguration(
const tensorflow::GraphDef& graph, const std::string& node_name,
tensorflow::Session* session,
std::vector<FixedLenFeature>* fixed_len_features,
std::vector<VarLenFeature>* var_len_features);
// Given a config proto, ostensibly extracted via python,
// fill a vector of C++ structs suitable for calling
// the tensorflow.Example -> Tensor conversion code.
absl::Status ExampleParserConfigurationProtoToFeatureVectors(
const ExampleParserConfiguration& config_proto,
std::vector<FixedLenFeature>* fixed_len_features,
std::vector<VarLenFeature>* var_len_features);
} // namespace tensorflow
#endif // TENSORFLOW_CORE_EXAMPLE_EXAMPLE_PARSER_CONFIGURATION_H_
@@ -0,0 +1,55 @@
// Copyright 2026 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ==============================================================================
// Protocol messages for describing the configuration of the ExampleParserOp.
syntax = "proto3";
package tensorflow;
import "tensorflow/core/framework/tensor.proto";
import "tensorflow/core/framework/tensor_shape.proto";
import "tensorflow/core/framework/types.proto";
option cc_enable_arenas = true;
option java_outer_classname = "ExampleParserConfigurationProtos";
option java_multiple_files = true;
option java_package = "org.tensorflow.example";
option go_package = "github.com/tensorflow/tensorflow/tensorflow/go/core/example/example_parser_configuration_go_proto";
message VarLenFeatureProto {
tensorflow.DataType dtype = 1;
string values_output_tensor_name = 2;
string indices_output_tensor_name = 3;
string shapes_output_tensor_name = 4;
}
message FixedLenFeatureProto {
tensorflow.DataType dtype = 1;
tensorflow.TensorShapeProto shape = 2;
tensorflow.TensorProto default_value = 3;
string values_output_tensor_name = 4;
}
message FeatureConfiguration {
oneof config {
FixedLenFeatureProto fixed_len_feature = 1;
VarLenFeatureProto var_len_feature = 2;
}
}
message ExampleParserConfiguration {
map<string, FeatureConfiguration> feature_map = 1;
}
@@ -0,0 +1,229 @@
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/core/example/example_parser_configuration.h"
#include <memory>
#include "tensorflow/core/framework/attr_value.pb.h"
#include "tensorflow/core/framework/node_def.pb.h"
#include "tensorflow/core/framework/tensor_testutil.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/platform/protobuf.h"
#include "tensorflow/core/platform/test.h"
#include "tensorflow/core/public/session_options.h"
#include "tensorflow/core/util/example_proto_helper.h"
namespace tensorflow {
namespace {
void ReadFileToStringOrDie(Env* env, const std::string& filename,
std::string* output) {
TF_CHECK_OK(ReadFileToString(env, filename, output));
}
std::unique_ptr<Session> CreateSession() {
SessionOptions options;
(*options.config.mutable_device_count())["CPU"] = 2;
return std::unique_ptr<Session>(NewSession(options));
}
class ExtractExampleParserConfigurationTest : public ::testing::Test {
protected:
void SetUp() override {
std::string proto_string;
std::string filename =
io::JoinPath(testing::TensorFlowSrcRoot(),
"core/example/testdata/parse_example_graph_def.pbtxt");
ReadFileToStringOrDie(Env::Default(), filename, &proto_string);
protobuf::TextFormat::ParseFromString(proto_string, &graph_def_);
session_ = CreateSession();
TF_CHECK_OK(session_->Create(graph_def_));
}
NodeDef* parse_example_node() {
for (auto& node : *graph_def_.mutable_node()) {
if (node.name() == "ParseExample/ParseExample") {
return &node;
}
}
return nullptr;
}
GraphDef graph_def_;
std::unique_ptr<Session> session_;
};
TEST_F(ExtractExampleParserConfigurationTest, OpNotFound) {
std::vector<FixedLenFeature> dense_vec;
std::vector<VarLenFeature> sparse_vec;
absl::Status status = ExtractExampleParserConfiguration(
graph_def_, "BlarseExample/ParseExample", session_.get(), &dense_vec,
&sparse_vec);
EXPECT_EQ(status.code(), error::INVALID_ARGUMENT);
}
TEST_F(ExtractExampleParserConfigurationTest, InconsistentAttrNsparse) {
std::vector<FixedLenFeature> dense_vec;
std::vector<VarLenFeature> sparse_vec;
NodeDef* node = parse_example_node();
auto mutable_attr = node->mutable_attr();
(*mutable_attr)["Nsparse"].set_i(3);
absl::Status status = ExtractExampleParserConfiguration(
graph_def_, "ParseExample/ParseExample", session_.get(), &dense_vec,
&sparse_vec);
EXPECT_EQ(status.code(), error::INVALID_ARGUMENT);
}
TEST_F(ExtractExampleParserConfigurationTest, InconsistentAttrNdense) {
std::vector<FixedLenFeature> dense_vec;
std::vector<VarLenFeature> sparse_vec;
NodeDef* node = parse_example_node();
auto mutable_attr = node->mutable_attr();
(*mutable_attr)["Ndense"].set_i(2);
absl::Status status = ExtractExampleParserConfiguration(
graph_def_, "ParseExample/ParseExample", session_.get(), &dense_vec,
&sparse_vec);
EXPECT_EQ(status.code(), error::INVALID_ARGUMENT);
}
TEST_F(ExtractExampleParserConfigurationTest, Basic) {
std::vector<FixedLenFeature> dense_vec;
std::vector<VarLenFeature> sparse_vec;
absl::Status status = ExtractExampleParserConfiguration(
graph_def_, "ParseExample/ParseExample", session_.get(), &dense_vec,
&sparse_vec);
EXPECT_EQ(absl::OkStatus(), status);
EXPECT_EQ(2, sparse_vec.size());
EXPECT_EQ(3, dense_vec.size());
EXPECT_EQ("sf0", sparse_vec[0].key);
EXPECT_EQ(DT_STRING, sparse_vec[0].dtype);
EXPECT_EQ("ParseExample/ParseExample:0",
sparse_vec[0].indices_output_tensor_name);
EXPECT_EQ("ParseExample/ParseExample:2",
sparse_vec[0].values_output_tensor_name);
EXPECT_EQ("ParseExample/ParseExample:4",
sparse_vec[0].shapes_output_tensor_name);
EXPECT_EQ("sf1", sparse_vec[1].key);
EXPECT_EQ(DT_STRING, sparse_vec[1].dtype);
EXPECT_EQ("ParseExample/ParseExample:1",
sparse_vec[1].indices_output_tensor_name);
EXPECT_EQ("ParseExample/ParseExample:3",
sparse_vec[1].values_output_tensor_name);
EXPECT_EQ("ParseExample/ParseExample:5",
sparse_vec[1].shapes_output_tensor_name);
EXPECT_EQ("x", dense_vec[0].key);
EXPECT_EQ(DT_FLOAT, dense_vec[0].dtype);
EXPECT_EQ("ParseExample/ParseExample:6",
dense_vec[0].values_output_tensor_name);
EXPECT_EQ("y", dense_vec[1].key);
EXPECT_EQ(DT_FLOAT, dense_vec[1].dtype);
EXPECT_EQ("ParseExample/ParseExample:7",
dense_vec[1].values_output_tensor_name);
EXPECT_EQ("z", dense_vec[2].key);
EXPECT_EQ(DT_FLOAT, dense_vec[2].dtype);
EXPECT_EQ("ParseExample/ParseExample:8",
dense_vec[2].values_output_tensor_name);
}
static const char kExampleParseConfigurationProto[] = R"( feature_map {
key: "x"
value {
fixed_len_feature {
dtype: DT_FLOAT
shape {
dim {
size: 1
}
}
default_value {
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
}
float_val: 33.0
}
values_output_tensor_name: "ParseExample/ParseExample:3"
}
}
}
feature_map {
key: "y"
value {
var_len_feature {
dtype: DT_STRING
values_output_tensor_name: "ParseExample/ParseExample:1"
indices_output_tensor_name: "ParseExample/ParseExample:0"
shapes_output_tensor_name: "ParseExample/ParseExample:2"
}
}
}
)";
class ExampleParserConfigurationProtoToFeatureVectorsTest
: public ::testing::Test {
protected:
void SetUp() override {
CHECK(protobuf::TextFormat::ParseFromString(kExampleParseConfigurationProto,
&config_proto_));
}
ExampleParserConfiguration config_proto_;
};
TEST_F(ExampleParserConfigurationProtoToFeatureVectorsTest, Basic) {
std::vector<FixedLenFeature> fixed_len_features;
std::vector<VarLenFeature> var_len_features;
TF_ASSERT_OK(ExampleParserConfigurationProtoToFeatureVectors(
config_proto_, &fixed_len_features, &var_len_features));
ASSERT_EQ(1, fixed_len_features.size());
ASSERT_EQ(1, var_len_features.size());
const FixedLenFeature& f = fixed_len_features[0];
ASSERT_EQ(DT_FLOAT, f.dtype);
ASSERT_EQ("x", f.key);
ASSERT_EQ("ParseExample/ParseExample:3", f.values_output_tensor_name);
TensorShape expected_shape({1});
ASSERT_EQ(expected_shape.dims(), f.shape.dims());
ASSERT_EQ(1, f.shape.dim_size(0));
Tensor expected_default(DT_FLOAT, TensorShape({1}));
test::FillIota<float>(&expected_default, 33.0);
test::ExpectTensorEqual<float>(expected_default, f.default_value);
const VarLenFeature& v = var_len_features[0];
ASSERT_EQ(DT_STRING, v.dtype);
ASSERT_EQ("ParseExample/ParseExample:0", v.indices_output_tensor_name);
ASSERT_EQ("ParseExample/ParseExample:1", v.values_output_tensor_name);
ASSERT_EQ("ParseExample/ParseExample:2", v.shapes_output_tensor_name);
}
} // namespace
} // namespace tensorflow
+125
View File
@@ -0,0 +1,125 @@
// Copyright 2026 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ==============================================================================
// Protocol messages for describing features for machine learning model
// training or inference.
//
// There are three base Feature types:
// - bytes
// - float
// - int64
//
// A Feature contains Lists which may hold zero or more values. These
// lists are the base values BytesList, FloatList, Int64List.
//
// Features are organized into categories by name. The Features message
// contains the mapping from name to Feature.
//
// Example Features for a movie recommendation application:
// feature {
// key: "age"
// value { float_list {
// value: 29.0
// }}
// }
// feature {
// key: "movie"
// value { bytes_list {
// value: "The Shawshank Redemption"
// value: "Fight Club"
// }}
// }
// feature {
// key: "movie_ratings"
// value { float_list {
// value: 9.0
// value: 9.7
// }}
// }
// feature {
// key: "suggestion"
// value { bytes_list {
// value: "Inception"
// }}
// }
// feature {
// key: "suggestion_purchased"
// value { int64_list {
// value: 1
// }}
// }
// feature {
// key: "purchase_price"
// value { float_list {
// value: 9.99
// }}
// }
//
syntax = "proto3";
package tensorflow;
option cc_enable_arenas = true;
option java_outer_classname = "FeatureProtos";
option java_multiple_files = true;
option java_package = "org.tensorflow.example";
option go_package = "github.com/tensorflow/tensorflow/tensorflow/go/core/example/example_protos_go_proto";
// LINT.IfChange
// Containers to hold repeated fundamental values.
message BytesList {
repeated bytes value = 1;
}
message FloatList {
repeated float value = 1 [packed = true];
}
message Int64List {
repeated int64 value = 1 [packed = true, jstype = JS_STRING];
}
// Containers for non-sequential data.
message Feature {
// Each feature can be exactly one kind.
oneof kind {
BytesList bytes_list = 1;
FloatList float_list = 2;
Int64List int64_list = 3;
}
}
message Features {
// Map from feature name to feature.
map<string, Feature> feature = 1;
}
// Containers for sequential data.
//
// A FeatureList contains lists of Features. These may hold zero or more
// Feature values.
//
// FeatureLists are organized into categories by name. The FeatureLists message
// contains the mapping from name to FeatureList.
//
message FeatureList {
repeated Feature feature = 1;
}
message FeatureLists {
// Map from feature name to feature list.
map<string, FeatureList> feature_list = 1;
}
// LINT.ThenChange(
// https://www.tensorflow.org/code/tensorflow/python/training/training.py)
+184
View File
@@ -0,0 +1,184 @@
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/core/example/feature_util.h"
#include <string>
#include "absl/strings/string_view.h"
namespace tensorflow {
namespace internal {
Feature& ExampleFeature(absl::string_view name, Example* example) {
return *GetFeature(name, example);
}
} // namespace internal
template <>
bool HasFeature<>(absl::string_view key, const Features& features) {
return features.feature().contains(internal::ProtoMapKey(key));
}
template <>
bool HasFeature<protobuf_int64>(absl::string_view key,
const Features& features) {
auto it = features.feature().find(internal::ProtoMapKey(key));
return (it != features.feature().end()) &&
(it->second.kind_case() == Feature::KindCase::kInt64List);
}
template <>
bool HasFeature<float>(absl::string_view key, const Features& features) {
auto it = features.feature().find(internal::ProtoMapKey(key));
return (it != features.feature().end()) &&
(it->second.kind_case() == Feature::KindCase::kFloatList);
}
template <>
bool HasFeature<std::string>(absl::string_view key, const Features& features) {
auto it = features.feature().find(internal::ProtoMapKey(key));
return (it != features.feature().end()) &&
(it->second.kind_case() == Feature::KindCase::kBytesList);
}
template <>
bool HasFeature<tstring>(absl::string_view key, const Features& features) {
auto it = features.feature().find(internal::ProtoMapKey(key));
return (it != features.feature().end()) &&
(it->second.kind_case() == Feature::KindCase::kBytesList);
}
bool HasFeatureList(absl::string_view key,
const SequenceExample& sequence_example) {
return sequence_example.feature_lists().feature_list().contains(
internal::ProtoMapKey(key));
}
template <>
const protobuf::RepeatedField<protobuf_int64>& GetFeatureValues<protobuf_int64>(
const Feature& feature) {
return feature.int64_list().value();
}
template <>
protobuf::RepeatedField<protobuf_int64>* GetFeatureValues<protobuf_int64>(
Feature* feature) {
return feature->mutable_int64_list()->mutable_value();
}
template <>
const protobuf::RepeatedField<float>& GetFeatureValues<float>(
const Feature& feature) {
return feature.float_list().value();
}
template <>
protobuf::RepeatedField<float>* GetFeatureValues<float>(Feature* feature) {
return feature->mutable_float_list()->mutable_value();
}
template <>
const protobuf::RepeatedPtrField<std::string>& GetFeatureValues<tstring>(
const Feature& feature) {
return feature.bytes_list().value();
}
template <>
const protobuf::RepeatedPtrField<std::string>& GetFeatureValues<std::string>(
const Feature& feature) {
return feature.bytes_list().value();
}
template <>
protobuf::RepeatedPtrField<std::string>* GetFeatureValues<tstring>(
Feature* feature) {
return feature->mutable_bytes_list()->mutable_value();
}
template <>
protobuf::RepeatedPtrField<std::string>* GetFeatureValues<std::string>(
Feature* feature) {
return feature->mutable_bytes_list()->mutable_value();
}
const protobuf::RepeatedPtrField<Feature>& GetFeatureList(
absl::string_view key, const SequenceExample& sequence_example) {
return sequence_example.feature_lists()
.feature_list()
.at(internal::ProtoMapKey(key))
.feature();
}
protobuf::RepeatedPtrField<Feature>* GetFeatureList(
absl::string_view feature_list_key, SequenceExample* sequence_example) {
return (*sequence_example->mutable_feature_lists()
->mutable_feature_list())[internal::ProtoMapKey(
feature_list_key)]
.mutable_feature();
}
template <>
void ClearFeatureValues<protobuf_int64>(Feature* feature) {
feature->mutable_int64_list()->Clear();
}
template <>
void ClearFeatureValues<float>(Feature* feature) {
feature->mutable_float_list()->Clear();
}
template <>
void ClearFeatureValues<std::string>(Feature* feature) {
feature->mutable_bytes_list()->Clear();
}
template <>
void ClearFeatureValues<tstring>(Feature* feature) {
feature->mutable_bytes_list()->Clear();
}
template <>
Features* GetFeatures<Features>(Features* proto) {
return proto;
}
template <>
Features* GetFeatures<Example>(Example* proto) {
return proto->mutable_features();
}
template <>
Features* GetFeatures<SequenceExample>(SequenceExample* proto) {
return proto->mutable_context();
}
template <>
const Features& GetFeatures<Features>(const Features& proto) {
return proto;
}
template <>
const Features& GetFeatures<Example>(const Example& proto) {
return proto.features();
}
template <>
const Features& GetFeatures<SequenceExample>(const SequenceExample& proto) {
return proto.context();
}
} // namespace tensorflow
+664
View File
@@ -0,0 +1,664 @@
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// A set of lightweight wrappers which simplify access to Feature protos.
//
// TensorFlow Example proto uses associative maps on top of oneof fields.
// SequenceExample proto uses associative map of FeatureList.
// So accessing feature values is not very convenient.
//
// For example, to read a first value of integer feature "tag":
// int id = example.features().feature().at("tag").int64_list().value(0);
//
// to add a value:
// auto features = example->mutable_features();
// (*features->mutable_feature())["tag"].mutable_int64_list()->add_value(id);
//
// For float features you have to use float_list, for string - bytes_list.
//
// To do the same with this library:
// int id = GetFeatureValues<int64_t>("tag", example).Get(0);
// GetFeatureValues<int64_t>("tag", &example)->Add(id);
//
// Modification of bytes features is slightly different:
// auto tag = GetFeatureValues<std::string>("tag", &example);
// *tag->Add() = "lorem ipsum";
//
// To copy multiple values into a feature:
// AppendFeatureValues({1,2,3}, "tag", &example);
//
// GetFeatureValues gives you access to underlying data - RepeatedField object
// (RepeatedPtrField for byte list). So refer to its documentation of
// RepeatedField for full list of supported methods.
//
// NOTE: Due to the nature of oneof proto fields setting a feature of one type
// automatically clears all values stored as another type with the same feature
// key.
//
// This library also has tools to work with SequenceExample protos.
//
// To get a value from SequenceExample.context:
// int id = GetFeatureValues<protobuf_int64>("tag", se.context()).Get(0);
// To add a value to the context:
// GetFeatureValues<protobuf_int64>("tag", se.mutable_context())->Add(42);
//
// To add values to feature_lists:
// AppendFeatureValues({4.0},
// GetFeatureList("images", &se)->Add());
// AppendFeatureValues({5.0, 3.0},
// GetFeatureList("images", &se)->Add());
// This will create a feature list keyed as "images" with two features:
// feature_lists {
// feature_list {
// key: "images"
// value {
// feature { float_list { value: [4.0] } }
// feature { float_list { value: [5.0, 3.0] } }
// }
// }
// }
// For string-valued features, note that the Append... and Set... functions
// support absl::string_view containers. This allows you to copy existing
// buffers into a Feature with only one copy:
// std::vector<absl::string_view> image;
// image.push_back(image_buffer); // No copy.
// SetFeatureValues(image, "image", &example); // Copy.
//
// Functions exposed by this library:
// HasFeature<[FeatureType]>(key, proto) -> bool
// Returns true if a feature with the specified key, and optionally
// FeatureType, belongs to the Features or Example proto.
// HasFeatureList(key, sequence_example) -> bool
// Returns true if SequenceExample has a feature_list with the key.
//
// GetFeatureValues<FeatureType>(key, proto) -> RepeatedField<FeatureType>
// Returns values for the specified key and the FeatureType.
// Supported types for the proto: Example, Features.
// GetFeatureList(key, sequence_example) -> RepeatedPtrField<Feature>
// Returns Feature protos associated with a key.
//
// AppendFeatureValues(begin, end, feature)
// AppendFeatureValues(container or initializer_list, feature)
// Copies values into a Feature.
// AppendFeatureValues(begin, end, key, proto)
// AppendFeatureValues(container or initializer_list, key, proto)
// Copies values into Features and Example protos with the specified key.
//
// ClearFeatureValues<FeatureType>(feature)
// Clears the feature's repeated field of the given type.
//
// SetFeatureValues(begin, end, feature)
// SetFeatureValues(container or initializer_list, feature)
// Clears a Feature, then copies values into it.
// SetFeatureValues(begin, end, key, proto)
// SetFeatureValues(container or initializer_list, key, proto)
// Clears Features or Example protos with the specified key,
// then copies values into them.
//
// Auxiliary functions, it is unlikely you'll need to use them directly:
// GetFeatures(proto) -> Features
// A convenience function to get Features proto.
// Supported types for the proto: Example, Features.
// GetFeature(key, proto) -> Feature
// Returns a Feature proto for the specified key.
// Supported types for the proto: Example, Features.
// GetFeatureValues<FeatureType>(feature) -> RepeatedField<FeatureType>
// Returns values of the feature for the FeatureType.
#ifndef TENSORFLOW_CORE_EXAMPLE_FEATURE_UTIL_H_
#define TENSORFLOW_CORE_EXAMPLE_FEATURE_UTIL_H_
#include <algorithm>
#include <iterator>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "tensorflow/core/example/example.pb.h"
#include "tensorflow/core/example/feature.pb.h"
#include "tensorflow/core/platform/protobuf.h"
#include "tensorflow/core/platform/stringpiece.h"
// Must come after the import for absl::string_view.
#ifdef ABSL_HAVE_STD_STRING_VIEW
#include <string_view>
#endif
namespace tensorflow {
namespace internal {
// TODO(gorban): Update all clients in a followup CL.
// Returns a reference to a feature corresponding to the name.
// Note: it will create a new Feature if it is missing in the example.
ABSL_DEPRECATED("Use GetFeature instead.")
Feature& ExampleFeature(absl::string_view name, Example* example);
// Specializations of RepeatedFieldTrait define a type of RepeatedField
// corresponding to a selected feature type.
template <typename FeatureType>
struct RepeatedFieldTrait;
template <>
struct RepeatedFieldTrait<protobuf_int64> {
using Type = protobuf::RepeatedField<protobuf_int64>;
};
template <>
struct RepeatedFieldTrait<float> {
using Type = protobuf::RepeatedField<float>;
};
template <>
struct RepeatedFieldTrait<tstring> {
using Type = protobuf::RepeatedPtrField<std::string>;
};
template <>
struct RepeatedFieldTrait<std::string> {
using Type = protobuf::RepeatedPtrField<std::string>;
};
// Specializations of FeatureTrait define a type of feature corresponding to a
// selected value type.
template <typename ValueType, class Enable = void>
struct FeatureTrait;
template <typename ValueType>
struct FeatureTrait<ValueType, typename std::enable_if<
std::is_integral<ValueType>::value>::type> {
using Type = protobuf_int64;
};
template <typename ValueType>
struct FeatureTrait<
ValueType,
typename std::enable_if<std::is_floating_point<ValueType>::value>::type> {
using Type = float;
};
template <typename T>
struct is_string
: public std::integral_constant<
bool,
std::is_same<char*, typename std::decay<T>::type>::value ||
std::is_same<const char*, typename std::decay<T>::type>::value> {
};
template <>
struct is_string<std::string> : std::true_type {};
template <>
struct is_string<absl::string_view> : std::true_type {};
template <>
struct is_string<tstring> : std::true_type {};
template <typename ValueType>
struct FeatureTrait<
ValueType, typename std::enable_if<is_string<ValueType>::value>::type> {
using Type = std::string;
};
// Port of the C++20 `requires` expressions.
template <typename... T, typename F>
constexpr bool Requires(F) {
return std::is_invocable<F, T...>::value;
}
struct NoneSuch {};
// True if the Feature map in a tf.Example supports heterogenous lookup.
// See https://abseil.io/tips/144.
// TODO(b/365531379): this cannot be replaced by a lambda because it exposes a
// Clang bug when used in modules.
struct CheckFindFunctor {
template <class Container>
auto operator()(Container&& c) -> decltype(c.find(NoneSuch{})) {}
};
inline constexpr bool kFeatureMapHasHeterogeneousLookup =
Requires<decltype(Features::default_instance().feature())>(
CheckFindFunctor());
// Converts an `absl::string_view` into a string-type compatible for use in the
// protobuf library (e.g. as lookup keys in `proto2::Map` or as elements addable
// to a `proto2::RepeatedPtrField`) depending on the BUILD mode.
//
// NOTE: While the newest versions of `proto2::Map` support heterogenous lookup,
// it does so through `std::string_view`. If the type is just an alias (as noted
// by `ABSL_USES_STD_STRING_VIEW`) then nothing more needs to be done; however,
// when the type is not an alias an explicit conversion to is necessary.
//
// NOTE: This conversion is only necessary until the migration for protobuf to
// take a dependency on ABSL is complete.
inline auto ProtoMapKey(absl::string_view str) {
if constexpr (kFeatureMapHasHeterogeneousLookup) {
#ifdef ABSL_USES_STD_STRING_VIEW
return str;
#else
#ifdef ABSL_HAVE_STD_STRING_VIEW
return std::string_view(str.data(), str.size());
#else
return std::string(str);
#endif
#endif
} else {
return std::string(str);
}
}
} // namespace internal
// Returns true if sequence_example has a feature_list with the specified key.
bool HasFeatureList(absl::string_view key,
const SequenceExample& sequence_example);
template <typename T>
struct TypeHasFeatures : std::false_type {};
template <>
struct TypeHasFeatures<SequenceExample> : std::true_type {};
template <>
struct TypeHasFeatures<Example> : std::true_type {};
template <>
struct TypeHasFeatures<Features> : std::true_type {};
// A family of template functions to return mutable Features proto from a
// container proto. Supported ProtoTypes: SequenceExample, Example, Features.
template <typename ProtoType>
typename std::enable_if<TypeHasFeatures<ProtoType>::value, Features*>::type
GetFeatures(ProtoType* proto);
template <>
Features* GetFeatures<Features>(Features* proto);
template <>
Features* GetFeatures<Example>(Example* proto);
template <>
Features* GetFeatures<SequenceExample>(SequenceExample* proto);
template <typename ProtoType>
typename std::enable_if<TypeHasFeatures<ProtoType>::value,
const Features&>::type
GetFeatures(const ProtoType& proto);
template <>
const Features& GetFeatures<Features>(const Features& proto);
template <>
const Features& GetFeatures<Example>(const Example& proto);
template <>
const Features& GetFeatures<SequenceExample>(const SequenceExample& proto);
// Base declaration of a family of template functions to return a read only
// repeated field of feature values.
template <typename FeatureType>
const typename internal::RepeatedFieldTrait<FeatureType>::Type&
GetFeatureValues(const Feature& feature);
template <>
const protobuf::RepeatedField<protobuf_int64>& GetFeatureValues<protobuf_int64>(
const Feature& feature);
template <>
const protobuf::RepeatedField<float>& GetFeatureValues<float>(
const Feature& feature);
template <>
const protobuf::RepeatedPtrField<std::string>& GetFeatureValues<tstring>(
const Feature& feature);
template <>
const protobuf::RepeatedPtrField<std::string>& GetFeatureValues<std::string>(
const Feature& feature);
// Returns a read only repeated field corresponding to a feature with the
// specified name and FeatureType. Supported ProtoTypes: SequenceExample,
// Example, Features.
template <typename FeatureType, typename ProtoType>
const typename internal::RepeatedFieldTrait<FeatureType>::Type&
GetFeatureValues(absl::string_view key, const ProtoType& proto) {
return GetFeatureValues<FeatureType>(
GetFeatures(proto).feature().at(internal::ProtoMapKey(key)));
}
// Returns a mutable repeated field of a feature values.
template <typename FeatureType>
typename internal::RepeatedFieldTrait<FeatureType>::Type* GetFeatureValues(
Feature* feature);
template <>
protobuf::RepeatedField<protobuf_int64>* GetFeatureValues<protobuf_int64>(
Feature* feature);
template <>
protobuf::RepeatedField<float>* GetFeatureValues<float>(Feature* feature);
template <>
protobuf::RepeatedPtrField<std::string>* GetFeatureValues<tstring>(
Feature* feature);
template <>
protobuf::RepeatedPtrField<std::string>* GetFeatureValues<std::string>(
Feature* feature);
// Returns a mutable repeated field corresponding to a feature with the
// specified name and FeatureType. Supported ProtoTypes: SequenceExample,
// Example, Features.
template <typename FeatureType, typename ProtoType>
typename internal::RepeatedFieldTrait<FeatureType>::Type* GetFeatureValues(
absl::string_view key, ProtoType* proto) {
::tensorflow::Feature& feature =
(*GetFeatures(proto)->mutable_feature())[internal::ProtoMapKey(key)];
return GetFeatureValues<FeatureType>(&feature);
}
// Returns a read-only Feature proto for the specified key, throws
// std::out_of_range if the key is not found. Supported types for the proto:
// SequenceExample, Example, Features.
template <typename ProtoType>
const Feature& GetFeature(absl::string_view key, const ProtoType& proto) {
return GetFeatures(proto).feature().at(internal::ProtoMapKey(key));
}
// Returns a read-only Feature proto for the specified key, returns nullptr
// if the key is not found. Supported types for the proto: SequenceExample,
// Example, Features.
template <typename ProtoType>
const Feature* MaybeGetFeature(absl::string_view key, const ProtoType& proto) {
const protobuf::Map<std::string, Feature>& feature_map =
GetFeatures(proto).feature();
auto it = feature_map.find(internal::ProtoMapKey(key));
if (it == feature_map.end()) {
return nullptr;
}
return &it->second;
}
// Base declaration of a family of template functions to return a read only
// repeated field of feature values or nullptr.
template <typename FeatureType>
const typename internal::RepeatedFieldTrait<FeatureType>::Type*
MaybeGetFeatureValues(const Feature& feature);
template <>
const protobuf::RepeatedField<protobuf_int64>*
MaybeGetFeatureValues<protobuf_int64>(const Feature& feature);
template <>
const protobuf::RepeatedField<float>* MaybeGetFeatureValues<float>(
const Feature& feature);
template <>
const protobuf::RepeatedPtrField<std::string>* MaybeGetFeatureValues<tstring>(
const Feature& feature);
template <>
const protobuf::RepeatedPtrField<std::string>*
MaybeGetFeatureValues<std::string>(const Feature& feature);
// Returns a read only repeated field corresponding to a feature with the
// specified name and FeatureType. Supported ProtoTypes: SequenceExample,
// Example, Features.
template <typename FeatureType, typename ProtoType>
const typename internal::RepeatedFieldTrait<FeatureType>::Type*
MaybeGetFeatureValues(absl::string_view key, const ProtoType& proto) {
const Feature* feature = MaybeGetFeature(key, proto);
if (feature == nullptr) {
return nullptr;
}
return &GetFeatureValues<FeatureType>(*feature);
}
// Returns a mutable Feature proto for the specified key, creates a new if
// necessary. Supported types for the proto: SequenceExample, Example, Features.
template <typename ProtoType>
Feature* GetFeature(absl::string_view key, ProtoType* proto) {
return &(*GetFeatures(proto)->mutable_feature())[internal::ProtoMapKey(key)];
}
// Returns a repeated field with features corresponding to a feature_list key.
const protobuf::RepeatedPtrField<Feature>& GetFeatureList(
absl::string_view key, const SequenceExample& sequence_example);
// Returns a mutable repeated field with features corresponding to a
// feature_list key. It will create a new FeatureList if necessary.
protobuf::RepeatedPtrField<Feature>* GetFeatureList(
absl::string_view feature_list_key, SequenceExample* sequence_example);
template <typename IteratorType>
void AppendFeatureValues(IteratorType first, IteratorType last,
Feature* feature) {
using FeatureType = typename internal::FeatureTrait<
typename std::iterator_traits<IteratorType>::value_type>::Type;
auto& values = *GetFeatureValues<FeatureType>(feature);
values.Reserve(values.size() + std::distance(first, last));
for (auto it = first; it != last; ++it) {
*values.Add() = *it;
}
}
template <typename ValueType>
void AppendFeatureValues(std::initializer_list<ValueType> container,
Feature* feature) {
using FeatureType = typename internal::FeatureTrait<ValueType>::Type;
auto& values = *GetFeatureValues<FeatureType>(feature);
values.Reserve(values.size() + container.size());
for (auto& elt : container) {
*values.Add() = std::move(elt);
}
}
namespace internal {
// HasSize<T>::value is true_type if T has a size() member.
template <typename T, typename = void>
struct HasSize : std::false_type {};
template <typename T>
struct HasSize<T, std::void_t<decltype(std::declval<T>().size())>>
: std::true_type {};
// Reserves additional size, if a container.size() method exists.
template <typename ContainerType, typename RepeatedFieldType>
auto ReserveAdditionalIfSizeAvailable(const ContainerType& container,
RepeatedFieldType& values) ->
typename std::enable_if_t<HasSize<ContainerType>::value, void> {
values.Reserve(values.size() + container.size());
}
template <typename ContainerType, typename RepeatedFieldType>
auto ReserveAdditionalIfSizeAvailable(const ContainerType& container,
RepeatedFieldType& values) ->
typename std::enable_if_t<!HasSize<ContainerType>::value, void> {}
} // namespace internal
template <typename ContainerType>
void AppendFeatureValues(const ContainerType& container, Feature* feature) {
using IteratorType = typename ContainerType::const_iterator;
using FeatureType = typename internal::FeatureTrait<
typename std::iterator_traits<IteratorType>::value_type>::Type;
auto* values = GetFeatureValues<FeatureType>(feature);
internal::ReserveAdditionalIfSizeAvailable(container, *values);
// This is equivalent to std::copy into `values` with a
// RepeatedFieldBackInserter, the difference is RFBI isn't compatible with
// types that we want to convert (e.g. absl::string_view -> std::string).
for (const auto& elt : container) {
if constexpr (internal::is_string<FeatureType>::value) {
*values->Add() = std::string(elt);
} else {
*values->Add() = elt;
}
}
}
// Extends the feature by `ext` elements, with a default value and returns a
// span of the new elements. Currently only the string type is not supported.
// Note that unlike the inference of internal feature types in
// `AppendFeatureValues`, the caller is responsible for passing the correct
// FeatureType to prevent implicit conversion of `default_value`.
template <typename FeatureType>
absl::Span<FeatureType> ExtendAndGetSpan(tensorflow::Feature* feature, int ext,
FeatureType default_value) {
static_assert(!internal::is_string<FeatureType>::value,
"ExtendAndGetSpan does not support string types.");
auto* values = GetFeatureValues<FeatureType>(feature);
int old_size = values->size();
values->Resize(old_size + ext, default_value);
return absl::MakeSpan(values->mutable_data() + old_size, ext);
}
// Copies elements from the range, defined by [first, last) into the feature
// obtainable from the (proto, key) combination.
template <typename IteratorType, typename ProtoType>
void AppendFeatureValues(IteratorType first, IteratorType last,
absl::string_view key, ProtoType* proto) {
AppendFeatureValues(first, last, GetFeature(key, GetFeatures(proto)));
}
// Copies all elements from the container into a feature.
template <typename ContainerType, typename ProtoType>
void AppendFeatureValues(const ContainerType& container, absl::string_view key,
ProtoType* proto) {
AppendFeatureValues<ContainerType>(container,
GetFeature(key, GetFeatures(proto)));
}
// Copies all elements from the initializer list into a Feature contained by
// Features or Example proto.
template <typename ValueType, typename ProtoType>
void AppendFeatureValues(std::initializer_list<ValueType> container,
absl::string_view key, ProtoType* proto) {
AppendFeatureValues<ValueType>(container,
GetFeature(key, GetFeatures(proto)));
}
// Clears the feature's repeated field (int64, float, or string).
template <typename... FeatureType>
void ClearFeatureValues(Feature* feature);
template <>
void ClearFeatureValues<protobuf_int64>(Feature* feature);
template <>
void ClearFeatureValues<float>(Feature* feature);
template <>
void ClearFeatureValues<std::string>(Feature* feature);
template <>
void ClearFeatureValues<tstring>(Feature* feature);
// Clears the feature's repeated field (int64, float, or string). Copies
// elements from the range, defined by [first, last) into the feature's repeated
// field.
template <typename IteratorType>
void SetFeatureValues(IteratorType first, IteratorType last, Feature* feature) {
using FeatureType = typename internal::FeatureTrait<
typename std::iterator_traits<IteratorType>::value_type>::Type;
ClearFeatureValues<FeatureType>(feature);
AppendFeatureValues(first, last, feature);
}
// Clears the feature's repeated field (int64, float, or string). Copies all
// elements from the initializer list into the feature's repeated field.
template <typename ValueType>
void SetFeatureValues(std::initializer_list<ValueType> container,
Feature* feature) {
using FeatureType = typename internal::FeatureTrait<ValueType>::Type;
ClearFeatureValues<FeatureType>(feature);
AppendFeatureValues(container, feature);
}
// Clears the feature's repeated field (int64, float, or string). Copies all
// elements from the container into the feature's repeated field.
template <typename ContainerType>
void SetFeatureValues(const ContainerType& container, Feature* feature) {
using IteratorType = typename ContainerType::const_iterator;
using FeatureType = typename internal::FeatureTrait<
typename std::iterator_traits<IteratorType>::value_type>::Type;
ClearFeatureValues<FeatureType>(feature);
AppendFeatureValues(container, feature);
}
// Clears the feature's repeated field (int64, float, or string). Copies
// elements from the range, defined by [first, last) into the feature's repeated
// field.
template <typename IteratorType, typename ProtoType>
void SetFeatureValues(IteratorType first, IteratorType last,
absl::string_view key, ProtoType* proto) {
SetFeatureValues(first, last, GetFeature(key, GetFeatures(proto)));
}
// Clears the feature's repeated field (int64, float, or string). Copies all
// elements from the container into the feature's repeated field.
template <typename ContainerType, typename ProtoType>
void SetFeatureValues(const ContainerType& container, absl::string_view key,
ProtoType* proto) {
SetFeatureValues<ContainerType>(container,
GetFeature(key, GetFeatures(proto)));
}
// Clears the feature's repeated field (int64, float, or string). Copies all
// elements from the initializer list into the feature's repeated field.
template <typename ValueType, typename ProtoType>
void SetFeatureValues(std::initializer_list<ValueType> container,
absl::string_view key, ProtoType* proto) {
SetFeatureValues<ValueType>(container, GetFeature(key, GetFeatures(proto)));
}
// Returns true if a feature with the specified key belongs to the Features.
// The template parameter pack accepts zero or one template argument - which
// is FeatureType. If the FeatureType not specified (zero template arguments)
// the function will not check the feature type. Otherwise it will return false
// if the feature has a wrong type.
template <typename... FeatureType>
bool HasFeature(absl::string_view key, const Features& features);
template <>
bool HasFeature<>(absl::string_view key, const Features& features);
template <>
bool HasFeature<protobuf_int64>(absl::string_view key,
const Features& features);
template <>
bool HasFeature<float>(absl::string_view key, const Features& features);
template <>
bool HasFeature<std::string>(absl::string_view key, const Features& features);
template <>
bool HasFeature<tstring>(absl::string_view key, const Features& features);
// Returns true if a feature with the specified key belongs to the Example.
// Doesn't check feature type if used without FeatureType, otherwise the
// specialized versions return false if the feature has a wrong type.
template <typename... FeatureType>
bool HasFeature(absl::string_view key, const Example& example) {
return HasFeature<FeatureType...>(key, GetFeatures(example));
}
// Returns true if a feature with the specified key belongs to the
// SequenceExample. Doesn't check feature type if used without FeatureType,
// otherwise the specialized versions return false if the feature has a wrong
// type.
template <typename... FeatureType>
bool HasFeature(absl::string_view key,
const SequenceExample& sequence_example) {
return HasFeature<FeatureType...>(key, GetFeatures(sequence_example));
}
// TODO(gorban): update all clients in a followup CL.
template <typename... FeatureType>
ABSL_DEPRECATED("Use HasFeature instead.")
bool ExampleHasFeature(absl::string_view key, const Example& example) {
return HasFeature<FeatureType...>(key, example);
}
} // namespace tensorflow
#endif // TENSORFLOW_CORE_EXAMPLE_FEATURE_UTIL_H_
@@ -0,0 +1,869 @@
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/core/example/feature_util.h"
#include <algorithm>
#include <cstdint>
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "tensorflow/core/example/example.pb.h"
#include "tensorflow/core/platform/test.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
namespace {
const float kTolerance = 1e-5;
TEST(GetFeatureValuesInt64Test, ReadsASingleValue) {
Example example;
(*example.mutable_features()->mutable_feature())["tag"]
.mutable_int64_list()
->add_value(42);
auto tag = GetFeatureValues<protobuf_int64>("tag", example);
ASSERT_EQ(1, tag.size());
EXPECT_EQ(42, tag.Get(0));
}
TEST(GetFeatureValuesInt64Test, ReadsASingleValueFromFeature) {
Feature feature;
feature.mutable_int64_list()->add_value(42);
auto values = GetFeatureValues<protobuf_int64>(feature);
ASSERT_EQ(1, values.size());
EXPECT_EQ(42, values.Get(0));
}
TEST(GetFeatureValuesInt64Test, ReadsASingleValueFromSequenceExampleContext) {
SequenceExample example;
(*example.mutable_context()->mutable_feature())["tag"]
.mutable_int64_list()
->add_value(42);
auto tag = GetFeatureValues<protobuf_int64>("tag", example);
ASSERT_EQ(1, tag.size());
EXPECT_EQ(42, tag.Get(0));
}
TEST(GetFeatureValuesInt64Test, WritesASingleValue) {
Example example;
GetFeatureValues<protobuf_int64>("tag", &example)->Add(42);
ASSERT_EQ(1,
example.features().feature().at("tag").int64_list().value_size());
EXPECT_EQ(42, example.features().feature().at("tag").int64_list().value(0));
}
TEST(GetFeatureValuesInt64Test, WritesASingleValueToFeature) {
Feature feature;
GetFeatureValues<protobuf_int64>(&feature)->Add(42);
ASSERT_EQ(1, feature.int64_list().value_size());
EXPECT_EQ(42, feature.int64_list().value(0));
}
TEST(GetFeatureValuesInt64Test, WritesASingleValueToSequenceExample) {
SequenceExample example;
GetFeatureValues<protobuf_int64>("tag", &example)->Add(42);
ASSERT_EQ(1, example.context().feature().at("tag").int64_list().value_size());
EXPECT_EQ(42, example.context().feature().at("tag").int64_list().value(0));
}
TEST(GetFeatureValuesInt64Test, CheckUntypedFieldExistence) {
Example example;
ASSERT_FALSE(HasFeature("tag", example));
GetFeatureValues<protobuf_int64>("tag", &example)->Add(0);
EXPECT_TRUE(HasFeature("tag", example));
}
TEST(GetFeatureValuesInt64Test, CheckUntypedFieldExistenceForSequenceExample) {
SequenceExample seq_example;
ASSERT_FALSE(HasFeature("tag", seq_example));
GetFeatureValues<protobuf_int64>("tag", &seq_example)->Add(0);
EXPECT_TRUE(HasFeature("tag", seq_example));
}
TEST(GetFeatureValuesInt64Test, CheckTypedFieldExistence) {
Example example;
GetFeatureValues<float>("tag", &example)->Add(3.14);
ASSERT_FALSE(HasFeature<protobuf_int64>("tag", example));
GetFeatureValues<protobuf_int64>("tag", &example)->Add(42);
EXPECT_TRUE(HasFeature<protobuf_int64>("tag", example));
auto tag_ro = GetFeatureValues<protobuf_int64>("tag", example);
ASSERT_EQ(1, tag_ro.size());
EXPECT_EQ(42, tag_ro.Get(0));
}
TEST(GetFeatureValuesInt64Test, CheckTypedFieldExistenceForSequenceExample) {
SequenceExample sequence_example;
GetFeatureValues<float>("tag", &sequence_example)->Add(3.14);
ASSERT_FALSE(HasFeature<protobuf_int64>("tag", sequence_example));
GetFeatureValues<protobuf_int64>("tag", &sequence_example)->Add(42);
EXPECT_TRUE(HasFeature<protobuf_int64>("tag", sequence_example));
auto tag_ro = GetFeatureValues<protobuf_int64>("tag", sequence_example);
ASSERT_EQ(1, tag_ro.size());
EXPECT_EQ(42, tag_ro.Get(0));
}
TEST(GetFeatureValuesInt64Test, CopyIterableToAField) {
Example example;
std::vector<int> values{1, 2, 3};
std::copy(values.begin(), values.end(),
protobuf::RepeatedFieldBackInserter(
GetFeatureValues<protobuf_int64>("tag", &example)));
auto tag_ro = GetFeatureValues<protobuf_int64>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_EQ(1, tag_ro.Get(0));
EXPECT_EQ(2, tag_ro.Get(1));
EXPECT_EQ(3, tag_ro.Get(2));
}
TEST(GetFeatureValuesFloatTest, ReadsASingleValueFromFeature) {
Feature feature;
feature.mutable_float_list()->add_value(3.14);
auto values = GetFeatureValues<float>(feature);
ASSERT_EQ(1, values.size());
EXPECT_NEAR(3.14, values.Get(0), kTolerance);
}
TEST(GetFeatureValuesFloatTest, ReadsASingleValue) {
Example example;
(*example.mutable_features()->mutable_feature())["tag"]
.mutable_float_list()
->add_value(3.14);
auto tag = GetFeatureValues<float>("tag", example);
ASSERT_EQ(1, tag.size());
EXPECT_NEAR(3.14, tag.Get(0), kTolerance);
}
TEST(GetFeatureValuesFloatTest, ReadsASingleValueFromSequenceExample) {
SequenceExample example;
(*example.mutable_context()->mutable_feature())["tag"]
.mutable_float_list()
->add_value(3.14);
auto tag = GetFeatureValues<float>("tag", example);
ASSERT_EQ(1, tag.size());
EXPECT_NEAR(3.14, tag.Get(0), kTolerance);
}
TEST(GetFeatureValuesFloatTest, WritesASingleValueToFeature) {
Feature feature;
GetFeatureValues<float>(&feature)->Add(3.14);
ASSERT_EQ(1, feature.float_list().value_size());
EXPECT_NEAR(3.14, feature.float_list().value(0), kTolerance);
}
TEST(GetFeatureValuesFloatTest, WritesASingleValue) {
Example example;
GetFeatureValues<float>("tag", &example)->Add(3.14);
ASSERT_EQ(1,
example.features().feature().at("tag").float_list().value_size());
EXPECT_NEAR(3.14,
example.features().feature().at("tag").float_list().value(0),
kTolerance);
}
TEST(GetFeatureValuesFloatTest, WritesASingleValueToSequenceExample) {
SequenceExample example;
GetFeatureValues<float>("tag", &example)->Add(3.14);
ASSERT_EQ(1, example.context().feature().at("tag").float_list().value_size());
EXPECT_NEAR(3.14, example.context().feature().at("tag").float_list().value(0),
kTolerance);
}
TEST(GetFeatureValuesFloatTest, CheckTypedFieldExistence) {
Example example;
GetFeatureValues<protobuf_int64>("tag", &example)->Add(42);
ASSERT_FALSE(HasFeature<float>("tag", example));
GetFeatureValues<float>("tag", &example)->Add(3.14);
EXPECT_TRUE(HasFeature<float>("tag", example));
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(1, tag_ro.size());
EXPECT_NEAR(3.14, tag_ro.Get(0), kTolerance);
}
TEST(GetFeatureValuesFloatTest, CheckTypedFieldExistenceForDeprecatedMethod) {
Example example;
GetFeatureValues<protobuf_int64>("tag", &example)->Add(42);
ASSERT_FALSE(ExampleHasFeature<float>("tag", example));
GetFeatureValues<float>("tag", &example)->Add(3.14);
EXPECT_TRUE(ExampleHasFeature<float>("tag", example));
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(1, tag_ro.size());
EXPECT_NEAR(3.14, tag_ro.Get(0), kTolerance);
}
TEST(GetFeatureValuesStringTest, ReadsASingleValueFromFeature) {
Feature feature;
feature.mutable_bytes_list()->add_value("FOO");
auto values = GetFeatureValues<std::string>(feature);
ASSERT_EQ(1, values.size());
EXPECT_EQ("FOO", values.Get(0));
}
TEST(GetFeatureValuesStringTest, ReadsASingleValue) {
Example example;
(*example.mutable_features()->mutable_feature())["tag"]
.mutable_bytes_list()
->add_value("FOO");
auto tag = GetFeatureValues<std::string>("tag", example);
ASSERT_EQ(1, tag.size());
EXPECT_EQ("FOO", tag.Get(0));
}
TEST(GetFeatureValuesStringTest, ReadsASingleValueFromSequenceExample) {
SequenceExample example;
(*example.mutable_context()->mutable_feature())["tag"]
.mutable_bytes_list()
->add_value("FOO");
auto tag = GetFeatureValues<std::string>("tag", example);
ASSERT_EQ(1, tag.size());
EXPECT_EQ("FOO", tag.Get(0));
}
TEST(GetFeatureValuesStringTest, WritesASingleValueToFeature) {
Feature feature;
*GetFeatureValues<std::string>(&feature)->Add() = "FOO";
ASSERT_EQ(1, feature.bytes_list().value_size());
EXPECT_EQ("FOO", feature.bytes_list().value(0));
}
TEST(GetFeatureValuesStringTest, WritesASingleValue) {
Example example;
*GetFeatureValues<std::string>("tag", &example)->Add() = "FOO";
ASSERT_EQ(1,
example.features().feature().at("tag").bytes_list().value_size());
EXPECT_EQ("FOO",
example.features().feature().at("tag").bytes_list().value(0));
}
TEST(GetFeatureValuesStringTest, WritesASingleValueSequenceExample) {
SequenceExample example;
*GetFeatureValues<std::string>("tag", &example)->Add() = "FOO";
ASSERT_EQ(1, example.context().feature().at("tag").bytes_list().value_size());
EXPECT_EQ("FOO", example.context().feature().at("tag").bytes_list().value(0));
}
TEST(GetFeatureValuesStringTest, CheckTypedFieldExistence) {
Example example;
GetFeatureValues<protobuf_int64>("tag", &example)->Add(42);
ASSERT_FALSE(HasFeature<std::string>("tag", example));
*GetFeatureValues<std::string>("tag", &example)->Add() = "FOO";
EXPECT_TRUE(HasFeature<std::string>("tag", example));
auto tag_ro = GetFeatureValues<std::string>("tag", example);
ASSERT_EQ(1, tag_ro.size());
EXPECT_EQ("FOO", tag_ro.Get(0));
}
TEST(AppendFeatureValuesTest, FloatValuesFromContainer) {
Example example;
std::vector<double> values{1.1, 2.2, 3.3};
AppendFeatureValues(values, "tag", &example);
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(1.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(2.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(3.3, tag_ro.Get(2), kTolerance);
}
TEST(AppendFeatureValuesTest, FloatValuesFromContainerWithStringViewKey) {
Example example;
std::vector<double> values{1.1, 2.2, 3.3};
absl::string_view key("tag");
AppendFeatureValues(values, key, &example);
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(1.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(2.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(3.3, tag_ro.Get(2), kTolerance);
}
TEST(AppendFeatureValuesTest, FloatValuesUsingInitializerList) {
Example example;
AppendFeatureValues({1.1, 2.2, 3.3}, "tag", &example);
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(1.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(2.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(3.3, tag_ro.Get(2), kTolerance);
}
TEST(AppendFeatureValuesTest,
FloatValuesUsingInitializerListWithStringViewKey) {
Example example;
absl::string_view key("tag");
AppendFeatureValues({1.1, 2.2, 3.3}, key, &example);
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(1.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(2.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(3.3, tag_ro.Get(2), kTolerance);
}
TEST(AppendFeatureValuesTest, FloatValuesUsingIterators) {
Example example;
std::vector<double> values{1.1, 2.2, 3.3};
AppendFeatureValues(values.begin(), values.end(), "tag", &example);
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(1.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(2.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(3.3, tag_ro.Get(2), kTolerance);
}
TEST(AppendFeatureValuesTest, FloatValuesUsingIteratorsWithStringViewKey) {
Example example;
absl::string_view key("tag");
std::vector<double> values{1.1, 2.2, 3.3};
AppendFeatureValues(values.begin(), values.end(), key, &example);
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(1.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(2.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(3.3, tag_ro.Get(2), kTolerance);
}
TEST(SetFeatureValuesTest, FloatValuesUsingInitializerList) {
Example example;
// The first set of values should be overwritten by the second.
AppendFeatureValues({1.1, 2.2, 3.3}, "tag", &example);
SetFeatureValues({10.1, 20.2, 30.3}, "tag", &example);
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(10.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(20.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(30.3, tag_ro.Get(2), kTolerance);
}
TEST(SetFeatureValuesTest, ContainerOfStringView) {
Example example;
std::vector<std::string> values = {"hello", "world"};
std::vector<absl::string_view> values_string_view(values.begin(),
values.end());
SetFeatureValues(values_string_view, "tag", &example);
auto tag_ro = GetFeatureValues<std::string>("tag", example);
ASSERT_EQ(tag_ro.size(), 2);
EXPECT_EQ(tag_ro.Get(0), "hello");
EXPECT_EQ(tag_ro.Get(1), "world");
}
TEST(AppendFeatureValuesTest, Int64ValuesUsingInitializerList) {
Example example;
std::vector<protobuf_int64> values{1, 2, 3};
AppendFeatureValues(values, "tag", &example);
auto tag_ro = GetFeatureValues<protobuf_int64>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_EQ(1, tag_ro.Get(0));
EXPECT_EQ(2, tag_ro.Get(1));
EXPECT_EQ(3, tag_ro.Get(2));
}
TEST(AppendFeatureValuesTest, StringValuesUsingInitializerList) {
Example example;
AppendFeatureValues({"FOO", "BAR", "BAZ"}, "tag", &example);
auto tag_ro = GetFeatureValues<std::string>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_EQ("FOO", tag_ro.Get(0));
EXPECT_EQ("BAR", tag_ro.Get(1));
EXPECT_EQ("BAZ", tag_ro.Get(2));
}
TEST(AppendFeatureValuesTest, StringVariablesUsingInitializerList) {
Example example;
std::string string1("FOO");
std::string string2("BAR");
std::string string3("BAZ");
AppendFeatureValues({string1, string2, string3}, "tag", &example);
auto tag_ro = GetFeatureValues<std::string>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_EQ("FOO", tag_ro.Get(0));
EXPECT_EQ("BAR", tag_ro.Get(1));
EXPECT_EQ("BAZ", tag_ro.Get(2));
}
TEST(AppendFeatureValuesTest, StringViewVariablesUsingInitializerList) {
Example example;
AppendFeatureValues({absl::string_view("FOO"), absl::string_view("BAR"),
absl::string_view("BAZ")},
"tag", &example);
auto tag_ro = GetFeatureValues<std::string>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_EQ("FOO", tag_ro.Get(0));
EXPECT_EQ("BAR", tag_ro.Get(1));
EXPECT_EQ("BAZ", tag_ro.Get(2));
}
TEST(AppendFeatureValuesTest, StringViewVariablesUsingIterators) {
Example example;
std::vector<absl::string_view> strings;
strings.push_back("FOO");
strings.push_back("BAR");
strings.push_back("BAZ");
AppendFeatureValues(strings.begin(), strings.end(), "tag", &example);
auto tag_ro = GetFeatureValues<std::string>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_EQ("FOO", tag_ro.Get(0));
EXPECT_EQ("BAR", tag_ro.Get(1));
EXPECT_EQ("BAZ", tag_ro.Get(2));
}
TEST(ExtendAndGetSpanTest, FloatValues) {
Example example;
Feature* feature = GetFeature("tag", &example);
constexpr float kDefaultValue = -1.0f;
absl::Span<float> values = ExtendAndGetSpan(feature, 3, kDefaultValue);
EXPECT_EQ(values.size(), 3);
EXPECT_EQ(values[0], kDefaultValue);
EXPECT_EQ(values[1], kDefaultValue);
EXPECT_EQ(values[2], kDefaultValue);
// Also verify if writing elements to the span is effective.
values[0] = 10.1;
values[1] = 20.2;
values[2] = 30.3;
auto tag_ro = GetFeatureValues<float>("tag", example);
EXPECT_EQ(values[0], tag_ro.Get(0));
EXPECT_EQ(values[1], tag_ro.Get(1));
EXPECT_EQ(values[2], tag_ro.Get(2));
}
TEST(ExtendAndGetSpanTest, Int64Values) {
Example example;
Feature* feature = GetFeature("tag", &example);
constexpr int64_t kDefaultValue = -1;
absl::Span<int64_t> values = ExtendAndGetSpan(feature, 3, kDefaultValue);
EXPECT_EQ(values.size(), 3);
EXPECT_EQ(values[0], kDefaultValue);
EXPECT_EQ(values[1], kDefaultValue);
EXPECT_EQ(values[2], kDefaultValue);
// Also verify if writing elements to the span is effective.
values[0] = 10;
values[1] = 20;
values[2] = 30;
auto tag_ro = GetFeatureValues<protobuf_int64>("tag", example);
EXPECT_EQ(values[0], tag_ro.Get(0));
EXPECT_EQ(values[1], tag_ro.Get(1));
EXPECT_EQ(values[2], tag_ro.Get(2));
}
TEST(GetFeatureTest, WritesAVectorToFeature) {
Example example;
Feature* feature = GetFeature("tag", &example);
AppendFeatureValues<float>({1.1, 2.2, 3.3}, feature);
auto tag_ro = GetFeatureValues<float>("tag", example);
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(1.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(2.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(3.3, tag_ro.Get(2), kTolerance);
}
TEST(GetFeatureTest, ReadsAVectorFromFeature) {
Example example;
AppendFeatureValues<float>({1.1, 2.2, 3.3}, "tag", &example);
const Feature& feature = GetFeature("tag", example);
auto tag_ro = GetFeatureValues<float>(feature);
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(1.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(2.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(3.3, tag_ro.Get(2), kTolerance);
}
TEST(SequenceExampleTest, ReadsASingleValueFromContext) {
SequenceExample se;
(*se.mutable_context()->mutable_feature())["tag"]
.mutable_int64_list()
->add_value(42);
auto values = GetFeatureValues<protobuf_int64>("tag", se.context());
ASSERT_EQ(1, values.size());
EXPECT_EQ(42, values.Get(0));
}
TEST(SequenceExampleTest, WritesASingleValueToContext) {
SequenceExample se;
GetFeatureValues<protobuf_int64>("tag", se.mutable_context())->Add(42);
ASSERT_EQ(1, se.context().feature().at("tag").int64_list().value_size());
EXPECT_EQ(42, se.context().feature().at("tag").int64_list().value(0));
}
TEST(SequenceExampleTest, AppendFeatureValuesToContextSingleArg) {
SequenceExample se;
AppendFeatureValues({1.1, 2.2, 3.3}, "tag", se.mutable_context());
auto tag_ro = GetFeatureValues<float>("tag", se.context());
ASSERT_EQ(3, tag_ro.size());
EXPECT_NEAR(1.1, tag_ro.Get(0), kTolerance);
EXPECT_NEAR(2.2, tag_ro.Get(1), kTolerance);
EXPECT_NEAR(3.3, tag_ro.Get(2), kTolerance);
}
TEST(SequenceExampleTest, CheckTypedFieldExistence) {
SequenceExample se;
GetFeatureValues<float>("tag", se.mutable_context())->Add(3.14);
ASSERT_FALSE(HasFeature<protobuf_int64>("tag", se.context()));
GetFeatureValues<protobuf_int64>("tag", se.mutable_context())->Add(42);
EXPECT_TRUE(HasFeature<protobuf_int64>("tag", se.context()));
auto tag_ro = GetFeatureValues<protobuf_int64>("tag", se.context());
ASSERT_EQ(1, tag_ro.size());
EXPECT_EQ(42, tag_ro.Get(0));
}
TEST(SequenceExampleTest, ReturnsExistingFeatureLists) {
SequenceExample se;
(*se.mutable_feature_lists()->mutable_feature_list())["tag"]
.mutable_feature()
->Add();
auto feature = GetFeatureList("tag", se);
ASSERT_EQ(1, feature.size());
}
TEST(SequenceExampleTest, CreatesNewFeatureLists) {
SequenceExample se;
GetFeatureList("tag", &se)->Add();
EXPECT_EQ(1, se.feature_lists().feature_list().at("tag").feature_size());
}
TEST(SequenceExampleTest, CheckFeatureListExistence) {
SequenceExample se;
ASSERT_FALSE(HasFeatureList("tag", se));
GetFeatureList("tag", &se)->Add();
ASSERT_TRUE(HasFeatureList("tag", se));
}
TEST(SequenceExampleTest, AppendFeatureValuesWithInitializerList) {
SequenceExample se;
AppendFeatureValues({1, 2, 3}, "ids", se.mutable_context());
AppendFeatureValues({"cam1-0", "cam2-0"},
GetFeatureList("images", &se)->Add());
AppendFeatureValues({"cam1-1", "cam2-2"},
GetFeatureList("images", &se)->Add());
SequenceExample expected_proto;
protobuf::TextFormat::ParseFromString(
"context {\n"
" feature {\n"
" key: \"ids\"\n"
" value {\n"
" int64_list {\n"
" value: 1\n"
" value: 2\n"
" value: 3\n"
" }\n"
" }\n"
" }\n"
"}\n"
"feature_lists {\n"
" feature_list {\n"
" key: \"images\"\n"
" value {\n"
" feature {\n"
" bytes_list {\n"
" value: \"cam1-0\"\n"
" value: \"cam2-0\"\n"
" }\n"
" }\n"
" feature {\n"
" bytes_list {\n"
" value: \"cam1-1\"\n"
" value: \"cam2-2\"\n"
" }\n"
" }\n"
" }\n"
" }\n"
"}\n",
&expected_proto);
EXPECT_EQ(se.DebugString(), expected_proto.DebugString());
}
TEST(SequenceExampleTest, AppendFeatureValuesWithVectors) {
SequenceExample se;
std::vector<float> readings{1.0, 2.5, 5.0};
AppendFeatureValues(readings, GetFeatureList("movie_ratings", &se)->Add());
SequenceExample expected_proto;
protobuf::TextFormat::ParseFromString(
"feature_lists {\n"
" feature_list {\n"
" key: \"movie_ratings\"\n"
" value {\n"
" feature {\n"
" float_list {\n"
" value: 1\n"
" value: 2.5\n"
" value: 5\n"
" }\n"
" }\n"
" }\n"
" }\n"
"}\n",
&expected_proto);
EXPECT_EQ(se.DebugString(), expected_proto.DebugString());
}
TEST(SequenceExampleTest, SetContextFeatureValuesWithInitializerList) {
SequenceExample se;
// The first set of values should be overwritten by the second.
SetFeatureValues({101, 102, 103}, "ids", se.mutable_context());
SetFeatureValues({1, 2, 3}, "ids", se.mutable_context());
// These values should be appended without overwriting.
AppendFeatureValues({4, 5, 6}, "ids", se.mutable_context());
SequenceExample expected_proto;
protobuf::TextFormat::ParseFromString(
"context {\n"
" feature {\n"
" key: \"ids\"\n"
" value {\n"
" int64_list {\n"
" value: 1\n"
" value: 2\n"
" value: 3\n"
" value: 4\n"
" value: 5\n"
" value: 6\n"
" }\n"
" }\n"
" }\n"
"}\n",
&expected_proto);
EXPECT_EQ(se.DebugString(), expected_proto.DebugString());
}
TEST(SequenceExampleTest, SetFeatureValuesWithInitializerList) {
SequenceExample se;
// The first set of values should be overwritten by the second.
AppendFeatureValues({1, 2, 3}, "ids", se.mutable_context());
SetFeatureValues({4, 5, 6}, "ids", se.mutable_context());
// Two distinct features are added to the same feature list, so both will
// coexist in the output.
AppendFeatureValues({"cam1-0", "cam2-0"},
GetFeatureList("images", &se)->Add());
SetFeatureValues({"cam1-1", "cam2-1"}, GetFeatureList("images", &se)->Add());
// The first set of values should be overwritten by the second.
AppendFeatureValues({"cam1-0", "cam2-0"},
GetFeatureList("more-images", &se)->Add());
SetFeatureValues({"cam1-1", "cam2-1"},
GetFeatureList("more-images", &se)->Mutable(0));
SequenceExample expected_proto;
protobuf::TextFormat::ParseFromString(
"context {\n"
" feature {\n"
" key: \"ids\"\n"
" value {\n"
" int64_list {\n"
" value: 4\n"
" value: 5\n"
" value: 6\n"
" }\n"
" }\n"
" }\n"
"}\n"
"feature_lists {\n"
" feature_list {\n"
" key: \"images\"\n"
" value {\n"
" feature {\n"
" bytes_list {\n"
" value: \"cam1-0\"\n"
" value: \"cam2-0\"\n"
" }\n"
" }\n"
" feature {\n"
" bytes_list {\n"
" value: \"cam1-1\"\n"
" value: \"cam2-1\"\n"
" }\n"
" }\n"
" }\n"
" }\n"
" feature_list {\n"
" key: \"more-images\"\n"
" value {\n"
" feature {\n"
" bytes_list {\n"
" value: \"cam1-1\"\n"
" value: \"cam2-1\"\n"
" }\n"
" }\n"
" }\n"
" }\n"
"}\n",
&expected_proto);
EXPECT_EQ(se.DebugString(), expected_proto.DebugString());
}
TEST(MaybeGetFeatureValuesTest, ReturnsNullPtr) {
const Example example;
auto tag = MaybeGetFeatureValues<protobuf_int64>("tag", example);
ASSERT_EQ(tag, nullptr);
}
TEST(MaybeGetFeatureValuesTest, ReadsASingleInt) {
Example example;
(*example.mutable_features()->mutable_feature())["tag"]
.mutable_int64_list()
->add_value(42);
auto tag = MaybeGetFeatureValues<protobuf_int64>("tag", example);
ASSERT_EQ(1, tag->size());
EXPECT_EQ(42, tag->Get(0));
}
TEST(MaybeGetFeatureValuesTest, ReadsASingleFloat) {
Example example;
(*example.mutable_features()->mutable_feature())["tag"]
.mutable_float_list()
->add_value(0.3);
auto tag = MaybeGetFeatureValues<float>("tag", example);
ASSERT_EQ(1, tag->size());
EXPECT_FLOAT_EQ(0.3, tag->Get(0));
}
TEST(MaybeGetFeatureValuesTest, ReadsASingleString) {
Example example;
(*example.mutable_features()->mutable_feature())["tag"]
.mutable_bytes_list()
->add_value("entry");
auto tag = MaybeGetFeatureValues<std::string>("tag", example);
ASSERT_EQ(1, tag->size());
EXPECT_EQ("entry", tag->Get(0));
}
} // namespace
} // namespace tensorflow
+16
View File
@@ -0,0 +1,16 @@
# Example parser test data.
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
default_visibility = [
"//tensorflow/core/example:__pkg__",
],
licenses = ["notice"],
)
filegroup(
name = "example_parser_configuration_testdata",
srcs = [
"parse_example_graph_def.pbtxt",
],
)
@@ -0,0 +1,334 @@
node {
name: "batch_constant"
op: "Const"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
}
string_val: "z"
}
}
}
}
node {
name: "ParseExample/Reshape/shape"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
}
float_val: 1
}
}
}
}
node {
name: "ParseExample/key_y"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 0.0
}
}
}
}
node {
name: "ParseExample/Reshape_1/shape"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
}
float_val: 1
}
}
}
}
node {
name: "ParseExample/key_z"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 0.0
}
}
}
}
node {
name: "ParseExample/Reshape_2/shape"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
}
float_val: 1
}
}
}
}
node {
name: "ParseExample/ParseExample/names"
op: "Const"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
dim {
}
}
}
}
}
}
node {
name: "ParseExample/ParseExample/sparse_keys_0"
op: "Const"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
}
string_val: "sf0"
}
}
}
}
node {
name: "ParseExample/ParseExample/sparse_keys_1"
op: "Const"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
}
string_val: "sf1"
}
}
}
}
node {
name: "ParseExample/ParseExample/dense_keys_0"
op: "Const"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
}
string_val: "x"
}
}
}
}
node {
name: "ParseExample/ParseExample/dense_keys_1"
op: "Const"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
}
string_val: "y"
}
}
}
}
node {
name: "ParseExample/ParseExample/dense_keys_2"
op: "Const"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
}
string_val: "z"
}
}
}
}
node {
name: "ParseExample/ParseExample"
op: "ParseExample"
input: "batch_constant"
input: "ParseExample/ParseExample/names"
input: "ParseExample/ParseExample/sparse_keys_0"
input: "ParseExample/ParseExample/sparse_keys_1"
input: "ParseExample/ParseExample/dense_keys_0"
input: "ParseExample/ParseExample/dense_keys_1"
input: "ParseExample/ParseExample/dense_keys_2"
input: "ParseExample/Reshape/shape"
input: "ParseExample/Reshape_1/shape"
input: "ParseExample/Reshape_2/shape"
attr {
key: "Nsparse"
value {
i: 2
}
}
attr {
key: "Ndense"
value {
i: 3
}
}
attr {
key: "sparse_types"
value {
list {
type: DT_STRING
type: DT_STRING
}
}
}
attr {
key: "Tdense"
value {
list {
type: DT_FLOAT
type: DT_FLOAT
type: DT_FLOAT
}
}
}
attr {
key: "dense_shapes"
value {
list {
shape {
dim {
size: 1
}
}
shape {
dim {
size: 1
}
}
shape {
dim {
size: 1
}
}
}
}
}
}
versions {
producer: 9
}