Skip to content

Commit 43ac9de

Browse files
gcarranza-1copybara-github
authored andcommitted
Add time logging to async segmentation demos
LiteRT-PiperOrigin-RevId: 828961301
1 parent c907ad1 commit 43ac9de

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

litert/samples/async_segmentation/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ cc_binary(
7171
deps = [
7272
":image_processor",
7373
":image_utils",
74+
":timing_utils",
7475
"//litert/cc:litert_api_with_dynamic_runtime",
7576
"@com_google_absl//absl/strings:string_view",
7677
"@com_google_absl//absl/time",
@@ -98,6 +99,7 @@ cc_binary(
9899
deps = [
99100
":image_processor",
100101
":image_utils",
102+
":timing_utils",
101103
"//litert/cc:litert_api_with_dynamic_runtime",
102104
"@com_google_absl//absl/strings:string_view",
103105
"@com_google_absl//absl/time",
@@ -131,6 +133,7 @@ cc_binary(
131133
deps = [
132134
":image_processor",
133135
":image_utils",
136+
":timing_utils",
134137
"//litert/cc:litert_api_with_dynamic_runtime",
135138
"@com_google_absl//absl/strings:string_view",
136139
"@com_google_absl//absl/time",
@@ -157,3 +160,12 @@ cc_library(
157160
"@com_google_absl//absl/log:absl_check",
158161
] + gles_deps(),
159162
)
163+
164+
cc_library(
165+
name = "timing_utils",
166+
srcs = ["timing_utils.cc"],
167+
hdrs = ["timing_utils.h"],
168+
deps = [
169+
"@com_google_absl//absl/time",
170+
],
171+
)

litert/samples/async_segmentation/main_cpu.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <vector>
2222

2323
#include <GLES2/gl2.h>
24+
#include "absl/time/clock.h" // from @com_google_absl
2425
#include "absl/types/span.h" // from @com_google_absl
2526
#include "litert/c/litert_common.h"
2627
#include "litert/cc/litert_compiled_model.h"
@@ -29,6 +30,7 @@
2930
#include "litert/cc/litert_model.h"
3031
#include "litert/samples/async_segmentation/image_processor.h"
3132
#include "litert/samples/async_segmentation/image_utils.h"
33+
#include "litert/samples/async_segmentation/timing_utils.h"
3234

3335
int main(int argc, char* argv[]) {
3436
if (argc != 4) {
@@ -74,6 +76,8 @@ int main(int argc, char* argv[]) {
7476

7577
// ================= PRE-PROCESSING =================
7678
// Load and preprocess the image
79+
ProfilingTimestamps profiling_timestamps;
80+
profiling_timestamps.load_image_start_time = absl::Now();
7781
int width_orig = 0, height_orig = 0, channels_file = 0, loaded_channels = 3;
7882
GLuint tex_id_orig = 0;
7983
auto img_data_cpu = ImageUtils::LoadImage(input_file, width_orig, height_orig,
@@ -82,6 +86,9 @@ int main(int argc, char* argv[]) {
8286
std::cerr << "Failed to load image file: " << input_file << std::endl;
8387
return 1;
8488
}
89+
profiling_timestamps.load_image_end_time =
90+
profiling_timestamps.e2e_start_time =
91+
profiling_timestamps.pre_process_start_time = absl::Now();
8592
tex_id_orig = processor.CreateOpenGLTexture(img_data_cpu, width_orig,
8693
height_orig, loaded_channels);
8794
if (!tex_id_orig) {
@@ -118,9 +125,13 @@ int main(int argc, char* argv[]) {
118125
LITERT_ABORT_IF_ERROR(
119126
input_buffers[0].Write(absl::MakeConstSpan(preprocessed_buffer_data)));
120127

128+
profiling_timestamps.pre_process_end_time =
129+
profiling_timestamps.inference_start_time = absl::Now();
121130
// ================= INFERENCE =================
122131
// Run inference
123132
LITERT_ABORT_IF_ERROR(compiled_model.Run(input_buffers, output_buffers));
133+
profiling_timestamps.inference_end_time =
134+
profiling_timestamps.post_process_start_time = absl::Now();
124135

125136
// ================= POST-PROCESSING =================
126137
// Post-process the results
@@ -162,6 +173,9 @@ int main(int argc, char* argv[]) {
162173
return 1;
163174
}
164175

176+
profiling_timestamps.post_process_end_time =
177+
profiling_timestamps.e2e_end_time =
178+
profiling_timestamps.save_image_start_time = absl::Now();
165179
// Save the output image
166180
std::vector<unsigned char> final_blended_uchar_data(out_blend_width *
167181
out_blend_height * 4);
@@ -177,6 +191,9 @@ int main(int argc, char* argv[]) {
177191
std::cout << "Successfully saved final blended image to " << output_file
178192
<< std::endl;
179193

194+
profiling_timestamps.save_image_end_time = absl::Now();
195+
PrintTiming(profiling_timestamps);
196+
180197
processor.DeleteOpenGLTexture(tex_id_orig);
181198
processor.DeleteOpenGLBuffer(preprocessed_buffer_id);
182199
for (GLuint id : mask_buffer_ids) {

litert/samples/async_segmentation/main_gpu.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <vector>
2424

2525
#include <GLES2/gl2.h>
26+
#include "absl/time/clock.h" // from @com_google_absl
2627
#include "absl/types/span.h" // from @com_google_absl
2728
#include "litert/c/litert_common.h"
2829
#include "litert/c/litert_tensor_buffer_types.h"
@@ -39,6 +40,7 @@
3940
#include "litert/cc/options/litert_gpu_options.h"
4041
#include "litert/samples/async_segmentation/image_processor.h"
4142
#include "litert/samples/async_segmentation/image_utils.h"
43+
#include "litert/samples/async_segmentation/timing_utils.h"
4244

4345
namespace {
4446

@@ -182,6 +184,8 @@ int main(int argc, char* argv[]) {
182184

183185
// ================= PRE-PROCESSING =================
184186
// Load and preprocess the image
187+
ProfilingTimestamps profiling_timestamps;
188+
profiling_timestamps.load_image_start_time = absl::Now();
185189
int width_orig = 0, height_orig = 0, channels_file = 0, loaded_channels = 3;
186190
GLuint tex_id_orig = 0;
187191
auto img_data_cpu = ImageUtils::LoadImage(input_file, width_orig, height_orig,
@@ -190,6 +194,9 @@ int main(int argc, char* argv[]) {
190194
std::cerr << "Failed to load image file: " << input_file << std::endl;
191195
return 1;
192196
}
197+
profiling_timestamps.load_image_end_time =
198+
profiling_timestamps.e2e_start_time =
199+
profiling_timestamps.pre_process_start_time = absl::Now();
193200
tex_id_orig = processor.CreateOpenGLTexture(img_data_cpu, width_orig,
194201
height_orig, loaded_channels);
195202
if (!tex_id_orig) {
@@ -232,12 +239,16 @@ int main(int argc, char* argv[]) {
232239
LITERT_ABORT_IF_ERROR(
233240
input_buffers[0].Write(absl::MakeConstSpan(preprocessed_buffer_data)));
234241
}
242+
profiling_timestamps.pre_process_end_time =
243+
profiling_timestamps.inference_start_time = absl::Now();
235244
// ================= INFERENCE =================
236245
// Run inference
237246

238247
bool async = false;
239248
LITERT_ABORT_IF_ERROR(
240249
compiled_model.RunAsync(0, input_buffers, output_buffers, async));
250+
profiling_timestamps.inference_end_time =
251+
profiling_timestamps.post_process_start_time = absl::Now();
241252

242253
// ================= POST-PROCESSING =================
243254
// Post-process the results
@@ -291,6 +302,9 @@ int main(int argc, char* argv[]) {
291302
return 1;
292303
}
293304

305+
profiling_timestamps.post_process_end_time =
306+
profiling_timestamps.e2e_end_time =
307+
profiling_timestamps.save_image_start_time = absl::Now();
294308
// Save the output image
295309
std::vector<unsigned char> final_blended_uchar_data(out_blend_width *
296310
out_blend_height * 4);
@@ -306,6 +320,9 @@ int main(int argc, char* argv[]) {
306320
std::cout << "Successfully saved final blended image to " << output_file
307321
<< std::endl;
308322

323+
profiling_timestamps.save_image_end_time = absl::Now();
324+
PrintTiming(profiling_timestamps);
325+
309326
processor.DeleteOpenGLTexture(tex_id_orig);
310327
if (!use_gl_buffers) {
311328
processor.DeleteOpenGLBuffer(preprocessed_buffer_id);

litert/samples/async_segmentation/main_npu.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <GLES2/gl2.h>
2525
#include "absl/strings/string_view.h" // from @com_google_absl
26+
#include "absl/time/clock.h" // from @com_google_absl
2627
#include "absl/types/span.h" // from @com_google_absl
2728
#include "litert/c/litert_common.h"
2829
#include "litert/cc/litert_compiled_model.h"
@@ -32,6 +33,7 @@
3233
#include "litert/cc/litert_options.h"
3334
#include "litert/samples/async_segmentation/image_processor.h"
3435
#include "litert/samples/async_segmentation/image_utils.h"
36+
#include "litert/samples/async_segmentation/timing_utils.h"
3537

3638
int main(int argc, char* argv[]) {
3739
if (argc != 4) {
@@ -85,6 +87,8 @@ int main(int argc, char* argv[]) {
8587

8688
// ================= PRE-PROCESSING =================
8789
// Load and preprocess the image
90+
ProfilingTimestamps profiling_timestamps;
91+
profiling_timestamps.load_image_start_time = absl::Now();
8892
int width_orig = 0, height_orig = 0, channels_file = 0, loaded_channels = 3;
8993
GLuint tex_id_orig = 0;
9094
auto img_data_cpu = ImageUtils::LoadImage(input_file, width_orig, height_orig,
@@ -93,6 +97,9 @@ int main(int argc, char* argv[]) {
9397
std::cerr << "Failed to load image file: " << input_file << std::endl;
9498
return 1;
9599
}
100+
profiling_timestamps.load_image_end_time =
101+
profiling_timestamps.e2e_start_time =
102+
profiling_timestamps.pre_process_start_time = absl::Now();
96103
tex_id_orig = processor.CreateOpenGLTexture(img_data_cpu, width_orig,
97104
height_orig, loaded_channels);
98105
if (!tex_id_orig) {
@@ -129,9 +136,13 @@ int main(int argc, char* argv[]) {
129136
LITERT_ABORT_IF_ERROR(
130137
input_buffers[0].Write(absl::MakeConstSpan(preprocessed_buffer_data)));
131138

139+
profiling_timestamps.pre_process_end_time =
140+
profiling_timestamps.inference_start_time = absl::Now();
132141
// ================= INFERENCE =================
133142
// Run inference
134143
LITERT_ABORT_IF_ERROR(compiled_model.Run(input_buffers, output_buffers));
144+
profiling_timestamps.inference_end_time =
145+
profiling_timestamps.post_process_start_time = absl::Now();
135146

136147
// ================= POST-PROCESSING =================
137148
// Post-process the results
@@ -173,6 +184,9 @@ int main(int argc, char* argv[]) {
173184
return 1;
174185
}
175186

187+
profiling_timestamps.post_process_end_time =
188+
profiling_timestamps.e2e_end_time =
189+
profiling_timestamps.save_image_start_time = absl::Now();
176190
// Save the output image
177191
std::vector<unsigned char> final_blended_uchar_data(out_blend_width *
178192
out_blend_height * 4);
@@ -188,6 +202,9 @@ int main(int argc, char* argv[]) {
188202
std::cout << "Successfully saved final blended image to " << output_file
189203
<< std::endl;
190204

205+
profiling_timestamps.save_image_end_time = absl::Now();
206+
PrintTiming(profiling_timestamps);
207+
191208
processor.DeleteOpenGLTexture(tex_id_orig);
192209
processor.DeleteOpenGLBuffer(preprocessed_buffer_id);
193210
for (GLuint id : mask_buffer_ids) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2025 The Google AI Edge Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "litert/samples/async_segmentation/timing_utils.h"
18+
19+
#include <iostream>
20+
21+
void PrintTiming(const ProfilingTimestamps& profiling_timestamps) {
22+
std::cout << "LoadImage took "
23+
<< profiling_timestamps.load_image_end_time -
24+
profiling_timestamps.load_image_start_time
25+
<< std::endl;
26+
std::cout << "PreprocessInputForSegmentation took "
27+
<< profiling_timestamps.pre_process_end_time -
28+
profiling_timestamps.pre_process_start_time
29+
<< std::endl;
30+
std::cout << "Inference took "
31+
<< profiling_timestamps.inference_end_time -
32+
profiling_timestamps.inference_start_time
33+
<< std::endl;
34+
std::cout << "PostprocessSegmentation took "
35+
<< profiling_timestamps.post_process_end_time -
36+
profiling_timestamps.post_process_start_time
37+
<< std::endl;
38+
std::cout << "SaveImage took "
39+
<< profiling_timestamps.save_image_end_time -
40+
profiling_timestamps.save_image_start_time
41+
<< std::endl;
42+
std::cout << "E2E (pre + inference + post) took "
43+
<< profiling_timestamps.e2e_end_time -
44+
profiling_timestamps.e2e_start_time
45+
<< std::endl;
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025 The Google AI Edge Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef THIRD_PARTY_ODML_LITERT_LITERT_SAMPLES_ASYNC_SEGMENTATION_TIMING_UTILS_H_
18+
#define THIRD_PARTY_ODML_LITERT_LITERT_SAMPLES_ASYNC_SEGMENTATION_TIMING_UTILS_H_
19+
20+
21+
#include "absl/time/time.h" // from @com_google_absl
22+
23+
struct ProfilingTimestamps {
24+
absl::Time load_image_start_time;
25+
absl::Time load_image_end_time;
26+
absl::Time e2e_start_time;
27+
absl::Time pre_process_start_time;
28+
absl::Time pre_process_end_time;
29+
absl::Time inference_start_time;
30+
absl::Time inference_end_time;
31+
absl::Time post_process_start_time;
32+
absl::Time post_process_end_time;
33+
absl::Time e2e_end_time;
34+
absl::Time save_image_start_time;
35+
absl::Time save_image_end_time;
36+
};
37+
38+
void PrintTiming(const ProfilingTimestamps& profiling_timestamps);
39+
40+
#endif // THIRD_PARTY_ODML_LITERT_LITERT_SAMPLES_ASYNC_SEGMENTATION_TIMING_UTILS_H_

0 commit comments

Comments
 (0)