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

Initial changes for iOS support #660

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

simlay
Copy link
Contributor

@simlay simlay commented Mar 26, 2023

I figured I'd give iOS building a shot. To even get started, one must build the iOS library for PyTorch. Make sure the TORCH_VERSION from torch-sys matches the PyTorch checkout.

For aarch64-apple-ios-sim you want to run BUILD_PYTORCH_MOBILE=1 IOS_PLATFORM=SIMULATOR IOS_ARCH=arm64 ./scripts/build_ios.sh.
For aarch64-apple-ios you want BUILD_PYTORCH_MOBILE=1 IOS_ARCH=arm64 ./scripts/build_ios.sh
It's unclear how to get x86_64-apple-ios (the x86-64 iOS simulator).

Once you've got that built, IPHONEOS_DEPLOYMENT_TARGET=16.1 LIBTORCH=$PWD/pytorch-checkout/build_ios/install cargo build --target aarch64-apple-ios-sim --example basics

TODO:

ld: in /Users/simlay/projects/tch-rs/target/debug/deps/libring-9ce1a2518f1231ce.rlib(aesv8-armx-ios64.o), building for macOS, but linking in object file built for iOS, file '/Users/simlay/projects/tch-rs/target/debug/deps/libring-9ce1a2518f1231ce.rlib' for architecture arm64

Work around is to build for host and then build for cross compiler target.

Here's the runtime error I'm seeing when I run the basics example in the iOS simulator:

"Could not find schema for aten::zeros.
Exception raised from findSchemaOrThrow at /Users/simlay/projects/torch-sys-source/pytorch/aten/src/ATen/core/dispatch/Dispatcher.cpp:131 (most recent call first):
frame #0: _ZN3c106detail14torchCheckFailEPKcS2_jRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE + 92 (0x100a7aa1c in basics)
frame #1: _ZN3c1010Dispatcher17findSchemaOrThrowEPKcS2_ + 516 (0x100a37234 in basics)
frame #2: _ZN2at4_opsL25create_zeros_typed_handleEv + 48 (0x100a138f0 in basics)
frame #3: _ZN2at4_ops5zeros4callEN3c108ArrayRefINS2_6SymIntEEENS2_8optionalINS2_10ScalarTypeEEENS6_INS2_6LayoutEEENS6_INS2_6DeviceEEENS6_IbEE + 176 (0x100a1387c in basics)
frame #4: _ZN2at5zerosEN3c108ArrayRefIxEENS0_13TensorOptionsE + 248 (0x1009ce27c in basics)
frame #5: _ZN5torch5zerosEN3c108ArrayRefIxEENS0_13TensorOptionsE + 136 (0x1009c592c in basics)
frame #6: at_tensor_of_data + 124 (0x1009c56c8 in basics)
frame #7: _ZN3tch8wrappers6tensor6Tensor10f_of_slice17h8c301062d1683d0dE + 136 (0x1009bdae4 in basics)
frame #8: _ZN3tch8wrappers6tensor6Tensor8of_slice17h3ef2e65ed55dda2cE + 36 (0x1009bdbd0 in basics)
frame #9: _ZN6basics4main17h4585665960def4e9E + 44 (0x1009be1ac in basics)
frame #10: _ZN4core3ops8function6FnOnce9call_once17h166a604c999bde6cE + 20 (0x1009bdd94 in basics)
frame #11: _ZN3std10sys_common9backtrace28__rust_begin_short_backtrace17h133407b50aa38c1eE + 24 (0x1009bdd38 in basics)
frame #12: _ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h8df4dd181cd92464E + 28 (0x1009bde84 in basics)
frame #13: _ZN3std2rt19lang_start_internal17h75a83dce357f8b7cE + 380 (0x1009f350c in basics)
frame #14: _ZN3std2rt10lang_start17h53972f716e62a038E + 84 (0x1009bde50 in basics)
frame #15: main + 36 (0x1009be614 in basics)
frame #16: start_sim + 20 (0x100c59fa0 in dyld)
frame #17: 0x0 + 4308835920 (0x100d39e50 in ???)
frame #18: 0x0 + 5216716480866942976 (0x4865800000000000 in ???)
")', /Users/simlay/projects/tch-rs/src/wrappers/tensor.rs:447:32

println!("Cudnn available: {}", tch::Cuda::cudnn_is_available());
let device = tch::Device::cuda_if_available();
//println!("Cuda available: {}", tch::Cuda::is_available());
//println!("Cudnn available: {}", tch::Cuda::cudnn_is_available());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these two lines are uncommented, I get the following compile error:

error: linking with `cc` failed: exit status: 1
  = note: Undefined symbols for architecture arm64:
            "torch::cuda::is_available()", referenced from:
                _atc_cuda_is_available in libtorch_sys-0d280fb35559980b.rlib(torch_api.o)
            "torch::cuda::cudnn_is_available()", referenced from:
                _atc_cudnn_is_available in libtorch_sys-0d280fb35559980b.rlib(torch_api.o)
          ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm not sure why. It doesn't happen on the macOS build.

@antimora
Copy link

antimora commented Apr 3, 2023

@simlay Does it mean it's possible to build and link the torch lib statically? I filed this ticket originally to find out: #647

@simlay
Copy link
Contributor Author

simlay commented Apr 4, 2023

@simlay Does it mean it's possible to build and link the torch lib statically? I filed this ticket originally to find out: #647

I believe that would be one result of iOS support. Given that my local iOS pytorchlib directory doesn't have any dylibs (only .a's) I believe so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants