調べ物した結果

現役SEが仕事と直接関係ないことを調べた結果とか感想とか

gpt-2のソースを眺めてみる②~適当リファクタ~

今回のお相手

https://github.com/openai/gpt-2
お借りします。
GPT-2の詳しいことを知りたくてこのページを見に来た人すみません。回れ右です。

前回


・テストのもとはできた。
・なぜかさがるCC値

とりあえず_bpeをうごかしてみる


前回でインスタンスは生成できたので、とりあえずそのまま_bpeをうごかしてみよう。

    def test_constractor(self):
        ins = self.testutil_create_instance()

        ins._bpe(None)

そのまま流すとエラーになる。

ERROR: test_constractor (__main__.Test_Encoder)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\git_refact\gpt-2\src\test_encoder.py", line 9, in test_constractor
    ins._bpe(None)
  File "d:\git_refact\gpt-2\src\encoder.py", line 57, in _bpe
    word = tuple(token)
TypeError: 'NoneType' object is not iterable

----------------------------------------------------------------------
Ran 3 tests in 11.394s
    def _bpe(self, token):
        if token in self.cache:
            return self.cache[token]
        word = tuple(token)

tokenがキャッシュ内にいればいいが、なければtokenでtupleを生成。
その時点でおちている。Noneを渡してるから当然だけど。 とりあえずこれはこれでいいのでテストに残しておくか。

    def test_constractor(self):
        ins = self.testutil_create_instance()
        try:
            ins._bpe(None)
            self.assertFalse("None Token")
        except Exception:
            None

なんか意味あるかなぁ。このテスト。せっかくなので、先頭のキャッシュに該当するToken
の辺りもテストを書いておこう。

    def test_bpe_hitcache(self):
        ins = self.testutil_create_instance()
        token = "dummy_token"
        value = 1
        ins.cache = {token:value, "dummy_2":2}
        actual = ins._bpe(token)
        
        self.assertEqual(value, actual, "hitcache")

テストもとおった。とりあえずキャッシュされていたらTokenから返す。は保証完了。
とりあえず今回はこの辺までにしておくか。全然すすまねー。