xref: /aosp_15_r20/external/pytorch/ios/TestApp/TestApp/ViewController.mm (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1#import "ViewController.h"
2
3
4#ifdef BUILD_LITE_INTERPRETER
5#import "Benchmark.h"
6#endif
7
8@interface ViewController ()
9@property(nonatomic, strong) UITextView* textView;
10@end
11
12@implementation ViewController
13
14- (void)viewDidLoad {
15  [super viewDidLoad];
16
17#ifdef BUILD_LITE_INTERPRETER
18  self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
19  self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
20  [self.view addSubview:self.textView];
21
22  NSData* configData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"config" ofType:@"json"]];
23  if (!configData) {
24    NSLog(@"Config.json not found!");
25    return;
26  }
27
28  NSError* err;
29  NSDictionary* config = [NSJSONSerialization JSONObjectWithData:configData options:NSJSONReadingAllowFragments error:&err];
30  if (err) {
31    NSLog(@"Parse config.json failed!");
32    return;
33  }
34// NB: When running tests on device, we need an empty app to launch the tests
35#ifdef RUN_BENCHMARK
36  [Benchmark setup:config];
37  [self runBenchmark];
38#endif
39#endif
40}
41
42#ifdef BUILD_LITE_INTERPRETER
43- (void)runBenchmark {
44  self.textView.text = @"Start benchmarking...\n";
45  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
46    NSString* text = [Benchmark run];
47    dispatch_async(dispatch_get_main_queue(), ^{
48      self.textView.text = [self.textView.text stringByAppendingString:text];
49    });
50  });
51}
52#endif
53
54@end
55