I Ran a 35B Param Model On My iPhone (Local AI)
BBetter Stack
Computing/SoftwareConsumer ElectronicsCell Phones
Transcript
00:00:00this 35 billion per ram model is running directly on my iphone i'm able to generate 11 tokens per
00:00:06second through some clever engineering this model was never designed for and in this video i'll
00:00:11show you exactly how to do the same what i'm running here is a mixture of experts model it's
00:00:2135 billion parameters which as a normal 4-bit file is about 20 gig and would have to sit inside ram
00:00:28but through a bunch of techniques like streaming experts off ssd on demand the weights that actually
00:00:33have to live in memory come down to only 1.4 gig i'll explain how all of this works and then we'll
00:00:39go through the iphone setup itself most models sit entirely in ram but the great thing about mixture
00:00:45of experts models is only a small portion are ever active at any one time this means the inactive parts
00:00:51of the model can just sit on ssd waiting for their time to shine the trick to running larger models
00:00:56unlimited memory is only streaming those experts into memory when they're needed we're running quen
00:01:023.5 the 35 billion param variant and the a3b on the end means only about 3 billion of those parameters
00:01:10are active for any one token this has 40 layers each with 256 small experts plus one shared expert a router
00:01:19picks eight of those experts per token so a single token only runs around 3 billion of the 35 billion
00:01:26total parameters now the entire model itself is split usually it all sits in memory but this wouldn't
00:01:32actually fit on an iphone instead the parts every token needs so the embeddings the attention the routers
00:01:39and the shared expert are only loaded once and stay resident on ram the entire time and that's about
00:01:441.4 gig and the experts 40 files of around 300 meg each totaling 12 gigabytes sit on ssd so for each token
00:01:53attention runs on the gpu then the router picks 8 experts and the engine reads those 8 straight off ssd
00:02:00into the gpu memory and runs them together with the shared expert itself this happens in every one of
00:02:06those 40 layers so it's 320 small reads for every single token and if you're unfamiliar with attention
00:02:14it's the part of the model that looks back over everything in the conversation so far and works out
00:02:19which earlier words matter for predicting the next one attention runs on every single token no matter
00:02:25what so it has to stay in memory the experts are the part that do the actual thinking about a token once
00:02:31attention has worked out the context but because only eight of those experts are used we can just leave
00:02:36the rest on disk there is no expert cache in the app at all every read goes through the operating system
00:02:42and the ios keeps recently used experts in its own page cache as long as it has spare ram the authors
00:02:49actually built their own 9.8 gig cache and then deleted it and that made things 38 faster because
00:02:55on a mac or an iphone any ram the app grabs is ram taken away from both the gpu and that page cache and
00:03:03that cache is doing most of the work here at 11 tokens per second if every expert came off flash
00:03:09the phone would need over 5 gigabytes a second of reads and the iphone's flash only does about 1.6 a
00:03:15second so the majority of experts are coming off ram the os is managing for us the next trick is tiered
00:03:22quantization quantization compresses the weights of a model so they take up less space but it also reduces
00:03:29the accuracy of those weights and therefore affects intelligence but with this model around 25 of the
00:03:35experts handle around 80 of the work so the hot experts stay at 4 bits and the cold ones get
00:03:41quantized down to 2 bits and that makes them 44 smaller the whole file shrinks 34 from around 19 gig to
00:03:5013 gig meaning more of it fits in that page cache on my iphone 17 this runs 11 tokens per second with the
00:03:57model in thinking mode i will say though the phone gets hot literally from saying hello i could feel
00:04:03the phone heat up in my hand so try not to explode the phone when you're testing this i'm actually a
00:04:08little bit scared of typing in anything too complex because this might actually melt through my hand
00:04:14the particular engine we're running is a project called flash moe from dan woods it was written for a
00:04:19macbook and there's a small ios port called flash ios that wraps the same engine in an iphone app and
00:04:26that's what lets me run the entire thing on my phone i also did initially hit a bug where the model
00:04:31thinking got stuck in a loop it would start off fine and then about 10 words in it would just repeat
00:04:35the same two tokens forever to fix this i updated a function called async pre-read weight so it checks
00:04:41each experts read against the experts own size cold experts are two bit and half the size of the hot ones
00:04:48so before the fix every cold expert failed the size check and got silently skipped the model was effectively
00:04:54running with half the experts and so it just looped and if you're enjoying this one guys you would do us
00:04:59a huge favor by subscribing to the channel so we can keep creating free content every single day now to
00:05:05get this set up on your phone you need to clone the repo apply the pre-read fix i just mentioned to
00:05:11metal infer slash infer.m point the xcode project at your team and bundle id you will need a paid apple
00:05:18developer account for this build a release build and install it on your iphone download the pre-packed
00:05:24tiered model which is about 13 gig push it to the app over usb which should take around seven minutes
00:05:30on the phone open the flash moe app tap the model and then start chatting and if you want to try running
00:05:36local models on your mac for even faster tokens per second check out this next video i'll be warren from
00:05:43betterstack thank you so much for watching and of course i'll see you in the next one
Community Posts
No posts yet. Be the first to write about this video!
Write about this video