File: Module.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 (90 lines) | stat: -rw-r--r-- 2,294 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <c10/util/error.h>
#include <torch/csrc/instruction_counter/Module.h>
#include <torch/csrc/utils/pybind.h>
#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>

#if defined(__linux__)
#include <linux/perf_event.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#endif

namespace torch::instruction_counter {

long start() {
#if !defined(__linux__)
  throw std::runtime_error("This systems seems not to be Linux");
#else

  // Construct base perf_event_attr struct
  perf_event_attr attr{};
  memset(&attr, 0, sizeof(attr));
  attr.size = sizeof(attr);
  attr.exclude_kernel = 1;
  attr.disabled = 1;
  attr.exclude_hv = 1;
  attr.sample_period = 0;
  // Enable hardware counting
  attr.type = PERF_TYPE_HARDWARE;
  attr.config = PERF_COUNT_HW_INSTRUCTIONS;

  long fd = syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
  if (fd == -1) {
    fprintf(
        stderr,
        "Failed to open instruction count event: %s.\n",
        c10::utils::str_error(errno).c_str());
    return -1;
  }
  ioctl((int)fd, PERF_EVENT_IOC_RESET, 0); // Reset the counter
  ioctl((int)fd, PERF_EVENT_IOC_ENABLE, 0); // Enable the counter
  return fd;
#endif
}

uint64_t end(int fd) {
#if !defined(__linux__)
  throw std::runtime_error("This systems seems not to be Linux");
#else
  // Disable the event group
  if (ioctl(fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP) == -1) {
    fprintf(
        stderr,
        "Error disabling perf event (fd: %d): %s\n",
        fd,
        c10::utils::str_error(errno).c_str());
    return -1;
  }

  uint64_t total_instructions = 0;

  // Read results
  long ret_val = read(fd, &total_instructions, sizeof(total_instructions));
  if (ret_val == -1) {
    fprintf(
        stderr,
        "Error reading perf event results: %s\n",
        c10::utils::str_error(errno).c_str());
    return -1;
  }

  close(fd);
  return total_instructions;
#endif
}

void initModule(PyObject* module) {
  auto m = py::handle(module).cast<py::module>();
  auto instruction_counter = m.def_submodule(
      "_instruction_counter", "instruction_counter related pybind.");
  instruction_counter.def("start", start);
  instruction_counter.def("end", end);
}

} // namespace torch::instruction_counter