Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 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 // See docs in ../ops/io_ops.cc.
     17 #include "tensorflow/core/kernels/save_restore_tensor.h"
     18 
     19 #include "tensorflow/core/framework/op_kernel.h"
     20 #include "tensorflow/core/lib/core/errors.h"
     21 #include "tensorflow/core/lib/gtl/array_slice.h"
     22 #include "tensorflow/core/platform/logging.h"
     23 #include "tensorflow/core/util/tensor_slice_reader.h"
     24 
     25 namespace tensorflow {
     26 
     27 class RestoreOp : public OpKernel {
     28  public:
     29   explicit RestoreOp(OpKernelConstruction* context) : OpKernel(context) {
     30     int preferred_shard;
     31     OP_REQUIRES_OK(context,
     32                    context->GetAttr("preferred_shard", &preferred_shard));
     33     if (preferred_shard == -1) {
     34       preferred_shard_ = checkpoint::TensorSliceReader::kLoadAllShards;
     35     } else {
     36       OP_REQUIRES(context, preferred_shard >= 0,
     37                   errors::InvalidArgument("Attribute 'preferred_shard' must be "
     38                                           "greater or equal to -1"));
     39       preferred_shard_ = preferred_shard;
     40     }
     41   }
     42   void Compute(OpKernelContext* context) override {
     43     RestoreTensor(context, &checkpoint::OpenTableTensorSliceReader,
     44                   preferred_shard_, false, 0);
     45   }
     46 
     47  private:
     48   int preferred_shard_;
     49 };
     50 
     51 REGISTER_KERNEL_BUILDER(Name("Restore").Device(DEVICE_CPU), RestoreOp);
     52 
     53 class RestoreSliceOp : public OpKernel {
     54  public:
     55   explicit RestoreSliceOp(OpKernelConstruction* context) : OpKernel(context) {
     56     int preferred_shard;
     57     OP_REQUIRES_OK(context,
     58                    context->GetAttr("preferred_shard", &preferred_shard));
     59     if (preferred_shard == -1) {
     60       preferred_shard_ = checkpoint::TensorSliceReader::kLoadAllShards;
     61     } else {
     62       OP_REQUIRES(context, preferred_shard >= 0,
     63                   errors::InvalidArgument("Attribute 'preferred_shard' must be "
     64                                           "greater or equal to -1"));
     65       preferred_shard_ = preferred_shard;
     66     }
     67   }
     68   void Compute(OpKernelContext* context) override {
     69     RestoreTensor(context, &checkpoint::OpenTableTensorSliceReader,
     70                   preferred_shard_, true, 0);
     71   }
     72 
     73  private:
     74   int preferred_shard_;
     75 };
     76 
     77 REGISTER_KERNEL_BUILDER(Name("RestoreSlice").Device(DEVICE_CPU),
     78                         RestoreSliceOp);
     79 
     80 }  // namespace tensorflow
     81