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 "tensorflow/core/framework/common_shape_fns.h" 17 #include "tensorflow/core/framework/op.h" 18 #include "tensorflow/core/framework/op_kernel.h" 19 #include "tensorflow/core/platform/macros.h" 20 21 namespace tensorflow { 22 namespace test { 23 24 // ErrorOp::Compute returns an error. 25 REGISTER_OP("Error") 26 .Input("in: T") 27 .Output("out: T") 28 .Attr("T: type") 29 .Attr("message: string") 30 .Attr("log_error: bool = false") 31 .SetShapeFn(shape_inference::UnknownShape); 32 class ErrorOp : public OpKernel { 33 public: ErrorOp(OpKernelConstruction * ctx)34 explicit ErrorOp(OpKernelConstruction* ctx) : OpKernel(ctx) { 35 OP_REQUIRES_OK(ctx, ctx->GetAttr("message", &errmsg_)); 36 OP_REQUIRES_OK(ctx, ctx->GetAttr("log_error", &log_error_)); 37 } 38 Compute(OpKernelContext * ctx)39 void Compute(OpKernelContext* ctx) override { 40 // Log only when CancellationManager is set to skip logging when Compute() 41 // is called during the optimization phase. 42 if (ctx->cancellation_manager() && log_error_) { 43 LOG(ERROR) << "ErrorOp: " << errmsg_; 44 } 45 ctx->SetStatus(errors::Internal(errmsg_)); 46 } 47 48 private: 49 string errmsg_; 50 bool log_error_ = false; 51 }; 52 REGISTER_KERNEL_BUILDER(Name("Error").Device(DEVICE_CPU), ErrorOp); 53 54 REGISTER_OP("InvalidRefType") 55 .Output("out: Ref(TIn)") 56 .Attr("TIn: type") 57 .Attr("TOut: type") 58 .SetShapeFn(shape_inference::UnknownShape); 59 class InvalidRefType : public OpKernel { 60 public: InvalidRefType(OpKernelConstruction * ctx)61 explicit InvalidRefType(OpKernelConstruction* ctx) : OpKernel(ctx) { 62 OP_REQUIRES_OK(ctx, ctx->GetAttr("TOut", &dtout_)); 63 output_ = Tensor(dtout_, TensorShape({})); 64 } 65 Compute(OpKernelContext * ctx)66 void Compute(OpKernelContext* ctx) override { 67 ctx->set_output_ref(0, &mu_, &output_); 68 } 69 70 private: 71 DataType dtout_; 72 mutex mu_; 73 Tensor output_; 74 }; 75 REGISTER_KERNEL_BUILDER(Name("InvalidRefType").Device(DEVICE_CPU), 76 InvalidRefType); 77 78 // DelayOp::AsyncCompute sleeps for "micros"-econd and then returns 79 // its input. 80 REGISTER_OP("Delay") 81 .Input("in: T") 82 .Output("out: T") 83 .Attr("T: type") 84 .Attr("micros: int") 85 .SetShapeFn(shape_inference::UnchangedShape); 86 class DelayOp : public AsyncOpKernel { 87 public: DelayOp(OpKernelConstruction * ctx)88 explicit DelayOp(OpKernelConstruction* ctx) : AsyncOpKernel(ctx) { 89 OP_REQUIRES_OK(ctx, ctx->GetAttr("micros", µs_)); 90 } 91 ComputeAsync(OpKernelContext * ctx,DoneCallback done)92 void ComputeAsync(OpKernelContext* ctx, DoneCallback done) override { 93 ctx->set_output(0, ctx->input(0)); 94 ctx->env()->SchedClosureAfter(micros_, done); 95 } 96 97 private: 98 int64_t micros_; 99 }; 100 REGISTER_KERNEL_BUILDER(Name("Delay").Device(DEVICE_CPU), DelayOp); 101 102 } // namespace test 103 } // namespace tensorflow 104