Press "Enter" to skip to content

Posts tagged as “power”

避坑指南:给模型训练加点速度,顺便聊聊 MBP 的电源模式

最近在折腾一个模型训练任务,结果发现了一个挺有意思的现象。我的 16″ M1 Max MBP 插着电源线,电量竟然还在嗖嗖往下掉。查了一下才发现,随手抓的那个苹果 67W 充电器在模型训练面前简直是“入不敷出”。

为了看看到底差多少,我针对 Low Power Mode(低电量模式)High Power Mode(高功率模式) 做了一组对比测试。数据非常直观,分享给大家参考。

注:高功率模式 (High Power Mode) 最早是在 2021 年 10 月 随 macOS Monterey 发布的,当时仅限配备 M1 Max 芯片的 16 英寸 MacBook Pro 使用。

性能实测:低功耗 vs 高功率

在模型训练压测下,我记录了 CPU、GPU 以及 ANE(神经网络引擎)的功耗表现:

指标低电量模式 (Low Power)高功率模式 (High Power)增幅 / 差异
CPU Power4,038 mW6,903 mW+70.9%
GPU Power13,524 mW26,947 mW+99.2%
ANE Power0 mW0 mW
总功耗 (Combined)17,619 mW33,849 mW+92.1%
GPU 主频 (Avg)871 MHz1,282 MHz+47.1%
GPU 空闲率8.57%1.19%负载更饱和
训练速度650ms/step475ms/step-36.8%

深度发现

  1. GPU 功耗几乎翻倍:在高功率模式下,GPU 的功耗从 13.5W 直接跳到了近 27W。观察频率分布可以发现,低功耗模式下 GPU 频率最高只到了 972 MHz (占比 29%),而高功率模式下 95% 的时间都顶在 1296 MHz 运行。训练速度提高了不到40%,比频率提升47.1%稍低一些。
  2. 风扇策略的差异:高功率模式不仅仅是放开了功耗墙,更重要的是它优化了散热策略。这也是为什么 16 寸 M1 Max 专享这个功能的原因——得有足够的散热模组才能压得住这额外的十几瓦发热。
  3. 电源适配器的重要性:测试数据显示,单单 SoC 部分的功耗就达到了 34W 左右。如果再加上屏幕亮度、内存读写以及其他硬件开销,整机功耗轻轻松松突破 50W。难怪 67W 的头充不进电,以后出门干重活,还是得带上原装的 140W 大头。

总结

macOS 的电源模式确实不是摆设。低电量模式适合出门在外写写代码、浏览网页,能显著延长续航;但如果你和我一样需要训练模型或者高强度渲染,一定要手动开启高功率模式

虽然这会让风扇声大一点,但节省下来的训练时间才是最宝贵的。顺便提醒一句:工欲善其事,必先带好那个 140W 的适配器!

花花酱 LeetCode 1922. Count Good Numbers

A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (235, or 7).

  • For example, "2582" is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd positions are prime. However, "3245" is not good because 3 is at an even index but is not even.

Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 109 + 7.

digit string is a string consisting of digits 0 through 9 that may contain leading zeros.

Example 1:

Input: n = 1
Output: 5
Explanation: The good numbers of length 1 are "0", "2", "4", "6", "8".

Example 2:

Input: n = 4
Output: 400

Example 3:

Input: n = 50
Output: 564908303

Constraints:

  • 1 <= n <= 1015

Solution: Fast Power

Easy to see that f(n) = (4 + (n & 1)) * f(n – 1), f(1) = 5

However, since n is huge, we need to rewrite f(n) as 4n/2 * 5(n+1)/2 and use fast power to compute it.

Time complexity: O(logn)
Space complexity: O(1)

C++