xref: /aosp_15_r20/external/tensorflow/tensorflow/c/eager/gradient_checker.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_C_EAGER_GRADIENT_CHECKER_H_
16 #define TENSORFLOW_C_EAGER_GRADIENT_CHECKER_H_
17 
18 #include <memory>
19 
20 #include "absl/types/span.h"
21 #include "tensorflow/c/eager/abstract_tensor_handle.h"
22 #include "tensorflow/c/eager/unified_api_testutil.h"
23 
24 namespace tensorflow {
25 namespace gradients {
26 
27 /* Returns numerical grad inside `dtheta_approx` given `forward` model and
28  * parameter specified by `input_index`.
29  *
30  * I.e. if y = <output of the forward model> and w = inputs[input_index],
31  * this will calculate dy/dw numerically.
32  *
33  * `use_function` indicates whether to use graph mode(true) or eager(false).
34  *
35  * `numerical_grad` is the pointer to the AbstractTensorHandle* which will
36  * hold the numerical gradient data at the end of the function.
37  */
38 Status CalcNumericalGrad(AbstractContext* ctx, Model forward,
39                          absl::Span<AbstractTensorHandle* const> inputs,
40                          int input_index, bool use_function,
41                          AbstractTensorHandle** numerical_grad);
42 
43 }  // namespace gradients
44 }  // namespace tensorflow
45 
46 #endif  // TENSORFLOW_C_EAGER_GRADIENT_CHECKER_H_
47