1223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
3223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowerLicensed under the Apache License, Version 2.0 (the "License");
4223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFloweryou may not use this file except in compliance with the License.
5223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowerYou may obtain a copy of the License at
6223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
7223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower    http://www.apache.org/licenses/LICENSE-2.0
8223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
9223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowerUnless required by applicable law or agreed to in writing, software
10223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowerdistributed under the License is distributed on an "AS IS" BASIS,
11223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowerWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowerSee the License for the specific language governing permissions and
13223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowerlimitations under the License.
14223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower==============================================================================*/
15223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
16223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/cc/ops/const_op.h"
17223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/cc/ops/image_ops.h"
18223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/cc/ops/nn_ops.h"
19223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/cc/ops/standard_ops.h"
20223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/core/common_runtime/function.h"
21223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/core/framework/tensor_shape.h"
22223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/core/framework/tensor_testutil.h"
23223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/core/graph/default_device.h"
24223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/core/graph/node_builder.h"
25223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/core/graph/testlib.h"
26223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/core/lib/core/status_test_util.h"
27223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/core/platform/test.h"
28223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/core/public/session.h"
29223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower#include "tensorflow/tools/graph_transforms/transform_utils.h"
30223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
31223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowernamespace tensorflow {
32223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowernamespace graph_transforms {
33223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
34223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower// Declared here so we don't have to put it in a public header.
35223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowerStatus RewriteQuantizedStrippedModelForHexagon(
36223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower    const GraphDef& input_graph_def, const TransformFuncContext& context,
37223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower    GraphDef* output_graph_def);
38223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
39223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowernamespace {
40223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
41223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlowerTEST(HexagonRewriteTransformTest, BasicRun) {
42223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  Scope root = tensorflow::Scope::NewRootScope();
43223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
44223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  // Create a simple graph that calculates (a + b) * placeholder.
45223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  Tensor a_data(DT_FLOAT, TensorShape({1, 1, 1, 1}));
46223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  test::FillIota<float>(&a_data, 1.0f);
47223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  Output a_const = ops::Const(root.WithOpName("a"), Input::Initializer(a_data));
48223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
49223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  Tensor b_data(DT_FLOAT, TensorShape({1, 1, 1, 1}));
50223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  test::FillIota<float>(&b_data, 1.0f);
51223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  Output b_const = ops::Const(root.WithOpName("b"), Input::Initializer(b_data));
52223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
53223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  Output add = ops::Add(root.WithOpName("add"), a_const, b_const);
54223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
55223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  Output placeholder =
56223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower      ops::Placeholder(root.WithOpName("placeholder"), DT_FLOAT);
57223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
58223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  Output mul = ops::Mul(root.WithOpName("output"), add, placeholder);
59223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
60223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  GraphDef graph_def;
61223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  TF_ASSERT_OK(root.ToGraphDef(&graph_def));
62223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
63223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  GraphDef result;
64223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  TransformFuncContext context;
65223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  context.input_names = {"placeholder"};
66223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  context.output_names = {"output"};
67223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  context.params.insert(std::pair<string, std::vector<string>>(
68223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower      {"input_shape0", {string("1,1,1,1")}}));
69223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  context.params.insert(std::pair<string, std::vector<string>>(
70223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower      {"input_type0", {string("float")}}));
71223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  TF_ASSERT_OK(
72223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower      RewriteQuantizedStrippedModelForHexagon(graph_def, context, &result));
73223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
74223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  // Node in the input graph is fused to
75223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  // 1 input placeholder node + 1 fused output node
76223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower  EXPECT_EQ(2, result.node_size());
77223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower}
78223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower
79223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower}  // namespace
80223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower}  // namespace graph_transforms
81223ae5414377c9500f6506de32674fe7285b0af3A. Unique TensorFlower}  // namespace tensorflow
82