Skip to content

Commit

Permalink
[iOS][OSS][BE] Add simulator tests for Metal
Browse files Browse the repository at this point in the history
Pull Request resolved: #64852


ghstack-source-id: 137970230

Differential Revision: [D30877961](https://our.internmc.facebook.com/intern/diff/D30877961/)
  • Loading branch information
xta0 committed Sep 13, 2021
1 parent 1b2f4f7 commit ab884c2
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .circleci/cimodel/data/simple/ios_definitions.py
Expand Up @@ -70,6 +70,9 @@ def gen_tree(self):
IOSJob(XCODE_VERSION, ArchVariant("arm64", "metal"), extra_props={
"use_metal": miniutils.quote(str(int(True))),
"lite_interpreter": miniutils.quote(str(int(True)))}),
IOSJob(XCODE_VERSION, ArchVariant("x86_64", "metal"), extra_props={
"use_metal": miniutils.quote(str(int(True))),
"lite_interpreter": miniutils.quote(str(int(True)))}),
IOSJob(XCODE_VERSION, ArchVariant("arm64", "full_jit"), extra_props={
"lite_interpreter": miniutils.quote(str(int(False)))}),
IOSJob(XCODE_VERSION, ArchVariant("arm64", "custom"), extra_props={
Expand Down
19 changes: 15 additions & 4 deletions .circleci/config.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .circleci/scripts/binary_ios_test.sh
Expand Up @@ -26,4 +26,4 @@ if ! [ -x "$(command -v xcodebuild)" ]; then
exit 1
fi
PROFILE=PyTorch_CI_2021
ruby ${PROJ_ROOT}/scripts/xcode_build.rb -i ${PROJ_ROOT}/build_ios/install -x ${PROJ_ROOT}/ios/TestApp/TestApp.xcodeproj -p ${IOS_PLATFORM} -c ${PROFILE} -t ${IOS_DEV_TEAM_ID} -f Accelerate,MetalPerformanceShaders,CoreML
ruby ${PROJ_ROOT}/scripts/xcode_build.rb -i ${PROJ_ROOT}/build_ios/install -x ${PROJ_ROOT}/ios/TestApp/TestApp.xcodeproj -p ${IOS_PLATFORM} -c ${PROFILE} -t ${IOS_DEV_TEAM_ID}
11 changes: 7 additions & 4 deletions .circleci/verbatim-sources/job-specs/job-specs-custom.yml
Expand Up @@ -525,9 +525,8 @@
fi
export IOS_ARCH=${IOS_ARCH}
export IOS_PLATFORM=${IOS_PLATFORM}
if [ ${IOS_PLATFORM} != "SIMULATOR" ]; then
export USE_PYTORCH_METAL=${USE_METAL}
fi
export USE_PYTORCH_METAL=${USE_METAL}
unbuffer ${PROJ_ROOT}/scripts/build_ios.sh 2>&1 | ts
- run:
name: Run Build Test
Expand Down Expand Up @@ -577,7 +576,11 @@
cd ${PROJ_ROOT}/ios/TestApp
instruments -s -devices
if [ ${BUILD_LITE_INTERPRETER} == 1 ]; then
fastlane scan --only_testing TestAppTests/TestAppTests/testLiteInterpreter
if [ ${USE_PYTORCH_METAL} == 1 ]; then
fastlane scan --only_testing TestAppTests/TestAppTests/testMetal
else
fastlane scan --only_testing TestAppTests/TestAppTests/testLiteInterpreter
fi
else
fastlane scan --only_testing TestAppTests/TestAppTests/testFullJIT
fi
Expand Down
10 changes: 10 additions & 0 deletions ios/TestApp/TestAppTests/TestLiteInterpreter.mm
Expand Up @@ -21,4 +21,14 @@ - (void)testLiteInterpreter {
XCTAssertTrue(outputTensor.numel() == 1000);
}

- (void)testMetal {
NSString* modelPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"model_metal"
ofType:@"ptl"];
auto module = torch::jit::_load_for_mobile(modelPath.UTF8String);
c10::InferenceMode mode;
auto input = torch::ones({1, 3, 224, 224}, at::kFloat).metal();
auto outputTensor = module.forward({input}).toTensor().cpu();
XCTAssertTrue(outputTensor.numel() == 1000);
}

@end
9 changes: 9 additions & 0 deletions ios/TestApp/benchmark/setup.rb
Expand Up @@ -93,6 +93,7 @@

puts "Linking static libraries..."
libs = ['libc10.a', 'libclog.a', 'libpthreadpool.a', 'libXNNPACK.a', 'libeigen_blas.a', 'libcpuinfo.a', 'libpytorch_qnnpack.a', 'libtorch_cpu.a', 'libtorch.a']
frameworks = ['CoreML', 'Metal', 'MetalPerformanceShaders', 'Accelerate', 'UIKit']
targets.each do |target|
target.frameworks_build_phases.clear
for lib in libs do
Expand All @@ -102,6 +103,14 @@
target.frameworks_build_phases.add_file_reference(libref)
end
end
# link system frameworks
frameworks.each do |framework|
path = "System/Library/Frameworks/#{framework}.framework"
framework_ref = project.frameworks_group.new_reference(path)
framework_ref.name = "#{framework}.framework"
framework_ref.source_tree = 'SDKROOT'
target.frameworks_build_phases.add_file_reference(framework_ref)
end
end

project.save
Expand Down
4 changes: 3 additions & 1 deletion ios/TestApp/benchmark/trace_model.py
Expand Up @@ -8,4 +8,6 @@
traced_script_module = torch.jit.trace(model, example)
optimized_scripted_module = optimize_for_mobile(traced_script_module)
torch.jit.save(optimized_scripted_module, '../models/model.pt')
exported_optimized_scripted_module = optimized_scripted_module._save_for_lite_interpreter("../models/model_lite.ptl")
optimized_scripted_module._save_for_lite_interpreter("../models/model_lite.ptl")
optimized_scripted_module = optimize_for_mobile(traced_script_module, backend='metal')
optimized_scripted_module._save_for_lite_interpreter("../models/model_metal.ptl")
1 change: 1 addition & 0 deletions ios/TestApp/fastlane/Scanfile
Expand Up @@ -4,3 +4,4 @@ clean(true)
suppress_xcode_output(true)
force_quit_simulator(true)
include_simulator_logs(false)
deployment_target_version("14.0")
5 changes: 1 addition & 4 deletions scripts/xcode_build.rb
Expand Up @@ -19,9 +19,6 @@
opts.on('-t', '--team_id ', 'development team ID') { |value|
options[:team_id] = value
}
opts.on('-f', '--framework ', 'system frameworks') { |value|
options[:framework] = value.split(',')
}
end.parse!
puts options.inspect

Expand Down Expand Up @@ -63,7 +60,7 @@
end
end
# link system frameworks
frameworks = options[:framework]
frameworks = ['CoreML', 'Metal', 'MetalPerformanceShaders', 'Accelerate', 'UIKit']
if frameworks
frameworks.each do |framework|
path = "System/Library/Frameworks/#{framework}.framework"
Expand Down

0 comments on commit ab884c2

Please sign in to comment.