mobile

**Mobile ML: iOS and Android** **Mobile ML Frameworks** **iOS** | Framework | Purpose | |-----------|---------| | Core ML | Apple ML inference | | Create ML | Training on Mac | | Vision | Computer vision | | Natural Language | NLP tasks | | Metal | GPU compute | **Android** | Framework | Purpose | |-----------|---------| | TensorFlow Lite | Google ML framework | | ML Kit | Pre-built ML features | | NNAPI | Neural network API | | PyTorch Mobile | PyTorch on Android | **Core ML Integration** ```swift import CoreML // Load model let model = try! MyModel() // Run inference let input = MyModelInput(text: "Hello world") let output = try! model.prediction(input: input) print(output.label) ``` **TensorFlow Lite Android** ```kotlin import org.tensorflow.lite.Interpreter val interpreter = Interpreter(loadModelFile()) // Prepare input val input = floatArrayOf(...) val output = Array(1) { FloatArray(numClasses) } // Run inference interpreter.run(input, output) ``` **Converting Models** **To Core ML** ```python import coremltools as ct # From PyTorch traced_model = torch.jit.trace(model, example_input) mlmodel = ct.convert(traced_model, inputs=[ct.TensorType(name="input", shape=(1, 512))]) mlmodel.save("model.mlpackage") ``` **To TensorFlow Lite** ```python import tensorflow as tf converter = tf.lite.TFLiteConverter.from_keras_model(model) converter.optimizations = [tf.lite.Optimize.DEFAULT] tflite_model = converter.convert() with open("model.tflite", "wb") as f: f.write(tflite_model) ``` **On-Device LLMs** **LLM on iOS** ```swift // Using llama.cpp Swift bindings let llama = LlamaModel(path: "model.gguf") let response = llama.generate("Hello, how are you?", maxTokens: 100) ``` **LLM on Android** ```kotlin // Using llama.android val llama = LlamaAndroid() llama.loadModel("/sdcard/model.gguf") val response = llama.generate("Tell me a joke") ``` **Size Constraints** | Platform | Typical Limit | |----------|---------------| | iOS App Store | 4GB download | | Android Play | 2GB (150MB ideal) | | iOS in-app | Limited by device | **Best Practices** - Use quantized models (INT8/INT4) - Download models on first launch - Batch operations for efficiency - Monitor battery impact - Test on diverse devices

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account