1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include <functional>
17 #include <memory>
18
19 #include "tensorflow/core/framework/allocator.h"
20 #include "tensorflow/core/framework/fake_input.h"
21 #include "tensorflow/core/framework/node_def_builder.h"
22 #include "tensorflow/core/framework/op_kernel.h"
23 #include "tensorflow/core/framework/summary.pb.h"
24 #include "tensorflow/core/framework/tensor.h"
25 #include "tensorflow/core/framework/types.h"
26 #include "tensorflow/core/kernels/ops_testutil.h"
27 #include "tensorflow/core/kernels/ops_util.h"
28 #include "tensorflow/core/lib/core/status_test_util.h"
29 #include "tensorflow/core/lib/histogram/histogram.h"
30 #include "tensorflow/core/lib/strings/strcat.h"
31 #include "tensorflow/core/platform/env.h"
32 #include "tensorflow/core/platform/logging.h"
33 #include "tensorflow/core/platform/protobuf.h"
34 #include "tensorflow/core/platform/test.h"
35
36 namespace tensorflow {
37 namespace {
38
EXPECT_SummaryMatches(const Summary & actual,const string & expected_str)39 static void EXPECT_SummaryMatches(const Summary& actual,
40 const string& expected_str) {
41 Summary expected;
42 CHECK(protobuf::TextFormat::ParseFromString(expected_str, &expected));
43 EXPECT_EQ(expected.DebugString(), actual.DebugString());
44 }
45
46 // --------------------------------------------------------------------------
47 // SummaryTensorOpV2
48 // --------------------------------------------------------------------------
49 class SummaryTensorOpV2Test : public OpsTestBase {
50 protected:
MakeOp()51 void MakeOp() {
52 TF_ASSERT_OK(NodeDefBuilder("myop", "TensorSummaryV2")
53 .Input(FakeInput(DT_STRING))
54 .Input(FakeInput(DT_STRING))
55 .Input(FakeInput(DT_STRING))
56 .Finalize(node_def()));
57 TF_ASSERT_OK(InitOp());
58 }
59 };
60
TEST_F(SummaryTensorOpV2Test,BasicPluginData)61 TEST_F(SummaryTensorOpV2Test, BasicPluginData) {
62 MakeOp();
63
64 // Feed and run
65 AddInputFromArray<tstring>(TensorShape({}), {"tag_foo"});
66 AddInputFromArray<tstring>(TensorShape({}), {"some string tensor content"});
67
68 // Create a SummaryMetadata that stores data for 2 plugins.
69 SummaryMetadata summary_metadata;
70 SummaryMetadata::PluginData* plugin_data =
71 summary_metadata.mutable_plugin_data();
72 plugin_data->set_plugin_name("foo");
73 plugin_data->set_content("content_for_plugin_foo");
74 AddInputFromArray<tstring>(TensorShape({}),
75 {summary_metadata.SerializeAsString()});
76
77 TF_ASSERT_OK(RunOpKernel());
78
79 // Check the output size.
80 Tensor* out_tensor = GetOutput(0);
81 ASSERT_EQ(0, out_tensor->dims());
82 Summary summary;
83 ParseProtoUnlimited(&summary, out_tensor->scalar<tstring>()());
84 ASSERT_EQ(1, summary.value_size());
85
86 // Check the content of the tensor stored in the summary.
87 Tensor string_content_tensor;
88 CHECK(string_content_tensor.FromProto(summary.value(0).tensor()));
89 ASSERT_EQ("some string tensor content",
90 string_content_tensor.scalar<tstring>()());
91
92 // Check plugin-related data.
93 ASSERT_EQ("tag_foo", summary.value(0).tag());
94 ASSERT_EQ("foo", summary.value(0).metadata().plugin_data().plugin_name());
95 ASSERT_EQ("content_for_plugin_foo",
96 summary.value(0).metadata().plugin_data().content());
97 }
98
99 } // namespace
100 } // namespace tensorflow
101