Loading multifunction models on iOS causes a crash

I used the multifunction models feature introduced in iOS 18 to merge three VAE Encoder models with different resolutions into a single model. However, loading this merged model on iOS causes a crash with the error EXC_BAD_ACCESS (code=1, address=0x0). In contrast, merging VAE Decoder models using the same method does not result in crashes. Additionally, merging only two VAE Decoder models with different resolutions also leads to a crash when loaded on iOS. As for the Stable Diffusion Unet model, merging two or even three models does not cause any crashes, and it successfully generates images as expected.

I use the following code to load the model:

let config = MLModelConfiguration()
config.computeUnits = .cpuAndNeuralEngine
config.functionName = "test"
try MLModel(contentsOf: url, configuration: config)

Could you share the crash trace? You can also file a feedback assistant with sysdiagnose.

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Codes: 0x0000000000000001, 0x0000000000000000
VM Region Info: 0 is not in any region.  Bytes before following region: 4337270784
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                   102858000-10285c000 [   16K] r-x/r-x SM=COW  /var/containers/Bundle/Application/CA996A85-5FA3-4827-AF59-737092DAC9FD/Sd.app/Sd
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [1052]

Triggered by Thread:  6

Thread 0 name:   Dispatch queue: com.apple.main-thread
Thread 0:
0   libsystem_kernel.dylib        	       0x1d4bf2788 mach_msg2_trap + 8
1   libsystem_kernel.dylib        	       0x1d4bf5e98 mach_msg2_internal + 80
2   libsystem_kernel.dylib        	       0x1d4bf5db0 mach_msg_overwrite + 424
3   libsystem_kernel.dylib        	       0x1d4bf5bfc mach_msg + 24
4   CoreFoundation                	       0x18360a7f4 __CFRunLoopServiceMachPort + 160
5   CoreFoundation                	       0x183609ea0 __CFRunLoopRun + 1212
6   CoreFoundation                	       0x18365c274 CFRunLoopRunSpecific + 588
7   GraphicsServices              	       0x1d07d54c0 GSEventRunModal + 164
8   UIKitCore                     	       0x1861a277c -[UIApplication _run] + 816
9   UIKitCore                     	       0x185dc8e64 UIApplicationMain + 340
10  SwiftUI                       	       0x187f5c660 closure #1 in KitRendererCommon(_:) + 168
11  SwiftUI                       	       0x187f5c590 runApp<a>(_:) + 100
12  SwiftUI                       	       0x187f5c474 static App.main() + 180
13  Sd.debug.dylib                	       0x1038e2394 static SdApp.$main() + 152
14  Sd.debug.dylib                	       0x1038e2518 __debug_main_executable_dylib_entry_point + 12
15  dyld                          	       0x1a9830de8 start + 2724

Thread 1:
0   libsystem_kernel.dylib        	       0x1d4bf268c _kernelrpc_mach_port_destruct_trap + 8
1   libsystem_kernel.dylib        	       0x1d4bf8a44 mach_port_destruct + 40
2   libsystem_pthread.dylib       	       0x20e1fd16c _pthread_terminate + 220
3   libsystem_pthread.dylib       	       0x20e1fd08c _pthread_terminate_invoke + 92
4   libsystem_pthread.dylib       	       0x20e1fcd50 _pthread_exit + 112
5   libsystem_pthread.dylib       	       0x20e1fcce0 _pthread_wqthread_exit + 56
6   libsystem_pthread.dylib       	       0x20e1fb708 _pthread_wqthread + 424
7   libsystem_pthread.dylib       	       0x20e1f9474 start_wqthread + 8

Thread 2:
0   libsystem_pthread.dylib       	       0x20e1f946c start_wqthread + 0

Thread 3 name:   Dispatch queue: com.apple.root.default-qos
Thread 3:
0   libxpc.dylib                  	       0x20e252750 xpc_connection_send_notification + 0
1   Foundation                    	       0x18223aac0 -[NSXPCConnection _sendDesistForProxy:] + 156
2   Foundation                    	       0x18223a488 -[_NSXPCDistantObject dealloc] + 60
3   libobjc.A.dylib               	       0x180b11bac object_cxxDestructFromClass(objc_object*, objc_class*) + 116
4   libobjc.A.dylib               	       0x180b10f4c objc_destructInstance + 80
5   libobjc.A.dylib               	       0x180b10eec _objc_rootDealloc + 80
6   libobjc.A.dylib               	       0x180b11bac object_cxxDestructFromClass(objc_object*, objc_class*) + 116
7   libobjc.A.dylib               	       0x180b10f4c objc_destructInstance + 80
8   libobjc.A.dylib               	       0x180b10eec _objc_rootDealloc + 80
9   libsystem_blocks.dylib        	       0x20e1f6858 bool HelperBase::disposeCapture<(HelperBase::BlockCaptureKind)3>(unsigned int, unsigned char*) + 68
10  libsystem_blocks.dylib        	       0x20e1f650c HelperBase::destroyBlock(Block_layout*, bool, unsigned char*) + 160
11  libsystem_blocks.dylib        	       0x20e1f5eac _call_dispose_helpers_excp + 72
12  libsystem_blocks.dylib        	       0x20e1f5e48 _Block_release + 256
13  libobjc.A.dylib               	       0x180b11bac object_cxxDestructFromClass(objc_object*, objc_class*) + 116
14  libobjc.A.dylib               	       0x180b10f4c objc_destructInstance + 80
15  libobjc.A.dylib               	       0x180b10eec _objc_rootDealloc + 80
16  UIKitCore                     	       0x1872c3b24 -[_UIAsyncInvocation dealloc] + 88
17  libsystem_blocks.dylib        	       0x20e1f6858 bool HelperBase::disposeCapture<(HelperBase::BlockCaptureKind)3>(unsigned int, unsigned char*) + 68
18  libsystem_blocks.dylib        	       0x20e1f650c HelperBase::destroyBlock(Block_layout*, bool, unsigned char*) + 160
19  libsystem_blocks.dylib        	       0x20e1f5eac _call_dispose_helpers_excp + 72
20  libsystem_blocks.dylib        	       0x20e1f5e48 _Block_release + 256
21  libsystem_blocks.dylib        	       0x20e1f5d0c bool HelperBase::disposeCapture<(HelperBase::BlockCaptureKind)4>(unsigned int, unsigned char*) + 68
22  libsystem_blocks.dylib        	       0x20e1f6520 HelperBase::destroyBlock(Block_layout*, bool, unsigned char*) + 180
23  libsystem_blocks.dylib        	       0x20e1f5eac _call_dispose_helpers_excp + 72
24  libsystem_blocks.dylib        	       0x20e1f5e48 _Block_release + 256
25  libdispatch.dylib             	       0x103420944 _dispatch_source_handler_dispose + 36
26  libdispatch.dylib             	       0x103420e20 _dispatch_source_latch_and_call + 616
27  libdispatch.dylib             	       0x10341f7d8 _dispatch_source_invoke + 864
28  libdispatch.dylib             	       0x103408ed0 _dispatch_queue_override_invoke + 564
29  libdispatch.dylib             	       0x10341ad34 _dispatch_root_queue_drain + 404
30  libdispatch.dylib             	       0x10341b724 _dispatch_worker_thread2 + 188
31  libsystem_pthread.dylib       	       0x20e1fb644 _pthread_wqthread + 228
32  libsystem_pthread.dylib       	       0x20e1f9474 start_wqthread + 8

Thread 4 name:   Dispatch queue: com.apple.root.default-qos
Thread 4:
0   libxpc.dylib                  	       0x20e252750 xpc_connection_send_notification + 0
1   Foundation                    	       0x18223aac0 -[NSXPCConnection _sendDesistForProxy:] + 156
2   Foundation                    	       0x18223a488 -[_NSXPCDistantObject dealloc] + 60
3   libobjc.A.dylib               	       0x180b11bac object_cxxDestructFromClass(objc_object*, objc_class*) + 116
4   libobjc.A.dylib               	       0x180b10f4c objc_destructInstance + 80
5   libobjc.A.dylib               	       0x180b10eec _objc_rootDealloc + 80
6   libobjc.A.dylib               	       0x180b11bac object_cxxDestructFromClass(objc_object*, objc_class*) + 116
7   libobjc.A.dylib               	       0x180b10f4c objc_destructInstance + 80
8   libobjc.A.dylib               	       0x180b10eec _objc_rootDealloc + 80
9   libsystem_blocks.dylib        	       0x20e1f6858 bool HelperBase::disposeCapture<(HelperBase::BlockCaptureKind)3>(unsigned int, unsigned char*) + 68
10  libsystem_blocks.dylib        	       0x20e1f650c HelperBase::destroyBlock(Block_layout*, bool, unsigned char*) + 160
11  libsystem_blocks.dylib        	       0x20e1f5eac _call_dispose_helpers_excp + 72
12  libsystem_blocks.dylib        	       0x20e1f5e48 _Block_release + 256
13  libobjc.A.dylib               	       0x180b11bac object_cxxDestructFromClass(objc_object*, objc_class*) + 116
14  libobjc.A.dylib               	       0x180b10f4c objc_destructInstance + 80
15  libobjc.A.dylib               	       0x180b10eec _objc_rootDealloc + 80
16  UIKitCore                     	       0x1872c3b24 -[_UIAsyncInvocation dealloc] + 88
17  libsystem_blocks.dylib        	       0x20e1f6858 bool HelperBase::disposeCapture<(HelperBase::BlockCaptureKind)3>(unsigned int, unsigned char*) + 68
18  libsystem_blocks.dylib        	       0x20e1f650c HelperBase::destroyBlock(Block_layout*, bool, unsigned char*) + 160
19  libsystem_blocks.dylib        	       0x20e1f5eac _call_dispose_helpers_excp + 72
20  libsystem_blocks.dylib        	       0x20e1f5e48 _Block_release + 256
21  libsystem_blocks.dylib        	       0x20e1f5d0c bool HelperBase::disposeCapture<(HelperBase::BlockCaptureKind)4>(unsigned int, unsigned char*) + 68
22  libsystem_blocks.dylib        	       0x20e1f6520 HelperBase::destroyBlock(Block_layout*, bool, unsigned char*) + 180
23  libsystem_blocks.dylib        	       0x20e1f5eac _call_dispose_helpers_excp + 72
24  libsystem_blocks.dylib        	       0x20e1f5e48 _Block_release + 256
25  libdispatch.dylib             	       0x103420944 _dispatch_source_handler_dispose + 36
26  libdispatch.dylib             	       0x103420e20 _dispatch_source_latch_and_call + 616
27  libdispatch.dylib             	       0x10341f7d8 _dispatch_source_invoke + 864
28  libdispatch.dylib             	       0x103408ed0 _dispatch_queue_override_invoke + 564
29  libdispatch.dylib             	       0x10341ad34 _dispatch_root_queue_drain + 404
30  libdispatch.dylib             	       0x10341b724 _dispatch_worker_thread2 + 188
31  libsystem_pthread.dylib       	       0x20e1fb644 _pthread_wqthread + 228
32  libsystem_pthread.dylib       	       0x20e1f9474 start_wqthread + 8

Thread 5 name:  com.apple.uikit.eventfetch-thread
Thread 5:
0   libsystem_kernel.dylib        	       0x1d4bf2788 mach_msg2_trap + 8
1   libsystem_kernel.dylib        	       0x1d4bf5e98 mach_msg2_internal + 80
2   libsystem_kernel.dylib        	       0x1d4bf5db0 mach_msg_overwrite + 424
3   libsystem_kernel.dylib        	       0x1d4bf5bfc mach_msg + 24
4   CoreFoundation                	       0x18360a7f4 __CFRunLoopServiceMachPort + 160
5   CoreFoundation                	       0x183609ea0 __CFRunLoopRun + 1212
6   CoreFoundation                	       0x18365c274 CFRunLoopRunSpecific + 588
7   Foundation                    	       0x1821c2b48 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212
8   Foundation                    	       0x18231f6f4 -[NSRunLoop(NSRunLoop) runUntilDate:] + 64
9   UIKitCore                     	       0x186235b80 -[UIEventFetcher threadMain] + 420
10  Foundation                    	       0x1822aea54 __NSThread__start__ + 724
11  libsystem_pthread.dylib       	       0x20e1f97d0 _pthread_start + 136
12  libsystem_pthread.dylib       	       0x20e1f9480 thread_start + 8

Thread 6 Crashed:
0   Espresso                      	       0x192b64124 Espresso::AOT::DispatchUnitToBlock(std::__1::shared_ptr, MIL::IRFunction const&, unsigned long const&) + 160
1   Espresso                      	       0x192b61804 Espresso::AOT::SegmenterShortestPath::BuildStateGraph(MIL::IRProgram const&, std::__1::basic_string, std::__1::allocator> const&) const + 1684
2   Espresso                      	       0x192be1718 Espresso::AOT::SegmenterShortestPath::SegmentFunction(MIL::IRProgram const&, std::__1::basic_string, std::__1::allocator> const&) const + 44
3   Espresso                      	       0x192bdf7d4 Espresso::AOT::SegmenterShortestPath::Run(MIL::IRProgram const&, std::__1::vector, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&) const + 324
4   Espresso                      	       0x192c26d50 Espresso::AOT::MILCompilerForE5::Run(MIL::IRProgram const&, std::__1::vector, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&) + 3040
5   Espresso                      	       0x192ba7fd0 Espresso::AOT::AbstractMILCompiler::Run() + 808
6   Espresso                      	       0x192ba7c3c Espresso::AOT::E5AOTCompilerBackend::Run() + 88
7   Espresso                      	       0x192b86180 E5RT::OnDeviceAOTCompilerInterface::Compile(std::__1::basic_string, std::__1::allocator> const&, std::__1::basic_string, std::__1::allocator> const&, E5RT::E5CompilerOptions const&, MIL::IRProgram const*) + 272
8   Espresso                      	       0x192c7631c E5RT::E5CompilerImpl::CompileInternal(std::__1::basic_string, std::__1::allocator> const&, E5RT::E5CompilerOptions const&, MIL::IRProgram const*) + 788
9   Espresso                      	       0x192b49f7c std::__1::unique_ptr> E5RT::E5CompilerImpl::Compile(MIL::IRProgram const&, E5RT::E5CompilerOptions const&) + 136
10  Espresso                      	       0x192cf17d0 0x192acf000 + 2238416
11  Espresso                      	       0x192b44724 E5RT::ExceptionSafeExecute(std::__1::function) + 60
12  Espresso                      	       0x192c1af7c e5rt_e5_compiler_compile_from_ir_program + 116
13  CoreML                        	       0x19efc8738 -[MLE5ProgramLibraryOnDeviceAOTCompilationImpl createProgramLibraryHandleWithRespecialization:error:] + 4080
14  CoreML                        	       0x19efc58f8 -[MLE5ProgramLibrary _programLibraryHandleWithForceRespecialization:error:] + 96
15  CoreML                        	       0x19efc5690 __44-[MLE5ProgramLibrary prepareAndReturnError:]_block_invoke + 60
16  libdispatch.dylib             	       0x103406578 _dispatch_client_callout + 20
17  libdispatch.dylib             	       0x103417f70 _dispatch_lane_barrier_sync_invoke_and_complete + 176
18  CoreML                        	       0x19efd0048 -[MLE5ProgramLibrary prepareAndReturnError:] + 220
19  CoreML                        	       0x19efcfd68 -[MLE5Engine initWithContainer:configuration:error:] + 220
20  CoreML                        	       0x19efcfa30 +[MLE5Engine loadModelFromCompiledArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] + 344
21  CoreML                        	       0x19efaf538 +[MLLoader _loadModelWithClass:fromArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] + 364
22  CoreML                        	       0x19efaedd8 +[MLLoader _loadModelFromArchive:configuration:modelVersion:compilerVersion:loaderEvent:useUpdatableModelLoaders:loadingClasses:error:] + 540
23  CoreML                        	       0x19efc0b58 +[MLLoader _loadWithModelLoaderFromArchive:configuration:loaderEvent:useUpdatableModelLoaders:error:] + 424
24  CoreML                        	       0x19efafa58 +[MLLoader _loadModelFromArchive:configuration:loaderEvent:useUpdatableModelLoaders:error:] + 460
25  CoreML                        	       0x19efc5de0 +[MLLoader _loadModelFromAssetAtURL:configuration:loaderEvent:error:] + 240
26  CoreML                        	       0x19efc5b08 +[MLLoader loadModelFromAssetAtURL:configuration:error:] + 104
27  CoreML                        	       0x19efc5a30 -[MLModelAssetResourceFactoryOnDiskImpl modelWithConfiguration:error:] + 116
28  CoreML                        	       0x19efc596c __60-[MLModelAssetResourceFactory modelWithConfiguration:error:]_block_invoke + 72
29  libdispatch.dylib             	       0x103406578 _dispatch_client_callout + 20
30  libdispatch.dylib             	       0x103417f70 _dispatch_lane_barrier_sync_invoke_and_complete + 176
31  CoreML                        	       0x19efc6bfc -[MLModelAssetResourceFactory modelWithConfiguration:error:] + 276
32  CoreML                        	       0x19efc69f8 -[MLModelAssetModelVendor modelWithConfiguration:error:] + 152
33  CoreML                        	       0x19efc67bc -[MLModelAsset modelWithConfiguration:error:] + 112
34  CoreML                        	       0x19efc653c +[MLModel modelWithContentsOfURL:configuration:error:] + 168
35  Sd.debug.dylib                	       0x1038c0e94 @nonobjc MLModel.__allocating_init(contentsOf:configuration:) + 168
36  Sd.debug.dylib                	       0x1038c0b08 closure #1 in closure #2 in GenPage.body.getter + 276
37  Sd.debug.dylib                	       0x1038c64e5 partial apply for closure #1 in closure #2 in GenPage.body.getter + 1
38  Sd.debug.dylib                	       0x103865a0d thunk for @escaping @isolated(any) @callee_guaranteed @async () -> (@out A) + 1
39  Sd.debug.dylib                	       0x103899969 partial apply for thunk for @escaping @isolated(any) @callee_guaranteed @async () -> (@out A) + 1
40  libswift_Concurrency.dylib    	       0x18eed2e39 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1

Thread 7 name:   Dispatch queue: CA DispatchGroup
Thread 7:
0   libsystem_kernel.dylib        	       0x1d4bf25fc _kernelrpc_mach_vm_deallocate_trap + 8
1   libsystem_kernel.dylib        	       0x1d4bf6bd8 mach_vm_deallocate + 88
2   QuartzCore                    	       0x18515e110 CA::Render::Shmem::~Shmem() + 112
3   QuartzCore                    	       0x18515e07c CA::Render::Shmem::~Shmem() + 24
4   QuartzCore                    	       0x185178d60 CABackingStoreDeleteBuffer(CABackingStoreBuffer*) + 100
5   QuartzCore                    	       0x185179b44 CABackingStoreCollect_(double, bool) + 380
6   QuartzCore                    	       0x185179954 CABackingStoreCollect + 40
7   QuartzCore                    	       0x1851798d4 async_collect_callback(void*) + 44
8   libdispatch.dylib             	       0x103406578 _dispatch_client_callout + 20
9   libdispatch.dylib             	       0x10340e454 _dispatch_lane_serial_drain + 840
10  libdispatch.dylib             	       0x10340f25c _dispatch_lane_invoke + 408
11  libdispatch.dylib             	       0x10341c6fc _dispatch_root_queue_drain_deferred_wlh + 328
12  libdispatch.dylib             	       0x10341bd0c _dispatch_workloop_worker_thread + 580
13  libsystem_pthread.dylib       	       0x20e1fb680 _pthread_wqthread + 288
14  libsystem_pthread.dylib       	       0x20e1f9474 start_wqthread + 8

Thread 8:
0   libsystem_kernel.dylib        	       0x1d4bf2704 semaphore_wait_trap + 8
1   libdispatch.dylib             	       0x103406ca0 _dispatch_sema4_wait + 28
2   libdispatch.dylib             	       0x103407354 _dispatch_semaphore_wait_slow + 132
3   UIKitCore                     	       0x1862bda40 0x185db4000 + 5282368
4   UIKitCore                     	       0x1860ad610 0x185db4000 + 3118608
5   Foundation                    	       0x1822aea54 __NSThread__start__ + 724
6   libsystem_pthread.dylib       	       0x20e1f97d0 _pthread_start + 136
7   libsystem_pthread.dylib       	       0x20e1f9480 thread_start + 8

Thread 9 name:  com.apple.SwiftUI.AsyncRenderer
Thread 9:
0   libsystem_kernel.dylib        	       0x1d4bf2788 mach_msg2_trap + 8
1   libsystem_kernel.dylib        	       0x1d4bf5e98 mach_msg2_internal + 80
2   libsystem_kernel.dylib        	       0x1d4bf5db0 mach_msg_overwrite + 424
3   libsystem_kernel.dylib        	       0x1d4bf5bfc mach_msg + 24
4   CoreFoundation                	       0x18360a7f4 __CFRunLoopServiceMachPort + 160
5   CoreFoundation                	       0x183609ea0 __CFRunLoopRun + 1212
6   CoreFoundation                	       0x18365c274 CFRunLoopRunSpecific + 588
7   Foundation                    	       0x1821c2b48 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212
8   Foundation                    	       0x1821bed10 -[NSRunLoop(NSRunLoop) run] + 64
9   SwiftUI                       	       0x18803ff60 specialized static DisplayLink.asyncThread(arg:) + 792
10  SwiftUI                       	       0x18803fc30 @objc static DisplayLink.asyncThread(arg:) + 72
11  Foundation                    	       0x1822aea54 __NSThread__start__ + 724
12  libsystem_pthread.dylib       	       0x20e1f97d0 _pthread_start + 136
13  libsystem_pthread.dylib       	       0x20e1f9480 thread_start + 8


Thread 6 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x00000001112ecdc0   x2: 0x00000001049c4970   x3: 0x0000000301bc80c0
    x4: 0x0000000301bc8080   x5: 0x00000000da40495c   x6: 0x000000030211eb60   x7: 0x00000003025c4e20
    x8: 0x00000000000035a0   x9: 0x000000010f890000  x10: 0x000000000000003f  x11: 0x0000000000000000
   x12: 0x00000000000007fb  x13: 0x00000000000007fd  x14: 0x00000000c9605801  x15: 0x00000000c9405022
   x16: 0x000000026c601a00  x17: 0x0a5200026c601a00  x18: 0x0000000000000000  x19: 0x000000016da00e60
   x20: 0x00000001049c4970  x21: 0x0000000302572ec0  x22: 0x0000000000000001  x23: 0x0000000000000028
   x24: 0xffffffffffffffff  x25: 0x00000001049c4978  x26: 0x00000003026c1f20  x27: 0x00000001937c92b0
   x28: 0x00000001049c4970   fp: 0x000000016da00d50   lr: 0x0000000192b6410c
    sp: 0x000000016da00c20   pc: 0x0000000192b64124 cpsr: 0x80000000
   far: 0x0000000000000000  esr: 0x92000006 (Data Abort) byte read Translation fault</a>

I also uploaded the model files and crash logs on Feedback Assistant.

Are you converting from Pytorch and does your model use torch.nn.functional.scaled_dot_product_attention method? I found that that method causes issues with multifunction models and you should use manual computation of attention instead

Loading multifunction models on iOS causes a crash
 
 
Q