Skip to content

Commit d4af7af

Browse files
kimishpatelfacebook-github-bot
authored andcommitted
Move benchmarking infra code to torchao
Summary: Add a test to validate that benchmarking works on ios deive Reviewed By: metascroy Differential Revision: D61501686
1 parent db03e63 commit d4af7af

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>get-task-allow</key>
6+
<true/>
7+
<key>keychain-access-groups</key>
8+
<array>
9+
<string>T84QZS65DQ.platformFamily</string>
10+
</array>
11+
</dict>
12+
</plist>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>${PRODUCT_NAME}</string>
9+
<key>CFBundleExecutable</key>
10+
<string>${EXECUTABLE_NAME}</string>
11+
<key>CFBundleIdentifier</key>
12+
<string>${FB_BUNDLE_ID}</string>
13+
<key>CFBundleInfoDictionaryVersion</key>
14+
<string>6.0</string>
15+
<key>CFBundleName</key>
16+
<string>${PRODUCT_NAME}</string>
17+
<key>CFBundlePackageType</key>
18+
<string>APPL</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>1.0</string>
21+
<key>CFBundleSignature</key>
22+
<string>????</string>
23+
<key>CFBundleVersion</key>
24+
<string>1.0</string>
25+
<key>LSRequiresIPhoneOS</key>
26+
<true/>
27+
<key>UILaunchStoryboardName</key>
28+
<string>LaunchScreen</string>
29+
<key>UISupportedInterfaceOrientations</key>
30+
<array>
31+
<string>UIInterfaceOrientationPortrait</string>
32+
</array>
33+
</dict>
34+
</plist>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
#include <iostream>
4+
5+
int main(int argc, char** argv) {
6+
std::cout << "Default main when no benchmarking deps are specified\n";
7+
return 0;
8+
}

torchao/experimental/benchmark_infra/ios/output_redirect.h

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
#import "output_redirect.h"
4+
5+
#import <cstdio>
6+
#import <fstream>
7+
#import <iostream>
8+
#import <string>
9+
10+
#import <Foundation/Foundation.h>
11+
12+
class STDIORedirector {
13+
public:
14+
STDIORedirector() {
15+
if (@available(iOS 17, *)) {
16+
/* duplicate stdout */
17+
std::string file_name =
18+
std::string(std::getenv("HOME")) + "/tmp/BENCH_LOG";
19+
redirect_out_ = fopen(file_name.c_str(), "w");
20+
stdout_dupfd_ = dup(1);
21+
/* replace stdout with our output fd */
22+
dup2(fileno(redirect_out_), 1);
23+
fflush(stdout);
24+
setvbuf(stdout, nil, _IONBF, 0);
25+
setvbuf(redirect_out_, nil, _IONBF, 0);
26+
}
27+
}
28+
29+
~STDIORedirector() {
30+
if (@available(iOS 17, *)) {
31+
fflush(stdout);
32+
/* restore stdout */
33+
dup2(stdout_dupfd_, 1);
34+
close(stdout_dupfd_);
35+
fclose(redirect_out_);
36+
}
37+
}
38+
39+
private:
40+
FILE* redirect_out_;
41+
int stdout_dupfd_;
42+
};
43+
44+
static STDIORedirector stdio_redirector_;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
#include <benchmark/benchmark.h>
4+
5+
#include <cstdint>
6+
#include <cstdlib>
7+
#include <memory>
8+
#include <random>
9+
10+
namespace {
11+
std::random_device rd;
12+
std::mt19937 generator(rd());
13+
14+
// This test is to validate that the benchmarking binary
15+
// can be run on any device. Right now, it is focusing
16+
// on benchmarking on laptop (x86 oe mac) and iOS
17+
static void TestBenchmark(benchmark::State& state) {
18+
const int32_t K = state.range(0);
19+
auto a = std::make_unique<float[]>(K);
20+
auto b = std::make_unique<float[]>(K);
21+
auto c = std::make_unique<float[]>(K);
22+
static std::uniform_real_distribution<> real_distrib(-1.0, 1.0);
23+
for (int ii = 0; ii < K; ++ii) {
24+
a[ii] = real_distrib(generator);
25+
b[ii] = real_distrib(generator);
26+
c[ii] = 0;
27+
}
28+
for (auto _ : state) {
29+
for (int ii = 0; ii < K; ++ii) {
30+
c[ii] = a[ii] + b[ii];
31+
}
32+
}
33+
}
34+
35+
BENCHMARK(TestBenchmark)->Args({4096 * 4})->UseRealTime();
36+
} // namespace
37+
38+
int main(int argc, char** argv) {
39+
char arg0_default[] = "benchmark";
40+
char* args_default = arg0_default;
41+
if (!argv) {
42+
argc = 1;
43+
argv = &args_default;
44+
}
45+
::benchmark::Initialize(&argc, argv);
46+
::benchmark::RunSpecifiedBenchmarks();
47+
::benchmark::Shutdown();
48+
return 0;
49+
}

0 commit comments

Comments
 (0)