1 /* Copyright 2016 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 <string>
17 #include <vector>
18
19 #include "tensorflow/core/framework/fake_input.h"
20 #include "tensorflow/core/framework/node_def_builder.h"
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/framework/tensor_shape.h"
23 #include "tensorflow/core/framework/types.h"
24 #include "tensorflow/core/kernels/ops_testutil.h"
25 #include "tensorflow/core/lib/core/status.h"
26 #include "tensorflow/core/lib/core/status_test_util.h"
27 #include "tensorflow/core/lib/gtl/array_slice.h"
28 #include "tensorflow/core/lib/io/path.h"
29 #include "tensorflow/core/platform/env.h"
30 #include "tensorflow/core/platform/test.h"
31 #include "tensorflow/core/platform/types.h"
32 #include "tensorflow/core/util/tensor_bundle/tensor_bundle.h"
33
34 namespace tensorflow {
35 namespace {
36
WriteCheckpoint(const string & prefix,gtl::ArraySlice<string> names,gtl::ArraySlice<Tensor> tensors)37 void WriteCheckpoint(const string& prefix, gtl::ArraySlice<string> names,
38 gtl::ArraySlice<Tensor> tensors) {
39 BundleWriter writer(Env::Default(), prefix);
40 ASSERT_TRUE(names.size() == tensors.size());
41 for (size_t i = 0; i < names.size(); ++i) {
42 TF_ASSERT_OK(writer.Add(names[i], tensors[i]));
43 }
44 TF_ASSERT_OK(writer.Finish());
45 }
46
47 template <typename T>
Constant(T v,TensorShape shape)48 Tensor Constant(T v, TensorShape shape) {
49 Tensor ret(DataTypeToEnum<T>::value, shape);
50 ret.flat<T>().setConstant(v);
51 return ret;
52 }
53
54 class MergeV2CheckpointsOpTest : public OpsTestBase {
55 protected:
MakeOp(bool delete_old_dirs)56 void MakeOp(bool delete_old_dirs) {
57 TF_ASSERT_OK(NodeDefBuilder("myop", "MergeV2Checkpoints")
58 .Input(FakeInput()) // checkpoint_prefixes
59 .Input(FakeInput()) // destination_prefix
60 .Attr("delete_old_dirs", delete_old_dirs)
61 .Finalize(node_def()));
62 TF_ASSERT_OK(InitOp());
63 }
64
RunMergeTest(bool delete_old_dirs)65 void RunMergeTest(bool delete_old_dirs) {
66 // Writes two checkpoints.
67 const std::vector<string> prefixes = {
68 io::JoinPath(testing::TmpDir(), "worker0/ckpt0"),
69 io::JoinPath(testing::TmpDir(), "worker1/ckpt1"),
70 io::JoinPath(testing::TmpDir(), "merged/ckpt") /* merged prefix */};
71 // In a different directory, to exercise "delete_old_dirs".
72 const string& kMergedPrefix = prefixes[2];
73
74 WriteCheckpoint(prefixes[0], {"tensor0"},
75 {Constant<float>(0, TensorShape({10}))});
76 WriteCheckpoint(prefixes[1], {"tensor1", "tensor2"},
77 {Constant<int64>(1, TensorShape({1, 16, 18})),
78 Constant<bool>(true, TensorShape({}))});
79
80 // Now merges.
81 MakeOp(delete_old_dirs);
82 // Add checkpoint_prefixes.
83 AddInput<tstring>(TensorShape({2}),
84 [&prefixes](int i) -> tstring { return prefixes[i]; });
85 // Add destination_prefix.
86 AddInput<tstring>(TensorShape({}), [kMergedPrefix](int unused) -> tstring {
87 return kMergedPrefix;
88 });
89 TF_ASSERT_OK(RunOpKernel());
90
91 // Check that the merged checkpoint file is properly written.
92 BundleReader reader(Env::Default(), kMergedPrefix);
93 TF_EXPECT_OK(reader.status());
94
95 // We expect to find all saved tensors.
96 {
97 Tensor val0;
98 TF_EXPECT_OK(reader.Lookup("tensor0", &val0));
99 test::ExpectTensorEqual<float>(Constant<float>(0, TensorShape({10})),
100 val0);
101 }
102 {
103 Tensor val1;
104 TF_EXPECT_OK(reader.Lookup("tensor1", &val1));
105 test::ExpectTensorEqual<int64>(
106 Constant<int64>(1, TensorShape({1, 16, 18})), val1);
107 }
108 {
109 Tensor val2;
110 TF_EXPECT_OK(reader.Lookup("tensor2", &val2));
111 test::ExpectTensorEqual<bool>(Constant<bool>(true, TensorShape({})),
112 val2);
113 }
114
115 // Exercises "delete_old_dirs".
116 for (int i = 0; i < 2; ++i) {
117 int directory_found =
118 Env::Default()->IsDirectory(string(io::Dirname(prefixes[i]))).code();
119 if (delete_old_dirs) {
120 EXPECT_EQ(error::NOT_FOUND, directory_found);
121 } else {
122 EXPECT_EQ(error::OK, directory_found);
123 }
124 }
125 }
126 };
127
TEST_F(MergeV2CheckpointsOpTest,MergeNoDelete)128 TEST_F(MergeV2CheckpointsOpTest, MergeNoDelete) {
129 RunMergeTest(false /* don't delete old dirs */);
130 }
131
TEST_F(MergeV2CheckpointsOpTest,MergeAndDelete)132 TEST_F(MergeV2CheckpointsOpTest, MergeAndDelete) {
133 RunMergeTest(true /* delete old dirs */);
134 }
135
136 } // namespace
137 } // namespace tensorflow
138