constant_folding.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include "ngraph/pass/pass.hpp"
20 
21 namespace ngraph
22 {
23  namespace pass
24  {
25  /**
26  * @brief Constant folding iterates over the function and tries to evaluate nodes
27  * with constant inputs. Such nodes are then replaced with new Constants containing
28  * the result of a folded operation.
29  */
30  class NGRAPH_API ConstantFolding : public FunctionPass
31  {
32  public:
33  NGRAPH_RTTI_DECLARATION;
34  bool run_on_function(std::shared_ptr<ngraph::Function> f) override;
35 
36  private:
37  void copy_runtime_info_to_target_inputs(const std::shared_ptr<Node>& node,
38  const Output<Node>& replacement);
39  /// \brief Folds pre-calculated output tensor values to constants in case lower and
40  /// upper estimations are equal. Traverses graph backwards starting from the results.
41  bool pre_calculated_values_folding(const std::shared_ptr<ngraph::Function>& f);
42  };
43  } // namespace pass
44 } // namespace ngraph
A handle for one of a node's outputs.
Definition: node_output.hpp:42
Constant folding iterates over the function and tries to evaluate nodes with constant inputs....
Definition: constant_folding.hpp:31
Definition: pass.hpp:106
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28