File: unique_name_manager.cpp

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (44 lines) | stat: -rw-r--r-- 1,376 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <torch/csrc/jit/tensorexpr/unique_name_manager.h>

#include <torch/csrc/jit/tensorexpr/ir.h>
#include <cctype>

namespace torch::jit::tensorexpr {

const std::string& UniqueNameManager::get_unique_name(const VarPtr& v) {
  // Find if we have already encountered this variable.
  auto iter = unique_name_mapping_.find(v);
  if (iter != unique_name_mapping_.end()) {
    return iter->second;
  }

  // First use the name_hint as a prefix to check if there is another name
  // with the same prefix.
  std::string name_hint = v->name_hint();
  if (name_hint.empty()) {
    name_hint = "v";
  } else if (std::isdigit(name_hint[0])) {
    name_hint = "v" + name_hint;
  }
  int& count = unique_name_count_[name_hint];
  while (true) {
    // Even if with a new count, this name might already be used. For example
    // ("x", 1) could collidewith ("x_1", 0)
    int count_v = count++;
    std::string unique_name = name_hint;
    if (count_v > 0) {
      unique_name += "_" + std::to_string(count_v);
    }
    if (all_unique_names_.count(unique_name) == 0) {
      all_unique_names_.insert(unique_name);
      auto result = unique_name_mapping_.emplace(v, unique_name);
      return result.first->second;
    }
  }
}

const std::string& UniqueNameManager::get_unique_name(const VarHandle& v) {
  return get_unique_name(v.node());
}

} // namespace torch::jit::tensorexpr