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/framework/types.h"
     21 #include "tensorflow/core/lib/gtl/array_slice.h"
     22 #include "tensorflow/core/lib/strings/stringprintf.h"
     23 #include "tensorflow/core/platform/logging.h"
     24 #include "tensorflow/core/platform/types.h"
     25 #include "tensorflow/core/util/tensor_slice_writer.h"
     26 
     27 namespace tensorflow {
     28 
     29 class SaveOp : public OpKernel {
     30  public:
     31   explicit SaveOp(OpKernelConstruction* context) : OpKernel(context) {}
     32 
     33   void Compute(OpKernelContext* context) override {
     34     SaveTensors(context, &checkpoint::CreateTableTensorSliceBuilder, false);
     35   }
     36 };
     37 
     38 REGISTER_KERNEL_BUILDER(Name("Save").Device(DEVICE_CPU), SaveOp);
     39 
     40 class SaveSlicesOp : public OpKernel {
     41  public:
     42   explicit SaveSlicesOp(OpKernelConstruction* context) : OpKernel(context) {}
     43 
     44   void Compute(OpKernelContext* context) override {
     45     SaveTensors(context, &checkpoint::CreateTableTensorSliceBuilder, true);
     46   }
     47 };
     48 
     49 REGISTER_KERNEL_BUILDER(Name("SaveSlices").Device(DEVICE_CPU), SaveSlicesOp);
     50 
     51 class ShardedFilenameOp : public OpKernel {
     52  public:
     53   explicit ShardedFilenameOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
     54 
     55   void Compute(OpKernelContext* ctx) override {
     56     static const char* input_names[3] = {"basename", "shard", "num_shards"};
     57     for (int i = 0; i < ctx->num_inputs(); ++i) {
     58       OP_REQUIRES(ctx, IsLegacyScalar(ctx->input(i).shape()),
     59                   errors::InvalidArgument(input_names[i],
     60                                           " must be a scalar, got shape ",
     61                                           ctx->input(i).shape().DebugString()));
     62     }
     63     Tensor* out = nullptr;
     64     OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape({}), &out));
     65     out->scalar<string>()() = strings::Printf(
     66         "%s-%05d-of-%05d", ctx->input(0).scalar<string>()().c_str(),
     67         ctx->input(1).scalar<int32>()(), ctx->input(2).scalar<int32>()());
     68   }
     69 };
     70 
     71 REGISTER_KERNEL_BUILDER(Name("ShardedFilename").Device(DEVICE_CPU),
     72                         ShardedFilenameOp);
     73 
     74 class ShardedFilespecOp : public OpKernel {
     75  public:
     76   explicit ShardedFilespecOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
     77 
     78   void Compute(OpKernelContext* ctx) override {
     79     static const char* input_names[2] = {"basename", "num_shards"};
     80     for (int i = 0; i < ctx->num_inputs(); ++i) {
     81       OP_REQUIRES(ctx, IsLegacyScalar(ctx->input(i).shape()),
     82                   errors::InvalidArgument(input_names[i],
     83                                           " must be a scalar, got shape ",
     84                                           ctx->input(i).shape().DebugString()));
     85     }
     86     Tensor* out = nullptr;
     87     OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape({}), &out));
     88     out->scalar<string>()() = strings::Printf(
     89         "%s-\?\?\?\?\?-of-%05d", ctx->input(0).scalar<string>()().c_str(),
     90         ctx->input(1).scalar<int32>()());
     91   }
     92 };
     93 REGISTER_KERNEL_BUILDER(Name("ShardedFilespec").Device(DEVICE_CPU),
     94                         ShardedFilespecOp);
     95 
     96 }  // namespace tensorflow
     97