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