跳到主要內容

發表文章

目前顯示的是有「Android」標籤的文章

從 DMA-BUF 到 AHardwareBuffer:拆解 Android 與 Linux 端側 AI 零拷貝管線 / From DMA-BUF to AHardwareBuffer: Deconstructing Zero-Copy Pipelines in Android and Linux Edge AI

daily-tech:2026-11-25 從 DMA-BUF 到 AHardwareBuffer:拆解 Android 與 Linux 端側 AI 零拷貝管線 / From DMA-BUF to AHardwareBuffer: Deconstructing Zero-Copy Pipelines in Android and Linux Edge AI 現象: 在智慧型手機或邊緣嵌入式裝置上執行即時影像 AI(例如物件偵測、語意分割或姿態辨識)時,許多團隊常遇到一種難堪的效能瓶頸:明明選用了輕量化的量化神經網路,推論核心花費的時間僅需 15 毫秒,然而整體管線的端到端延遲卻高達 45 毫秒以上,甚至伴隨著 CPU 使用率飆高與手機發燙。深入分析效能剖析數據後會發現,推論本身很快,耗時全卡在「相機取像後把點陣圖複製給 GPU 前處理」、「GPU 處理完再拷貝成張量陣列交給 NPU」這兩次不必要的記憶體搬移上。 Symptom: When deploying real-time vision AI (such as object detection, semantic segmentation, or pose estimation) on smartphones or edge embedded devices, engineering teams often face an unexpected performance bottleneck: despite choosing an ultra-lightweight quantized neural network whose core inference takes only 15 milliseconds, the end-to-end pipeline latency exceeds 45 milliseconds, accompanied by soaring CPU utilization and device throttling. Profiling reveals that raw inference is swift; the real culprits are redundant memory copies between camera ISP buffers, GPU preproces...

從 LiteRT 與 ExecuTorch 看 Android 端側 AI 記憶體預算:為什麼模型不大卻容易 OOM / On-Device AI Memory Budgeting: Why Models OOM Despite Small Weights

daily-tech:2026-11-24 從 LiteRT 與 ExecuTorch 看 Android 端側 AI 記憶體預算:為什麼模型不大卻容易 OOM / On-Device AI Memory Budgeting: Why Models OOM Despite Small Weights 現象: 在 Android 手機上部署端側 AI 模型時,許多團隊常遇到令人困惑的記憶體崩潰。直覺上,開發者看到量化後的 INT8 或 INT4 權重檔案只有約 1.2GB,心想設備還有超過 2GB 的可用記憶體,應該游刃有餘。然而一旦實際載入並開始接收推論請求,系統卻迅速觸發 Low Memory Killer(LMK),將前台 App 或重要的背景服務強制終止。這並非模型參數本身膨脹,而是推論時伴隨的動態暫存空間與系統底層限制悄悄耗盡了預算。 Symptom: When deploying on-device AI models on Android, developers often face puzzling memory crashes. Intuitively, seeing an INT8 or INT4 quantized weight file of only 1.2 GB makes one assume that 2 GB of available device RAM is more than enough. Yet in practice, as inference begins, the Android Low Memory Killer (LMK) quickly terminates the foreground app or background tasks. The weight parameters do not expand; rather, dynamic runtime buffers and system-level allocations quietly exhaust the available memory budget. 端側 AI 執行期記憶體組成拆解 / On-Device AI Runtime Memory Breakdown 靜態權重參數 Static Model Weights INT8 / INT4 量...

Android 端側 AI 為什麼要在 DMA-BUF 停一下?零拷貝不等於零同步 / Android On-Device AI: DMA-BUF Zero-Copy Still Needs Synchronization

daily-tech:2026-11-20 現象:模型沒有變大,畫面卻開始卡 / Symptom: The model is unchanged, but frames stall 手機把相機畫面交給端側 AI 模型時,最直覺的做法是先複製到 CPU,再複製到 GPU 或 NPU。資料量一大,模型本身可能沒有變慢,真正增加的是搬運、等待與格式轉換。共享 buffer 可以減少複製,但若忽略同步,畫面可能讀到未完成的資料,或因等待 producer 而出現尾端延遲。 When a phone sends camera frames to an on-device AI model, the obvious path copies pixels through the CPU and then to the GPU or NPU. With larger frames, transport, waiting, and format conversion can dominate even when the model is unchanged. Shared buffers can reduce copies, but missing synchronization can expose incomplete data or create tail latency. Android AI buffer path Camera / CPU producer AHardwareBuffer shared handle DMA-BUF fd import + fence GPU / NPU consumer cache sync + fence + lifetime still matter 圖 1|共享 buffer 減少搬運,但 cache、fence 與生命週期仍需管理。 / Figure 1 | Shared buffers reduce copies, but cache, fences, and lifetime still need explicit management. 機制:HardwareBuffer 是入口,DMA-BUF 是共享路徑 / Mechanism: HardwareBuffer is the Android entry, DMA-BUF i...

Android 端側 AI 為什麼要在 DMA-BUF 停一下?零拷貝不等於零同步 / Android On-Device AI: DMA-BUF Zero-Copy Still Needs Synchronization

daily-tech:2026-11-19 現象:模型沒有變大,畫面卻開始卡 / Symptom: The model is unchanged, but frames stall 手機把相機畫面交給端側 AI 模型時,最直覺的做法是先複製到 CPU,再複製到 GPU 或 NPU。資料量一大,模型本身可能沒有變慢,真正增加的是搬運、等待與格式轉換。共享 buffer 可以減少複製,但若忽略同步,畫面可能讀到未完成的資料,或因等待 producer 而出現尾端延遲。 When a phone sends camera frames to an on-device AI model, the obvious path copies pixels through the CPU and then to the GPU or NPU. With larger frames, transport, waiting, and format conversion can dominate even when the model is unchanged. Shared buffers can reduce copies, but missing synchronization can expose incomplete data or create tail latency. Android AI buffer path Camera / CPU producer AHardwareBuffer shared handle DMA-BUF fd import + fence GPU / NPU consumer cache sync + fence + lifetime still matter 圖 1|共享 buffer 減少搬運,但 cache、fence 與生命週期仍需管理。 / Figure 1 | Shared buffers reduce copies, but cache, fences, and lifetime still need explicit management. 機制:HardwareBuffer 是入口,DMA-BUF 是共享路徑 / Mechanism: HardwareBuffer is the Android entry, DMA-BUF i...

Android ART 為什麼會突然卡住?看懂 GC、堆積與延遲尖峰 / Android ART GC: Why Short Pauses Become Latency Spikes

daily-tech:2026-11-18 現象:App 沒有崩潰,畫面卻突然停住 / Symptom: The app is alive, but frames suddenly stop Android App 在滑動、開啟相機或載入模型時,可能沒有 Crash,畫面卻短暫停住。很多人先把問題歸咎於 CPU 不夠快,但 ART 的記憶體回收也會讓應用程式執行緒暫停。要找原因,先把配置物件、堆積成長和 GC 暫停分開觀察。 An Android app may not crash, yet briefly freeze while scrolling, opening the camera, or loading a model. CPU speed is an easy suspect, but ART garbage collection can also pause application threads. Separate allocation, heap growth, and GC pauses before changing code or hardware. 機制:ART 如何把垃圾回收放進執行流程 / Mechanism: how ART places GC into execution ART 使用 managed heap 管理 Java 與 Kotlin 物件。程式配置新物件時,堆積會逐步接近成長界線;當回收條件成立,GC 會找出仍被參考的物件,回收不可達空間,並依回收器與階段產生不同程度的工作。背景回收不代表完全沒有停頓:根掃描、執行緒協調與記憶體壓縮仍可能反映在畫面幀時間。 ART manages Java and Kotlin objects in a managed heap. Allocations move the heap toward its growth boundary; when collection is needed, GC identifies reachable objects and reclaims unreachable space. Concurrent work does not mean zero pauses: root scanning, thread coordination, and com...

【Android 底層篇】Binder IPC 內部機制拆解:一次拷貝背後的 mmap 記憶體映射黑魔法與執行緒池調度 / [Android Internals] Demystifying Binder IPC: The "Single-Copy" mmap Black Magic and Thread Pool Scheduling

只要開發過 Android 應用程式,你幾乎肯定遇過令人抓狂的 TransactionTooLargeException 。當你試圖在 Intent、Bundle 或 AIDL 介面塞入一張稍微清晰的高解析圖片或龐大資料列表時,應用程式便無情崩潰。為什麼在動輒配備 16 GB 記憶體的旗艦手機上,一次跨行程通訊只要超過區區 1 MB 就會引發系統報警? If you have ever developed an Android application, you have almost certainly encountered the notorious TransactionTooLargeException . When attempting to pass a high-resolution bitmap or an extensive serialized dataset across an Intent, Bundle, or AIDL interface, the application abruptly terminates. Why does an inter-process transaction exceeding a mere 1 MB trigger immediate system panic on a flagship smartphone packing 16 GB of physical RAM? 要徹底解開這個謎題,我們必須告別抽象的 Java 介面,穿透 Android Runtime(ART),直接潛入 Linux 核心驅動 drivers/android/binder.c 。Binder 作為 Android 系統的血脈神經,憑藉著「僅需一次記憶體拷貝」的精巧設計,支撐起整座作業系統的骨架。今天我們就來深度拆解 Binder 的核心映射機制、執行緒池調度與工程師除錯指南。 To unravel this puzzle completely, we must look beyond abstract Java interfaces, bypass the Android Runtime (ART), and dive directly into the Linux kernel driver at driver...

DMA-BUF 零拷貝:Linux 如何讓相機與 NPU 共享記憶體 / DMA-BUF Zero-Copy Between Camera and NPU

在當前智慧型手機與邊緣裝置全面邁向「生成式 AI 與即時神經網路推論」的時代,你是否曾思考過:當手機鏡頭開啟 4K 60FPS 即時串流,相機影像要如何在短短幾毫秒內送進 NPU(神經網路處理單元)執行物體偵測或語義分割,而手機卻不會在幾分鐘內發燙降頻? In an era where smartphones and edge devices rapidly converge toward generative AI and real-time neural network inference, have you ever wondered: When your camera streams 4K 60FPS video, how are raw image frames routed into the NPU (Neural Processing Unit) within milliseconds for object detection or segmentation without turning your handset into a scalding thermal furnace? 如果你曾試著在 Android 應用層呼叫傳統的 read() 或位元組陣列搬運影像,很快就會發現系統陷入嚴重的快取洗刷(Cache Thrashing)與卡頓。要解開即時 AI 高吞吐量的秘密,關鍵鑰匙就深藏在 Linux 核心的 dma-buf (Direct Memory Access Buffer)子系統。今天我們直接深入 Linux 6.18 核心原始碼,剖析 Linux 與 Android 如何透過 DMA-BUF 打造零拷貝(Zero-Copy)的高速資料通道。 If you have ever tried pulling raw camera frames into an application buffer using conventional read() calls or byte arrays, you inevitably observed severe cache thrashing, latency spikes, and frame drops. The definitive solution for real-time ...

讓 AI 在手機裡真正『起飛』:Aether Android 本地 AI Agent 全解析 | Letting AI Truly 'Take Off' on Android: An In-depth Look at Aether

告別臃腫的虛擬機,把強大的 AI Agent 直接塞進 Android 手機裡。 | Say goodbye to bloated VMs and bring a powerful AI Agent directly into your Android device. 🔎 工具速覽 / AT A GLANCE Category Local AI Agent / Android Productivity Pricing Open Source (Free) BestFor Power users, Android developers, and AI enthusiasts who hate latency and love privacy. GitHub Stars ⭐ 68 🚀 引言 / Introduction 「各位工程師,還在為了跑個本地模型而忍受那種像是在用 20 年前電腦的虛擬機嗎? Aether 就像是給 Android 裝上了大鵬之翼,讓你不用在 Termux 的黑視窗裡修不完的 Bug,就能享有 ChatGPT 等級的絲滑體驗。」 "Fellow engineers, still enduring those VM configurations that feel like using a 20-year-old PC just to run a local model? Aether is like giving Android the wings of the Peng bird, allowing you to enjoy a ChatGPT-level smooth experience without spending your afternoon debugging terminal errors in Termux." 🛠️ 核心功能 / Key Features 頂級極簡設計,告別『開源軟體就長得像 90 年代』的刻...