Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS][OSS][BE] Add simulator tests for Metal #64852

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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