Skip to content

Commit 0cbb079

Browse files
committed
Expose profiler to python
1 parent 39f48f2 commit 0cbb079

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

python/cpp/module.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ PYBIND11_MODULE(_ext, m)
7676
m.def("set_random_seed", &ctranslate2::set_random_seed, py::arg("seed"),
7777
"Sets the seed of random generators.");
7878

79+
ctranslate2::python::register_profiling(m);
7980
ctranslate2::python::register_logging(m);
8081
ctranslate2::python::register_storage_view(m);
8182
ctranslate2::python::register_translation_stats(m);

python/cpp/module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#pragma once
22

33
#include <pybind11/pybind11.h>
4+
#include <pybind11/chrono.h>
45

56
namespace py = pybind11;
67

78
namespace ctranslate2 {
89
namespace python {
910

11+
void register_profiling(py::module& m);
1012
void register_encoder(py::module& m);
1113
void register_generation_result(py::module& m);
1214
void register_generator(py::module& m);

python/cpp/profiling.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "module.h"
2+
#include <sstream>
3+
#include <ctranslate2/profiler.h>
4+
5+
namespace ctranslate2 {
6+
namespace python {
7+
8+
void register_profiling(py::module& m) {
9+
10+
m.def("init_profiling", &ctranslate2::init_profiling);
11+
m.def("dump_profiling", []() {
12+
std::ostringstream oss;
13+
ctranslate2::dump_profiling(oss);
14+
return oss.str();
15+
});
16+
}
17+
18+
}
19+
}

python/ctranslate2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
)
4343
from ctranslate2.extensions import register_extensions
4444
from ctranslate2.logging import get_log_level, set_log_level
45+
from ctranslate2.profiling import init_profiler, dump_profiler
4546

4647
register_extensions()
4748
del register_extensions

python/ctranslate2/profiling.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from ctranslate2 import _ext, Device
2+
import sys
3+
4+
def init_profiler(device = Device.cpu, num_threads = 1):
5+
_ext.init_profiling(device, num_threads)
6+
7+
8+
def dump_profiler():
9+
profiling_data = _ext.dump_profiling()
10+
sys.stdout.write(profiling_data)

0 commit comments

Comments
 (0)