# LibTorch on WatchOS?

**URL:** <https://discuss.pytorch.org/t/libtorch-on-watchos/69073>\
**Category:** Mobile\
**Created:** [February 8, 2020, 7:13pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073 "2020-02-08T19:13:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [February 8, 2020, 7:13pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/1 "2020-02-08T19:13:16Z")

</div>

Hi all,

Does anyone have a guide/tutorial on how to get LibTorch (C++) installed/setup for a WatchOS project?  
I was hoping it would be as easy as installing a Pod like the iOS version… but I get

`The platform of the target `XXX WatchKit Extension`(watchOS 6.1) is not compatible with`LibTorch (1.4.0)`, which does not support `watchOS`.`

I’ve also tried pulling the source and building using:

`IOS_PLATFORM=WATCHOS scripts/build_ios.sh`

But I keep running into various build issues… and even then I’m not totally sure how to get that built version into my XCode project if I did manage to get it built.

Has anyone out there successfully set up a WatchOS project with LibTorch?

Thanks in advance!!

---

<div class="post-metadata">

**Author:** ![xta0](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xta0/32/17717_2.png) [@xta0](https://discuss.pytorch.org/u/xta0)\
**Post date:** [February 10, 2020, 10:45pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/2 "2020-02-10T22:45:58Z")

</div>

Hi @mapes911, we haven’t run any tests on WatchOS yet. But you should be able to compile the code for that architecture. Can you double check your build command? The one below works on my machine.

```auto
BUILD_PYTORCH_MOBILE=1 IOS_PLATFORM=WATCHOS ./scripts/build_ios.sh

```

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [February 10, 2020, 11:39pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/3 "2020-02-10T23:39:04Z")

</div>

Hi @xta0,

Thanks! That did it. The build worked this time 🙂

I’m running up against another issue now though… not sure if it’s because I was messing around with Eigen libraries earlier or not… but I’m getting these math errors after importing pytorch into my XCode library.

```auto
.../pytorch/include/caffe2/perfkernels/math.h:3:10: 'cstdint' file not found
.../WatchSimulator6.1.sdk/usr/include/dispatch/dispatch.h:25:10: Could not build module 'Darwin'
.../WatchSimulator6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:6:10: Could not build module 'CoreFoundation'

```

I’ve uninstalled and re-installed XCode as per some Stack Overflow posts I’ve found with similar issues and no luck.

Any ideas?

Thanks again for responding!

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [February 10, 2020, 11:56pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/4 "2020-02-10T23:56:20Z")

</div>

Scratch that, I found the problem. I had my Header Search Paths set to recursive and that caused my issues.

For anyone that finds this… here’s what I did to get this installed on WatchOS.

- cloned the pytorch repo
- cd pytorch
- git checkout v1.4.0
- git submodule sync
- git submodule update --init --recursive
- BUILD\_PYTORCH\_MOBILE=1 IOS\_PLATFORM=WATCHOS scripts/build\_ios.sh
- add files from build\_ios/install/include/ and build\_ios/install/lib/ to my XCode project
- in XCode target build settings, add the include directory to Header Search Paths, make sure it’s non-recursive.
- in XCode target build settings, Other Linker Flags, -force\_load “path to lib directory”

That should do it!

---

<div class="post-metadata">

**Author:** ![xta0](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xta0/32/17717_2.png) [@xta0](https://discuss.pytorch.org/u/xta0)\
**Post date:** [February 11, 2020, 12:59am UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/5 "2020-02-11T00:59:48Z")

</div>

That’s awesome news. Thanks for sharing this.

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [February 11, 2020, 7:59am UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/6 "2020-02-11T07:59:46Z")

</div>

@xta0 I’m afraid I spoke too soon. I thought my build was ok because it was just failing on one of my own bugs, but once I fixed that I’ve run into a couple of issues that I’m not sure how to solve.

I’m getting this error.

> libtorch.a(SparseCPUType.cpp.o)’ does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE\_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture armv7k

there is no setting for WatchOS to disable bitcode in the build like there is for iOS. is there a way to build the pytorch libs with bitcode enabled?

another issue I was seeing was…

> building for watchOS-arm64\_32 but attempting to link with file built for watchOS-armv7k

I am attempting to load onto a series 4 watch… any ideas?

Thanks again for your help!!

---

<div class="post-metadata">

**Author:** ![xta0](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xta0/32/17717_2.png) [@xta0](https://discuss.pytorch.org/u/xta0)\
**Post date:** [February 11, 2020, 6:44pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/7 "2020-02-11T18:44:28Z")

</div>

@mapes911, Below is my command that work on my machine. Feel free to try it out.

```auto
BUILD_PYTORCH_MOBILE=1 ENABLE_BITCODE=1 IOS_PLATFORM=WATCHOS IOS_ARCH=arm64_32 ./scripts/build_ios.sh

```

As for the bitcode, you can use the commands below to verify

```auto
#check the architecure
> lipo -i libcpuinfo.a
#Non-fat file: libcpuinfo.a is architecture: arm64_32

#verify bitcode
> otool -l libtorch_cpu.a | grep bitcode
# sectname __bitcode

```

Again, I haven’t run any tests on watchOS platforms. So I’m not sure whether they’ll work or not. But I’m curious to see your results. Please let me know if you have any questions. THanks.

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [February 12, 2020, 6:04am UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/8 "2020-02-12T06:04:11Z")

</div>

@xta0

feels like we’re getting closer!  
re-built using the command you sent.

the lipo command shows all the libraries built properly for arm64\_32  
the otool command you gave is showing “-i functionality obsolete” but the bitcode errors went away so I’m assuming that worked ok.

after doing that I still got a bunch of errors, but i was able to get rid of most by doing:

- Build Settings → Valid Architectures. Removing armv7k and just leaving it as arm64\_32. Is this ok to do? Or will I run into issues later because I removed armv7k.

But… now my (hopefully) last hurdle is this error:

> libpytorch\_qnnpack.a(8x8-aarch64-neon.S.o), building for watchOS, but linking in object file ( …pytorch/lib//libpytorch\_qnnpack.a(8x8-aarch64-neon.S.o)) built for macOS, for architecture arm64\_32

so… I’m not sure how to resolve this issue. any ideas?

Thanks Tao!!

---

<div class="post-metadata">

**Author:** ![xta0](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xta0/32/17717_2.png) [@xta0](https://discuss.pytorch.org/u/xta0)\
**Post date:** [February 12, 2020, 10:19pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/9 "2020-02-12T22:19:07Z")

</div>

@mapes911 that’s because the QNNPACK kernel was written in assembly which has conficts with Apple’s LLVM IR (bitcode). If your model is not quantized, you don’t have to include QNNPACK.

Go to the root `CMakeLists.txt`, search for `set(USE_PYTORCH_QNNPACK ON)`, and turn it off

```auto
set(USE_PYTORCH_QNNPACK OFF)

```

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [February 13, 2020, 3:08am UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/10 "2020-02-13T03:08:13Z")

</div>

@xta0

awesome. it’s compiling, linking and loading on my watch now!! thank you.

however, I am now getting this error when I attempt to load up a trained model.

> libc++abi.dylib: terminating with uncaught exception of type torch::jit::script::ErrorReport:  
> Unknown builtin op: aten::mul.  
> Could not find any similar ops to aten::mul. This op may not exist or may not be currently supported in TorchScript

I’ve tried a couple of models, including one that I was able to load on my phone with LibTorch 1.4.0 using the Podfile install.

How would I look into what this aten::mul method does? Any idea if this has to do with the way I’ve built the libraries? I’ve found a few other forum posts with similar errors but no immediate solution.

Just in case, here’s the code that loads the model.

```auto
torch::jit::script::Module module;
module = torch::jit::load(file_path);

```

---

<div class="post-metadata">

**Author:** ![xta0](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xta0/32/17717_2.png) [@xta0](https://discuss.pytorch.org/u/xta0)\
**Post date:** [February 13, 2020, 5:37am UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/11 "2020-02-13T05:37:40Z")

</div>

@mapes911 That’s good news to hear. `aten::mul` is a very basic op that should be registered by default. Did you apply the `-all_load` to the static libraries? (`force_load` on libtorch\_cpu.a should also work). Also, does that model work on your phone?

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [February 13, 2020, 7:22am UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/12 "2020-02-13T07:22:03Z")

</div>

yes! thank you so much. it is compiled/linked and i can now read in my model.

i appreciate all your help @xta0

---

<div class="post-metadata">

**Author:** ![xta0](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xta0/32/17717_2.png) [@xta0](https://discuss.pytorch.org/u/xta0)\
**Post date:** [February 13, 2020, 7:15pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/13 "2020-02-13T19:15:19Z")

</div>

@mapes911 No problem, were you able to run your model on your watch?

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [February 13, 2020, 7:28pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/14 "2020-02-13T19:28:27Z")

</div>

Yes and no 🙂

I was able to load the model and run one of my data points through… but the second time through my event handler crashes. Assuming this is me not really knowing C++ very well anymore… and I’m probably doing something bad with memory or not setting up my tensor properly.

So… I’m not fully sure that the data is going through the model yet. Will try more when I get home tonight.

Thanks

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [February 15, 2020, 8:52pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/15 "2020-02-15T20:52:53Z")

</div>

@xta0 looks like i’m able to run data through my model just fine on my watch now, thank you very much for your help.

any idea when a WatchOS version will be supported in the LibTorch Podfile? would love to be able to keep up with new versions without going through this pain.

---

<div class="post-metadata">

**Author:** ![xta0](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xta0/32/17717_2.png) [@xta0](https://discuss.pytorch.org/u/xta0)\
**Post date:** [February 18, 2020, 9:23pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/16 "2020-02-18T21:23:35Z")

</div>

Hey @mapes911 That’s good news to hear! Thanks for letting me know. We haven’t discussed about supporting watchos via Cocoapods yet. However, we did simplify the build script - [https://github.com/pytorch/pytorch/pull/33318](https://github.com/pytorch/pytorch/pull/33318).

---

<div class="post-metadata">

**Author:** ![Harsh\_Thaker](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/harsh_thaker/32/22299_2.png) [@Harsh\_Thaker](https://discuss.pytorch.org/u/Harsh_Thaker)\
**Post date:** [April 3, 2020, 12:27pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/17 "2020-04-03T12:27:14Z")

</div>

Hey, I am facing the same issue. Where is CMakeLists.txt file located?

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [April 3, 2020, 2:44pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/18 "2020-04-03T14:44:18Z")

</div>

> <https://github.com/pytorch/pytorch/blob/master/CMakeLists.txt>

---

<div class="post-metadata">

**Author:** ![Harsh\_Thaker](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/harsh_thaker/32/22299_2.png) [@Harsh\_Thaker](https://discuss.pytorch.org/u/Harsh_Thaker)\
**Post date:** [April 4, 2020, 8:56am UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/19 "2020-04-04T08:56:14Z")

</div>

Thanks for the reply. That’s the path of PyTorch repo. How can changes there resolves the error in XCode? Can you please elaborate the process ?

Error:  
ld: warning: building for iOS, but linking in object file (/path-to-project/Pods/LibTorch/install/lib/libpytorch\_qnnpack.a(8x8-aarch64-neon.S.o)) built for macOS  
ld: warning: building for iOS, but linking in object file (/path-to-project/Pods/LibTorch/install/lib/libpytorch\_qnnpack.a(8x8-aarch64-neon.S.o)) built for macOS  
Undefined symbols for architecture arm64:  
“_OBJC\_CLASS_$\_TorchModule”, referenced from:  
objc-class-ref in ViewController.o  
ld: symbol(s) not found for architecture arm64  
clang: error: linker command failed with exit code 1 (use -v to see invocation)

---

<div class="post-metadata">

**Author:** ![mapes911](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mapes911/32/20357_2.png) [@mapes911](https://discuss.pytorch.org/u/mapes911)\
**Post date:** [April 4, 2020, 5:25pm UTC](https://discuss.pytorch.org/t/libtorch-on-watchos/69073/20 "2020-04-04T17:25:17Z")

</div>

Hey there,

The CMakeLists.txt changes I made were to turn off QNNPACK which was causing issues for me when I loaded the model in my XCode project.  
I’m not sure what your issue is, but if you read through the thread I had to go through several iterations (and issues) to arrive at a LibTorch build that allowed me to get my trained model running in a WatchOS project.

What is the process you are going through that is getting you to this error?  
(I’m no expert in this, but @xta0 's comments really helped )

[Next page](https://discuss.pytorch.org/t/libtorch-on-watchos/69073.md?page=2)
