-
Notifications
You must be signed in to change notification settings - Fork 9.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed bad memory access exception on ios 17 #3527
Conversation
( @cebtenzzre ) That looks like a magic number to me: Line 95 in c47066d
Whether it's related or not, I don't think it's supposed to be there, in this form. |
@staviq that buffer is only used for debugging, and it is perfectly fine to leave it at a fixed size. Regarding this PR, I don't think that this is a reasonable approach to solve this. We need to understand what is the cause of the issue. |
The only thing I can think of right now is the original method uses a compound literal assignment. This creates a temporary variable on the stack (maybe? see: https://stackoverflow.com/questions/70689138/compound-literals-in-c-do-they-create-duplicate-copies). When debugging through the code, I see the fixed arrays are relatively large (hash table is fixed at 32771 length, with each element containing a ggml_tensor). Could an implementation detail on iOS stops the optimisation and makes it so a temporary variable is created on the stack first then copied to the heap? Stack size is limited on iPhone per thread, so I can see some issues if this was duplicated on the stack first. |
That looks like could be the case. I would expect the compiler to optimize away the copies in the stack and write directly to the heap, but we cannot rely on that. The source of the issue is the increase of |
I found the problem is also happened on the server example with |
You mean it happens with server example on not iOS? I mean, people run debug builds all the time, I wouldn't dismiss platform specific problem just yet. Can you post a bit more context for this server problem ? |
To be clarify, the problem should be We should used |
This issue was observed even on TestFlight build. Those should be compiled with the release flags.
…________________________________
From: Jhen-Jie Hong ***@***.***>
Sent: Sunday, October 8, 2023 9:44:43 AM
To: ggerganov/llama.cpp ***@***.***>
Cc: l3utterfly ***@***.***>; Author ***@***.***>
Subject: Re: [ggerganov/llama.cpp] fixed bad memory access exception on ios 17 (PR #3527)
You mean it happens with server example on not iOS? I mean, people run debug builds all the time, I wouldn't dismiss platform specific problem just yet.
Can you post a bit more context for this server problem ?
To be clarify, the problem should be -O0 within a thread (It should be similar to what was discussed above), so I mentioned this can also reproduce in server example on Mac. The swift package currently should use -O0 (default) as well, so I think this is why it was first defined as an iOS-specific issue.
We should used -O3 -DNDEBUG by default, not debug builds.
—
Reply to this email directly, view it on GitHub<#3527 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ABVFX25OFIDRU3XMBO5ZFXLX6IAQXAVCNFSM6AAAAAA5W7DPN2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJRHA4DQMJTGQ>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Is it possible to override the swift package unsafe flags by project? or have you already used |
No, I didn’t change any flags. Shouldn’t we be turning releases flags on for release mode build? There should be significant performance differences
…________________________________
From: Jhen-Jie Hong ***@***.***>
Sent: Sunday, October 8, 2023 9:54:12 AM
To: ggerganov/llama.cpp ***@***.***>
Cc: l3utterfly ***@***.***>; Author ***@***.***>
Subject: Re: [ggerganov/llama.cpp] fixed bad memory access exception on ios 17 (PR #3527)
This issue was observed even on TestFlight build. Those should be compiled with the release flags.
Is it possible to override the swift package unsafe flags by project? or have you already used -O3 on a fork? Otherwise it should always use -O0 even on release builds.
—
Reply to this email directly, view it on GitHub<#3527 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ABVFX2YCZWE4TFGEFPZLKGDX6IBUJAVCNFSM6AAAAAA5W7DPN2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJRHA4TAOJXHA>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Can you show me where this is defined? I looked in the swift package, and my xcode project settings, the release build has the normal -Os optimisations enabled. |
The O0 is a default build setting, and currently the swift package is not defined any unsafe flag for O. As I know the root project compile flags will not apply to the package dependencies, but I'm not very sure. We can add EDIT: Also, I tested add |
Further testing confirms adding This PR's changes to |
What is the error that you get? |
@ggerganov The app crashes with no errors. This could be due to the fact it's built in release mode (with the This PR is tested and confirmed to fix the issue. The changes in this PR tentatively points to the fact that using the default constructor for |
Sure, will check. Looking at the code in the #3912, it should fix this issue. So I'll wait for the PR to be merged. |
On iOS 17 (tested on iPhone 15 Pro), I keep getting Bad Memory Access exception everywhere.
Digging deeper, the changes in this PR fixes it.
To be honest, I have no idea why it broke in the first place. It seems we can't use the struct constructor shorthand?
Writing the construction out by manually allocating memory, then zero-ing out the arrays fixes this. Tested on iPhone 12-15.
Perhaps someone more knowledgeable than me on memory management can shed some light on this.