Skip to content

2025

使用 Numba 加速 Python —— GPU 程式設計入門

Python 因其簡潔性與強大的科學運算函式庫,成為開發者的首選。然而,對於計算密集型任務,Python 的執行效率可能成為瓶頸。這時,Numba —— 一款即時編譯器(Just-In-Time Compiler),能夠將數值運算為主的 Python 代碼在 CPU 和 GPU 上大幅提速。

在本文中,我們將探討如何使用 Numba 簡化基於 NVIDIA CUDA 平台的 GPU 程式設計,即便是對 C/C++ 不熟悉的開發者,也能輕鬆上手。

什麼是 Numba?

Numba 是一款 即時編譯(JIT)、類型專門化(Type-Specializing)、函式編譯器(Function Compiler),可將 Python 函式轉換為最佳化的機器碼。無論是 CPU 或 NVIDIA GPU,Numba 都能在最少代碼改動的情況下大幅提升效能。

Numba 的主要特點包括: - 函式編譯器:優化單獨的函式,而非整個程式。 - 類型專門化:根據參數類型生成高效實作。 - 即時編譯:函式執行時才編譯,以適應 Python 動態類型特性。 - 數值運算優化:專注於 intfloatcomplex 等數據類型。

為何選擇 GPU 程式設計?

GPU 具備大規模並行運算能力,能夠同時執行數千個線程,特別適用於矩陣運算、模擬計算、圖像處理等數據並行任務。NVIDIA 的 CUDA 平台釋放了 GPU 的潛能,而 Numba 提供了 Python 友好的介面,使開發者能夠利用 CUDA,而無需深入學習 C/C++。

開始使用 Numba

CPU 優化

在深入 GPU 運算前,先來看看 Numba 如何加速 Python 的 CPU 運算。透過 @jit 修飾器,Numba 可優化以下計算斜邊長度的函式:

from numba import jit
import math

@jit
def hypot(x, y):
    return math.sqrt(x**2 + y**2)

當函式首次被呼叫時,Numba 會將其編譯為機器碼,從而加快運行速度。

GPU 加速

Numba 提供對 CUDA 的支援,使 GPU 編程變得簡單。我們可以利用 @vectorize 修飾器,將 NumPy 通用函式(ufuncs)加速到 GPU。例如,向量化的標量加法可如下實現:

from numba import vectorize
import numpy as np

@vectorize(['int64(int64, int64)'], target='cuda')
def add(x, y):
    return x + y

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(add(a, b))  # 輸出: [5, 7, 9]

這行代碼會觸發 GPU 運算,包括記憶體分配、數據傳輸及核心(kernel)執行。

Numba 的進階功能

自訂 CUDA 核心函式

對於超越元素級別運算的場景,Numba 支援使用 @cuda.jit 修飾器編寫自訂 CUDA 核心(kernel),讓開發者可以更精細地控制執行緒行為,進一步優化複雜演算法。

共享記憶體與多維網格

在更高級的 GPU 程式設計中,Numba 支援 2D 和 3D 資料結構,以及 CUDA 共享記憶體機制,幫助開發者打造針對特定應用的高效 GPU 代碼。

CUDA 程式設計選項比較

Numba 不是唯一的 Python GPU 編程庫,下表對比了幾種常見選項:

框架 優勢 劣勢
CUDA C/C++ 高效能,完整 CUDA API 需要 C/C++ 專業知識
pyCUDA Python 介面,可直接使用 CUDA API 需要較多代碼改動
Numba 代碼改動少,Pythonic 語法 相較於 pyCUDA,效能稍遜一籌

GPU 編程的最佳實踐

雖然 GPU 能提供極大加速,但錯誤的使用方式可能導致效能低下。以下是幾個最佳實踐: - 使用大規模數據集:GPU 在高並行度的場景中表現最佳。 - 最大化計算密度:確保計算量足夠,以彌補記憶體存取的開銷。 - 優化數據傳輸:最小化 CPU 與 GPU 之間的數據移動,以減少傳輸延遲。

結論

Numba 讓 Python 開發者能夠輕鬆發揮 GPU 的強大運算能力,讓高效能計算變得更可及。無論是數據科學家、研究人員,還是開發者,Numba 都提供了一種簡單且高效的方式來加速 Python 應用。

準備好進一步探索了嗎?深入學習 Numba 和 CUDA,釋放 GPU 運算的潛力,提升你的計算工作負載!

First Principles - A Foundation for Groundbreaking Thinking

In a world brimming with assumptions, habits, and established norms, how do we carve a path toward true innovation? The answer lies in embracing the concept of first principles—a way of thinking that strips down complexity to uncover foundational truths.

The Essence of First Principles

The French philosopher and scientist René Descartes described first principles as systematically doubting everything that can be doubted until reaching undeniable truths. It’s a call to challenge the status quo, to question deeply ingrained assumptions, and to venture beyond surface-level thinking.

First principles thinking requires a mindset shift: - Stop accepting existing systems and solutions as immutable. - Refuse to let others' visions dictate your path. - Break down assumptions to their core components, as if forging a new trail through a dense jungle.

In short, everything beyond those fundamental truths becomes negotiable.

Seeing the World Differently

Adopting first principles enables us to see insights hidden in plain sight—insights often overlooked because they seem too obvious or because convention blinds us. As philosopher Arthur Schopenhauer aptly put it: "The capable achieve what others cannot, and the wise see what others overlook."

When you think in terms of first principles, you stop being a cover band playing others' music. Instead, you become the artist creating original masterpieces. You transition from what James Carse calls a “finite player” bound by rules and limits to an “infinite player” who transcends boundaries and redefines possibilities.

Elon Musk and the Power of First Principles

A striking example of first principles thinking comes from Elon Musk. After failing to secure a deal for affordable rocket parts in Russia, Musk realized the primary obstacle to space exploration wasn’t physical but mental. Decades of societal conditioning had led people to believe that reaching for the stars was prohibitively expensive and out of reach.

Instead of accepting the conventional wisdom, Musk applied first principles: - He analyzed the fundamental components of rockets—raw materials like aluminum, carbon fiber, and titanium. - He questioned why building rockets should cost so much. - He discovered it was possible to reduce costs dramatically by designing and manufacturing rockets in-house.

The result? SpaceX, a company that has revolutionized space exploration.

Escaping the Trap of Low Expectations

As David Schwartz wrote, the biggest barriers to our ambitions often exist in our minds. Society trains us to believe that flying low is safer than soaring high, that gliding with inertia is better than taking bold leaps, and that modest dreams are wiser than audacious ones.

This conditioning becomes a self-fulfilling prophecy. When we pursue mediocrity, we achieve mediocrity. Conversely, when we aim high—even if we miss—we achieve far more than we would by playing it safe.

As the Rolling Stones remind us, “You can't always get what you want.” But if you strive for the moon with conviction and clarity, you may not just land among the stars—you might redefine the limits of possibility itself.

Becoming a Creator, Not a Follower

First principles thinking is hard. It requires effort, creativity, and courage. It’s about questioning assumptions that others take for granted and envisioning solutions that don’t yet exist. But the rewards are transformative. By applying this mindset, you move from being a passive consumer of existing ideas to an active creator of new paradigms.

So, the next time you face a complex challenge, take a step back. Break it down. Ask yourself: - What are the unquestioned assumptions? - What is truly essential? - How can I approach this differently?

When you embrace first principles, you stop being limited by the way things are—and start building the way things could be.

第一性原則——突破性思維的基礎

在這個充滿假設、習慣與既定規範的世界中,我們如何開闢通往真正創新的道路?答案在於擁抱 第一性原則——一種剝離複雜性、探尋事物本質的思維方式。

第一性原則的本質

法國哲學家兼科學家 笛卡兒(René Descartes)將第一性原則描述為:對一切可懷疑的事物進行系統性懷疑,直到抵達無法否認的真理。這是一種挑戰現狀、質疑根深蒂固假設的思維方式,引導我們跳脫表面層次的思考。

第一性原則思維需要轉變心態: - 不再將現有系統和解決方案視為不可改變的真理。 - 不讓別人的願景決定自己的道路。 - 將問題拆解至最基本的組成部分,像是在茂密叢林中開闢新的小徑。

簡而言之,除了這些最基本的真理之外,其他一切都是可重新構思和談判的。

以不同的角度看世界

運用第一性原則,可以讓我們發現那些隱藏在顯而易見中的洞見——那些因為過於明顯,或因為傳統思維的遮蔽,而常常被忽略的洞見。正如哲學家 叔本華(Arthur Schopenhauer)所說:

「有能力的人能做到他人無法做到的事,而智慧的人能看見他人忽略的事物。」

當你以第一性原則思考時,你不再是只會翻唱別人歌曲的樂隊,而是創作原創音樂的藝術家。你不再是 James Carse 所說的「有限遊戲玩家」(finite player),被規則和限制束縛,而是「無限遊戲玩家」(infinite player),超越界限、重塑可能性。

Elon Musk 與第一性原則的力量

第一性原則的經典案例來自 Elon Musk。當他無法在俄羅斯以合理價格購買火箭零件時,他意識到,太空探索的主要障礙不是技術上的,而是心理上的。數十年的社會認知讓人們認為進入太空昂貴且遙不可及。

然而,Musk 並未接受這一既定認知,而是運用第一性原則進行思考: - 分析火箭的基本組成——鋁、碳纖維、鈦等原材料。 - 質疑為何製造火箭的成本如此高昂。 - 發現如果能自行設計和生產火箭,就能大幅降低成本。

結果如何?SpaceX 誕生,徹底顛覆了太空探索產業。

擺脫低標準的陷阱

正如 David Schwartz 所寫,阻礙我們實現抱負的最大障礙,往往存在於我們自己的心中。 社會讓我們相信:「飛得低比飛得高更安全」、「順著慣性滑行比大膽躍進更好」、「平庸的夢想比遠大的夢想更明智」。

這種思維方式會形成自我實現的預言——當我們只追求平庸,就只能收穫平庸。然而,當我們勇於追求卓越,即使未能完全達成目標,也遠比安於現狀來得成功。

正如 滾石樂隊(Rolling Stones)唱道:

「你不一定能得到你想要的,但如果你努力爭取,你可能會得到你真正需要的。」

如果我們懷抱信念與清晰的願景去追逐卓越,即使未能直達月球,也可能會在群星間閃耀,甚至改寫可能性的邊界。

成為創造者,而非追隨者

第一性原則思維並不容易,它需要努力、創造力和勇氣。這種思維方式的核心在於: - 挑戰他人視為理所當然的假設。 - 想像尚未存在的解決方案。 - 在逆境中開創新局面。

但這種努力的回報是巨大的。當你培養這種思維,你將不再只是現有想法的被動消費者,而是新概念的積極創造者。

所以下次當你面對複雜的挑戰時,不妨退一步思考,問自己: - 有哪些未經質疑的假設? - 什麼才是不可或缺的核心本質? - 我能如何用不同的方式來解決這個問題?

當你真正掌握第一性原則,你將不再受限於「世界的現狀」,而是開始塑造「世界的可能」。

The Joy of Being Wrong - Embracing Discovery and Growth

In the world of research and discovery, there’s an often-overlooked truth: being wrong is one of the best things that can happen to you. It’s not just a sign of humility or a badge of courage—it’s a gateway to learning and progress. This might sound counterintuitive, but as I reflect on my experiences and those of others, it’s clear that embracing errors with an open mind can lead to breakthroughs that far outweigh the sting of being incorrect.

I’ll never forget a conversation I had with Danny, a senior researcher whose insights I deeply admire. One day, Danny came across data that completely contradicted one of his long-held assumptions. Instead of frustration or defensiveness, his reaction was one of pure joy. His eyes lit up, and a wide grin spread across his face as he exclaimed, “This is fantastic—my idea was wrong!”

Later, over lunch, I asked him about his reaction. For me, it was a revelation. How could anyone be so thrilled to discover their mistake? Danny explained with calm confidence, “Every time I find out I’m wrong, I know I’ve learned something important. It means I’ve gotten a little closer to the truth, and my understanding has improved.”

At 85 years old, Danny said, he still relishes the chance to uncover errors in his thinking. “If no one ever pointed out my mistakes, I wouldn’t grow,” he added. “Being wrong means I’ve reduced my blind spots, even if just by a little.”

Danny’s attitude resonated deeply with me because it mirrored my own journey. When I was a university student, I was initially drawn to social sciences because of their ability to challenge my expectations. I loved reading studies that conflicted with my assumptions, eagerly rethinking my beliefs and sharing these revelations with my roommates.

During my first independent research project, I tested several hypotheses I had painstakingly developed. To my surprise—and slight embarrassment—most of them turned out to be completely wrong. But instead of feeling defeated, I was thrilled. Those “mistakes” meant I had learned something new. It was as if the process itself rewarded me with a sense of progress and discovery.

Why does being wrong sometimes feel so liberating? It’s because error is an unmistakable sign that we’re learning. Growth doesn’t come from being right all the time—it comes from curiosity, from challenging our own ideas, and from seeking out perspectives that don’t align with ours.

For example, when we study individuals who excel at making predictions, like expert forecasters, one key takeaway is their willingness to revise their views in the face of new evidence. Whether it’s refining statistical models or rethinking political predictions, the best forecasters don’t cling to their initial assumptions. Instead, they see error as an opportunity to improve.

Even if forecasting or statistical analysis isn’t your passion, observing how experts like these adapt their thinking can teach us a lot about intellectual humility and continuous improvement.

This mindset isn’t just for researchers and forecasters. Whether you’re working on a complex project, making decisions in your career, or simply navigating daily life, embracing the possibility of being wrong opens the door to growth.

Next time you encounter a mistake or a challenge to your assumptions, pause for a moment. Instead of feeling defensive, try to channel a bit of Danny’s enthusiasm. Ask yourself: What can I learn from this? How can this reshape my understanding?

In doing so, you’ll not only improve your knowledge but also cultivate a resilience and curiosity that will serve you well in every aspect of life.

Learning to see being wrong as a gift takes practice, but it’s a perspective that can transform how you approach challenges. After all, the joy of discovery isn’t just in being right—it’s in the journey toward deeper understanding. As Danny reminded me, the only way to be sure you’re learning is to celebrate the moments when your assumptions are proven wrong.

What’s the last mistake you learned from? Let’s embrace our errors and share the wisdom they bring.

錯得其樂——擁抱發現與成長

在研究與發現的世界裡,有一個經常被忽視的真理:犯錯可能是最棒的事情之一。這不僅僅是謙遜的表現,也不只是勇氣的象徵——它是通往學習與進步的大門。這聽起來或許有點違反直覺,但當我回顧自己的經歷,以及那些影響我最深的人時,便更加確信:用開放的心態擁抱錯誤,往往能帶來遠超過正確的突破與成長

我永遠不會忘記與丹尼(Danny)的一次對話。他是一位資深研究員,我一直很敬佩他的見解。有一天,丹尼發現了一組數據,徹底推翻了他長期以來堅信的假設。然而,他的反應並非沮喪或防禦,而是純粹的喜悅。他的眼睛閃閃發亮,臉上露出燦爛的笑容,興奮地說:「太棒了!我的想法是錯的!

午餐時,我問他為何如此高興。對我來說,這是個震撼的發現——怎麼會有人因為發現自己的錯誤而如此興奮?丹尼平靜而自信地回答:「每次我發現自己錯了,就代表我學到了重要的東西。我更接近真相了,我的理解變得更清晰了。

丹尼今年 85 歲了,但他依然熱衷於發現自己的思維漏洞。他說:「如果沒有人指出我的錯誤,我就無法成長。犯錯代表我的盲點少了一點,即使只是微不足道的一點。

他的態度深深觸動了我,因為這正是我成長過程中的體驗。當我還是大學生時,我最喜歡社會科學,因為它不斷挑戰我的預設立場。我熱愛閱讀那些顛覆我原有觀念的研究,興奮地重新思考自己的信念,並與室友們分享這些新發現。

在我第一次獨立進行研究時,我精心設計了幾個假設。結果令我驚訝——甚至有些尷尬——大多數假設竟然全錯了。然而,我並未因此氣餒,反而感到興奮。這些「錯誤」意味著我學到了新的東西,彷彿整個過程本身就獎勵了我成長與發現的快感。

為什麼「犯錯」有時會讓人感到解放?

因為錯誤是最明確的學習標誌。成長並非來自持續的正確,而是來自好奇心,來自挑戰自己的想法,來自積極尋找與自己觀點不同的觀點。

舉例來說,那些擅長預測未來的專家,如頂尖的預測家(superforecasters),他們的成功關鍵之一,就是願意在新證據出現時修正自己的觀點。無論是優化統計模型,還是重新思考政治預測,這些頂尖專家都不會執著於最初的假設,而是將錯誤視為改進的機會。

即使你對預測或數據分析不感興趣,觀察這些專家如何適應新資訊,也能讓我們學習到關於智慧謙遜(intellectual humility)與持續精進的寶貴態度

這種思維模式不僅適用於研究或預測

無論是處理複雜的專案、做出職涯決策,或只是面對日常生活中的挑戰,擁抱「犯錯的可能性」都能為我們帶來成長。

下次當你發現自己犯錯,或者當你的假設受到挑戰時,先停下來想一想。別急著防禦,試著學習丹尼的熱情,問問自己:「我能從這次錯誤中學到什麼?這如何改變我的理解?

當我們這麼做時,不僅能提升自己的知識,也能培養出一種韌性與好奇心,而這種心態將在生活的各個層面帶來幫助。

把錯誤視為禮物,這需要練習

但一旦做到,你將發現這種觀點能徹底改變你面對挑戰的方式。畢竟,發現的樂趣不僅僅來自正確,而是來自不斷追求更深層次的理解。正如丹尼提醒我的:唯一能確保你正在學習的方法,就是慶祝那些推翻自己假設的時刻。

你最近從哪個錯誤中學到了東西?讓我們一起擁抱錯誤,分享它帶來的智慧吧!