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 <pybind11/stl.h>
16
17 #include "pybind11/pybind11.h"
18 #include "tensorflow/c/eager/gradients.h"
19 #include "tensorflow/c/experimental/gradients/math_grad.h"
20 #include "tensorflow/c/experimental/gradients/nn_grad.h"
21 #include "tensorflow/c/experimental/gradients/tape/tape_context.h"
22 #include "tensorflow/core/platform/status.h"
23 #include "tensorflow/python/lib/core/pybind11_status.h"
24
25 namespace py = pybind11;
26
27 namespace tensorflow {
28 namespace gradients {
29
RegisterGradients(GradientRegistry * registry)30 Status RegisterGradients(GradientRegistry* registry) {
31 // TODO(srbs): Rename ops::Add and AddRegisterer to AddV2.
32 TF_RETURN_IF_ERROR(registry->Register("AddV2", AddRegisterer));
33 TF_RETURN_IF_ERROR(registry->Register("Exp", ExpRegisterer));
34 TF_RETURN_IF_ERROR(registry->Register("MatMul", MatMulRegisterer));
35 TF_RETURN_IF_ERROR(registry->Register("Relu", ReluRegisterer));
36 TF_RETURN_IF_ERROR(
37 registry->Register("SparseSoftmaxCrossEntropyWithLogits",
38 SparseSoftmaxCrossEntropyWithLogitsRegisterer));
39 TF_RETURN_IF_ERROR(registry->Register("Neg", NegRegisterer));
40 TF_RETURN_IF_ERROR(registry->Register("Sub", SubRegisterer));
41 TF_RETURN_IF_ERROR(registry->Register("Mul", MulRegisterer));
42 TF_RETURN_IF_ERROR(registry->Register("Log1p", Log1pRegisterer));
43 TF_RETURN_IF_ERROR(registry->Register("DivNoNan", DivNoNanRegisterer));
44 return Status::OK();
45 }
46
PYBIND11_MODULE(_tape,m)47 PYBIND11_MODULE(_tape, m) {
48 py::class_<Tape>(m, "Tape")
49 .def(py::init([](bool persistent) { return new Tape(persistent); }))
50 .def("Watch", [](Tape* self, AbstractTensorHandle* t) { self->Watch(t); })
51 .def("ComputeGradient",
52 [](Tape* self, AbstractContext* ctx,
53 std::vector<AbstractTensorHandle*> target_tensors,
54 std::vector<AbstractTensorHandle*> source_tensors,
55 std::vector<AbstractTensorHandle*> output_gradients) {
56 std::vector<AbstractTensorHandle*> results(source_tensors.size());
57 Status s = self->ComputeGradient(ctx, target_tensors,
58 source_tensors, output_gradients,
59 absl::MakeSpan(results));
60 MaybeRaiseRegisteredFromStatus(s);
61 return results;
62 });
63 py::class_<GradientRegistry>(m, "GradientRegistry").def(py::init([]() {
64 auto registry = new GradientRegistry();
65 MaybeRaiseRegisteredFromStatus(RegisterGradients(registry));
66 return registry;
67 }));
68 py::class_<TapeContext, AbstractContext>(m, "TapeContext")
69 .def(py::init(
70 [](AbstractContext* ctx, Tape* tape, GradientRegistry* registry) {
71 return new TapeContext(ctx, tape, *registry);
72 }));
73 }
74 } // namespace gradients
75 } // namespace tensorflow
76