Skip to content

Commit f102d06

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Add llama tests. (#5874)
Summary: Pull Request resolved: #5874 . Reviewed By: kirklandsign Differential Revision: D63901612 fbshipit-source-id: 7baaa0fd5a83c2b2a067a0e36754e776d40f73f0
1 parent b9e9479 commit f102d06

File tree

6 files changed

+278
-41
lines changed

6 files changed

+278
-41
lines changed

extension/apple/Benchmark/Benchmark.xcodeproj/project.pbxproj

Lines changed: 139 additions & 30 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Resources
22

3-
This directory and all files in it will be copied to the bundle’s resources directory, accessible via `NSBundle.resourcePath`.
4-
Place any resource files you want to access at runtime here.
3+
This directory and all files in it will be copied to the bundle’s root directory.
4+
Place here any resource files you want to access at runtime.

extension/apple/Benchmark/TestUtils/ResourceTestCase.m

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,18 @@ @implementation ResourceTestCase
6969

7070
for (NSBundle *bundle in self.bundles) {
7171
for (NSString *directory in self.directories) {
72+
NSArray<NSURL *> *resourceURLs =
73+
[bundle URLsForResourcesWithExtension:nil subdirectory:directory];
74+
if (!resourceURLs.count) {
75+
continue;
76+
};
7277
NSMutableDictionary<NSString *, NSMutableArray<NSString *> *>
7378
*matchesByResource = [NSMutableDictionary new];
74-
NSString *dirPath =
75-
[bundle.resourcePath stringByAppendingPathComponent:directory];
76-
NSArray<NSString *> *files =
77-
[NSFileManager.defaultManager contentsOfDirectoryAtPath:dirPath
78-
error:nil]
79-
?: @[];
80-
for (NSString *file in files) {
81-
NSString *fullPath = [dirPath stringByAppendingPathComponent:file];
79+
80+
for (NSURL *url in resourceURLs) {
81+
NSString *file = url.lastPathComponent;
82+
NSString *fullPath = url.path;
83+
8284
for (NSString *key in sortedKeys) {
8385
if (predicates[key](file)) {
8486
matchesByResource[key] =

extension/apple/Benchmark/Tests/GenericTests.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ @implementation GenericTests
3030
+ (NSArray<NSString *> *)directories {
3131
return @[
3232
@"Resources",
33-
@"aatp/data", // AWS Farm devides look at this subdirectory.
33+
@"aatp/data", // AWS Farm devices look for resources here.
3434
];
3535
}
3636

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#import "ResourceTestCase.h"
10+
11+
#import <executorch/examples/models/llama2/runner/runner.h>
12+
13+
using namespace ::executorch::extension;
14+
using namespace ::executorch::runtime;
15+
16+
@interface TokensPerSecondMetric : NSObject<XCTMetric>
17+
18+
@property(nonatomic, assign) NSUInteger tokenCount;
19+
20+
@end
21+
22+
@implementation TokensPerSecondMetric
23+
24+
- (id)copyWithZone:(NSZone *)zone {
25+
TokensPerSecondMetric *copy = [[[self class] allocWithZone:zone] init];
26+
copy.tokenCount = self.tokenCount;
27+
return copy;
28+
}
29+
30+
- (NSArray<XCTPerformanceMeasurement *> *)
31+
reportMeasurementsFromStartTime:
32+
(XCTPerformanceMeasurementTimestamp *)startTime
33+
toEndTime:
34+
(XCTPerformanceMeasurementTimestamp *)endTime
35+
error:(NSError **)error {
36+
double elapsedTime =
37+
(endTime.absoluteTimeNanoSeconds - startTime.absoluteTimeNanoSeconds) /
38+
(double)NSEC_PER_SEC;
39+
return @[ [[XCTPerformanceMeasurement alloc]
40+
initWithIdentifier:NSStringFromClass([self class])
41+
displayName:@"Tokens Per Second"
42+
doubleValue:(self.tokenCount / elapsedTime)
43+
unitSymbol:@"t/s"] ];
44+
}
45+
46+
@end
47+
48+
@interface LLaMATests : ResourceTestCase
49+
@end
50+
51+
@implementation LLaMATests
52+
53+
+ (NSArray<NSString *> *)directories {
54+
return @[
55+
@"Resources",
56+
@"aatp/data", // AWS Farm devices look for resources here.
57+
];
58+
}
59+
60+
+ (NSDictionary<NSString *, BOOL (^)(NSString *)> *)predicates {
61+
return @{@"model" : ^BOOL(NSString *filename){
62+
return [filename hasSuffix:@".pte"] && [filename containsString:@"llama"];
63+
}
64+
, @"tokenizer" : ^BOOL(NSString *filename) {
65+
return [filename isEqual:@"tokenizer.model"];
66+
},
67+
}
68+
;
69+
}
70+
71+
+ (NSDictionary<NSString *, void (^)(XCTestCase *)> *)dynamicTestsForResources:
72+
(NSDictionary<NSString *, NSString *> *)resources {
73+
NSString *modelPath = resources[@"model"];
74+
NSString *tokenizerPath = resources[@"tokenizer"];
75+
return @{@"generate" : ^(XCTestCase *testCase){
76+
auto __block runner = std::make_unique<example::Runner>(
77+
modelPath.UTF8String, tokenizerPath.UTF8String);
78+
const auto status = runner->load();
79+
if (status != Error::Ok) {
80+
XCTFail("Load failed with error %i", status);
81+
return;
82+
}
83+
TokensPerSecondMetric *tokensPerSecondMetric = [TokensPerSecondMetric new];
84+
[testCase measureWithMetrics:@[ tokensPerSecondMetric, [XCTMemoryMetric new] ]
85+
block:^{
86+
tokensPerSecondMetric.tokenCount = 0;
87+
const auto status = runner->generate(
88+
"Once upon a time",
89+
128,
90+
[=](const std::string &token) {
91+
tokensPerSecondMetric.tokenCount++;
92+
},
93+
nullptr,
94+
false);
95+
XCTAssertEqual(status, Error::Ok);
96+
}];
97+
}
98+
}
99+
;
100+
}
101+
102+
@end

extension/apple/Benchmark/Tests/Tests.xcconfig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
HEADER_SEARCH_PATHS[sdk=iphonesimulator*] = $(inherited) \
2+
$(SRCROOT)/../../../.. \
3+
$(TEMP_DIR)/cmake/include
4+
5+
LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*] = $(inherited) \
6+
$(TEMP_DIR)/cmake/lib
7+
18
OTHER_LDFLAGS[sdk=iphonesimulator*] = $(inherited) \
9+
@$(TEMP_DIR)/cmake/linker_flags \
210
-force_load $(BUILT_PRODUCTS_DIR)/libexecutorch-simulator-release.a \
311
-force_load $(BUILT_PRODUCTS_DIR)/libbackend_coreml-simulator-release.a \
412
-force_load $(BUILT_PRODUCTS_DIR)/libbackend_mps-simulator-release.a \
@@ -7,7 +15,15 @@ OTHER_LDFLAGS[sdk=iphonesimulator*] = $(inherited) \
715
-force_load $(BUILT_PRODUCTS_DIR)/libkernels_optimized-simulator-release.a \
816
-force_load $(BUILT_PRODUCTS_DIR)/libkernels_quantized-simulator-release.a
917

18+
HEADER_SEARCH_PATHS[sdk=iphoneos*] = $(inherited) \
19+
$(SRCROOT)/../../../.. \
20+
$(TEMP_DIR)/cmake/include
21+
22+
LIBRARY_SEARCH_PATHS[sdk=iphoneos*] = $(inherited) \
23+
$(TEMP_DIR)/cmake/lib
24+
1025
OTHER_LDFLAGS[sdk=iphoneos*] = $(inherited) \
26+
@$(TEMP_DIR)/cmake/linker_flags \
1127
-force_load $(BUILT_PRODUCTS_DIR)/libexecutorch-ios-release.a \
1228
-force_load $(BUILT_PRODUCTS_DIR)/libbackend_coreml-ios-release.a \
1329
-force_load $(BUILT_PRODUCTS_DIR)/libbackend_mps-ios-release.a \
@@ -16,7 +32,15 @@ OTHER_LDFLAGS[sdk=iphoneos*] = $(inherited) \
1632
-force_load $(BUILT_PRODUCTS_DIR)/libkernels_optimized-ios-release.a \
1733
-force_load $(BUILT_PRODUCTS_DIR)/libkernels_quantized-ios-release.a
1834

35+
HEADER_SEARCH_PATHS[sdk=macos*] = $(inherited) \
36+
$(SRCROOT)/../../../.. \
37+
$(TEMP_DIR)/cmake/include
38+
39+
LIBRARY_SEARCH_PATHS[sdk=macos*] = $(inherited) \
40+
$(TEMP_DIR)/cmake/lib
41+
1942
OTHER_LDFLAGS[sdk=macos*] = $(inherited) \
43+
@$(TEMP_DIR)/cmake/linker_flags \
2044
-force_load $(BUILT_PRODUCTS_DIR)/libexecutorch-macos-release.a \
2145
-force_load $(BUILT_PRODUCTS_DIR)/libbackend_coreml-macos-release.a \
2246
-force_load $(BUILT_PRODUCTS_DIR)/libbackend_mps-macos-release.a \

0 commit comments

Comments
 (0)