1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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/testutil.h"
19#include "tensorflow/cc/gradients/grad_testutil.h"
20#include "tensorflow/cc/ops/standard_ops.h"
21#include "tensorflow/core/framework/tensor_testutil.h"
22#include "tensorflow/core/lib/core/status_test_util.h"
23#include "tensorflow/core/lib/random/random.h"
24
25namespace tensorflow {
26namespace {
27
28using ops::Const;
29using ops::DynamicPartition;
30using ops::DynamicStitch;
31using ops::Placeholder;
32
33class DataFlowGradTest : public ::testing::Test {
34 protected:
35  DataFlowGradTest() : scope_(Scope::NewRootScope()) {}
36
37  void RunTest(const OutputList& xs, const std::vector<TensorShape>& x_shapes,
38               const OutputList& ys, const std::vector<TensorShape>& y_shapes) {
39    TF_ASSERT_OK(scope_.status());
40    float max_error;
41    TF_ASSERT_OK((ComputeGradientError<float, float, float>(
42        scope_, xs, x_shapes, ys, y_shapes, &max_error)));
43    EXPECT_LT(max_error, 1e-4);
44  }
45
46  Scope scope_;
47};
48
49TEST_F(DataFlowGradTest, DynamicPartitionGrad) {
50  TensorShape data_shape({2, 3, 2});
51  auto data = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(data_shape));
52  auto partitions = Const(scope_, {{2, 1, 0}, {1, 2, 0}});
53  auto y = DynamicPartition(scope_, data, partitions, 3);
54  TensorShape partition_shape({2, 2});
55  RunTest({data}, {data_shape}, y.outputs,
56          {partition_shape, partition_shape, partition_shape});
57}
58
59TEST_F(DataFlowGradTest, DynamicStitchGrad) {
60  TensorShape d1_shape({2});
61  TensorShape d2_shape({2, 2});
62  std::vector<Output> indices = {Const(scope_, 2), Const(scope_, {1, 0})};
63  std::vector<Output> data = {
64      Placeholder(scope_, DT_FLOAT, Placeholder::Shape(d1_shape)),
65      Placeholder(scope_, DT_FLOAT, Placeholder::Shape(d2_shape))};
66  auto y = DynamicStitch(scope_, indices, data);
67  TensorShape y_shape({3, 2});
68  RunTest(data, {d1_shape, d2_shape}, {y}, {y_shape});
69}
70
71}  // namespace
72}  // namespace tensorflow
73