Upgrade to Pro — share decks privately, control downloads, hide ads and more …

実践的!FPGA開発セミナーvol.21 / FPGA_seminar_21_fixstars_corporation_20230426

実践的!FPGA開発セミナーvol.21 / FPGA_seminar_21_fixstars_corporation_20230426

2023年4月26日に開催した、「実践的!FPGA開発セミナーvol.21」の当日資料です。

More Decks by 株式会社フィックスターズ

Other Decks in Science

Transcript

  1. Copyright© Fixstars Group
    実践的!FPGA開発セミナー
    vol.21
    2023/04/26 18:00~
    1

    View Slide

  2. Copyright© Fixstars Group
    AI Engine によるテンプレート
    マッチング高速化手法の解説
    2

    View Slide

  3. Copyright© Fixstars Group
    Who I am
    写真
    Hiroki
    NISHIMOTO
    西本 宏樹
    ソリューション第四事業部 
    エンジニア
    3

    View Slide

  4. Copyright© Fixstars Group
    アウトライン
    - AI Engineとは?
    - テンプレートマッチングの紹介
    - 実装にあたっての7つのステップ
    - まとめ
    4

    View Slide

  5. Copyright© Fixstars Group
    アウトライン
    - AI Engineとは?
    - テンプレートマッチングの紹介
    - 実装にあたっての7つのステップ
    - まとめ
    5

    View Slide

  6. Copyright© Fixstars Group
    AI Engineとは?
    - AMD社によって開発されたプロセッサ
    - 電力効率が良い & 演算処理が高速
    - PL(FPGA)とAXI4-Streamで接続されている
    6
    ※ https://japan.xilinx.com/products/technology/ai-engine.htmlより引用
    ※ ACAP概要図
    → FPGA + AI Engineでより高速に演算が可能に

    View Slide

  7. Copyright© Fixstars Group
    どんなアーキテクチャか
    - 強力なベクトル演算ユニットを搭載
    - 沢山のAI Engineコアが接続されたAIE Arrayで構成されている
    - データ処理→次のコアへ流す
    → マルチコアの並列性とパイプラインを兼ね備えたアーキテクチャ
    応用範囲はAIだけではない
    7
    ※ https://japan.xilinx.com/products/technology/ai-engine.htmlより引用


    View Slide

  8. Copyright© Fixstars Group
    どういう構成なのか
    - ホストからメモリを介してPL→AIE Arrayに送る
    - PL->AIE Arrayの送信方法はAXI4-Stream
    8
    AIE Array ※
    ※ https://japan.xilinx.com/products/technology/ai-engine.htmlより引用

    View Slide

  9. Copyright© Fixstars Group
    アウトライン
    - AI Engineとは?
    - テンプレートマッチングの紹介
    - 実装にあたっての7つのステップ
    - まとめ
    9

    View Slide

  10. Copyright© Fixstars Group
    テンプレートマッチングとは?
    画像の一部を切り出したものから、
    その一部が切り出し元の画像のどの位置かを探索するタスク
    テンプレート画像
    元画像
    10
    応用事例)
    - 深度推定
    - 物体追跡

    View Slide

  11. Copyright© Fixstars Group
    どういうアルゴリズムを使用する?
    色々あるが、今回はSAD(Sum of Absolute Difference)を採用
    テンプレートのサイズと同じ枠を走査し、
    画素間の差異が最小の位置を探索する。
    元画像
    11

    View Slide

  12. Copyright© Fixstars Group
    アウトライン
    - AI Engineとは?
    - テンプレートマッチングの紹介
    - 実装にあたっての7つのステップ
    - まとめ
    12

    View Slide

  13. Copyright© Fixstars Group
    テンプレートマッチングのAI Engineへの落とし込み
    AI Engine
    ① テンプレート画像のAIEへのロード
    ② 検証対象データのAIEへのロード
    ③ AIE内部でSADを計算
    結果を
    ホストへ送信

    13

    View Slide

  14. Copyright© Fixstars Group
    実装にあたっての7つのステップ
    1. リファレンスプログラムの実装
    2. AI Engine上で動作するカーネルの実装
    3. AI Engine Graphの実装
    4. AI Engineのシミュレーション
    5. AI Engineとホストを繋ぐ、PLの実装
    6. ホストプログラムを書く
    7. HW Emulation or 実機での動作確認
    14

    View Slide

  15. Copyright© Fixstars Group
    実装にあたっての7つのステップ
    1. リファレンスプログラムの実装
    2. AI Engine上で動作するカーネルの実装
    3. AI Engine Graphの実装
    4. AI Engineのシミュレーション
    5. AI Engineとホストを繋ぐ、PLの実装
    6. ホストプログラムを書く
    7. HW Emulation
    8. 実機での動作確認
    15

    View Slide

  16. Copyright© Fixstars Group
    リファレンスプログラムの実装
    16
    元・テンプレート画像読み込み
    グレースケール化
    SAD計算
    終了
    for( int hi = 0 ; hi < SRC_H - TMP_H ; ++hi ){
    for( int wi = 0 ; wi < SRC_W - TMP_W ; ++wi ){
    // 検証範囲を切り出し
    uint8_t *ins_img_gray = cut_img();
    // sad score計算関数実行
    uint32_t score = sad_part(
    ins_img_gray,
    tmp_img_gray,
    TMP_SIZE)
    best_score = max(score, best_score);
    }
    }

    View Slide

  17. Copyright© Fixstars Group
    SAD値計算部分概要
    uint32_t sad_part(const uint8_t *src, const uint8_t *tmp, const uint32_t TMP_SIZE
    ){
    uint32_t score = 0;
    for( uint32_t idx = 0 ; idx < TMP_SIZE ; ++idx ){
    const uint8_t comp_src_val = src[ idx ];
    const uint8_t comp_tmp_val = tmp[ idx ];
    if( comp_src_val >= comp_tmp_val )
    score += (comp_src_val - comp_tmp_val);
    else
    score += (comp_tmp_val - comp_src_val);
    }
    return score;
    }
    元画像とテンプレート画像の
    全ピクセルを走査し、
    2つの画像の差異を取得
    17

    View Slide

  18. Copyright© Fixstars Group
    実装にあたっての8つのステップ
    1. リファレンスプログラムの実装
    2. AI Engine上で動作するカーネルの実装
    3. AI Engine Graphの実装
    4. AI Engineのシミュレーション
    5. AI Engineとホストを繋ぐ、PLの実装
    6. ホストプログラムを書く
    7. HW Emulation or 実機での動作確認
    18

    View Slide

  19. Copyright© Fixstars Group
    テンプレートマッチングのAI Engineへの落とし込み
    AI Engine
    ① テンプレート画像のAIEへのロード
    ② 検証対象データのAIEへのロード
    ③ AIE内部でSADを計算
    結果を
    ホストへ送信

    19

    View Slide

  20. Copyright© Fixstars Group
    AIEへの落とし込み(入力部分)
    void compute_krnl( input_stream* dat, output_stream* out){
    aie::vector tmp_vecs[READ_NUM];
    for ( itr_t i = 0 ; i < READ_NUM ; ++i )
    tmp_vecs[i] = readincr_v(dat);
    while(true){
    uint32_t sad_val = 0;
    for( itr_t bi = 0 ; bi < READ_NUM ; bi++ ){
    aie::vector src_vec = readincr_v(dat);
    const uint32_t score = compute_score( src_vec, tmp_vecs[bi]);
    sad_val += score;
    }
    writeincr(out, sad_val);
    }
    }
    20
    テンプレートデータを入力
    切り出した元画像を入力し、差異を計算
    テンプレートサイズ画像と
    同ピクセル計算した後、出力

    View Slide

  21. Copyright© Fixstars Group
    SADカーネルの実装 -aie::vectorをフル活用する-


    検証対象画像
    テンプレート

    大小マスク


    大きい方のベクタ
    小さい方のベクタ

    絶対値差分のベクタ
    絶対値差分の総和
    aie::vector関数を活用して高速化
    21

    View Slide

  22. Copyright© Fixstars Group
    AIEへの落とし込み(画素の差異の計算)
    uint32_t compute_score(
    aie::vector src_vec,
    aie::vector tmp_vec
    ){
    auto msk_lt = aie::lt( src_vec, tmp_vec );
    aie::vector grt_vec = aie::select(src_vec, tmp_vec, msk_lt );
    aie::vector les_vec = aie::select(tmp_vec, src_vec, msk_lt );
    uint32_t score = 0;
    aie::vector sub_vec = aie::sub(grt_vec, les_vec);
    aie::vector red_vec; // reduce用の配列. 16bit
    for( itr_t wi = 0; wi < READ_BYTE_SIZE/2; ++wi )
    red_vec[wi] = sub_vec[wi] + sub_vec[wi + READ_BYTE_SIZE / 2];
    score = aie::reduce_add(red_vec);
    return score;
    }
    大小のマスクを取得
    対応ピクセルごとに大小を分ける
    大きい方から小さい方を引いた値を取得
    差をreduceで計算
    22

    View Slide

  23. Copyright© Fixstars Group
    実装にあたっての8つのステップ
    1. リファレンスプログラムの実装
    2. AI Engine上で動作するカーネルの実装
    3. AI Engine Graphの実装
    4. AI Engineのシミュレーション
    5. AI Engineとホストを繋ぐ、PLの実装
    6. ホストプログラムを書く
    7. HW Emulation or 実機での動作確認
    23

    View Slide

  24. Copyright© Fixstars Group
    AI Engine Graphでカーネル同士を繋ぎフローを作成
    2入力を受け取り、
    その和を出力するカーネルを作成し
    先ほどのカーネルを並列化
    24

    View Slide

  25. Copyright© Fixstars Group
    AI Engine Graphでカーネル同士を繋ぎフローを作成
    class compute_graph : public adf::graph {
    private :
    adf::kernel krnl;
    public :
    adf::port dat_in;
    adf::port res_out;
    compute_graph(){
    krnl = adf::kernel::create(compute_krnl);
    adf::source(krnl) = "../src/compute_krnl.cpp"; // カーネル読み込み
    adf::connect(dat_in, krnl.in[0]);
    adf::connect(krnl.out[0], res_out);
    adf::runtime(krnl) = 1.0;
    };
    };
    25
    入出力ポート
    カーネル
    入力→カーネル→出力

    View Slide

  26. Copyright© Fixstars Group
    AI Engine Graphでカーネル同士を繋ぎフローを作成
    // 0段目
    for( itr_t ii = 0 ; ii < PARALLEL_NUM / 2 ; ++ii ){
    adf::connect( reduce_in[ii * 2 ] , krnl[ii].in[0]);
    adf::connect( reduce_in[ii * 2 + 1 ], krnl[ii].in[1]);
    }
    // 中段
    uint32_t ofs = 0;
    for( itr_t pi = PARALLEL_NUM / 2; pi > 1; pi = pi >> 1 ){
    for( itr_t ii = 0 ; ii < pi; ++ii)
    adf::connect( krnl[ ii + ofs ].out[0], krnl[ ii / 2 + ofs + pi].in[ ii % 2 ]);
    ofs += pi;
    }
    // 最終段
    adf::connect( krnl[ PARALLEL_NUM - 1 - 1 ].out[0] , reduce_out );
    26

    View Slide

  27. Copyright© Fixstars Group
    実装にあたっての8つのステップ
    1. リファレンスプログラムの実装
    2. AI Engine上で動作するカーネルの実装
    3. AI Engine Graphの実装
    4. AI Engineのシミュレーション
    5. AI Engineとホストを繋ぐ、PLの実装
    6. ホストプログラムを書く
    7. HW Emulation
    8. 実機での動作確認
    27

    View Slide

  28. Copyright© Fixstars Group
    AI Engine『だけ』のシミュレーション
    各ポートへの入力ファイルを用意するだけで、
    AI Engine部分だけのシミュレーションが可能!
    ここだけ!
    ここでテストを通しておけば安心して以降の実装に進める
    1923201977
    3648631153
    3015563216
    2931403443
    3535452630
    1352627154
    1599623816
    1095908160
    ……
    input_0.txt
    8bit × 4 = 32
    28

    View Slide

  29. Copyright© Fixstars Group
    シミュレーションの出力
    ?
    =
    ホストプログラムなしで出来るので、問題の切り分けが楽
    期待値 シミュレーション出力
    29

    View Slide

  30. Copyright© Fixstars Group
    実装にあたっての8つのステップ
    1. リファレンスプログラムの実装
    2. AI Engine上で動作するカーネルの実装
    3. AI Engine Graphの実装
    4. AI Engineのシミュレーション
    5. AI Engineとホストを繋ぐ、PLの実装
    6. ホストプログラムを書く
    7. HW Emulation or 実機での動作確認
    30

    View Slide

  31. Copyright© Fixstars Group
    AI Engineとホストを繋ぐ、PLの実装
    31

    View Slide

  32. Copyright© Fixstars Group
    AI Engineとホストを繋ぐ、PLの実装
    void mm2s( ap_uint<32>* mem, hls::stream>& str, int size ) {
    for (int i = 0; i < size; i++) {
    ap_axiu<32, 0, 0, 0> x;
    x.data = mem[i];
    x.keep = -1; // バイトイネーブルのフラグをすべて立てる
    str.write(x);
    }
    }
    void s2mm( ap_uint<32>* mem, hls::stream>& str, int size) {
    for (int i = 0; i < size; i++){
    auto x = str.read();
    mem[i] = x.data;
    }
    }
    32

    View Slide

  33. Copyright© Fixstars Group
    実装にあたっての7つのステップ
    1. リファレンスプログラムの実装
    2. AI Engine上で動作するカーネルの実装
    3. AI Engine Graphの実装
    4. AI Engineのシミュレーション
    5. AI Engineとホストを繋ぐ、PLの実装
    6. ホストプログラムを書く
    7. HW Emulation or 実機での動作確認
    33

    View Slide

  34. Copyright© Fixstars Group
    リファレンスのC++実装に
    何を足せばAI Engineを使えるのか
    1. デバイスオープン
    2. xclbinをデバイスに書き込み
    3. カーネルの作成
    4. バッファオブジェクトの作成
    5. ホスト側のバッファポインタをユーザー空間にマップ
    6. バッファへのデータ書き込み&ホストからデバイスバッファへの内容の同期
    7. カーネルの起動&完了待機
    8. デバイスからホストへバッファの内容を同期
    34

    View Slide

  35. Copyright© Fixstars Group
    1&2. デバイスオープン&xclbinの書き込み
    35
    const int device_index = 0;
    const std::string xclbin_file = argv[1];
    auto device = xrt::device(device_index); // デバイスのオープン
    auto uuid = device.load_xclbin(xclbin_file); // xcl_binのデバイスの書き込み
    用意されている関数に渡すだけです

    View Slide

  36. Copyright© Fixstars Group
    3. カーネル作成
    36
    // PARALLEL_NUM 並列でのカーネル送信作成
    std::vector< xrt::kernel > mm2s_krnls;
    for(itr_t pi = 0; pi < PARALLEL_NUM ; pi++ ){
    mm2s_krnls.push_back(
    xrt::kernel(device, uuid, "mm2s:{mm2s_" + std::to_string(pi + 1) + "}")
    );
    }
    // 受信カーネルの作成
    auto s2mm_krnl = xrt::kernel(device, uuid, "s2mm:{s2mm_1}");
    用意されている関数をほぼ叩くだけでできます

    View Slide

  37. Copyright© Fixstars Group
    4. バッファオブジェクト作成
    37
    // PARALLEL_NUM並列での送信用バッファの作成
    std::vector< xrt::bo > mm2s_bos;
    for(itr_t pi = 0; pi < PARALLEL_NUM ; pi++ ){
    mm2s_bos.push_back( xrt::bo(device, sizeof(uint8_t) * IMG_SIZE /       
    PARALLEL_NUM, mm2s_krnls[pi].group_id(0)) );
    }
    // 受信用バッファオブジェクトの作成
    auto s2mm_bo = xrt::bo(device, sizeof(uint32_t) * TRIAL_CNT, \
    s2mm_krnl.group_id(0));
    欲しいサイズをbyte単位で指定するだけでできます

    View Slide

  38. Copyright© Fixstars Group
    5. ホスト側のバッファポインタを
    ユーザー空間にマップする
    38
    // PARALLEL_NUM並列での送信用バッファの作成
    std::vector mm2s_mapd_bufs;
    for(itr_t pi = 0; pi < PARALLEL_NUM ; pi++ ){
    mm2s_mapd_bufs.push_back( mm2s_bos[pi].map() );
    }
    // 受信用バッファオブジェクトのマップ
    auto s2mm_mapd_buf = s2mm_bo.map();
    バッファオブジェクトのmap関数を叩くだけです

    View Slide

  39. Copyright© Fixstars Group
    6. バッファへのデータ書き込み
    &ホストからデバイスバッファへの内容の同期
    39
    // マップした送信用バッファへのデータを書き込み
    for ( itr_t pi = 0 ; pi < PARALLEL_NUM ; pi++ ){
    const uint32_t SIZE_PER_UNIT = IMG_SIZE / PARALLEL_NUM;
    const uint32_t OFS = SIZE_PER_UNIT * pi;
    for( int bi = 0; bi < SIZE_PER_UNIT ; bi++ ){
    mm2s_mapd_bufs[pi][bi] = tmp_img_gray[ bi + OFS ];
    }
    }
    for ( itr_t pi = 0 ; pi < PARALLEL_NUM ; pi++ )
    mm2s_bos[pi].sync(XCL_BO_SYNC_BO_TO_DEVICE); // 同期
    書き込んで同期関数を叩くだけです

    View Slide

  40. Copyright© Fixstars Group
    7. カーネルの起動&完了待機
    40
    std::vector< xrt::run > mm2s_runs;
    for ( itr_t pi = 0 ; pi < PARALLEL_NUM ; pi++ ){
    mm2s_runs.push_back (
    mm2s_krnls[pi]( mm2s_bos[pi], nullptr, sizeof(uint8_t)
    * IMG_SIZE / PARALLEL_NUM / 4) );
    }
    for ( itr_t pi = 0 ; pi < PARALLEL_NUM ; pi++ )
    mm2s_runs[pi].wait(); // カーネル終了の待機
    3で作ったカーネルにbuffer objectと
    『何回実行してほしいか』を渡すだけです

    View Slide

  41. Copyright© Fixstars Group
    リファレンスプログラムのAIE 対応
    41
    元・テンプレート画像読み込み
    グレースケール化
    SAD計算
    終了
    [AIE 準備]
    [テンプレート画像送信
    ]
    [s2mm 受信開始]
    for( int hi = 0 ; hi < SRC_H - TMP_H ; ++hi ){
    for( int wi = 0 ; wi < SRC_W - TMP_W ; ++wi ){
    [ mm2s送信処理(一枚ずつ) ]
    }
    }
    [s2mm 受信待機]

    View Slide

  42. Copyright© Fixstars Group
    実装にあたっての7つのステップ
    1. リファレンスプログラムの実装
    2. AI Engine上で動作するカーネルの実装
    3. AI Engine Graphの実装
    4. AI Engineのシミュレーション
    5. AI Engineとホストを繋ぐ、PLの実装
    6. ホストプログラムを書く
    7. HW Emulation or 実機での動作確認
    42

    View Slide

  43. Copyright© Fixstars Group
    ホストを含めたHW Emulation
    実機で動かないときも波形で確認できる
    合成後、ホストで先に実行したが
    期待値一致しなかったため
    HW Emulationで波形で確認した
    43

    View Slide

  44. Copyright© Fixstars Group
    動作確認
    - 期待値との一致を確認
    44
    元画像 テンプレート画像 出力画像
    - 速度検証
    CPU : 600 ms
    AIE : 7000ms

    View Slide

  45. Copyright© Fixstars Group
    改良 ホスト→AIE の送信を減らす
    45
    元・テンプレート画像読み込み
    グレースケール化
    SAD計算
    終了
    [AIE 準備]
    [テンプレート画像送信
    ]
    for( int hi = 0 ; hi < SRC_H - TMP_H ; ++hi ){
    for( int wi = 0 ; wi < SRC_W - TMP_W ; ++wi ){
    [検証用元画像格納]
    }
    }
    [s2mm 受信開始]
    [mm2s送信処理 (全部) ]
    [s2mm 受信待機]
    AIE : 650 ms

    View Slide

  46. Copyright© Fixstars Group
    アウトライン
    - AI Engineとは?
    - テンプレートマッチングの紹介
    - 実装にあたっての7つのステップ
    - まとめ
    46

    View Slide

  47. Copyright© Fixstars Group
    まとめ
    - AI Engineでテンプレートマッチングを実装
    - 実機で動かすまでの7ステップの解説
    - 動かなかった際のデバッグは大変ですが、
    シミュレーションが充実しているので追える
    - 今後、カーネルの変更含め、最適化します
    47

    View Slide

  48. Copyright© Fixstars Group
    Lightning Talk!
    48

    View Slide

  49. Copyright© Fixstars Group
    AI Engine 適用例の解説
    ~FFT 演算の実装~
    49

    View Slide

  50. Copyright© Fixstars Group
    Who I am
    写真
    Ryuji
    NISHIDA
    西田 竜之
    ソリューション第四事業部 
    シニアエンジニア
    50

    View Slide

  51. Copyright© Fixstars Group
    自己紹介
    ● 西田竜之
    ○ FPGAを用いたシステム開発に従事
    ○ ハードウェア開発をメインに担当
    ○ 略歴
    ■ 半導体ベンダ
    ● サーバー向けASIC開発
    ■ 映像事務機メーカー
    ● 高画質エンジンLSI 映像機器向けFPGA開発
    ■ フィックスターズ
    ● FPGAを用いた高速取引金融システム
    ● OpenCLによるアプリの高速化
    51

    View Slide

  52. Copyright© Fixstars Group
    アジェンダ
    ● AI Engine FFT 演算 背景&目的
    ● 単一カーネルでの FFT の実装
    ● 複数カーネルを用いた FFT の実装
    ● 参考:AMD 提供サンプルデザイン(XAPP1356)の実装
    ● まとめ
    52

    View Slide

  53. Copyright© Fixstars Group
    AI Engine FFT 演算 背景&目的
    ● Versal AI Engine
    ○ 演算負荷の高いアプリケーションに有用
    ○ 性能を発揮するには・・・
    ■ AI Engine に適したアルゴリズムとプログラミングが必要
    ■ AI Engine のアーキテクチャ、特徴の把握が必要
    ● FFT 演算
    ○ 演算負荷の高い代表的なアプリケーションの1つ
    ■ AI Engine FFT 演算は、DSP Library を用いてすでに利用可能
    今回、AI Engine の理解を深めるために、なじみのある FFT 実装を題材に
    試行した結果を共有する
    53

    View Slide

  54. Copyright© Fixstars Group
    AI Engine FFT 演算 背景&目的
    ● FFT 演算
    (図:時間間引き16点)
    ○ 演算(複素数の加算、乗算)⇒ メモリ保持(データ並び替え)⇒ … の繰り返し
    ○ 演算全体を時間的、空間的にどう割り付けるかによって構成が変わる
    54
    データ数分
    繰り返し
    O(N)
    stage 数分繰り返し O(logN)

    View Slide

  55. Copyright© Fixstars Group
    単一カーネルでの FFT 実装
    ● データ型:単精度複素数
    ○ cfloat 型を使用する ⇒ 複素数演算の記述が容易に可能
    ■ .real, .imag で実部、虚部にアクセスする
    ● AI Engine メモリ容量
    ○ 1コア辺りのデータメモリ 32KByte
    ○ 隣接する4コアのメモリを共有可能 Total 128KByte
    ● FFT データ点数 1024点
    ○ 入出力、中間のバッファが必要(4 ケ程度)
    ○ 8Byte (=sizeof(cfloat)) x 1024 点 x ~4ケ < 32KByte
    ※ 隣接コアのメモリを使用すれば、これより大きな点数も実現できる可能性はある(未試行)
    55
    参照URL
    https://docs.xilinx.com/r/ja-JP/am009-versal-ai-engine/AI-%E3%82%A8%E3%83%B3%E3%82%B8%E3%83%B3%E3%8
    1%AE%E3%83%A1%E3%83%A2%E3%83%AA-%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB

    View Slide

  56. Copyright© Fixstars Group
    単一カーネルでの FFT 実装
    ● カーネルコード
    56
    ← Butterfly演算
      &並び替え
    10 stage分
    繰り返し
    Windowアクセス
    (=共有メモリを介した転送)
    ● グラフコード
    外部 I/F からWindowアクセスする
    ための接続

    View Slide

  57. Copyright© Fixstars Group
    単一カーネルでの FFT 実装
    ● 演算機能(バタフライ演算)の記述
    ○ AI Engine Intrinsics
    より高速、効率的な動作をさせるには、Intrinsics を利用する
    https://www.xilinx.com/htmldocs/xilinx2022_2/aiengine_intrinsics/intrinsics/index.html
    ■ ベクタ演算エンジンの利用、パイプライン動作など
    ■ FFT 用の Intrinsics も存在
    ただし、cfloat 対応の FFT 演算が見当たらない、
    仕様理解に時間がかかる(難解)ため、今回未適用
    ○ AI Engine API
    Intrinsics よりも抽象度の高い C++ ヘッダライブラリ
    https://www.xilinx.com/htmldocs/xilinx2022_2/aiengine_api/aie_api/doc/index.html
    ■ fft_dit も使用可能
    適用を試みたが期待通りの演算ができず適用断念
    パラメータ、入出力データ設定を探ったものの、
    適切な使い方がつかめなかった
    57
    参照URL
    https://japan.xilinx.com/developer/articles/aie-kernel-programming-vitis-ai
    e-api.html

    View Slide

  58. Copyright© Fixstars Group
    単一カーネルでの FFT 実装
    ● FFT 1000 point プロファイル結果 (aiesimulator 実行)
    ○ 演算実行サイクルで埋まっている 効率的に演算を実行させる工夫が必要
    58
    Total 5,720,000 cycle
    = 5.72msec @ 1GHz
    (拡大)

    View Slide

  59. Copyright© Fixstars Group
    複数カーネルでの FFT 実装
    ● カーネル分割
    ○ 共有メモリを介して
    カーネルを直列に接続
    ○ 連続したデータ投入でも
    スループットを確保できる
    構成
    59
    データ数分
    繰り返し
    O(N)
    stage 数分カーネルを直列に配置
    参照URL
    https://docs.xilinx.com/r/ja-JP/am009-versal-ai-engine/%E5%85%B1%E6%9C%89%E3%83%A1%E3%83%A2%E3%83%AA%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81
    %9F-AI-%E3%82%A8%E3%83%B3%E3%82%B8%E3%83%B3%E3%81%8B%E3%82%89-AI-%E3%82%A8%E3%83%B3%E3%82%B8%E3%83%B3%E3%81%B8%E3%81%AE%E3%83
    %87%E3%83%BC%E3%82%BF%E9%80%9A%E4%BF%A1

    View Slide

  60. Copyright© Fixstars Group
    ● カーネルコード
    複数カーネルでの FFT 実装
    60
    ← Butterfly演算
      &並び替え
    2 stageずつ
    5 ケのカーネルに
    分割

    View Slide

  61. Copyright© Fixstars Group
    複数カーネルでの FFT 実装
    61
    ● グラフコード
    カーネル間をWindowアクセス
    するための接続
    5 カーネル分
    を作成

    View Slide

  62. Copyright© Fixstars Group
    複数カーネルでの FFT 実装
    62
    ● コンパイル結果
    ○ グラフ接続
    ○ タイル配置
    ■ 配置指定はせず、
    自動で配置

    View Slide

  63. Copyright© Fixstars Group
    複数カーネルでの FFT 実装
    63
    ● FFT 1000 point プロファイル結果 (aiesimulator 実行) Total 5,714,000 cycle
    = 5.71msec @ 1GHz
    カーネル間で順番に処理している
    自動で同期をとりながら動作できている
    (カーネルコードは独立に記述し、
    グラフ接続しているだけ)
    ※単一カーネルと同じ
    連続したデータ投入ができて
    いないため、カーネル分割し
    た利点が見えていない
     ↓
    スループットの改善効果は
    実機で確認する必要がある

    View Slide

  64. Copyright© Fixstars Group
    ● FFT カーネル並列化方式の検討
    ○ 試行した構成
    ○ 別構成案
    ■ スループットは同等、カーネルはシンプル、タイル配置の自由度も高い
    (これでよかったのでは?)
    ■ AI Engine は比較的新しいデバイスなので、最適構成を探る余地は大きい
    複数カーネルでの FFT 実装
    64

    View Slide

  65. Copyright© Fixstars Group
    参考:AMD 提供サンプルデザイン(XAPP1356)の実装
    65
    ● ブロックごとにコンフィギュレーション可能な高速フーリエ変換
    の AI エンジンでの実装
    ○ 1024 point FFT x4 カーネルから
    共有メモリを介して結果を集約する構成
    参照URL
    https://docs.xilinx.com/r/ja-JP/xapp1356-fft-ai-engine/%E8%A4%87%E6%95%B0%E3%81%AE-AI-%E3%82%A8%E3%83%B3%E3%82%B8%E3%83%B3%E3%81%AB%E3%82%88%E3%82%8B-FFT

    View Slide

  66. Copyright© Fixstars Group
    参考:AMD 提供サンプルデザイン(XAPP1356)の実装
    66
    ● 演算処理
    ○ Intrinsics を駆使して作り込んでいる
    ● プロファイル結果
    ○ 効率的にすき間なく演算が行われている様子が見える

    View Slide

  67. Copyright© Fixstars Group
    まとめ
    67
    ● AI Engine を用いた FFT 演算の実装を通して、AI Engine の構成、
    特徴の共有を行った
    ○ 演算機能の最適化方法
    ○ メモリ構成、共有メモリを介したカーネル間接続方法
    ● 比較的簡単な方法で、複数のカーネルが連携して動作することを示した
    ● 演算処理の最適化が不足しており、AI Engine API や Intrinsics を用いた
    高速化が必須であることが明らかになった

    View Slide

  68. Copyright© Fixstars Group
    Generative AI を活用した
    FPGA 開発談
    68

    View Slide

  69. Copyright© Fixstars Group
    Who I am
    写真
    Shinya KAJI
    梶 信也
    ソリューション第四事業部 
    事業部長
    69

    View Slide

  70. Copyright© Fixstars Group 70
    今日の LT でお話しすること
    Generative AI の代表的なサービスの 1 つである『ChatGPT』を
    FPGA 開発に活かそうという話
    引用元 : https://investingnews.com/invest-in-openai-chatgpt/

    View Slide

  71. Copyright© Fixstars Group
    注意事項
    ● ChatGPT の使用を推奨するものではありません
    ● ChatGPT を使用する際には重要情報の流出には十分注意
    してください
    71

    View Slide

  72. Copyright© Fixstars Group 72
    ChatGPT とは?
    ChatGPT(チャットジーピーティー、英語: Chat Generative Pre-trained
    Transformer)は、OpenAIが2022年11月に公開した人工知能チャットボッ
    ト。原語のGenerative Pre-trained Transformerとは、「生成可能な事前学
    習済み変換器」という意味である。
    OpenAIのGPT-3ファミリーの言語モデルを基に構築されており、教師あり
    学習と強化学習の両方の手法で転移学習されている。
    Wikipedia より引用 : https://ja.wikipedia.org/wiki/ChatGPT

    View Slide

  73. Copyright© Fixstars Group 73
    Google Trend より : https://trends.google.co.jp/home
    世界中?が熱狂した WBC と
    ChatGPT は同程度の注目度!

    View Slide

  74. Copyright© Fixstars Group
    FPGA 開発における ChatGPT の活用どころは?
    74
    構想段階
    仕様検討段階
    設計/実装段階
    シミュレーション
    タイミング検証
    実機検証
    etc.
    上流工程で活用してみました!

    View Slide

  75. Copyright© Fixstars Group
    ①構想段階での活用:
    75
    Intel FPGA を使った SmartNIC を
    独自開発したいけど、オープンソー
    スの TOE を活用できないだろう
    か?

    View Slide

  76. Copyright© Fixstars Group
    ①構想段階での活用: (cont.)
    76
    I want to find the TOE open source for Intel FPGA. Please let me know the best OSS
    and its reason.
    SH
    The TOE (TCP Offload Engine) open source for Intel FPGA that I would recommend is the TOE project maintained by the Open
    Programmable Acceleration Engine (OPAE) community. This project provides a full-featured, high-performance TCP/IP stack for
    FPGA-based NICs (network interface cards) and is optimized for use with Intel FPGAs.
    The TOE project offers several benefits that make it a great choice for Intel FPGA users:
    1. High-performance: The TOE project is designed to offer high-performance TCP/IP offloading capabilities for FPGA-based
    NICs, allowing for increased throughput and reduced CPU utilization.
    2. Open source: The TOE project is open source, which means that users can modify and customize the code to suit their
    specific needs. This also means that the project is constantly being improved and updated by a community of
    contributors.
    3. Easy to use: The TOE project is designed to be easy to use, with a straightforward API that allows developers to quickly
    integrate it into their applications.
    4. Compatibility: The TOE project is compatible with a wide range of Intel FPGA devices and development boards, making it
    a versatile option for developers working on FPGA-based networking projects.
    Overall, the TOE project is a solid choice for anyone looking to implement TCP/IP offloading on Intel FPGAs. Its high-performance,
    open-source nature, ease of use, and compatibility with a wide range of Intel FPGA devices make it a top contender in this space.

    View Slide

  77. Copyright© Fixstars Group
    ①構想段階での活用: (cont.)
    77
    Where can I find it? Give me the link to download the software.
    SH
    You can find the TOE project on the Open Programmable Acceleration Engine (OPAE) GitHub
    repository. Here's the link to the repository:
    https://github.com/OPAE/toe
    You can download the latest version of the TOE project from the repository's releases page:
    https://github.com/OPAE/toe/releases
    From there, you can download the source code or pre-built binaries for your platform. The
    repository also contains documentation, examples, and other resources to help you get started
    with the project.

    View Slide

  78. Copyright© Fixstars Group
    ①構想段階での活用: (cont.)
    78
    Do you have any alternatives?
    SH
    Yes, there are alternative TCP Offload Engine (TOE) open source projects available for Intel
    FPGAs. Here are a few options:
    1. DPDK: The Data Plane Development Kit (DPDK) is an open source set of libraries and
    drivers for fast packet processing on Intel architectures, including FPGAs. DPDK provides
    a high-performance TCP/IP stack that can be used for TOE offloading.
    2. NetCOPE: NetCOPE is an open source framework for FPGA-based network processing
    that includes a TCP/IP stack optimized for use with Intel FPGAs. NetCOPE offers a range
    of features, including support for multiple protocols and hardware acceleration.
    3. NetFPGA: NetFPGA is an open source platform for networking research and education
    that includes a TCP/IP stack optimized for FPGA-based NICs. NetFPGA offers a range of
    features, including support for multiple protocols and hardware acceleration.
    These alternative projects offer similar capabilities to the TOE project, but each has its own
    strengths and weaknesses. Ultimately, the best choice will depend on your specific needs and
    requirements.

    View Slide

  79. Copyright© Fixstars Group
    ①構想段階での活用: (cont.)
    79
    人力でググって調べる作業はかなり代替できそう!
    (ChatGPT と数分対話するだけで OK)

    View Slide

  80. Copyright© Fixstars Group
    ②仕様検討段階での活用:
    80
    TOE は決めたけど、どの FPGA
    ボードを採用したら良いだろうか?
    コストも限られているし…

    View Slide

  81. Copyright© Fixstars Group
    ②仕様検討段階での活用: (cont.)
    81
    Which FPGA board do you recommend to implement the TOE project on the OPAE,
    and why?
    SH
    There are several FPGA boards that can be used to implement the TOE project on the OPAE, depending on your
    specific requirements and budget. Here are a few options that I would recommend:
    1. Intel PAC with Intel Arria 10 GX FPGA: This FPGA board is designed specifically for network acceleration and
    includes an Arria 10 GX FPGA along with 8 GB of DDR4 memory and a 100 Gbps QSFP28 network interface.
    The board is well-suited for implementing the TOE project, as it provides high-speed networking capabilities
    and powerful FPGA resources.
    2. Terasic DE5a-Net FPGA Development Kit: This development kit includes a Stratix V GX FPGA along with 4 GB
    of DDR3 memory and multiple high-speed networking interfaces. The board is a good choice for
    implementing the TOE project, as it provides a large FPGA with plenty of resources and support for multiple
    networking protocols.
    3. Intel FPGA Development Kit: This development kit includes a Cyclone V GX FPGA along with 1 GB of DDR3
    memory and multiple high-speed networking interfaces. The board is a more budget-friendly option for
    implementing the TOE project, while still providing a powerful FPGA and networking capabilities.
    Ultimately, the choice of FPGA board will depend on your specific requirements, including performance, cost, and
    development environment. All of the boards listed above are compatible with the OPAE and provide a good starting
    point for implementing the TOE project.

    View Slide

  82. Copyright© Fixstars Group
    ②仕様検討段階での活用: (cont.)
    82
    The cost is limited to 10,000 USD, and higher performance is better. Which FPGA
    board is your best choice?
    SH
    With a budget of $10,000 and a requirement for higher performance, I would recommend the Intel
    PAC with Intel Arria 10 GX FPGA. This board is designed specifically for network acceleration and
    offers a powerful Arria 10 GX FPGA, along with 8 GB of DDR4 memory and a 100 Gbps QSFP28
    network interface. The board is well-suited for implementing the TOE project and can provide the
    high performance you require.
    The Intel PAC with Intel Arria 10 GX FPGA has a list price of around $3,000, leaving plenty of room
    in your budget for additional components, such as power supplies, cables, and development tools.
    The board is also well-supported by the OPAE community, which can help simplify the
    development process and provide access to a range of resources and examples.
    Overall, the Intel PAC with Intel Arria 10 GX FPGA is a great choice for implementing the TOE
    project on a budget of $10,000 while still providing the high performance you require.

    View Slide

  83. Copyright© Fixstars Group
    ②仕様検討段階での活用: (cont.)
    83
    PM や PdM を助けてくれる良きパートナー!
    (ChatGPT と数分対話するだけで OK)

    View Slide

  84. Copyright© Fixstars Group
    ③設計/実装段階での活用:
    84
    非同期 FIFO を Verilog で実装し
    たいけど誰かが実装してるはずだ
    から流用したい…

    View Slide

  85. Copyright© Fixstars Group
    ③設計/実装段階での活用:
    85
    Write Verilog code for an asynchronous FIFO with parameterizable width and depth.
    SH
    Here's an example of an asynchronous FIFO with parameterizable width and depth in Verilog:
    module fifo #
    (
    parameter WIDTH = 8,
    parameter DEPTH = 16
    )
    (
    input clk,
    input rst,
    input wr_en,
    input rd_en,
    input [WIDTH-1:0] wr_data,
    output [WIDTH-1:0] rd_data,
    output full,
    output empty
    );
    reg [WIDTH-1:0] memory [0:DEPTH-1];
    reg [WIDTH-1:0] rd_data_reg;
    reg [WIDTH-1:0] wr_ptr;
    reg [WIDTH-1:0] rd_ptr;
    reg [DEPTH-1:0] occupancy;
    always @(posedge clk) begin
    if (rst) begin
    wr_ptr <= {WIDTH{1'b0}};
    rd_ptr <= {WIDTH{1'b0}};
    occupancy <= {DEPTH{1'b0}};
    rd_data_reg <= {WIDTH{1'b0}};
    end
    else begin
    if (wr_en && !full) begin
    memory[wr_ptr] <= wr_data;
    wr_ptr <= wr_ptr + 1;
    occupancy <= occupancy + 1;
    end
    if (rd_en && !empty) begin
    rd_data_reg <= memory[rd_ptr];
    rd_ptr <= rd_ptr + 1;
    occupancy <= occupancy - 1;
    end
    end
    end
    assign rd_data = rd_data_reg;
    assign full = (occupancy == DEPTH);
    assign empty = (occupancy == 0);
    endmodule

    View Slide

  86. Copyright© Fixstars Group
    ③設計/実装段階での活用: (cont.)
    86
    Write Verilog code for an asynchronous FIFO with parameterizable width and depth.
    SH
    This module defines an asynchronous FIFO with a configurable WIDTH and DEPTH. The module has
    input ports for the clock signal (clk), reset signal (rst), write enable (wr_en), and read enable
    (rd_en). The module also has input ports for write data (wr_data) and output ports for read data
    (rd_data), full flag (full), and empty flag (empty).
    The module uses an array (memory) to store data in the FIFO, and read and write pointers (rd_ptr
    and wr_ptr) to keep track of the current positions in the FIFO. The module also uses an
    occupancy counter (occupancy) to keep track of how many elements are currently in the FIFO.
    In the always block, the module checks for a reset signal and updates the read and write pointers
    and occupancy counter based on the write and read enable signals. The read data is stored in a
    register (rd_data_reg) to avoid race conditions.
    The assign statements assign the read data, full flag, and empty flag based on the current
    occupancy of the FIFO.

    View Slide

  87. Copyright© Fixstars Group
    ③設計/実装段階での活用: (cont.)
    87
    簡単な FPGA 実装は任せられそう!
    (お願いすればテストベンチも作成してくれる)

    View Slide

  88. Copyright© Fixstars Group
    ChatGPT で実現できないことはまだまだある!
    88
    Write the fastest TCP Offload Engine HDL code for Intel FPGA as detailed as possible.
    SH
    Designing a TCP Offload Engine (TOE) is a complex task that requires a deep understanding of both
    the TCP/IP protocol and FPGA architecture. Here are some general steps that may be involved in
    implementing a TOE on an Intel FPGA, along with some tips for optimizing the HDL code for
    maximum performance:
    (snip)
    抽象度が高くかつ難易度の高い非機能要件を持つ要件から
    具体的な実装に落とし込むのはエンジニアの仕事!

    View Slide

  89. Copyright© Fixstars Group
    Takeaway
    89
    ● ChatGPT と対話することで人力検索を効率化できる
    ● 非エンジニア (PM/PdM) とエンジニアの溝が埋まる
    ● ChatGPT に簡単なプログラミングを任せることができる
    ● FPGA エンジニアの仕事がなくなることは (おそらく) ない
    Happy FPGA Life!

    View Slide

  90. Copyright © Fixstars Group
    Thank you!
    お問い合わせ窓口 : [email protected]
    90

    View Slide