Takuya Sakuma
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 【論文読み】Image Classification at Supercomputer Scale ## 意義 - レプリカごとのバッチサイズを制御するための分散BacthNormalization - モデルのスループットを維持しながらの入力パイプラインの最適化 - 勾配の総和を高速で計算するための2Dトーラスの[All-Resuce](https://hackmd.io/PJ3cBX82TWeoJkMBl8C90w?view#All-Reduce) 以上の最適化によってResNet-50に対するImageNetの学習を2.2分で終わらせ76.3%のAccuracyという記録を達成した ↑最適化についてよくわかっていない ## 背景 ### 浮動小数の計算負荷 ニューラルネットワークではコア上でSGDで最適化するが、収束するまでに非常に多くの浮動小数計算が行われる - [浮動小数の表示方法](http://www.altima.jp/column/fpga_edison/bit_number_float.html)から分かるように明らかに計算に時間がかかる - ResNet-50では$3.2×10^16$の浮動小数計算がある ### 分散深層学習の問題 深層学習の計算時間を短縮するために分散SGDを利用することで複数のデバイス間で並列化することができる。こうした状況では歴史的にしばしば非同期分散SGDが利用される。しかし、非同期分散SGDは収束時間と最終的な検証制度の点から同期SGDと同様にパフォーマンスは得られない([後で読む]((https://arxiv.org/pdf/1604.00981.pdf)))。大規模な機械学習においては同期分散学習においてもモデルのクオリティを維持するためには通常と同じような機械学習の技術が必要(ここよくわからん、上のを読む必要あり?) ##### 現状の問題点 - モデルの精度はバッチサイズや各Replicaごとのバッチサイズに依存する - CPUによる入力がボトルネックになる - 分散同期SGDでは拡張性の高い(おそらくどのくらいの頻度で行うかということ)All-Redeceが必要 #### 並列分散訓練 うまくまとまっているサイトがあったので[参照](https://serihiro.hatenablog.com/entry/2018/06/02/224748)のこと。 ##### 同期分散SGD 深層学習の計算時間を短縮するために多数のプロセス(WorkerまたはReplica)に分散して計算する手法。通常の学習と異なり、BackwardとOptimizeのステップの間にAll-Redeceステップが挟まれる。 イメージは以下の通り。なお、必ずしも各ステップごとにAll-Redeceを行うわけではない。(設計者が定義する必要あり) ![](https://i.imgur.com/iPB1xYi.png) ###### All-Reduce 各WorkerがBackwardで求めた勾配の平均を計算し全てのWorkerに配る。その後のOptimizeステップではこうして求められた平均の勾配が利用される。All-Redeceでは全てのプロセスが持っている配列データを集約しその結果を再配分する操作を指す。 ([参照](https://research.preferred.jp/2017/05/chainermn-beta-release/)) ##### 非同期分散SGD 各Workerが求めた勾配をパラメーターサーバに送信し、その勾配を用いてパラメータサーバが各Workerの平均勾配を計算し重みを更新する。各Workerは次の訓練を開始する前に、パラメーターサーバから最新の鮮度の高い勾配を取得し、それを用いて訓練を行う。 アルゴリズムは以下。 [![Image from Gyazo](https://i.gyazo.com/b5ef612ecb9c596dc68d24b3dfeacee1.png)](https://gyazo.com/b5ef612ecb9c596dc68d24b3dfeacee1) [[Jianmin Chen, Rajat Monga, Samy Bengio, and Rafal Józefowicz, 2016](https://arxiv.org/pdf/1604.00981.pdf)] ## 方法 #### 混合精度の利用 各ステップごとに異なる精度の浮動小数を利用することで高速化を図る。 - 畳み込み層、入力画像、アクティベーション 16bit(半精度floatまたはhalf)を利用。モデル内の全ての浮動小数計算の中で畳み込み層が占める割合が圧倒的に多く、計算とメモリアクセスのオーバーヘッドの体部分を占めているため。精度を下げてもほとんど(全く)モデルの精度に影響がないらしい。 - その他(Batch Normalization、loss計算、勾配の合計など) 32bitを利用。精度の維持が目的。 #### 学習率 学習率はバッチサイズに比例させると良いという研究結果をもとに([後で読む](https://arxiv.org/pdf/1705.08741.pdf))linear learning rate scalingを採用(バッチサイズを2倍すると同時に学習率も2倍にする)。さらに段階的な学習速度のウォームアップと学習速度の減衰を利用(何それ)。 #### バッチサイズ [LARS](https://arxiv.org/pdf/1708.03888.pdf)最適化によってモデルのクオリティを下げることなくバッチサイズを32768まで向上させることができる。これによってTPUにおけるスループットも上昇させることができる。 ##### スループット モデルにデータを食わせる速度 ### Distributed Batch Normalization #### 以前までの問題点 - 通常の分散深層学習においてはBatch Normalizationは各Workerごとに行うことで通信によるオーバヘッドを削減する。 - 実際にはバッチサイズによってモデルの性能が大きく変動する - ResNet-50では各replicaのバッチサイズを32以下にすると検証精度のピークで収束しなくなる - Batch Normalizationにおいては学習時と推論時で正規化のされ方が異なる(方法は様々あるが、一般的には学習時の全体の平均と分散を覚えておく)ことが原因。 - バッチサイズが小さい 母集団の統計量から乖離してしまう - 事例が独立同分布でない バッチごとに過学習してしまう - 大規模にデータを並列して学習させるには、全体のバッチサイズ大きくするまたはreplicaごとのバッチサイズを小さくさせる必要がある。(常識的に考えてあたり前)一方で全体のバッチサイズが大きくなるほど学習に悪影響をもたらすことが示されている。 - 同じエポック数で学習を行う場合、イテレーション数が減ってしまうため、モデルが成熟しない。 - 勾配の分散が小さくなること(多分各バッチの目的関数がほとんど同じ形になってしまうこと)により、性質の悪い局所解(sharp minima と呼ばれます)に進みやすくなってしまい、結果として得られるモデルの汎化性能が悪くなってしまう。 #### 提案手法 1. レプリカごとに平均と分散を計算 2. いくつかのレプリカをサブグループとしてまとめ、1.を利用してサブグループの平均と分散を計算 3. 以上をBatch Normalizationの平均と分散として利用する ![](https://i.imgur.com/bh2aILm.png) このようにすることでサブグループの数によってBatch Normalizationに利用するバッチ数を全体のバッチ数から独立して制御することができる #### 分析 以下にグループサイズの大きさと検証精度及びトレーニング時間との関係を示す。この実験はアクセラレータとしてTPU v2 Podを利用しており、LARS最適化をせずにResNet-50をImageNetで学習させている。効果を及ぼすバッチサイズが16の時、すなわちグループサイズが1の時(=レプリカがそれぞれ平均と分散を共有しない時)をベースラインとするとバッチサイズが64、すなわちグループサイズを4としたときに最も検証精度が高い。 ![Image from Gyazo](https://i.gyazo.com/370b8b04e05e941307bfdac23a85fcc0.png) ### Input pipeline optimization #### 以前までの問題点 - ファイルの入力、パース、前処理、シャッフル等のインプットがfoward backward処理よりも遅くなった場合、ボトルネックとなる - 実際多くの面でこうした処理はアクセラレータ上でサポートされていないため、CPUで処理するしかない #### 提案手法 ##### データセットの共有とキャッシュ 本来ならば一度読み込んだデータセットはそれ以上利用しなくなるまでホストのメモリ上に保持していたいが、実際にはデータセットは膨大であるため不可能。大量のワーカーを利用することによって、ワーカー間でのデータセットの共有及び効率的なアクセスパターンの実現が可能となる。 ##### 入力や計算に向けたデータの事前読み込み CPUアクセラレータ上でバッチ学習をしている間、次のバッチを入力パイプラインで同時に処理することで、CPUとアクセラレータ間での待機時間の短縮を行うことができる。 ```さらに、ImageNet データセットにはさまざまなサイズのイメージがあるため、小さなイメージを処理するときにプリフェッチによってヘッドルームが作成されるため、より大きなイメージを処理する必要がある場合でも、デバイスの前にとどまることができます。(よくわからん)``` ##### JPEGのデコードとトリミング ImageNetなどのデータセットは多くの場合、JPEGにエンコードされている。トリミングはData Augmentationとして利用される。元の画像をデコードしてからパースするよりもトリミングした後にエンコードされた画像の関連部分だけを取り出す方がオーバーヘッドが少なく効率的 ##### データパースの並列 データのパースや前処理は入力パイプラインの中でも最も負荷の高いものとなることがある。マルチコアのCPUによって並列化することによってスループットを向上させることができる。 #### 分析 以下に提案した4つの手法を加えた場合、及び削除した場合のスループットの変化を示す。なお、全ての実験は1台のIntel Skylakeプロセッサーを用いて、TensorFlowのAPIを利用している。この実験の結果は2000万枚の画像を1000枚ごとに平均して実行されたものであり、値はホストCPUごとに観測された平均のスループットである。 この実験結果から並列化が最も効果的であり、ベースラインの2倍以上のスループットを記録している。 ![](https://i.imgur.com/GA3G1Qx.png) ### 2次元の勾配和計算 #### 以前までの問題点 以前のAll-Redeceではリングベースで全体の勾配和計算が行われていたが、TPU Podにおいては、チャンクを全てのプロセスに渡って運ぶ遅延によって制限されていた。 [^2]:https://research.preferred.jp/2018/07/prototype-allreduce-library/ ##### リングベースのAll-Redece[^2] 総プロセス数$P$とし、各プロセスに$1$から$P$までの番号がついているとする。そしてあるプロセス$p$はプロセス$p+1$と接続していると考える。ただし、プロセス$P$はプロセス$1$に接続しており、全体としてリングを構成しているものとする。この時、次のアルゴリズムによってAll-Redeceを計算する 1. プロセス$p$は自身の配列を$P$個のチャンクに分割する。この時、$p$個目のチャンクを$[p]$と表す。 2. $[p]$をプロセス$p+1$へと送信し、同時にプロセス$p-1$から受信した$[p-1]$を自身の$[p-1]$と足し合わせる(=集約する)。 3. 足し合わせた$[p-1]$を再びプロセス$p+1$に送信する。 4. 1-3の操作を$P-1$回繰り返す。 5. 各プロセスはは集約済みのチャンクを一つずつ保持することになるため、各々がリング上にあるチャンクに自分のチャンクを併せていくことで最終的に全プロセスのチャンクの集約を手に入れることができる。 #### 提案手法 総プロセス数を$P^2$とし、各プロセスを2次元に並べた場合を考える。それぞれが持つテンソル(上で言う配列)を半分に分割する。そして以下の2段階の処理を行う。 ##### 第一段階 1. 左半分のテンソルは各々縦方向に、右半分のテンソルは横方向にそれぞれリングを構成する。 2. 構成されたリングに対してリングベースのAll-Redece処理を適応する ##### 第二段階 1. 左半分のテンソルは横方向、右半分のテンソルは縦方向にリングを構成する 2. 第一段階と同様にAll-Redeceを適応する <!-- 総プロセス数を$P^2$とする。プロセスを2次元に並べたとき、$(i,j)$に位置するプロセスをプロセス$P_{ij}$と表す。そして、プロセス$P_{ij}$が持つテンソル(配列)を半分に分割し、左半分を$[P_{ij}]_L$、右半分を$[P_{ij}]_R$とする。 --> ![](https://i.imgur.com/L9sQLJ2.png) $3 \times 3$のプロセスの場合の処理 #### 分析 以下にResNet-50の学習がTPU v2チップの数によってどのように変化するかを示したグラフを示す。2次元の勾配計算和計算は1次元と比べて遅延が少なく、高いスループットを維持する。256チップではトーラスのリンクはより効率的に計算が行われていることが読み取れる。 ![Image from Gyazo](https://i.gyazo.com/94197e2ac34df1fa534b517f97302721.png) ## 結果 以下にResNet-50をImageNetで学習した結果を示す。比較対象は公表当時SOTAであったものである。なお、必要なエポック数を減らすことでより精度を改善できることが期待されるが、他の他の手法と比較できるように、意図的にエポック数を90としている。なお、時間の計測はTPUバイナリのジャストインタイムコンパイルが終了した瞬間からファイルに終了時間を書き込むまでとしている。精度は検証セットからブラックリストサンプルを抜いたものに対して計測したものである。 ![Image from Gyazo](https://i.gyazo.com/702734ce40dd2575af29260f0364dabf.png) ## 以下メモ ### 大きなバッチ学習 大きなバッチサイズを設けた学習では汎化性能が落ちてしまうという問題がある。これは、各バッチの目的関数がほとんど同じ形をしてしまうことで局所解に陥りやすくなるため(と聞いたことがある・・・) ## 参考 [^1][^2] [^1]:https://www.slideshare.net/DeepLearningJP2016/dlbatch-renormalization-towards-reducing-minibatch-dependence-in-batchnormalized-models [^2]:https://jinbeizame.hateblo.jp/entry/understanding_batchnorm [エポック数の話](http://st-hakky.hatenablog.com/entry/2017/01/17/16513) [chainerMNの話](https://research.preferred.jp/2017/02/chainermn-benchmark-results/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully