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 #define EIGEN_USE_THREADS 17 18 #include "tensorflow/core/kernels/conditional_accumulator_base_op.h" 19 20 namespace tensorflow { 21 22 /** 23 * Defines a AccumulatorSetGlobalStepOp, the execution of which sets the 24 * global_step variable of the given ConditionalAccumulator. 25 */ 26 class AccumulatorSetGlobalStepOp 27 : public ConditionalAccumulatorBaseSyncOpKernel { 28 public: 29 explicit AccumulatorSetGlobalStepOp(OpKernelConstruction* context) 30 : ConditionalAccumulatorBaseSyncOpKernel(context) {} 31 32 protected: 33 void Compute(OpKernelContext* ctx, 34 ConditionalAccumulatorBase* accumulator) override { 35 // Check signature 36 OP_REQUIRES_OK(ctx, ctx->MatchSignature({DT_STRING_REF, DT_INT64}, {})); 37 38 // Get input new_global_step 39 const Tensor* new_global_step_tensor; 40 OP_REQUIRES_OK(ctx, ctx->input("new_global_step", &new_global_step_tensor)); 41 if (!TensorShapeUtils::IsScalar(new_global_step_tensor->shape())) { 42 ctx->CtxFailureWithWarning(errors::InvalidArgument( 43 "Argument num_required must be scalar, but had bad shape ", 44 new_global_step_tensor->shape().DebugString())); 45 } 46 47 Status status = 48 accumulator->SetGlobalStep(new_global_step_tensor->scalar<int64>()()); 49 if (!status.ok()) ctx->CtxFailureWithWarning(status); 50 } 51 52 private: 53 TF_DISALLOW_COPY_AND_ASSIGN(AccumulatorSetGlobalStepOp); 54 }; 55 56 REGISTER_KERNEL_BUILDER(Name("AccumulatorSetGlobalStep").Device(DEVICE_CPU), 57 AccumulatorSetGlobalStepOp); 58 59 /** 60 * Defines a AccumulatorNumAccumulatedOp, which returns the number of gradients 61 * that have been accumulated in the given ConditionalAccumulator, and emits it 62 * as an output tensor. 63 */ 64 class AccumulatorNumAccumulatedOp 65 : public ConditionalAccumulatorBaseSyncOpKernel { 66 public: 67 explicit AccumulatorNumAccumulatedOp(OpKernelConstruction* context) 68 : ConditionalAccumulatorBaseSyncOpKernel(context) {} 69 70 protected: 71 void Compute(OpKernelContext* ctx, 72 ConditionalAccumulatorBase* accumulator) override { 73 // Check signature 74 OP_REQUIRES_OK(ctx, ctx->MatchSignature({DT_STRING_REF}, {DT_INT32})); 75 Tensor* Taccumulator_size = nullptr; 76 OP_REQUIRES_OK( 77 ctx, ctx->allocate_output(0, TensorShape({}), &Taccumulator_size)); 78 Taccumulator_size->flat<int32>().setConstant( 79 accumulator->num_accumulated()); 80 } 81 82 private: 83 TF_DISALLOW_COPY_AND_ASSIGN(AccumulatorNumAccumulatedOp); 84 }; 85 86 REGISTER_KERNEL_BUILDER(Name("AccumulatorNumAccumulated").Device(DEVICE_CPU), 87 AccumulatorNumAccumulatedOp); 88 89 } // namespace tensorflow 90