torch/csrc/autograd/engine.cpp
/* Computes the number of dependencies for each function which requires grad */
Computes the number of dependencies for each function which requires grad
Computes the number of dependencies for each function which requires grad
auto Engine::compute_dependencies(Function* root, GraphTask& task) -> void {
  std::unordered_set<Function*> seen;
  std::vector<Function*> queue { root };
  auto& dependencies = task.dependencies;
  while (queue.size() > 0) {
    auto fn = queue.back(); queue.pop_back();
    for (const auto& edge : fn->next_edges()) {
      if (auto next_ptr = edge.function.get()) {
        dependencies[next_ptr] += 1;
        const bool was_inserted = seen.insert(next_ptr).second;
        if (was_inserted) queue.push_back(next_ptr);
      }
    }
  }
}
auto Engine :: compute_dependencies ( Function * root , GraphTask & task ) -> void { std :: unordered_set < Function * > seen ; std :: vector < Function * > queue { root } ; auto & dependencies = task . dependencies ; while ( queue . size ( ) > 0 ) { auto fn = queue . back ( ) ; queue . pop_back ( ) ; for ( const auto & edge : fn -> next_edges ( ) ) { if ( auto next_ptr = edge . function . get ( ) ) { dependencies [ next_ptr ] += 1 ; const bool was_inserted = seen . insert ( next_ptr ) . second ; if ( was_inserted ) queue . push_back ( next_ptr ) ; } } } }
Computes the number of dependencies for each function which requires grad
Computes the number of dependencies for each function which requires grad
// Just to make sure that they will never be added to the queue again // Queue contains all nodes that will start propagating gradients. // We no longer have to expand functions that don't require grad.