1 /* Copyright 2020 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 #include "tensorflow/c/experimental/gradients/array_grad.h" 16 17 #include "tensorflow/c/eager/abstract_context.h" 18 19 namespace tensorflow { 20 namespace gradients { 21 namespace { 22 class IdentityNGradientFunction : public GradientFunction { 23 public: Compute(AbstractContext * ctx,absl::Span<AbstractTensorHandle * const> grad_outputs,absl::Span<AbstractTensorHandle * > grad_inputs)24 Status Compute(AbstractContext* ctx, 25 absl::Span<AbstractTensorHandle* const> grad_outputs, 26 absl::Span<AbstractTensorHandle*> grad_inputs) override { 27 for (int i = 0; i < grad_outputs.size(); i++) { 28 auto grad_input = grad_outputs[i]; 29 // TODO(srbs): Should we add a copy contructor to AbstractTensorHandle 30 // that takes care of this similar to `Tensor`? 31 if (grad_input) { 32 grad_input->Ref(); 33 } 34 grad_inputs[i] = grad_input; 35 } 36 return Status::OK(); 37 } ~IdentityNGradientFunction()38 ~IdentityNGradientFunction() override {} 39 }; 40 } // namespace 41 IdentityNRegisterer(const ForwardOperation & op)42GradientFunction* IdentityNRegisterer(const ForwardOperation& op) { 43 return new IdentityNGradientFunction; 44 } 45 46 } // namespace gradients 47 } // namespace tensorflow 48