apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.34k stars 626 forks source link

Network that loaded fine now crashes after updating to Xcode 12.0 #932

Closed kretash closed 1 year ago

kretash commented 4 years ago

❓Question

I had this mlmodel generated with coremltools 3.4, it's now generated with coremltools 4.0b3. The same issue with both versions.

I have tested this on 2 devices, iPhone 11 with iOS 14 and an iPhone 11 Pro with iOS 13.7.

The model has custom layers, but there is no signs that this might be the issue.

The model crashes when loading in this generated function with Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

    /**
        Construct a model with URL of the .mlmodelc directory and configuration

        - parameters:
           - modelURL: the file url of the model
           - configuration: the desired model configuration

        - throws: an NSError object that describes the problem
    */
    @available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 5.0, *)
    convenience init(contentsOf modelURL: URL, configuration: MLModelConfiguration) throws {
        try self.init(model: MLModel(contentsOf: modelURL, configuration: configuration))
    }

There are no console logs available with extra information.

This is how I'm initializing the model. The do catch statement does not work, I'm guessing you can't catch EXC_BAD_ACCESS.

        let conf = MLModelConfiguration()
        do {
            model = try HairSegmentationNetwork(configuration: conf)
        } catch {
            print(error.localizedDescription)
        }

Any help is appreciated!

anilkatti commented 4 years ago

@kretash We are looking into this and will get back to you soon.

anilkatti commented 4 years ago

@kretash would it be possible for you share your project and model for us to repro? If that's sensitive, could you share the crash stack?

aseemw commented 4 years ago

@kretash you could also share the coreml model via a bug report (https://developer.apple.com/bug-reporting/) or a variant of the model that has the same issue.

kretash commented 4 years ago

I'm trying to get a simplified version of my network crashing too, but this is the crash stack.

Thanks!

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00000001951b6c6c Espresso`Espresso::ANECompilerEngine::is_valid_for_engine_conv(Espresso::convolution_uniforms const&, Espresso::base_kernel::validate_for_engine_args_t const&, std::__1::shared_ptr<Espresso::kernels_validation_status_t>, bool) + 224
    frame #1: 0x00000001951b9f50 Espresso`Espresso::ANECompilerEngine::deconvolution_kernel::is_valid_for_engine(std::__1::shared_ptr<Espresso::kernels_validation_status_t>, Espresso::base_kernel::validate_for_engine_args_t const&) const + 76
    frame #2: 0x0000000195252570 Espresso`is_kernel_available_on_engine(unsigned long, std::__1::shared_ptr<Espresso::base_kernel>, Espresso::kernel_info_t const&, std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::shared_ptr<Espresso::abstract_context>, Espresso::compute_path, std::__1::shared_ptr<Espresso::net_info_ir_t>, std::__1::shared_ptr<Espresso::kernels_validation_status_t>) + 268
    frame #3: 0x00000001952520c0 Espresso`Espresso::cpu_context_transfer_algo_t::check_all_kernels_availability_on_context(std::__1::vector<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::allocator<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> > > const&, std::__1::shared_ptr<Espresso::abstract_context>&, Espresso::compute_path, std::__1::shared_ptr<Espresso::net_info_ir_t>&) + 296
    frame #4: 0x00000001952528e0 Espresso`Espresso::cpu_context_transfer_algo_t::create_net_info_ir(std::__1::vector<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::allocator<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> > > const&, std::__1::shared_ptr<Espresso::abstract_context>, Espresso::network_shape const&, Espresso::compute_path, std::__1::shared_ptr<Espresso::net_info_ir_t>) + 108
    frame #5: 0x00000001952b4038 Espresso`Espresso::get_net_info_ir(std::__1::shared_ptr<Espresso::abstract_context>, std::__1::shared_ptr<Espresso::net>, std::__1::vector<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::allocator<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> > > const&, Espresso::network_shape const&, Espresso::compute_path const&, Espresso::platform const&, Espresso::compute_path const&, std::__1::shared_ptr<Espresso::cpu_context_transfer_algo_t>&, std::__1::shared_ptr<Espresso::net_info_ir_t>&, std::__1::shared_ptr<Espresso::kernels_validation_status_t>&) + 736
    frame #6: 0x00000001952b59dc Espresso`Espresso::run_dispatch_v2(std::__1::shared_ptr<Espresso::abstract_context>, std::__1::shared_ptr<Espresso::net>, std::__1::vector<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::allocator<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> > > const&, Espresso::network_shape const&, Espresso::compute_path const&, std::__1::basic_istream<char, std::__1::char_traits<char> >*) + 3200
    frame #7: 0x00000001952c4be8 Espresso`Espresso::load_network_layers_internal(std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<Espresso::abstract_context> const&, Espresso::network_shape const&, std::__1::basic_istream<char, std::__1::char_traits<char> >*, Espresso::compute_path, bool, std::__1::shared_ptr<Espresso::blob_storage_abstract> const&) + 244
    frame #8: 0x00000001952c0fd4 Espresso`Espresso::load_and_shape_network(std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<Espresso::abstract_context> const&, Espresso::network_shape const&, Espresso::compute_path, std::__1::shared_ptr<Espresso::blob_storage_abstract> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 508
    frame #9: 0x00000001952c3a94 Espresso`Espresso::load_network(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<Espresso::abstract_context> const&, Espresso::compute_path, bool) + 1892
    frame #10: 0x00000001951ea520 Espresso`EspressoLight::espresso_plan::add_network(char const*, espresso_storage_type_t) + 304
    frame #11: 0x00000001951f7ba4 Espresso`espresso_plan_add_network + 340
    frame #12: 0x0000000194dbecb0 CoreML`-[MLNeuralNetworkEngine _setupContextAndPlanWithConfiguration:usingCPU:error:] + 752
    frame #13: 0x0000000194dc008c CoreML`-[MLNeuralNetworkEngine initWithContainer:configuration:error:] + 384
    frame #14: 0x0000000194dc5124 CoreML`+[MLNeuralNetworkEngine loadModelFromCompiledArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] + 208
    frame #15: 0x0000000194e16610 CoreML`+[MLLoader loadModelFromArchive:configuration:loaderEvent:error:] + 624
    frame #16: 0x0000000194e179c0 CoreML`+[MLLoader loadModelFromAssetAtURL:configuration:loaderEvent:error:] + 280
    frame #17: 0x0000000194e17c54 CoreML`+[MLLoader loadModelFromAssetAtURL:configuration:error:] + 120
    frame #18: 0x0000000194dfea2c CoreML`-[MLModelAsset load:] + 220
    frame #19: 0x0000000194dfe83c CoreML`-[MLModelAsset modelWithError:] + 68
    frame #20: 0x0000000194e40138 CoreML`+[MLModel modelWithContentsOfURL:configuration:error:] + 156
    frame #21: 0x0000000105870b90 OctiDev`@nonobjc MLModel.__allocating_init(contentsOf:configuration:) at <compiler-generated>:0
  * frame #22: 0x0000000105870a20 OctiDev`HairSegmentationNetwork.__allocating_init(modelURL=Foundation.URL @ 0x000000016b18f510, configuration=0x00000002805bb340) at HairSegmentationNetwork.swift:135:30
    frame #23: 0x00000001058708b0 OctiDev`HairSegmentationNetwork.__allocating_init(configuration=0x00000002805bb340) at HairSegmentationNetwork.swift:110:18
    frame #24: 0x0000000104dc3198 OctiDev`HairSegmentation.init(self=0x00000002833e7de0) at HairSegmentation.swift:41:25
    frame #25: 0x0000000104dc2ca0 OctiDev`HairSegmentation.__allocating_init() at HairSegmentation.swift:0
    frame #26: 0x00000001058172b0 OctiDev`ViewController.init(coder=0x000000010b2c1200) at ViewController.swift:23:46
    frame #27: 0x000000010581756c OctiDev`@objc ViewController.init(coder:) at <compiler-generated>:0
    frame #28: 0x0000000185fcec64 UIKitCore`-[UIClassSwapper initWithCoder:] + 2096
    frame #29: 0x000000018ca56190 UIFoundation`UINibDecoderDecodeObjectForValue + 700
    frame #30: 0x000000018c9e32c4 UIFoundation`-[UINibDecoder decodeObjectForKey:] + 312
    frame #31: 0x0000000185fd2fe0 UIKitCore`-[UIRuntimeConnection initWithCoder:] + 132
    frame #32: 0x000000018ca56190 UIFoundation`UINibDecoderDecodeObjectForValue + 700
    frame #33: 0x000000018ca563a8 UIFoundation`UINibDecoderDecodeObjectForValue + 1236
    frame #34: 0x000000018c9e32c4 UIFoundation`-[UINibDecoder decodeObjectForKey:] + 312
    frame #35: 0x0000000185fcde48 UIKitCore`-[NSCoder(UIIBDependencyInjectionInternal) _decodeObjectsWithSourceSegueTemplate:creator:sender:forKey:] + 492
    frame #36: 0x0000000185fd0860 UIKitCore`-[UINib instantiateWithOwner:options:] + 1068
    frame #37: 0x0000000186528218 UIKitCore`-[UIStoryboard __reallyInstantiateViewControllerWithIdentifier:creator:storyboardSegueTemplate:sender:] + 284
    frame #38: 0x000000010570c790 OctiDev`HomeCameraViewController.flipCamera(self=0x000000010a86ea00) at HomeCameraViewController.swift:394:45
    frame #39: 0x0000000105719090 OctiDev`HomeCameraViewController.homeCameraControlsViewDidSelectFlipCamera(view=0x000000010a75fc60, self=0x000000010a86ea00) at HomeCameraViewController.swift:1042:9
    frame #40: 0x000000010571c504 OctiDev`protocol witness for HomeCameraControlsViewDelegate.homeCameraControlsViewDidSelectFlipCamera(_:) in conformance HomeCameraViewController at <compiler-generated>:0
    frame #41: 0x00000001055f3af4 OctiDev`HomeCameraControlsView.flipCameraPressed(self=0x000000010a75fc60) at HomeCameraControlsView.swift:479:19
    frame #42: 0x00000001055f3b38 OctiDev`@objc HomeCameraControlsView.flipCameraPressed() at <compiler-generated>:0
    frame #43: 0x00000001863b35a0 UIKitCore`-[UIApplication sendAction:to:from:forEvent:] + 100
    frame #44: 0x0000000185d118d4 UIKitCore`-[UIControl sendAction:to:forEvent:] + 208
    frame #45: 0x0000000185d11c28 UIKitCore`-[UIControl _sendActionsForEvents:withEvent:] + 356
    frame #46: 0x0000000185d104f8 UIKitCore`-[UIControl touchesEnded:withEvent:] + 536
    frame #47: 0x00000001863f0c04 UIKitCore`-[UIWindow _sendTouchesForEvent:] + 988
    frame #48: 0x00000001863f256c UIKitCore`-[UIWindow sendEvent:] + 3976
    frame #49: 0x00000001863cc190 UIKitCore`-[UIApplication sendEvent:] + 712
    frame #50: 0x0000000186453560 UIKitCore`__dispatchPreprocessedEventFromEventQueue + 7360
    frame #51: 0x00000001864564b8 UIKitCore`__processEventQueue + 6460
    frame #52: 0x000000018644da3c UIKitCore`__eventFetcherSourceCallback + 160
    frame #53: 0x0000000183a0f81c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
    frame #54: 0x0000000183a0f718 CoreFoundation`__CFRunLoopDoSource0 + 208
    frame #55: 0x0000000183a0ea28 CoreFoundation`__CFRunLoopDoSources0 + 268
    frame #56: 0x0000000183a08d20 CoreFoundation`__CFRunLoopRun + 824
    frame #57: 0x0000000183a084bc CoreFoundation`CFRunLoopRunSpecific + 600
    frame #58: 0x000000019a48d820 GraphicsServices`GSEventRunModal + 164
    frame #59: 0x00000001863ac734 UIKitCore`-[UIApplication _run] + 1072
    frame #60: 0x00000001863b1e10 UIKitCore`UIApplicationMain + 168
    frame #61: 0x0000000105803da0 OctiDev`main at AppDelegate.swift:17:26
    frame #62: 0x00000001836cfe60 libdyld.dylib`start + 4
kretash commented 4 years ago

Hi guys, any updates on this issue or a way to get around it for now?

kretash commented 3 years ago

I got it to work by compiling the mlmodel to mlmodelc in Xcode11 and then loading it in the .cpuAndGPU instead of .all

Select Xcode11 sudo xcode-select -s /Applications/Xcode11.7.app/Contents/Developer

Compile the Model xcrun coremlc compile Network.mlmodel .

rename Network.mlmodelc to Network_Xcode11.mlmodelc

Load the mlmodelc

        let config = MLModelConfiguration()
        config.computeUnits = .cpuAndGPU
        do {
            let url = Bundle.main.url(forResource: "Network_Xcode11", withExtension: "mlmodelc")!
            let model = try MLModel(contentsOf: url, configuration: config )
            print( model )
        } catch { print("Unexpected error: \(error).")}
kretash commented 3 years ago

After updating to iOS 14.1 and Xcode 12.1 the model that i built in the previous post now runs at CPU speeds. The standard mlmodel still crashes when loading.

ab-zephyr commented 3 years ago

Almost the same here using Xcode 12.4 (12.5 beta). Model with custom layer loads only in cpu or cpuAndGPU mode, but not 'all' mode.

Thread 4 Queue : com.zm.video.url.generator (serial)
#0  0x00000001b6392ab4 in Espresso::ANECompilerEngine::mix_elementwise_kernel::is_valid_for_engine(std::__1::shared_ptr<Espresso::kernels_validation_status_t>, Espresso::base_kernel::validate_for_engine_args_t const&) const ()
#1  0x00000001b612b890 in is_kernel_available_on_engine(unsigned long, std::__1::shared_ptr<Espresso::base_kernel>, Espresso::kernel_info_t const&, std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::shared_ptr<Espresso::abstract_context>, Espresso::compute_path, std::__1::shared_ptr<Espresso::net_info_ir_t>, std::__1::shared_ptr<Espresso::kernels_validation_status_t>) ()
#2  0x00000001b612b3e0 in Espresso::cpu_context_transfer_algo_t::check_all_kernels_availability_on_context(std::__1::vector<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::allocator<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> > > const&, std::__1::shared_ptr<Espresso::abstract_context>&, Espresso::compute_path, std::__1::shared_ptr<Espresso::net_info_ir_t>&) ()
#3  0x00000001b612bc00 in Espresso::cpu_context_transfer_algo_t::create_net_info_ir(std::__1::vector<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::allocator<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> > > const&, std::__1::shared_ptr<Espresso::abstract_context>, Espresso::network_shape const&, Espresso::compute_path, std::__1::shared_ptr<Espresso::net_info_ir_t>) ()
#4  0x00000001b618f17c in Espresso::get_net_info_ir(std::__1::shared_ptr<Espresso::abstract_context>, std::__1::shared_ptr<Espresso::net>, std::__1::vector<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::allocator<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> > > const&, Espresso::network_shape const&, Espresso::compute_path const&, Espresso::platform const&, Espresso::compute_path const&, std::__1::shared_ptr<Espresso::cpu_context_transfer_algo_t>&, std::__1::shared_ptr<Espresso::net_info_ir_t>&, std::__1::shared_ptr<Espresso::kernels_validation_status_t>&) ()
#5  0x00000001b6190b80 in Espresso::run_dispatch_v2(std::__1::shared_ptr<Espresso::abstract_context>, std::__1::shared_ptr<Espresso::net>, std::__1::vector<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::allocator<std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> > > const&, Espresso::network_shape const&, Espresso::compute_path const&, std::__1::basic_istream<char, std::__1::char_traits<char> >*) ()
#6  0x00000001b61a0470 in Espresso::load_network_layers_internal(std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<Espresso::abstract_context> const&, Espresso::network_shape const&, std::__1::basic_istream<char, std::__1::char_traits<char> >*, Espresso::compute_path, bool, std::__1::shared_ptr<Espresso::blob_storage_abstract> const&) ()
#7  0x00000001b619c7c8 in Espresso::load_and_shape_network(std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<Espresso::abstract_context> const&, Espresso::network_shape const&, Espresso::compute_path, std::__1::shared_ptr<Espresso::blob_storage_abstract> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) ()
#8  0x00000001b619f31c in Espresso::load_network(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<Espresso::abstract_context> const&, Espresso::compute_path, bool) ()
#9  0x00000001b60c0e60 in EspressoLight::espresso_plan::add_network(char const*, espresso_storage_type_t) ()
#10 0x00000001b60cf2d0 in espresso_plan_add_network ()
#11 0x00000001b5c9036c in -[MLNeuralNetworkEngine _setupContextAndPlanWithConfiguration:usingCPU:error:] ()
#12 0x00000001b5c91754 in -[MLNeuralNetworkEngine initWithContainer:configuration:error:] ()
#13 0x00000001b5c968e8 in +[MLNeuralNetworkEngine loadModelFromCompiledArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] ()
#14 0x00000001b5ce8cb0 in +[MLLoader loadModelFromArchive:configuration:loaderEvent:error:] ()
#15 0x00000001b5cea1ec in +[MLLoader loadModelFromAssetAtURL:configuration:loaderEvent:error:] ()
#16 0x00000001b5cea48c in +[MLLoader loadModelFromAssetAtURL:configuration:error:] ()
#17 0x00000001b5cd0acc in -[MLModelAsset load:] ()
#18 0x00000001b5cd08d0 in -[MLModelAsset modelWithError:] ()
#19 0x00000001b5d1328c in +[MLModel modelWithContentsOfURL:configuration:error:] ()
#20 0x0000000100beeed4 in @nonobjc MLModel.__allocating_init(contentsOf:configuration:) ()
TobyRoseman commented 2 years ago

Is this still an issue in the latest version of Xcode?