Caffe2 - C++ API
A deep learning, cross platform ML framework
scope.h
1 #pragma once
2 #include <ATen/core/interned_strings.h>
3 #include <c10/util/intrusive_ptr.h>
4 #include <torch/csrc/WindowsTorchApiMacro.h>
5 
6 namespace torch {
7 namespace jit {
8 
9 // Scope is a node of a trie that represents the tree of nested scopes.
10 // Individual scopes are pushed and popped from Graph, which holds a
11 // pointer to the current scope. Each Node in Graph holds a pointer
12 // to the scope that was current when the node was created.
13 // The trie never needs to shrink, it only grows until it is disposed
14 // of when Graph is deallocated. Hence, pointers to scopes held by nodes
15 // will always be valid as long as Graph is alive.
16 struct Scope;
17 using ScopePtr = c10::intrusive_ptr<Scope>;
18 using c10::Symbol;
19 
20 struct TORCH_API Scope : public c10::intrusive_ptr_target {
21  private:
22  ScopePtr parent_;
23  Symbol name_;
24  ScopePtr intrusive_from_this() {
25  c10::raw::intrusive_ptr::incref(this); // we are creating a new pointer
26  // from a raw `this` pointer
27  // so we need to bump the refcount
28  // to account for this ownership
30  }
31 
32  public:
33  Scope() {
34  name_ = Symbol::scope("");
35  }
36  Scope(ScopePtr parent, Symbol name) {
37  name_ = name;
38  parent_ = std::move(parent);
39  }
40  ScopePtr push(Symbol name);
41 
42  ScopePtr parent() {
43  if (!parent_) {
44  throw std::runtime_error("Cannot get parent from Scope with no parent");
45  }
46  return parent_;
47  }
48  bool isRoot() const {
49  return !parent_;
50  }
51  bool isBlank() const {
52  static const Symbol blank = Symbol::scope("");
53  return isRoot() && name() == blank;
54  }
55 
56  ScopePtr getRoot();
57 
58  size_t getDepth();
59 
60  Symbol name() const {
61  return name_;
62  }
63 
64  std::string namesFromRoot(const std::string& separator = "/") const;
65 };
66 
67 } // namespace jit
68 } // namespace torch
Definition: jit_type.h:17
intrusive_ptr<T> is an alternative to shared_ptr<T> that has better performance because it does the r...
Definition: intrusive_ptr.h:35
static intrusive_ptr reclaim(TTarget *owning_ptr)
Takes an owning pointer to TTarget* and creates an intrusive_ptr that takes over ownership.