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 #include <iostream>
     17 #include "tensorflow/core/framework/op_kernel.h"
     18 #include "tensorflow/core/lib/core/status.h"
     19 #include "tensorflow/core/lib/strings/str_util.h"
     20 #include "tensorflow/core/platform/logging.h"
     21 
     22 namespace tensorflow {
     23 
     24 class AssertOp : public OpKernel {
     25  public:
     26   explicit AssertOp(OpKernelConstruction* ctx) : OpKernel(ctx) {
     27     OP_REQUIRES_OK(ctx, ctx->GetAttr("summarize", &summarize_));
     28   }
     29 
     30   void Compute(OpKernelContext* ctx) override {
     31     const Tensor& cond = ctx->input(0);
     32     OP_REQUIRES(ctx, IsLegacyScalar(cond.shape()),
     33                 errors::InvalidArgument("In[0] should be a scalar: ",
     34                                         cond.shape().DebugString()));
     35 
     36     if (cond.scalar<bool>()()) {
     37       return;
     38     }
     39     string msg = "assertion failed: ";
     40     for (int i = 1; i < ctx->num_inputs(); ++i) {
     41       strings::StrAppend(&msg, "[", ctx->input(i).SummarizeValue(summarize_),
     42                          "]");
     43       if (i < ctx->num_inputs() - 1) strings::StrAppend(&msg, " ");
     44     }
     45     ctx->SetStatus(errors::InvalidArgument(msg));
     46   }
     47 
     48  private:
     49   int32 summarize_ = 0;
     50 };
     51 
     52 REGISTER_KERNEL_BUILDER(Name("Assert").Device(DEVICE_CPU), AssertOp);
     53 
     54 class PrintOp : public OpKernel {
     55  public:
     56   explicit PrintOp(OpKernelConstruction* ctx)
     57       : OpKernel(ctx), call_counter_(0) {
     58     OP_REQUIRES_OK(ctx, ctx->GetAttr("message", &message_));
     59     OP_REQUIRES_OK(ctx, ctx->GetAttr("first_n", &first_n_));
     60     OP_REQUIRES_OK(ctx, ctx->GetAttr("summarize", &summarize_));
     61   }
     62 
     63   void Compute(OpKernelContext* ctx) override {
     64     if (IsRefType(ctx->input_dtype(0))) {
     65       ctx->forward_ref_input_to_ref_output(0, 0);
     66     } else {
     67       ctx->set_output(0, ctx->input(0));
     68     }
     69     if (first_n_ >= 0) {
     70       mutex_lock l(mu_);
     71       if (call_counter_ >= first_n_) return;
     72       call_counter_++;
     73     }
     74     string msg;
     75     strings::StrAppend(&msg, message_);
     76     for (int i = 1; i < ctx->num_inputs(); ++i) {
     77       strings::StrAppend(&msg, "[", ctx->input(i).SummarizeValue(summarize_),
     78                          "]");
     79     }
     80     std::cerr << msg << std::endl;
     81   }
     82 
     83  private:
     84   mutex mu_;
     85   int64 call_counter_ GUARDED_BY(mu_) = 0;
     86   int64 first_n_ = 0;
     87   int32 summarize_ = 0;
     88   string message_;
     89 };
     90 
     91 REGISTER_KERNEL_BUILDER(Name("Print").Device(DEVICE_CPU), PrintOp);
     92 
     93 }  // end namespace tensorflow
     94