2020年3月30日 星期一

PyTorch add function


在 PyTorch 中的 add function,可以實現將兩個張量相加,可以寫成 c = torch.add(a, b),也可以直接寫成 c = a + b。

其中 a 和 b 分別可以是一個張量加上一個純量,結果 c 就會是把純量加到張量中的每一個元素上。

a 和 b 也可以是兩個張量,但是若要實現兩個張量的相加,a 和 b 的 size 就有特定的限制 (broadcastable)。

以下幾種 a 和 b 的 size 是允許相加的:

Size of a:  (5)
Size of b:  (3, 2, 1, 5)

Size of a:  (1, 5)
Size of b:  (3, 2, 1, 5)

Size of a:  (2, 1, 5)
Size of b:  (3, 2, 1, 5)

Size of a:  (3, 2, 1, 5)
Size of b:  (3, 2, 1, 5)

由以上的例子可以看出,其中一個張量 (a) 的維度必須小於或等於另一個張量 (b),而且 a 的 size 必須與另一個張量 b 的最後幾個維度的 size 相同。

以下幾種 a 和 b 也是允許相加的:

Size of a:  (3, 2, 2, 5)
Size of b:  (1, 5)

Size of a:  (3, 2, 2, 5)
Size of b:  (1, 1, 5)

Size of a:  (3, 2, 2, 5)
Size of b:  (2, 1, 5)

Size of a:  (3, 2, 2, 5)
Size of b:  (3, 1, 1, 5)

Size of a:  (1, 5)
Size of b:  (2, 1)

Size of a:  (1, 1, 1, 5)
Size of b:  (1, 2, 1, 1)

由以上的例子可以看出,在其中一個張量的維度中,可以允許一個或多個維度的大小為 1。在大小為 1 的那個維度中,b (a) 的所有元素會被廣播到 a (b) 裡面同一個維度中所有的 index 上並執行加法運算。


2020年3月26日 星期四

PyTorch 中 weight 和 weight.data 的差異


當我們做完 backpropagation 得到 weight 和 bias 的 grad 之後,要進行 weight 和 bias 的更新時,有兩種不同的 coding style。

(1) 使用 torch.no_grad()
with torch.no_grad():
    weight -= learning_rate * weight.grad
    bias -= learning_rate * bias.grad

(2) 使用 weight.data 和 weight.grad.data
weight.data -= learning_rate * weight.grad.data
bias.data -= learning_rate * bias.grad.data

這兩種方法有何區別呢?根據 PyTorch 官方網站給的解釋:
Recall that tensor.data gives a tensor that shares the storage with tensor, but doesn't track history.
 tensor 和 tensor.data 共享同一個儲存空間,但是當我們使用 tensor.data 時,PyTorch 不會追蹤運算的歷史,也就是當我們進行 backward() 時,在 tensor.data 中不會進行 gradient 的計算。


Reference
https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_autograd.html

PyTorch 的 nn 和 nn.functional 的差別


在 nn 裡面實現了很多搭建神經網路時常用的 building blocks 如 Conv2D、ReLu 等等。但是相同的名稱在 nn.functional 裡面也能找到,那它們之間有何差別?

差別在於 nn 裡面的 building blocks 是用 Python 的 Class 來實現的,他繼承了nn.Module 這個 Class,像 weight 和 bias 等 variable 會存在 nn.Conv2D 裡面。但是對於 nn.functional.Conv2D 來說,因為它只是一個 function 而不是 class,function 本身是沒有狀態的 (state),所有一切 variable 在 function 結束以後就消失了,因此它的 weight 和 bias 等需要保留的 variable 就必須從外面傳入。

這也是為什麼,當我們創建一個 network 時,nn.Conv2D 要寫在 self.__init__ 裡面,但是 nn.functional.relu 卻不用。


Reference
https://discuss.pytorch.org/t/what-is-the-difference-between-torch-nn-and-torch-nn-functional/33597

2020年3月25日 星期三

PyTorch 的 backward function


在呼叫 t.backward() 時,若 t 是一個純量,就不需要傳入任何的引數。但是當 t 是一個張量的時候,就需要傳入一個和 t 同樣 size 的張量,這個張量是做什麼用的?

假設我們的 t = [t1, t2],這個張量的運算圖裡面有一個 leaf node 是 x = [x1, x2]。如果在呼叫 backward 時,我們傳入 [1, 1],PyTorch 會將 t 當中的每一個元素 (t1 和 t2) 都對 x1 進行偏微分,並且將所有的結果加起來得到 x.grad = [g1, g2] 中的 g1。

此時如果我們只想要知道 t1 對 x1 的偏微分結果,就要在呼叫 backward 時,傳入引數 [1, 0]。

這個傳入的引數,可以看成一個權重,可以控制 t 裡面每一個元素對偏微分的影響程度。在針對一個運算圖進行 backward 時,每一個 operation 的 backward function 會接收它的下游所傳來的 gradient,因為在進行 back propagation 時,上游的 gradient 是透過微分的鏈鎖律 (chain rule) 算出來的,也就是 backward function 的 input 會和該 operation 的 gradient 相乘,去得到上游的 gradient。


Reference
Sherlock, "PyTorch中的backward," https://zhuanlan.zhihu.com/p/27808095.

2020年2月4日 星期二

神經型態運算架構


SpiNNaker
Affiliation: University of Manchester, UK
Architecture: ARM processor-based
Neuron: LIF, Izhikevich
On-Chip Learning: STDP
Platform: PyNN
Reference: Furber, S. B., Galluppi, F., Temple, S., & Plana, L. A. (2014). The spinnaker project. Proceedings of the IEEE, 102(5), 652-665.

TrueNorth
Affiliation: IBM, USA
Architecture: digital ASIC
Neuron: LIF
On-Chip Learning: N/A
Reference:
[1] Merolla, P. A., Arthur, J. V., Alvarez-Icaza, R., Cassidy, A. S., Sawada, J., Akopyan, F., ... & Brezzo, B. (2014). A million spiking-neuron integrated circuit with a scalable communication network and interface. Science, 345(6197), 668-673.
[2] Esser, S. K., Merolla, P. A., Arthur, J. V., Cassidy, A. S., Appuswamy, R., Andreopoulos, A., ... & Barch, D. R. (2016). Convolutional networks for fast, energy-efficient neuromorphic computing. 2016. Preprint on ArXiv. http://arxiv. org/abs/1603.08270. Accessed, 27.

Loihi
Affiliation: Intel, USA
Architecture: digital ASIC
On-Chip Learning: pairwise STDP, triplet STDP, reinforcement learning
Platform: Nengo
Reference: Davies, M., Srinivasa, N., Lin, T. H., Chinya, G., Cao, Y., Choday, S. H., ... & Liao, Y. (2018). Loihi: A neuromorphic manycore processor with on-chip learning. IEEE Micro, 38(1), 82-99.

BrainScaleS/HiCANN
Affiliation: Heidelberg University, Germany
Architecture: mixed-signal (analog neuron and digital communication) wafer-scale system
Neuron: adaptive exponential IF
On-Chip Learning: highly flexible learning rules
Platform: PyNN
Reference:
[1] Meier, K. (2015, December). A mixed-signal universal neuromorphic computing system. In 2015 IEEE International Electron Devices Meeting (IEDM) (pp. 4-6). IEEE.
[2] Schemmel, J., Briiderle, D., Griibl, A., Hock, M., Meier, K., & Millner, S. (2010, May). A wafer-scale neuromorphic hardware system for large-scale neural modeling. In Proceedings of 2010 IEEE International Symposium on Circuits and Systems (pp. 1947-1950). IEEE.
[3] Friedmann, S., Schemmel, J., Grübl, A., Hartel, A., Hock, M., & Meier, K. (2016). Demonstrating hybrid learning in a flexible neuromorphic hardware system. IEEE transactions on biomedical circuits and systems, 11(1), 128-142.

NeuroGrid/BrainDrop
Affiliation: Stanford University, USA
Architecture: mixed-signal ASIC
Reference: Benjamin, B. V., Gao, P., McQuinn, E., Choudhary, S., Chandrasekaran, A. R., Bussat, J. M., ... & Boahen, K. (2014). Neurogrid: A mixed-analog-digital multichip system for large-scale neural simulations. Proceedings of the IEEE, 102(5), 699-716.

DYNAP
Affiliation: Institute of Neuroinformatics (INI), University of Zurich and ETH Zurich
Architecture: mixed-signal ASIC (ultralow-power subthreshold analog neuron, digital circuit for reprogramming of connectivity, supporting for RNN and multilayer network)
Reference: Moradi, S., Qiao, N., Stefanini, F., & Indiveri, G. (2017). A scalable multicore architecture with heterogeneous memory structures for dynamic neuromorphic asynchronous processors (DYNAPs). IEEE transactions on biomedical circuits and systems, 12(1), 106-122.

ODIN
Affiliation: Catholic University Louvain, Belgium
Architecture: digital ASIC
Neuron: LIF, Izhikevich
On-Chip Learning: spike-driven plasticity
Reference: Frenkel, C., Lefebvre, M., Legat, J. D., & Bol, D. (2018). A 0.086-mm $^ 2 $12.7-pJ/SOP 64k-synapse 256-neuron online-learning digital spiking neuromorphic processor in 28-nm CMOS. IEEE transactions on biomedical circuits and systems, 13(1), 145-158.

Reference
Rajendran, B., Sebastian, A., Schmuker, M., Srinivasa, N., & Eleftheriou, E. (2019). Low-Power Neuromorphic Hardware for Signal Processing Applications: A review of architectural and system-level design approaches. IEEE Signal Processing Magazine, 36(6), 97-110.

2020年1月15日 星期三

Interconnect RC Delay


Delay 的來源

  • Gate delay
  • Interconnect delay
    • Speed-of-light delay
    • RC delay
Speed-of-light delay 正比於連線長度,而 RC delay 則正比於連線長度的二次方。在舊製程中,由於 gate delay 較大,因此 RC delay 可以忽略不計。然而當製程微縮時,gate delay 越來越小,此時 RC delay 的影響就越來越大。

Coupling Noise 的成因

根據 Maxwell's Equation,當一個導體上的電壓和電流改變時,所產生的電場和磁場會對周遭的其他導體產生影響,稱為電容性串擾 (capacitive crosstalk) 和電感性串擾 (inductive crosstalk)。當導體距離越近,或是導體邊緣越尖銳,crosstalk 現象就越嚴重。

Wire Resistance

R = pL/Wt
p: resistivity
L: wire length
W: wire width
t: wire thickness

Wire Capacitance

一般導線上對 substrate 的寄生電容大小大約是相同大小的電晶體的十分之一。隨著製程越來越先進,導線彼此間的距離越來越小,因此導線間的 coupling capacitance 的影響也越來越顯著。

Wire Delay Modeling

Pi model 和 T model 的準確度比 L model 還要高,相同的一段導線上的 delay,若用 4 段 Pi model 來模擬,其誤差可小至 5%。然而若使用 L model,則需要分成 64 段。

Capacitive Crosstalk

當一條導線和另外一條導線相鄰時,會產生 coupling capacitance。這個 coupling capacitance 可以合併到 substrate capacitance 當中,以簡化 delay modeling,其轉換的方法為:若發生 coupling 的對象導線上是靜態的訊號,其 coupling capacitance 可以直接併入 substrate capacitance;若發生 coupling 的對象導線上的訊號是反向的 (一個上升一個下降),則其 coupling capacitance 需加倍計入 substrate capacitance 中;若兩條導線上的訊號是同方向 (一起上升或一起下降),則其 coupling capacitance 可忽略不計。以上所描述的是很理想的情況,實際上,導線上的訊號改變複雜得多。一條導線上訊號的改變,常常會對另一條訊號線造成干擾,進而產生 noise,此 noise 的大小,可以藉由 coupling capacitance 與 substrate capacitance 的分壓來得知。

其他問題


  • IR Drop: 由於電流在導線上傳遞時,會因導線上的電阻而產生電壓下降,在供應電壓較低的晶片上,IR drop 將造成晶片內部或距離 power pad 較遠處的供應電壓不足的問題,因此晶片需要有更多 power pad 從不同位置供電。
  • Electromigration: 在直流電流經一條導線時,電子移動時會撞擊導線中的金屬原子,經年累月的使用後,可能造成導線中的金屬原子離開原來所在位置,造成導線斷線。一般來說,一條寬 1 um 的導線,其電流不得超過 1 mA。
  • Self-Heating: 當電流在導線中流動時,會因導線電阻而產生熱,由於周圍的氧化物絕緣體同時也是熱的不良導體,因此熱會逐漸累積在導線上。而當導線溫度上升時,其電阻也會上升,因此產生更多的熱,最終可能造成導線熔斷。


Reference
Harris, D. (1997). High Speed CMOS VLSI Design–Lecture 4: Interconnect RC

2020年1月7日 星期二

RRAM Variation Model


RRAM Device Model


  • 2-D filament model 的主要變數:(1) tunneling gap distance、(2) conducting filament (CF) radius。
  • SET model:(1) CF length、(2) CF radius。
  • RESET model:(2) 電極釋放氧離子的速度、(2) 氧離子和空缺結合的速度。
  • CF 溫度的影響。
  • Device 的電流路徑包含: (1) hopping current path、(2) metallic conduction path。
  • 寄生電容電阻效應:(1) 電極間電容、(2) 電極外之 contact 電阻、(3) 氧化物漏電路徑。

Variation Model

  • LRS 的電阻值 variation 受到 CF radius fluctuations 的影響。
  • HRS 的電阻值 variation 受到 tunneling gap distance fluctuations 的影響。
  • Ion migration energy barrier 的隨機性會影響 switching voltage 的 variation。
  • Overshoot 現象:由寄生電容造成。
  • Read / write disturb 由 sub-threshold switching 造成。

Device 實驗結果

  • Abrupt set and gradual reset。
  • 改變 RESET maximum voltage 會使得 HRS 的電阻值不同。
  • 改變 SET compliance current 會使得 LRS 的電阻值不同。


Reference
Li, H., Jiang, Z., Huang, P., Wu, Y., Chen, H. Y., Gao, B., ... & Wong, H. S. (2015, March). Variation-aware, reliability-emphasized design and optimization of RRAM using SPICE model. In 2015 Design, Automation & Test in Europe Conference & Exhibition (DATE) (pp. 1425-1430). IEEE.