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