Home | History | Annotate | Download | only in framework
      1 /* Copyright 2016 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 #ifndef TENSORFLOW_CC_FRAMEWORK_SCOPE_INTERNAL_H_
     17 #define TENSORFLOW_CC_FRAMEWORK_SCOPE_INTERNAL_H_
     18 
     19 #include "tensorflow/cc/framework/scope.h"
     20 
     21 namespace tensorflow {
     22 
     23 class ShapeRefiner;
     24 
     25 // NewInternalScope returns a new scope which doesn't take ownership of
     26 // graph, status, name_map, and refiner.
     27 // This is intended to enable the C API (which are used by other language
     28 // bindings) to create a Scope and access C++ functionality (i.e. gradients).
     29 Scope NewInternalScope(Graph* graph, Status* status, ShapeRefiner* refiner);
     30 
     31 class Scope::Impl {
     32  public:
     33   // A NameMap is used to keep track of suffixes for names used in a scope. A
     34   // name that has not been used so far in a scope will get no suffix. Later
     35   // uses of the same name will get suffixes _1, _2, _3, etc. Multiple scopes
     36   // can share the same NameMap. For instance, a new scope created using
     37   // WithControlDependencies() should would share the same NameMap with the
     38   // parent.
     39   typedef std::unordered_map<string, int> NameMap;
     40 
     41   Impl(const std::shared_ptr<Graph>& graph,
     42        const std::shared_ptr<Status>& status,
     43        const std::shared_ptr<NameMap>& name_map,
     44        const std::shared_ptr<ShapeRefiner>& refiner);
     45 
     46   const string& name() const { return name_; }
     47   const std::vector<Operation>& control_deps() const { return control_deps_; }
     48 
     49  private:
     50   friend class Scope;
     51 
     52   // Tag types to choose the constructor to dispatch.
     53   struct Tags {
     54     enum class ScopeName;
     55     enum class OpName;
     56     enum class ControlDeps;
     57     enum class Device;
     58     enum class SingleUseScope;
     59     enum class ExitOnError;
     60     enum class KernelLabel;
     61     enum class Colocate;
     62   };
     63 
     64   Impl(Graph* graph, Status* status, NameMap* name_map, ShapeRefiner* refiner,
     65        bool disable_shape_inference);
     66   Impl(const Scope& other, Tags::ScopeName, const string& name,
     67        bool copy_names);
     68   Impl(const Scope& other, Tags::OpName, const string& name,
     69        const string& op_name);
     70   Impl(const Scope& other, Tags::ControlDeps,
     71        std::vector<Operation> control_deps, bool clear_control_deps);
     72   Impl(const Scope& other, Tags::Device, const string& device);
     73   Impl(const Scope& other, Tags::SingleUseScope, const string& op_name);
     74   Impl(const Scope& other, Tags::ExitOnError);
     75   Impl(const Scope& other, Tags::KernelLabel, const string& kernel_label);
     76   Impl(const Scope& other, Tags::Colocate, const Operation& colocate_with_op,
     77        bool clear_colocations);
     78 
     79   std::unordered_set<string> GetColocationConstraints(
     80       const Operation& colocate_with_op) const;
     81 
     82   // Helper functions to get a unique names.
     83   string GetUniqueName(const string& prefix, bool check_single_use) const;
     84   string GetNameForOp(const string& default_name) const;
     85 
     86   bool single_use_scope() const { return scope_used_ != nullptr; }
     87 
     88   // The graph, status, and name maps are shared by all child scopes
     89   // created from a single 'root' scope. A root scope is created by calling the
     90   // Scope::NewRootScope function, which creates a new graph, a new status and
     91   // the name maps.
     92   std::shared_ptr<Graph> graph_ = nullptr;
     93   std::shared_ptr<Status> status_ = nullptr;
     94   std::shared_ptr<NameMap> name_map_ = nullptr;
     95   std::shared_ptr<ShapeRefiner> refiner_ = nullptr;
     96 
     97   // If scope_used_ is not nullptr, op_name_ should be empty and
     98   // GetUniqueNameForOp can only be called once on this scope. More calls to
     99   // GetUniqueNameForOp will cause an error status to be set on this scope.
    100   std::shared_ptr<bool> scope_used_ = nullptr;
    101 
    102   const std::vector<Operation> control_deps_;
    103 
    104   // The fully-qualified name of this scope (i.e. includes any parent scope
    105   // names).
    106   const string name_ = "";
    107   const string op_name_ = "";
    108   const bool exit_on_error_ = false;
    109   const string kernel_label_ = "";
    110   const string device_ = "";
    111   const std::unordered_set<string> colocation_constraints_;
    112 
    113   // If true, Scope::DoShapeInference() always returns Status:OK().
    114   // TODO(skyewm): remove this when possible
    115   const bool disable_shape_inference_;
    116 };
    117 
    118 }  // namespace tensorflow
    119 
    120 #endif  // TENSORFLOW_CC_FRAMEWORK_SCOPE_INTERNAL_H_
    121