• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tensorflow/cc/framework/grad_op_registry.h"
17 #include "tensorflow/cc/framework/gradient_checker.h"
18 #include "tensorflow/cc/framework/ops.h"
19 #include "tensorflow/cc/framework/testutil.h"
20 #include "tensorflow/cc/gradients/grad_testutil.h"
21 #include "tensorflow/cc/ops/array_ops.h"
22 #include "tensorflow/cc/ops/functional_ops.h"
23 #include "tensorflow/cc/ops/standard_ops.h"
24 #include "tensorflow/core/framework/function_testlib.h"
25 #include "tensorflow/core/framework/tensor_testutil.h"
26 #include "tensorflow/core/lib/core/status_test_util.h"
27 
28 namespace tensorflow {
29 namespace ops {
30 namespace {
31 
32 class FunctionGradTest : public ::testing::Test {
33  protected:
FunctionGradTest()34   FunctionGradTest() : scope_(Scope::NewRootScope()) {}
35 
RunTest(const Output & x,const TensorShape & x_shape,const Output & y,const TensorShape & y_shape)36   void RunTest(const Output& x, const TensorShape& x_shape, const Output& y,
37                const TensorShape& y_shape) {
38     TF_ASSERT_OK(scope_.status());
39     float max_error;
40     auto result = (ComputeGradientError<float, float, float>(
41         scope_, {x}, {x_shape}, {y}, {y_shape}, &max_error));
42     TF_CHECK_OK(result);
43     TF_ASSERT_OK(result);
44     EXPECT_LT(max_error, 1e-3);
45   }
46 
RunTest(const OutputList & xs,const std::vector<TensorShape> & x_shapes,const OutputList & ys,const std::vector<TensorShape> & y_shapes)47   void RunTest(const OutputList& xs, const std::vector<TensorShape>& x_shapes,
48                const OutputList& ys, const std::vector<TensorShape>& y_shapes) {
49     TF_ASSERT_OK(scope_.status());
50     float max_error;
51     TF_ASSERT_OK((ComputeGradientError<float, float, float>(
52         scope_, xs, x_shapes, ys, y_shapes, &max_error)));
53     EXPECT_LT(max_error, 1e-3);
54   }
55 
56   Scope scope_;
57 };
58 
TEST_F(FunctionGradTest,PartitionedCallGrad)59 TEST_F(FunctionGradTest, PartitionedCallGrad) {
60   FunctionDefLibrary f_lib_proto;
61   *(f_lib_proto.add_function()) = test::function::XTimesTwo();
62 
63   // Construct a graph:
64   //   A = Placeholder[dtype=int32]
65   //   B = XTimesTwo[_tpu_replicate="cluster"](A)
66   //   C = XTimesTwo[_xla_compile_id="cluster"](A)
67   TF_ASSERT_OK(scope_.graph()->AddFunctionLibrary(f_lib_proto));
68 
69   Output x = Placeholder(scope_, DT_FLOAT);
70   NameAttrList f;
71   f.set_name("XTimesTwo");
72   (*f.mutable_attr())["T"].set_type(DT_FLOAT);
73   auto results =
74       PartitionedCall(scope_, std::initializer_list<Input>{x}, {DT_FLOAT}, f);
75   RunTest(x, {}, results[0], {});
76 
77   auto stateful_results = StatefulPartitionedCall(
78       scope_, std::initializer_list<Input>{x}, {DT_FLOAT}, f);
79   RunTest(x, {}, stateful_results[0], {});
80 }
81 
82 }  // namespace
83 }  // namespace ops
84 }  // namespace tensorflow
85