Android - attempt to load module crashes app

username: adonnini
cooked: <p>Hi,<br>
This is my first post. I come from the Java world and am a Python/pyTorch newbie.<br>
I was able to train the network I want to use in my Android app and produce the .pth file for the network using torchScript (following <a href="https://pytorch.org/tutorials/recipes/model_preparation_android.html" rel="noopener nofollow ugc">Model Preparation for Android Recipe — PyTorch Tutorials 2.1.0+cu121 documentation</a>).</p>
<p>I added code to load the network and run it to my Android app.</p>
<p>When I run the app, inference crashes when attempting to load the module.<br>
The error is<br>
libc: Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR)<br>
A debug log is produced (see below).</p>
<p>Here are the pyTorch related code fragments from the app:</p>
<pre><code class="lang-auto">//tmpData contains the input dataset
			float[] flat = ArrayUtil.flattenFloatArray(tmpData);
			final long[] shapeArrDataPytorchFlattened = new long[]{1, flat.length};
			arrDataPytorch = Tensor.fromBlob(flat, shapeArrDataPytorchFlattened);
</code></pre>
<pre><code class="lang-auto">			mModule = Module.load(moduleFileAbsoluteFilePath);
			final Tensor outputTensor = mModule.forward(IValue.from(arrDataPytorch)).toTensor();
			final float[] scores = outputTensor.getDataAsFloatArray();
</code></pre>
<p>I searched for<br>
libc: Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR) “pytorch”<br>
it did not yield any results I found helpful to resolve my problem.</p>
<pre><code class="lang-auto">mModule = Module.load(moduleFileAbsoluteFilePath);
</code></pre>
<p>I admit that I have no clue what the debug log reports. Could someone help me understand what happened?</p>
<h1><a name="debug-log-1" class="anchor" href="#debug-log-1"></a>DEBUG LOG</h1>
<pre><code class="lang-auto">10-18 12:54:26.629: A/libc(5595): Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x6c28433000 in tid 5622 (Thread-2), pid 5595 (lNetworkService)
10-18 12:54:26.676: I/crash_dump64(7136): obtaining output fd from tombstoned, type: kDebuggerdTombstoneProto
10-18 12:54:26.677: I/tombstoned(684): received crash request for pid 5622
10-18 12:54:26.680: I/crash_dump64(7136): performing dump of process 5595 (target tid = 5622)
10-18 12:54:26.688: E/DEBUG(7136): failed to read /proc/uptime: Permission denied
10-18 12:54:26.864: A/DEBUG(7136): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
10-18 12:54:26.864: A/DEBUG(7136): Build fingerprint: 'Fairphone/FP4eea/FP4:12/SKQ1.220201.001/SP2K:user/release-keys'
10-18 12:54:26.864: A/DEBUG(7136): Revision: '0'
10-18 12:54:26.864: A/DEBUG(7136): ABI: 'arm64'
10-18 12:54:26.864: A/DEBUG(7136): Timestamp: 2023-10-18 12:54:26.687588970+0200
10-18 12:54:26.864: A/DEBUG(7136): Process uptime: 0s
10-18 12:54:26.864: A/DEBUG(7136): Cmdline: com.android.contextq:ContextQNeuralNetworkService
10-18 12:54:26.864: A/DEBUG(7136): pid: 5595, tid: 5622, name: Thread-2  &gt;&gt;&gt; com.android.contextq:ContextQNeuralNetworkService &lt;&lt;&lt;
10-18 12:54:26.864: A/DEBUG(7136): uid: 10207
10-18 12:54:26.864: A/DEBUG(7136): signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x6c28433000
10-18 12:54:26.865: A/DEBUG(7136):     x0  0000006c2842f6a0  x1  0000006c2842e9b8  x2  0000000000000001  x3  b400006d5c7f29b0
10-18 12:54:26.865: A/DEBUG(7136):     x4  0000006c2842f6a0  x5  0000006bf6790be0  x6  fefeff8ca3fc9b0a  x7  7f7f7fffffffff7f
10-18 12:54:26.865: A/DEBUG(7136):     x8  0000006c28433000  x9  ed61a80466e7daef  x10 0000000000004001  x11 0000000000000000
10-18 12:54:26.865: A/DEBUG(7136):     x12 0000000000000000  x13 0000000000000001  x14 ffffffffffffffff  x15 0000000000000010
10-18 12:54:26.865: A/DEBUG(7136):     x16 0000006bf3eb8260  x17 0000006fcd5c6140  x18 0000006bfb534000  x19 0000006c2842e9b8
10-18 12:54:26.865: A/DEBUG(7136):     x20 0000006c2842ead8  x21 0000006c28433000  x22 0000000000000002  x23 0000006c2842f6a0
10-18 12:54:26.865: A/DEBUG(7136):     x24 0000000000000000  x25 0000006c28430800  x26 0000006c28430670  x27 0000006c28433000
10-18 12:54:26.865: A/DEBUG(7136):     x28 0000000000000001  x29 0000006c2842e990
10-18 12:54:26.865: A/DEBUG(7136):     lr  0000006bf6794b28  sp  0000006c2842e990  pc  0000006c28433000  pst 0000000000001000
10-18 12:54:26.865: A/DEBUG(7136): backtrace:
10-18 12:54:26.865: A/DEBUG(7136):       #00 pc 0000000000104000  [anon:stack_and_tls:5622]
10-18 12:54:26.865: A/DEBUG(7136):       #01 pc 00000000000eab24  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libc++_shared.so (BuildId: a59088f9640cd272bc9542d94dc84a0c88afd558)
10-18 12:54:26.865: A/DEBUG(7136):       #02 pc 00000000000ea818  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libc++_shared.so (BuildId: a59088f9640cd272bc9542d94dc84a0c88afd558)
10-18 12:54:26.865: A/DEBUG(7136):       #03 pc 00000000000e6e30  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libc++_shared.so (BuildId: a59088f9640cd272bc9542d94dc84a0c88afd558)
10-18 12:54:26.865: A/DEBUG(7136):       #04 pc 00000000000e6cc0  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libc++_shared.so (__gxx_personality_v0+224) (BuildId: a59088f9640cd272bc9542d94dc84a0c88afd558)
10-18 12:54:26.865: A/DEBUG(7136):       #05 pc 0000000003808300  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #06 pc 0000000003808808  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #07 pc 000000000366bcd4  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;)+172) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #08 pc 00000000029693f0  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (caffe2::serialize::PyTorchStreamReader::valid(char const*, char const*)+156) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #09 pc 000000000296a1a0  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (caffe2::serialize::PyTorchStreamReader::getRecordID(std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;)+124) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #10 pc 0000000002969654  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (caffe2::serialize::PyTorchStreamReader::getRecord(std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;)+148) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #11 pc 000000000312e500  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (torch::jit::readArchiveAndTensors(std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;, std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;, std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;, c10::optional&lt;std::__ndk1::function&lt;c10::StrongTypePtr (c10::QualifiedName const&amp;)&gt; &gt;, c10::optional&lt;std::__ndk1::function&lt;c10::intrusive_ptr&lt;c10::ivalue::Object, c10::detail::intrusive_target_default_null_type&lt;c10::ivalue::Object&gt; &gt; (c10::StrongTypePtr const&amp;, c10::IValue)&gt; &gt;, c10::optional&lt;c10::Device&gt;, caffe2::serialize::PyTorchStreamReader&amp;, c10::Type::SingletonOrSharedTypePtr&lt;c10::Type&gt; (*)(std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;), std::__ndk1::shared_ptr&lt;torch::jit::DeserializationStorageContext&gt;)+192) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #12 pc 0000000003118bf0  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #13 pc 0000000003110e20  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #14 pc 0000000003113a40  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (torch::jit::import_ir_module(std::__ndk1::shared_ptr&lt;torch::jit::CompilationUnit&gt;, std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;, c10::optional&lt;c10::Device&gt;, std::__ndk1::unordered_map&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt;, std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt;, std::__ndk1::hash&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; &gt;, std::__ndk1::equal_to&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; &gt;, std::__ndk1::allocator&lt;std::__ndk1::pair&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const, std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; &gt; &gt; &gt;&amp;, bool, bool)+776) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #15 pc 0000000003114e30  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (torch::jit::load(std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;, c10::optional&lt;c10::Device&gt;, std::__ndk1::unordered_map&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt;, std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt;, std::__ndk1::hash&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; &gt;, std::__ndk1::equal_to&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; &gt;, std::__ndk1::allocator&lt;std::__ndk1::pair&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const, std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; &gt; &gt; &gt;&amp;, bool)+148) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #16 pc 000000000066feb0  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (pytorch_jni::PytorchJni::PytorchJni(facebook::jni::alias_ref&lt;_jstring*&gt;, facebook::jni::alias_ref&lt;facebook::jni::JMap&lt;facebook::jni::JString, facebook::jni::JString&gt; &gt;, int)+496) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #17 pc 000000000066faec  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (facebook::jni::basic_strong_ref&lt;facebook::jni::detail::HybridData, facebook::jni::LocalReferenceAllocator&gt; facebook::jni::HybridClass&lt;pytorch_jni::PytorchJni, facebook::jni::detail::BaseHybridClass&gt;::makeCxxInstance&lt;facebook::jni::alias_ref&lt;_jstring*&gt;&amp;, facebook::jni::alias_ref&lt;facebook::jni::JMap&lt;facebook::jni::JString, facebook::jni::JString&gt; &gt;&amp;, int&amp;&gt;(facebook::jni::alias_ref&lt;_jstring*&gt;&amp;, facebook::jni::alias_ref&lt;facebook::jni::JMap&lt;facebook::jni::JString, facebook::jni::JString&gt; &gt;&amp;, int&amp;)+96) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #18 pc 000000000066f984  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (pytorch_jni::PytorchJni::initHybrid(facebook::jni::alias_ref&lt;_jclass*&gt;, facebook::jni::alias_ref&lt;_jstring*&gt;, facebook::jni::alias_ref&lt;facebook::jni::JMap&lt;facebook::jni::JString, facebook::jni::JString&gt; &gt;, int)+56) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #19 pc 000000000066fa14  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/base.apk!libpytorch_jni.so (facebook::jni::detail::FunctionWrapper&lt;facebook::jni::basic_strong_ref&lt;facebook::jni::detail::JTypeFor&lt;facebook::jni::detail::HybridData, facebook::jni::JObject, void&gt;::_javaobject*, facebook::jni::LocalReferenceAllocator&gt; (*)(facebook::jni::alias_ref&lt;_jclass*&gt;, facebook::jni::alias_ref&lt;_jstring*&gt;, facebook::jni::alias_ref&lt;facebook::jni::JMap&lt;facebook::jni::JString, facebook::jni::JString&gt; &gt;, int), _jclass*, facebook::jni::basic_strong_ref&lt;facebook::jni::detail::JTypeFor&lt;facebook::jni::detail::HybridData, facebook::jni::JObject, void&gt;::_javaobject*, facebook::jni::LocalReferenceAllocator&gt;, facebook::jni::alias_ref&lt;_jstring*&gt;, facebook::jni::alias_ref&lt;facebook::jni::JMap&lt;facebook::jni::JString, facebook::jni::JString&gt; &gt;, int&gt;::call(_JNIEnv*, _jobject*, _jstring*, facebook::jni::detail::JTypeFor&lt;facebook::jni::JMap&lt;facebook::jni::JString, facebook::jni::JString&gt;, facebook::jni::JObject, void&gt;::_javaobject*, int, facebook::jni::basic_strong_ref&lt;facebook::jni::detail::JTypeFor&lt;facebook::jni::detail::HybridData, facebook::jni::JObject, void&gt;::_javaobject*, facebook::jni::LocalReferenceAllocator&gt; (*)(facebook::jni::alias_ref&lt;_jclass*&gt;, facebook::jni::alias_ref&lt;_jstring*&gt;, facebook::jni::alias_ref&lt;facebook::jni::JMap&lt;facebook::jni::JString, facebook::jni::JString&gt; &gt;, int))+96) (BuildId: 7f6c1256ae63cd16c1787429611d4930ad9e3888)
10-18 12:54:26.865: A/DEBUG(7136):       #20 pc 000000000034dc30  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+144) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #21 pc 0000000000337480  /apex/com.android.art/lib64/libart.so (art_quick_invoke_static_stub+640) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #22 pc 0000000000519914  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;(art::ArtMethod*, art::Thread*, art::ShadowFrame&amp;, art::Instruction const*, unsigned short, bool, art::JValue*)+2364) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #23 pc 00000000004a31ac  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;(art::interpreter::SwitchImplContext*)+1840) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #24 pc 00000000003503d8  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #25 pc 0000000003dc1b38  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/oat/arm64/base.vdex (org.pytorch.NativePeer.&lt;init&gt;+0)
10-18 12:54:26.865: A/DEBUG(7136):       #26 pc 000000000036c700  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&amp;, art::ShadowFrame&amp;, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.3816585244953842104)+232) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #27 pc 000000000051a45c  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;(art::ArtMethod*, art::Thread*, art::ShadowFrame&amp;, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #28 pc 00000000004a3274  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;(art::interpreter::SwitchImplContext*)+2040) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #29 pc 00000000003503d8  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #30 pc 0000000003dc1a70  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/oat/arm64/base.vdex (org.pytorch.Module.load+0)
10-18 12:54:26.865: A/DEBUG(7136):       #31 pc 000000000036c700  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&amp;, art::ShadowFrame&amp;, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.3816585244953842104)+232) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #32 pc 000000000051a45c  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;(art::ArtMethod*, art::Thread*, art::ShadowFrame&amp;, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #33 pc 00000000004a31ac  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;(art::interpreter::SwitchImplContext*)+1840) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #34 pc 00000000003503d8  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #35 pc 0000000003dc1a50  /data/app/~~ZkOf9by7LZhZ5LJbfzPQeA==/com.android.contextq-GqEEz7RZdo46XVXZXV3xVQ==/oat/arm64/base.vdex (org.pytorch.Module.load+0)
10-18 12:54:26.865: A/DEBUG(7136):       #36 pc 000000000036c700  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&amp;, art::ShadowFrame&amp;, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.3816585244953842104)+232) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #37 pc 000000000051a45c  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;(art::ArtMethod*, art::Thread*, art::ShadowFrame&amp;, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #38 pc 00000000004a31ac  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;(art::interpreter::SwitchImplContext*)+1840) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #39 pc 00000000003503d8  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #40 pc 0000000000010934  /data/data/com.android.contextq/code_cache/.overlay/base.apk/classes15.dex (com.android.contextq.neuralnetwork.NeuralNetworkService.neuralNetworkloadAndRunPytorch+0)
10-18 12:54:26.865: A/DEBUG(7136):       #41 pc 000000000036c700  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&amp;, art::ShadowFrame&amp;, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.3816585244953842104)+232) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #42 pc 000000000051a45c  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;(art::ArtMethod*, art::Thread*, art::ShadowFrame&amp;, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #43 pc 00000000004a31ac  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;(art::interpreter::SwitchImplContext*)+1840) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #44 pc 00000000003503d8  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #45 pc 0000000000008288  /data/data/com.android.contextq/code_cache/.overlay/base.apk/classes15.dex (com.android.contextq.neuralnetwork.NeuralNetworkService$NeuralNetworkServiceRunnable.run+0)
10-18 12:54:26.865: A/DEBUG(7136):       #46 pc 000000000036c700  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&amp;, art::ShadowFrame&amp;, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.3816585244953842104)+232) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #47 pc 000000000051a45c  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;(art::ArtMethod*, art::Thread*, art::ShadowFrame&amp;, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #48 pc 00000000004a3ce8  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;(art::interpreter::SwitchImplContext*)+4716) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #49 pc 00000000003503d8  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #50 pc 000000000000308c  [anon:dalvik-/apex/com.android.art/javalib/core-oj.jar-transformed] (java.lang.Thread.run+0)
10-18 12:54:26.865: A/DEBUG(7136):       #51 pc 000000000036c700  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&amp;, art::ShadowFrame&amp;, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.3816585244953842104)+232) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #52 pc 000000000036bff8  /apex/com.android.art/lib64/libart.so (artQuickToInterpreterBridge+964) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #53 pc 000000000034dd68  /apex/com.android.art/lib64/libart.so (art_quick_to_interpreter_bridge+88) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #54 pc 00000000003371a4  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+612) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #55 pc 000000000023ea64  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+144) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #56 pc 000000000054436c  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallback(void*)+1600) (BuildId: a5fcf27f4a71b07dff05c648ad58e3cd)
10-18 12:54:26.865: A/DEBUG(7136):       #57 pc 00000000000b6a24  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+264) (BuildId: 369edc656806aeaf384cbeb8f7a347af)
10-18 12:54:26.865: A/DEBUG(7136):       #58 pc 00000000000532bc  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+68) (BuildId: 369edc656806aeaf384cbeb8f7a347af)
</code></pre>

username: kimishpatel
cooked: <p>issue is due to “c10::detail::torchCheckFail”. You may not have shipped the model with the app or check the path of the model and make sure it exist.</p>
<p>Also, did you follow all the steps in the tutorial? I would expect it to generate pt file</p>

username: adonnini
cooked: <p>Thanks!<br>
I checked and the path to the model. It exists. Below, you will find the code.</p>
<p>The log statement (<code>Log.i(TAG, " - neuralNetworkloadAndRunPytorch - savedNetworkArchiveLength - "+savedNetworkArchiveLength);</code>)<br>
printing the length of the model reports a non-zero length.</p>
<p>I must be doing something else wrong.</p>
<p>I did follow the steps to produce a model for mobile. I used the torchscript method not trace.</p>
<h1><a name="code-1" class="anchor" href="#code-1"></a>CODE</h1>
<pre><code class="lang-java">			String locationInformationDir = ContextqApp.getApplicationRef().getFilesDir().getAbsolutePath()+"/locationInformation/";
			Log.i(TAG, " - neuralNetworkloadAndRunPytorch - locationInformationDir - "+locationInformationDir);

			File savedNetworkArchive = new File(locationInformationDir+"epoch1.pt");

			long savedNetworkArchiveLength = savedNetworkArchive.length();
			Log.i(TAG, " - neuralNetworkloadAndRunPytorch - savedNetworkArchiveLength - "+savedNetworkArchiveLength);

			String moduleFileAbsoluteFilePath = locationInformationDir+"epoch1.pt";

			try {
				Log.i(TAG, " - neuralNetworkloadAndRunPytorch - Abut to load module --- ");
				mModule = Module.load(moduleFileAbsoluteFilePath);
			} catch (Exception e) {
				Log.i(TAG, " - neuralNetworkloadAndRunPytorch - Loading of module FAILED --- ");
				throw new RuntimeException(e);
			}
</code></pre>

username: adonnini
cooked: <p>Hi Kimish,</p>
<p>The problem was caused by two facts:</p>
<ol>
<li>
<p>I did not have this</p>
<pre><code>          // 
</code></pre>
</li>
</ol>
<p><a href="https://mvnrepository.com/artifact/com.facebook.fbjni/fbjni-java-only" class="onebox" target="_blank" rel="noopener nofollow ugc">https://mvnrepository.com/artifact/com.facebook.fbjni/fbjni-java-only</a><br>
implementation group: ‘com.facebook.fbjni’, name:<br>
‘fbjni-java-only’, version: ‘0.2.2’</p>
<p>in my build.gradle</p>
<p>and</p>
<ol start="2">
<li>I did not use pytorch_android_lite</li>
</ol>
<p>In fact, I think it is important that all documentation about pytorch<br>
mobile on Android be modified to state that the facebook library is<br>
included among the dependencies, and. as important, that it is stated<br>
CLEARLY that pytorch_android_lite should be used NOT pytorch_android.<br>
This is true both on Android (i.e. loading the module with<br>
LiteModuleLoader.load NOT Module.load), and when saving the model<br>
produced with torchscript.</p>
<p>Thanks for your help,</p>
<p>Alex Donnini</p>

username: kimishpatel
cooked: <p>Thanks for the feedback Alex.</p>
<p>Have you also checked <a href="https://pytorch.org/mobile/android/" class="inline-onebox" rel="noopener nofollow ugc">Android | PyTorch</a>?</p>
<p>What you point out is fair and I was surprised that some details are missing.</p>

username: kimishpatel
cooked: <p>By the way we are also developing new solution for mobile that we announced this year at PTC. <a href="https://pytorch.org/executorch-overview" class="inline-onebox" rel="noopener nofollow ugc">PyTorch ExecuTorch | PyTorch</a>.</p>
<p>So our investment in the old stack will be somewhat limited</p>

username: adonnini
cooked: <p>Hi Kimish,</p>
<p>Android/PyTorch was the first site I accessed when I started working on<br>
running the model I am using in my Android app.</p>
<p>Since you are not investing in the old stack much any longer, should I<br>
transition to ExecuTorch now?</p>
<p>Thanks,</p>
<p>Alex</p>

username: kimishpatel
cooked: <p>If you are on-boarding a new usage in your app for the first time, I would suggest experiment with ExecuTorch first. I would rather have your feedback and on that and fix whatever we need to. But do note that, the new stack is in very early stages of development so likely will have some rough edges. For example, its java bindings are not that mature and we put them together for demo purposes.</p>

username: adonnini
cooked: <p>Hi Kimish,</p>
<p>I set up the ExecuTorch environment. The set-up instructions are very good.</p>
<p>Rather than exporting a sample model, I would prefer working with the<br>
one I want to use in my Android application.</p>
<p>Is this</p>
<pre><code class="lang-auto">
&gt;&gt;&gt; import  executorch.exir  as  exir
&gt;&gt;&gt; from  executorch.exir.tests.models  import  Mul
&gt;&gt;&gt; m  =  Mul()
&gt;&gt;&gt; print(exir.capture(m,  m.get_random_inputs()).to_edge())
&gt;&gt;&gt; open("mul.pte",  "wb").write(exir.capture(m,  m.get_random_inputs()).to_edge().to_executorch().buffer)

</code></pre>
<p>the script I should use? If it is, I have a couple of questions:</p>
<ol>
<li>I should run the above script in my model’s top directory, right?</li>
<li>I should replace “from executorch.exir.tests.models import Mul” wih<br>
the path to my model.py and the name of the class which implements my<br>
model, right?</li>
</ol>
<p>If I am off-base, could you please point me in the right direction? The<br>
“generate a smaple ExecuTorch program” section of<br>
<a href="https://pytorch.org/executorch/stable/getting-started-setup.html" class="inline-onebox" rel="noopener nofollow ugc">Setting Up ExecuTorch — ExecuTorch documentation</a> does<br>
not address explicitly how one would generate the corresponding<br>
ExecuTorch program from one’s model.</p>
<p>Thanks,</p>
<p>Alex</p>

username: kimishpatel
cooked: <aside class="quote no-group" data-username="adonnini" data-post="9" data-topic="190304">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adonnini/48/65046_2.png" class="avatar"> adonnini:</div>
<blockquote>
<ol>
<li>I should run the above script in my model’s top directory, right?</li>
<li>I should replace “from executorch.exir.tests.models import Mul” wih<br>
the path to my model.py and the name of the class which implements my<br>
model, right?</li>
</ol>
</blockquote>
</aside>
<p>For 1. Yes that is correct. I would also suggest that you take a look at examples export here <a href="https://github.com/pytorch/executorch/blob/main/examples/portable/scripts/export.py#L42" rel="noopener nofollow ugc">https://github.com/pytorch/executorch/blob/main/examples/portable/scripts/export.py#L42</a>, <a href="https://github.com/pytorch/executorch/tree/main/examples/portable" rel="noopener nofollow ugc">https://github.com/pytorch/executorch/tree/main/examples/portable</a> and <a href="https://github.com/pytorch/executorch/blob/main/examples/portable/utils.py#L81" rel="noopener nofollow ugc">https://github.com/pytorch/executorch/blob/main/examples/portable/utils.py#L81</a></p>
<p>For 2. also correct.</p>

username: kimishpatel
cooked: <p>Do note that for getting performant model you likely want to employ delegation. follow <a href="https://pytorch.org/executorch/stable/tutorial-xnnpack-delegate-lowering.html" class="inline-onebox" rel="noopener nofollow ugc">Building and Running ExecuTorch with XNNPACK Backend — ExecuTorch documentation</a></p>

username: adonnini
cooked: <p>Dos this mean that I should go straight to using the xnnpack backend and<br>
not follow the instructions in<br>
<a href="https://pytorch.org/executorch/stable/getting-started-setup.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/executorch/stable/getting-started-setup.html</a><br>
?</p>
<p>Are the two approaches exclusive of each other?</p>
<p>Is using delegation just to get better performance?</p>
<p>Thanks</p>

username: adonnini
cooked: <p>Hi Kimish,</p>
<p>I hope I am not bothering you (too much).</p>
<p>Could you please let me know what you think I might try to resolve the<br>
problem caused by the missing torch._export? (please see below).</p>
<p>Thanks,</p>
<p>Alex</p>
<p>ERROR</p>

username: adonnini
cooked: <p>Hi,</p>
<p>I commented out import torch._export.</p>
<p>Now the code fails with the error listed below.</p>
<p>I searched<br>
<a href="https://pytorch.org/docs/stable/fx.html#api-reference" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/fx.html#api-reference</a><br>
for passes, passes.infra and infra<br>
No results returned</p>
<p>Any suggestions as to what to do next?</p>
<p>Thanks</p>
<p>ERROR</p>

username: adonnini
cooked: <p>Hi Kimish,</p>
<p>Sorry for this additional message. I resolved all export issues. I<br>
realized I had to ensure that all required pacakges are installed. Once<br>
I did that, the export script worked (I ran into anoter issue but Ill<br>
try to resolve it myself before contacting you).</p>
<p>Thanks,</p>
<p>Alex</p>

username: adonnini
cooked: <p>Hi Kimish,</p>
<p>I am not sure where to post questions regarding Executorch. I tried the<br>
Executorch category in the pytorch forum. Is that the right place?</p>
<p>Here is what I posted:</p>
<p>Hi,<br>
I am trying to export my model for use in my Android app using Executorch.<br>
I set up the executorch environment as per<br>
<a href="https://pytorch.org/executorch/stable/getting-started-setup.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/executorch/stable/getting-started-setup.html</a></p>
<p>I am confused as to how to proceed.</p>
<p>I tried using this</p>
<pre><code class="lang-auto">         m = tf_model
         print(exir.capture(m, m.get_random_inputs()).to_edge())
         open("tfmodel.pte", "wb").write(exir.capture(m,
         m.get_random_inputs()).to_edge().to_executorch().buffer)

</code></pre>
<p>It failed with this error</p>
<pre><code class="lang-auto">Traceback (most recent call last):
   File 
"/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/trajectory-prediction-transformers-master/train.py", 
line 280, in &lt;module&gt;
     print(exir.capture(m, m.get_random_inputs()).to_edge())
   File 
"/home/adonnini1/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", 
line 1696, in __getattr__
     raise AttributeError(f"'{type(self).__name__}' object has no 
attribute '{name}'")
AttributeError: 'TFModel' object has no attribute 'get_random_inputs'

</code></pre>
<p>Should I use this<br>
<a href="https://pytorch.org/executorch/stable/tutorials/export-to-executorch-tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/executorch/stable/tutorials/export-to-executorch-tutorial.html</a></p>
<p>I would appreciate guidance non how to proceed.</p>
<p>Thanks</p>

username: kimishpatel
cooked: <p>Alex,<br>
for future reference this is the category for executorch post on pytorch forums. <a href="https://discuss.pytorch.org/c/executorch/42" class="inline-onebox">ExecuTorch - PyTorch Forums</a></p>

username: kimishpatel
cooked: <p>What is tf_model in your example? If you are following pattern of <a href="https://github.com/pytorch/executorch/blob/main/examples/models/inception_v3/model.py#L28" rel="noopener nofollow ugc">https://github.com/pytorch/executorch/blob/main/examples/models/inception_v3/model.py#L28</a>, then the <code>TFModel</code> needs to defined get_random_inputs method.</p>

username: adonnini
cooked: <p>Thanks. I did post in the executorch section of the pytorch forums.<br>
There was no answer.</p>
<p>I will from now on.</p>

username: adonnini
cooked: <p>I just responded to your message creating a new topic in the executorch<br>
section of pytorch forums . I hope you will be able to respond. Thanks</p>