Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 739faea

Browse files
Merge pull request #317 from nvkevihu/cpp-manual-profiler-export
[C++ Benchmark]: Replace ExportToTensorBoard
2 parents 0e85879 + d574fa5 commit 739faea

File tree

6 files changed

+46
-96
lines changed

6 files changed

+46
-96
lines changed

tftrt/benchmarking-cpp/BUILD

Lines changed: 0 additions & 33 deletions
This file was deleted.

tftrt/benchmarking-cpp/README.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,6 @@ python3 convert_model.py --model-dir /path/to/model/dir --output-dir /path/to/de
2626

2727
## Building
2828

29-
The binary relies on a modified Tensorflow, which will need to be rebuilt. Internal users can use a container with Tensorflow already modified and built, instead of building with Bazel, which will take much longer.
30-
31-
### Bazel
32-
33-
The `setup.sh` script applies the Tensorflow patch and prepares the container for the Bazel build.
34-
35-
```
36-
/workspace/tensorrt/tftrt/benchmarking-cpp/build-scripts/setup.sh
37-
cd /opt/tensorflow
38-
./tftrt-build.sh
39-
```
40-
41-
The binary will be located at `/opt/tensorflow/tensorflow-source/bazel-bin/tensorflow/examples/benchmarking-cpp/tftrt_benchmark_runner`.
42-
43-
### Prebuilt
44-
45-
For internal NVIDIA users, a container with a prebuilt modified Tensorflow is available. In the container, use CMake to build the binary without needing to rebuild Tensorflow:
46-
4729
```
4830
cd /workspace/tensorrt/tftrt/benchmarking-cpp
4931
mkdir build && cd build
@@ -61,4 +43,6 @@ The binary will be located at `/workspace/tensorrt/tftrt/benchmarking-cpp/tftrt_
6143

6244
### Profiling
6345

64-
To profile, set the `--out_dir` flag. Run `tensorboard --logdir [out_dir]` to view results.
46+
To profile, set the `--out_dir` flag. This creates a log directory and serializes the `XSpace` to a location that TensorBoard expects (i.e. `[out_dir]/plugins/profile/[run_id]/[host_id].xplane.pb`).
47+
48+
Run `tensorboard --logdir [out_dir]` to view results. TensorBoard will generate the available dashboards using the `XSpace` directly.

tftrt/benchmarking-cpp/build-scripts/setup.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

tftrt/benchmarking-cpp/build-scripts/tf-profiler.patch

Lines changed: 0 additions & 22 deletions
This file was deleted.

tftrt/benchmarking-cpp/build-scripts/tftrt-build.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

tftrt/benchmarking-cpp/main.cc

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include <chrono>
2+
#include <ctime>
3+
#include <fstream>
4+
#include <iomanip>
25
#include <iostream>
36
#include <numeric>
7+
#include <sstream>
48
#include <vector>
59

610
#include "tensorflow/cc/ops/array_ops.h"
@@ -14,9 +18,11 @@
1418
#include "tensorflow/core/platform/init_main.h"
1519
#include "tensorflow/core/platform/logging.h"
1620
#include "tensorflow/core/platform/types.h"
21+
#include "tensorflow/core/platform/env.h"
22+
#include "tensorflow/core/platform/path.h"
23+
#include "tensorflow/core/platform/host_info.h"
1724
#include "tensorflow/core/profiler/lib/profiler_session.h"
1825
#include "tensorflow/core/profiler/lib/traceme.h"
19-
#include "tensorflow/core/profiler/rpc/client/capture_profile.h"
2026
#include "tensorflow/core/public/session.h"
2127
#include "tensorflow/core/util/command_line_flags.h"
2228

@@ -35,6 +41,10 @@ using tensorflow::Tensor;
3541
} \
3642
} while (0)
3743

44+
// Directory required by Tensorboard to load the .xplane.pb properly
45+
const string kProfilePath = "plugins/profile";
46+
const string kProfileSuffix = ".xplane.pb";
47+
3848
// Get the GPU and its default context.
3949
Status GetDevice(std::unique_ptr<tensorflow::Session>& session,
4050
tensorflow::Device** device,
@@ -144,6 +154,26 @@ Status SetupCallable(std::unique_ptr<tensorflow::Session>& session,
144154
return session->MakeCallable(opts, handle);
145155
}
146156

157+
// Get the current time as a string.
158+
string GetTime() {
159+
std::stringstream ss;
160+
std::time_t now = std::time(nullptr);
161+
ss << std::put_time(std::localtime(&now), "%EY_%m_%d_%H_%M_%S");
162+
return ss.str();
163+
}
164+
165+
// Create the directory structure expected by TensorBoard.
166+
Status GetAndCreateProfilePath(const string& out_dir,
167+
string* profile_path) {
168+
// Use date for run ID
169+
string run_id = GetTime();
170+
string host_id = tensorflow::port::Hostname();
171+
string run_dir = tensorflow::io::JoinPath(out_dir, kProfilePath, run_id);
172+
*profile_path = tensorflow::io::JoinPath(run_dir, host_id + kProfileSuffix);
173+
TF_RETURN_IF_ERROR(tensorflow::Env::Default()->RecursivelyCreateDir(run_dir));
174+
return Status::OK();
175+
}
176+
147177
// Start the profiling session.
148178
Status StartProfiling(std::unique_ptr<tensorflow::ProfilerSession>& profiler) {
149179
profiler = tensorflow::ProfilerSession::Create(
@@ -157,7 +187,18 @@ Status StopProfiling(std::unique_ptr<tensorflow::ProfilerSession>& profiler,
157187
const string& out_dir) {
158188
tensorflow::profiler::XSpace xspace;
159189
TF_RETURN_IF_ERROR(profiler->CollectData(&xspace));
160-
tensorflow::profiler::ExportToTensorBoard(xspace, out_dir);
190+
191+
// Get export path
192+
string profile_path;
193+
TF_RETURN_IF_ERROR(GetAndCreateProfilePath(out_dir, &profile_path));
194+
195+
// Serialize and write to disk
196+
string xspace_serialized;
197+
xspace.SerializeToString(&xspace_serialized);
198+
std::ofstream ofs(profile_path);
199+
ofs << xspace_serialized;
200+
ofs.close();
201+
161202
profiler.reset();
162203
return Status::OK();
163204
}

0 commit comments

Comments
 (0)