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 | |
16 | #include <iostream> |
17 | #include <vector> |
18 | |
19 | #include "tensorflow/cc/framework/grad_op_registry.h" |
20 | #include "tensorflow/cc/framework/gradients.h" |
21 | #include "tensorflow/cc/ops/functional_ops.h" |
22 | |
23 | namespace tensorflow { |
24 | namespace ops { |
25 | namespace { |
26 | |
27 | Status PartitionedCallGrad(const Scope& scope, const Operation& op, |
28 | const std::vector<Output>& grad_inputs, |
29 | std::vector<Output>* grad_outputs) { |
30 | NameAttrList f; |
31 | TF_RETURN_IF_ERROR(GetNodeAttr(op.node()->attrs(), "f" , &f)); |
32 | for (const auto& attr : op.node()->attrs()) { |
33 | (*f.mutable_attr())[attr.first] = attr.second; |
34 | } |
35 | |
36 | std::vector<Output> func_inputs; |
37 | std::vector<DataType> input_dtypes; |
38 | const int num_inputs = op.num_inputs(); |
39 | func_inputs.reserve(num_inputs + grad_inputs.size()); |
40 | input_dtypes.reserve(num_inputs); |
41 | |
42 | for (int i = 0; i < num_inputs; i++) { |
43 | func_inputs.push_back(op.input(i)); |
44 | input_dtypes.push_back(op.input_type(i)); |
45 | } |
46 | |
47 | func_inputs.insert(std::end(func_inputs), std::begin(grad_inputs), |
48 | std::end(grad_inputs)); |
49 | |
50 | auto grad = SymbolicGradient(scope, func_inputs, input_dtypes, f); |
51 | for (int i = 0; i < num_inputs; i++) { |
52 | grad_outputs->push_back(grad[i]); |
53 | } |
54 | |
55 | return scope.status(); |
56 | } |
57 | |
58 | REGISTER_GRADIENT_OP("PartitionedCall" , PartitionedCallGrad); |
59 | REGISTER_GRADIENT_OP("StatefulPartitionedCall" , PartitionedCallGrad); |
60 | |
61 | } // anonymous namespace |
62 | } // namespace ops |
63 | } // namespace tensorflow |
64 | |