Android - attempt to load module crashes app

Hi,
This is my first post. I come from the Java world and am a Python/pyTorch newbie.
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 Model Preparation for Android Recipe — PyTorch Tutorials 2.1.0+cu121 documentation).

I added code to load the network and run it to my Android app.

When I run the app, inference crashes when attempting to load the module.
The error is
libc: Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR)
A debug log is produced (see below).

Here are the pyTorch related code fragments from the app:

//tmpData contains the input dataset
			float[] flat = ArrayUtil.flattenFloatArray(tmpData);
			final long[] shapeArrDataPytorchFlattened = new long[]{1, flat.length};
			arrDataPytorch = Tensor.fromBlob(flat, shapeArrDataPytorchFlattened);
			mModule = Module.load(moduleFileAbsoluteFilePath);
			final Tensor outputTensor = mModule.forward(IValue.from(arrDataPytorch)).toTensor();
			final float[] scores = outputTensor.getDataAsFloatArray();

I searched for
libc: Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR) “pytorch”
it did not yield any results I found helpful to resolve my problem.

mModule = Module.load(moduleFileAbsoluteFilePath);

I admit that I have no clue what the debug log reports. Could someone help me understand what happened?

DEBUG LOG

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  >>> com.android.contextq:ContextQNeuralNetworkService <<<
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<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)+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<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)+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<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)+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<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, c10::optional<std::__ndk1::function<c10::StrongTypePtr (c10::QualifiedName const&)> >, c10::optional<std::__ndk1::function<c10::intrusive_ptr<c10::ivalue::Object, c10::detail::intrusive_target_default_null_type<c10::ivalue::Object> > (c10::StrongTypePtr const&, c10::IValue)> >, c10::optional<c10::Device>, caffe2::serialize::PyTorchStreamReader&, c10::Type::SingletonOrSharedTypePtr<c10::Type> (*)(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&), std::__ndk1::shared_ptr<torch::jit::DeserializationStorageContext>)+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<torch::jit::CompilationUnit>, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, c10::optional<c10::Device>, std::__ndk1::unordered_map<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::hash<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > >, std::__ndk1::equal_to<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > >, std::__ndk1::allocator<std::__ndk1::pair<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > > > >&, 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<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, c10::optional<c10::Device>, std::__ndk1::unordered_map<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::hash<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > >, std::__ndk1::equal_to<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > >, std::__ndk1::allocator<std::__ndk1::pair<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > > > >&, 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<_jstring*>, facebook::jni::alias_ref<facebook::jni::JMap<facebook::jni::JString, facebook::jni::JString> >, 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<facebook::jni::detail::HybridData, facebook::jni::LocalReferenceAllocator> facebook::jni::HybridClass<pytorch_jni::PytorchJni, facebook::jni::detail::BaseHybridClass>::makeCxxInstance<facebook::jni::alias_ref<_jstring*>&, facebook::jni::alias_ref<facebook::jni::JMap<facebook::jni::JString, facebook::jni::JString> >&, int&>(facebook::jni::alias_ref<_jstring*>&, facebook::jni::alias_ref<facebook::jni::JMap<facebook::jni::JString, facebook::jni::JString> >&, int&)+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<_jclass*>, facebook::jni::alias_ref<_jstring*>, facebook::jni::alias_ref<facebook::jni::JMap<facebook::jni::JString, facebook::jni::JString> >, 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<facebook::jni::basic_strong_ref<facebook::jni::detail::JTypeFor<facebook::jni::detail::HybridData, facebook::jni::JObject, void>::_javaobject*, facebook::jni::LocalReferenceAllocator> (*)(facebook::jni::alias_ref<_jclass*>, facebook::jni::alias_ref<_jstring*>, facebook::jni::alias_ref<facebook::jni::JMap<facebook::jni::JString, facebook::jni::JString> >, int), _jclass*, facebook::jni::basic_strong_ref<facebook::jni::detail::JTypeFor<facebook::jni::detail::HybridData, facebook::jni::JObject, void>::_javaobject*, facebook::jni::LocalReferenceAllocator>, facebook::jni::alias_ref<_jstring*>, facebook::jni::alias_ref<facebook::jni::JMap<facebook::jni::JString, facebook::jni::JString> >, int>::call(_JNIEnv*, _jobject*, _jstring*, facebook::jni::detail::JTypeFor<facebook::jni::JMap<facebook::jni::JString, facebook::jni::JString>, facebook::jni::JObject, void>::_javaobject*, int, facebook::jni::basic_strong_ref<facebook::jni::detail::JTypeFor<facebook::jni::detail::HybridData, facebook::jni::JObject, void>::_javaobject*, facebook::jni::LocalReferenceAllocator> (*)(facebook::jni::alias_ref<_jclass*>, facebook::jni::alias_ref<_jstring*>, facebook::jni::alias_ref<facebook::jni::JMap<facebook::jni::JString, facebook::jni::JString> >, 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<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, 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<false>(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.<init>+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&, art::ShadowFrame&, 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<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, 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<false>(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&, art::ShadowFrame&, 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<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, 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<false>(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&, art::ShadowFrame&, 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<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, 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<false>(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&, art::ShadowFrame&, 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<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, 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<false>(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&, art::ShadowFrame&, 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<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, 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<false>(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&, art::ShadowFrame&, 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)

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.

Also, did you follow all the steps in the tutorial? I would expect it to generate pt file

Thanks!
I checked and the path to the model. It exists. Below, you will find the code.

The log statement (Log.i(TAG, " - neuralNetworkloadAndRunPytorch - savedNetworkArchiveLength - "+savedNetworkArchiveLength);)
printing the length of the model reports a non-zero length.

I must be doing something else wrong.

I did follow the steps to produce a model for mobile. I used the torchscript method not trace.

CODE

			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);
			}

Hi Kimish,

The problem was caused by two facts:

  1. I did not have this

              // 
    

https://mvnrepository.com/artifact/com.facebook.fbjni/fbjni-java-only
implementation group: ‘com.facebook.fbjni’, name:
‘fbjni-java-only’, version: ‘0.2.2’

in my build.gradle

and

  1. I did not use pytorch_android_lite

In fact, I think it is important that all documentation about pytorch
mobile on Android be modified to state that the facebook library is
included among the dependencies, and. as important, that it is stated
CLEARLY that pytorch_android_lite should be used NOT pytorch_android.
This is true both on Android (i.e. loading the module with
LiteModuleLoader.load NOT Module.load), and when saving the model
produced with torchscript.

Thanks for your help,

Alex Donnini

Thanks for the feedback Alex.

Have you also checked Android | PyTorch?

What you point out is fair and I was surprised that some details are missing.

By the way we are also developing new solution for mobile that we announced this year at PTC. PyTorch ExecuTorch | PyTorch.

So our investment in the old stack will be somewhat limited

Hi Kimish,

Android/PyTorch was the first site I accessed when I started working on
running the model I am using in my Android app.

Since you are not investing in the old stack much any longer, should I
transition to ExecuTorch now?

Thanks,

Alex

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.

Hi Kimish,

I set up the ExecuTorch environment. The set-up instructions are very good.

Rather than exporting a sample model, I would prefer working with the
one I want to use in my Android application.

Is this


>>> import  executorch.exir  as  exir
>>> from  executorch.exir.tests.models  import  Mul
>>> m  =  Mul()
>>> print(exir.capture(m,  m.get_random_inputs()).to_edge())
>>> open("mul.pte",  "wb").write(exir.capture(m,  m.get_random_inputs()).to_edge().to_executorch().buffer)

the script I should use? If it is, I have a couple of questions:

  1. I should run the above script in my model’s top directory, right?
  2. I should replace “from executorch.exir.tests.models import Mul” wih
    the path to my model.py and the name of the class which implements my
    model, right?

If I am off-base, could you please point me in the right direction? The
“generate a smaple ExecuTorch program” section of
Setting Up ExecuTorch — ExecuTorch documentation does
not address explicitly how one would generate the corresponding
ExecuTorch program from one’s model.

Thanks,

Alex

For 1. Yes that is correct. I would also suggest that you take a look at examples export here https://github.com/pytorch/executorch/blob/main/examples/portable/scripts/export.py#L42, https://github.com/pytorch/executorch/tree/main/examples/portable and https://github.com/pytorch/executorch/blob/main/examples/portable/utils.py#L81

For 2. also correct.

Do note that for getting performant model you likely want to employ delegation. follow Building and Running ExecuTorch with XNNPACK Backend — ExecuTorch documentation

Dos this mean that I should go straight to using the xnnpack backend and
not follow the instructions in
https://pytorch.org/executorch/stable/getting-started-setup.html
?

Are the two approaches exclusive of each other?

Is using delegation just to get better performance?

Thanks

Hi Kimish,

I hope I am not bothering you (too much).

Could you please let me know what you think I might try to resolve the
problem caused by the missing torch._export? (please see below).

Thanks,

Alex

ERROR

Hi,

I commented out import torch._export.

Now the code fails with the error listed below.

I searched
https://pytorch.org/docs/stable/fx.html#api-reference
for passes, passes.infra and infra
No results returned

Any suggestions as to what to do next?

Thanks

ERROR

Hi Kimish,

Sorry for this additional message. I resolved all export issues. I
realized I had to ensure that all required pacakges are installed. Once
I did that, the export script worked (I ran into anoter issue but Ill
try to resolve it myself before contacting you).

Thanks,

Alex

Hi Kimish,

I am not sure where to post questions regarding Executorch. I tried the
Executorch category in the pytorch forum. Is that the right place?

Here is what I posted:

Hi,
I am trying to export my model for use in my Android app using Executorch.
I set up the executorch environment as per
https://pytorch.org/executorch/stable/getting-started-setup.html

I am confused as to how to proceed.

I tried using this

         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)

It failed with this error

Traceback (most recent call last):
   File 
"/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/trajectory-prediction-transformers-master/train.py", 
line 280, in <module>
     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'

Should I use this
https://pytorch.org/executorch/stable/tutorials/export-to-executorch-tutorial.html

I would appreciate guidance non how to proceed.

Thanks

Alex,
for future reference this is the category for executorch post on pytorch forums. ExecuTorch - PyTorch Forums

What is tf_model in your example? If you are following pattern of https://github.com/pytorch/executorch/blob/main/examples/models/inception_v3/model.py#L28, then the TFModel needs to defined get_random_inputs method.

Thanks. I did post in the executorch section of the pytorch forums.
There was no answer.

I will from now on.

I just responded to your message creating a new topic in the executorch
section of pytorch forums . I hope you will be able to respond. Thanks