メインコンテンツへスキップ
見出し画像

大規模言語モデルのフルスクラッチ作成の予習〜データセット取得、クリーニングまで〜

    東京大学松尾研究室から、LLMスクラッチ作成のための雛形となるリポジトリが公開されています。

    これを使ってデータセットの取得、クリーニングまでを試してみました。
    実行環境はMacOS(M2 MBP)です。
    GPUが入ってる学習可能なマシンが今ないので、とりあえず手持ちのMacで予習しておきます。

    データ収集加工手順のREADMEを元に作業してみました。

    リポジトリ取得

    git clone https://github.com/matsuolab/ucllm_nedo_prod.git

    とりあえず、リポジトリを取得します。
    ダウンロードされるデータセットの容量が大きいので空きが大きいハードディスクで取得したほうが良いです。

    データのダウンロード

    cd ucllm_nedo_prod/preprocessing
    # sudo apt-get install git-lfs
    # pip install git-lfs
    # apt-get, pipで入れる方法もあるがMacOSなのでbrewでgit-lstをインストール
    brew install git-lfs
    ./bin/setup

    git-lfsはMacOSで実行したのでbrewで入れてます。
    ./bin/setupの実行内容を見てみます。

    less bin/setup

    venvでpythonの仮想環境を作成しているのがわかります。

    #!/bin/bash
    
    # Create a virtual environment named 'venv'
    python3 -m venv venv

    仮想環境が次のコマンドでアクティブになります。

    source venv/bin/activate

    日本語mC4のダウンロード

    #c4 -jaの一部ダウンロード

    
    python -m preprocessing.download_dataset --dataset=c4 --split=train --output_base=output --index_from=0 --index_to=10

    自分の場合はgit-lfsのインストール前に、このコマンドを実行したためか、つぎのようなエラーが出て処理が完了しませんでした。

    FileNotFoundError: [Errno 2] No such file or directory: 'output/tmp/allenai/c4/multilingual/c4-ja.tfrecord-00000-of-01024.json.gz'
    zsh: command not found: copy
    

    とりあえず、ばっさりダウンロードが中途半端に終わったと思われるoutputディレクトを削除して、もう一度ダウンロードすると解決しました。

    ucllm_nedo_prod/preprocessing/output/datasets/allenai/c4/c4-ja_00000-00010.jsonl.gz

    と、 jsonlファイルのgz圧縮のファイルがダウンロードされます。
    以降のフィルタリング処理のため解凍します。

    gzip -d c4-ja_00000-00010.jsonl.gz
    画像
    c4-ja_00000-00010.jsonl

    容量16.46GBのファイルが作成されます。

    wikipedia日本語版のダウンロード

    python -m preprocessing.download_dataset --dataset=wikipedia --split=20240301

    jawikipedia dumpのインデックスを指定してダウンロードできるので、最新のダンプを取得できます。
    ucllm_nedo_prod/preprocessing/output/datasets/wikipediaにインデックスのディレクトリ名でダウンロードされます。

    画像
    wikipedia日本語版のダウンロードディレクトリ

    容量は13.86GB。

    データ前処理

    必要なライブラリのインストール

    pip install hojichar
    pip install mecab-python3
    pip install 'fugashi[unidic]'
    python -m unidic download

    日本語mc4のフィルタリング

    date;python -m preprocessing.filtering --input_dir=output/datasets/allenai/c4 --output_dir=output;date;
    2024年 3月 8日 金曜日 15時32分45秒 JST
    2024年 3月 9日 土曜日 16時41分51秒 JST

    実行時間を計測したかったので、前後にdateコマンドを入れました。
    約25時間ほどかかってます。
    preprocessing/output/20240308153247(実行日時)
    というフォルダが作成されています。

    画像
    mc4フィルタリング済みディレクトリ

    results.filtering.jsonlがOK判定された項目。
    rejected.filtering.jsonlがNG判定された項目。

    results.filtering.jsonlはサブディレクトリが複数ある場合、すべてのresult.filtering.jsonlをくっつけたファイルという位置づけで、このc4-jaのフィルタリングではサブディレクトリが一つなので同一の内容になります。

    less results.filtering.jsonl 

    lessコマンドでresults.filtering.jsonlの中を表示してみます。

    画像
    c4-ja results.filtering.jsonl
    less c4-ja_00000-00010/rejected.filtering.jsonl 

    lessコマンドでrejected.filtering.jsonlの中を表示してみます。

    画像
    c4-ja_00000-00010/rejected.filtering.jsonl

    wikipedia日本語版のフィルタリング

    date;python -m preprocessing.filtering --input_dir=output/datasets/wikipedia/20240301/ja --output_dir=output;date;
    2024年 3月 8日 金曜日 13時58分53秒 JST
    2024年 3月 9日 土曜日 16時09分42秒 JST

    約26時間ほど。
    preprocessing/output/20240308135907(実行日時)
    というフォルダが作成されています。

    画像
    ウィキペディア フィルタリング済みディレクトリ


    results.filtering.jsonlがOK判定された項目。
    rejected.filtering.jsonlがNG判定された項目。

    画像
    results.filtering.jsonl

    VSCodeで開いてみました。1.58Gもあるテキストなのに開けて優秀。

    画像
    rejected.filtering.jsonl

    こちらはリジェクト判定されたもの。

    重複削除(Dedup)

    filteredというディレクトリを作って、mc4でフィルタリング処理したjsonlを入れます。
    c4-ja_00000-00010/result.filtering.jsonl
    をコピーしてリネーム。
    c4-result.filtering.jsonlという名前にしました。

    画像
    filteredディレクトリの中身

    重複削除処理を実行します。

    cd ucllm_nedo_prod/preprocessing
    date;python -m preprocessing.dedup --input_dir=output/filtered --output_dir=output;date                           
    
    2024年 3月10日 日曜日 18時48分53秒 JST
    2024年 3月11日 月曜日 06時09分46秒 JST
    

    c4の重複削除処理に17時間半ほどかかりました。

    画像
    重複処理(Dedup)の結果

    重複していて、はじかれたデータはrejected.dedup.jsonlに書かれています。
    中身を見てみます。

    less rejected.dedup.jsonl
    {"text": "くじ引き-FC2動画2010/02/1815:02:24<scriptsrc=\"//static.fc2.com/video/js/outerplayer.min.js\"url=\"https://video.fc2.com/ja/content/20100218SUaqMXkH/\"tk=\"\"tl=\"くじ引き\"sj=\"46000\"d=\"25\"w=\"448\"h=\"380\"suggest=\"on\"charset=\"UTF-8\"></script><objectclassid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\"wmode=\"transparent\"width=\"448\"height=\"380\"id=\"flv2\"align=\"middle\"><paramname=\"allowScriptAccess\"value=\"sameDomain\"/><paramname=\"movie\"value=\"http://video.fc2.com/flv2.swf?i=20100218SUaqMXkH&d=25&movie_stop=off&no_progressive=1&otag=1&sj=46000&rel=1\"/><paramname=\"quality\"value=\"high\"/><paramname=\"bgcolor\"value=\"#ffffff\"/><paramname=\"allowFullScreen\"value=\"true\"/><embedsrc=\"http://video.fc2.com/flv2.swf?i=20100218SUaqMXkH&d=25&movie_stop=off&no_progressive=1&otag=1&sj=46000&rel=1\"quality=\"high\"bgcolor=\"#ffffff\"wmode=\"transparent\"width=\"448\"height=\"380\"name=\"flv2\"align=\"middle\"allowScriptAccess=\"sameDomain\"type=\"application/x-shockwave-flash\"pluginspage=\"http://www.macromedia.com/go/getflashplayer\"allowFullScreen=\"true\"/></object><br/><ahref=\"http://video.fc2.com/content/20100218SUaqMXkH/\"title=\"FC2動画視聴も投稿も無料の共有動画ブログや携帯にも対応\"rel=\"nofollow\">くじ引き</a><ahref=\"https://video.fc2.com/content/%E3%81%8F%E3%81%98%E5%BC%95%E3%81%8D/20100218SUaqMXkH/\"title=\"動画:くじ引き\"rel=\"nofollow\"><imgsrc=\"https://vip-video46000-thumbnail2.fc2.com/up/thumb/201002/18/9/FCUT_20100218SUaqMXkH.gif\"alt=\"動画:くじ引き\"></a>"}
    {"text": "ゆい(@0807Apple)xxxx@yyy.Update0807Apple'sstatsnow.@0807Apple-TrackingゆいTwitterprofilesinceMarch7,2014学芸音科→国立音楽大学3鍵/アンサンブル・ピアノコースゆい,xxxx@yyy.ActiveonTwittersinceJanuary2014,ゆいmadeittohavingarespectable302Twitterfollowersandtobeingranked38,783,202fornumberoffollowersamongallTwitterusers.Uh-oh...seemslikesomeonehasfallenasleepwhileondutyandisdroppinginsocialrelevance.Theplotthickenswhenconsideringゆい'sfollower-to-followingratio,whichis0.89.Thisactuallydoesn'treflectsowellonthisaccount'sstrength.Tweetingisapparentlyoneof@0807Apple'sfavoritepastimes,withanaverageof0tweet(s)perdayinthepast30days.That'sprettyconsistentwithatotalof1,700since@0807ApplejoinedTwitter.@0807AppleTwitterStatsOverview@0807Applehasposted0tweetsinthelast10days,whichtranslatestoanaverageof0tweetsperday.@0807Applehas302followersonTwitter.@0807Appleisfollowing338Twitteraccounts.@0807Applewillhit302followersinthenext3months,and302inoneyear.Curiousaboutthefutureof@0807Apple’sfollowergrowthoryours?WetracktheseTwitterstatssinceMarch7,2014.Youcanseehowmanyfollowersゆいlostorgainedandwhatthepredictionisfortomorroworthenext15days,togetherwithallkindsofotherstatslikerankcomparedtoallTwitterusers,xxxx@yyy.Howwelldidyoudo?"}
    {"text": "こーすけおく(@09kosuke90)xxxx@yyy.Update09kosuke90'sstatsnow.こーすけおくTwitterStats@09kosuke90-TrackingこーすけおくTwitterprofilesinceMarch25,2013深谷第一神田外語大学発言に責任持てないんであん
    まフォローしないでくださいな(^з^)-☆こーすけおく'sstoryこーすけおく,xxxx@yyy.ActiveonTwittersinceNovember2012,こーすけおくmadeittohavingarespectable67Twitterfollowersandtobeingranked115,663,693fornumberoffollowersamongallTwitterusers.Theplotthickenswhenconsideringこーすけおく'sfollower-to-followingratio,whichis1.97.Tweetingisapparentlyoneof@09kosuke90'sfavoritepastimes,withanaverageof0tweet(s)perdayinthepast30days.That'sprettyconsistentwithatotalof3,705since@09kosuke90joinedTwitter.@09kosuke90TwitterStatsOverviewこーすけおくTweets@09kosuke90hasposted0tweetsinthelast21days,whichtranslatestoanaverageof0tweetsperday.こーすけおくTwitterFollowers@09kosuke90has67followersonTwitter.こーすけおくFollowingonTwitter@09kosuke90isfollowing34Twitteraccounts.こーすけおくPredictions&Milestones@09kosuke90willhit67followersinthenext3months,and67inoneyear.Curiousaboutthefutureof@09kosuke90’sfollowergrowthoryours?Youareonこーすけおく'sTwitterstatspageWetracktheseTwitterstatssinceMarch25,2013.Youcanseehowmanyfollowersこーすけおくlostorgainedandwhatthepredictionisfortomorroworthenext15days,togetherwithallkindsofotherstatslikerankcomparedtoallTwitterusers,tweetsetc.Whydon'tyougiveitatryandcompareこーすけおくtoyourself?Orifyouareこーすけおくxxxx@yyy.Howwelldidyoudo?"}

    よくわからないけど、まぁきっとどこかと重複してたんでしょう。

    参考

    https://github.com/matsuolab/ucllm_nedo_prod/blob/main/data_management/README.md


     
     
    大規模言語モデル(LLM) / 画像生成AI / DaVinci Resolveのメモ / ギターや音楽関連のメモ

    あなたへのおすすめ