summaryrefslogtreecommitdiff
path: root/compiler/moco/pass/src/Passes/ResolveConstantShape.cpp
blob: 2a1323fbc924cd878ee3bdd210938a46d98acf6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "moco/Pass/Passes/ResolveConstantShape.h"

#include <moco/Support/TFShapeInferenceHelper.h>
#include <moco/Support/NodeAs.h>

#include <moco/IR/Nodes/TFShape.h>
#include <moco/IR/Nodes/TFConst.h>

#include <loco.h>

#include <oops/UserExn.h>

#include <cassert>

namespace
{

/**
 * WHEN:
 *   - TFShape's input shape is determined
 * DO:
 *   - Replace TFShape into TFConst
 *
 *
 * <Before>
 *     in ---- TFShape ---- out(s)
 *
 * <After>
 *     in ---- TFShape
 *
 *             TFConst ---- out(s)
 */
bool resolve_constant_shape(loco::Graph *graph, moco::TFShape *shape_node)
{
  auto input_shape = moco::node_shape(shape_node->input());

  // Check condition
  if (input_shape.domain() == loco::Domain::Unknown)
  {
    // Cannot resolve without known input_shape
    return false;
  }

  auto input_tensor_shape = input_shape.as<loco::TensorShape>();

  auto shape_rank = input_tensor_shape.rank();
  for (uint32_t axis = 0; axis < shape_rank; ++axis)
  {
    if (!input_tensor_shape.dim(axis).known())
    {
      // Cannot resolve with unknown dimension
      return false;
    }
  }

  // Make TFConst to replace TFShape
  auto const_node = graph->nodes()->create<moco::TFConst>();

  // set dtype
  auto dtype = shape_node->dtype();
  const_node->dtype(dtype);

  // set shape
  const_node->rank(1);
  const_node->dim(0) = shape_rank;

  // set data
  if (dtype == loco::DataType::S32)
  {
    // TODO Better to make template for this when support new dtype
    const_node->size<loco::DataType::S32>(shape_rank);
    for (uint32_t axis = 0; axis < shape_rank; ++axis)
    {
      int32_t dim = (int32_t)input_tensor_shape.dim(axis).value();
      if (!(dim > 0))
      {
        throw oops::UserExn("Invalid input shape", shape_node->name());
      }
      const_node->at<loco::DataType::S32>(axis) = dim;
    }
  }
  else
  {
    throw oops::UserExn("Unsupported data type", shape_node->name());
  }

  // replace
  loco::replace(shape_node).with(const_node);

  return true;
}

} // namespace

namespace moco
{

bool ResolveConstantShape::run(loco::Graph *graph)
{
  bool changed = false;
  for (auto node : loco::active_nodes(loco::output_nodes(graph)))
  {
    if (auto shape_node = as<moco::TFShape>(node))
    {
      if (resolve_constant_shape(graph, shape_node))
        changed = true;
    }
  }

  return changed;
}

} // namespace moco