0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RWKV/rwkv-4-world-3bはパソコンのキーボードを知っているのか

Last updated at Posted at 2024-11-17

昨日の記事RWKV/v6-Finch-3B-HFだが、内蔵トークナイザhf_rwkv_tokenizer.pyにバグがあってdecodeconvert_tokens_to_idsがマトモに動かない。モデル定義のmodeling_rwkv6.pyにもバグがあって、性能低下を起こしているらしい。どうも、内蔵プログラムのレビューが、ちゃんとおこなわれていないように見える。

旧版のRWKV/rwkv-4-world-3bも試してみたのだが、こちらも内蔵トークナイザtokenization_rwkv_world.pyにバグがある。さすがにイラっと来たので、trust_remote_code=Trueを使わない形でトークナイザを再設計し、無理矢理RWKV/rwkv-4-world-3bを動かすことにした。Google Colaboratory (GPU版)だと、こんな感じ。

!pip install transformers
import ast,json,torch
from transformers import GPT2TokenizerFast,AutoModelForCausalLM
from transformers.utils import cached_file
from transformers.models.gpt2.tokenization_gpt2 import bytes_to_unicode
from tokenizers.models import WordPiece
f=cached_file("RWKV/rwkv-4-world-3b","rwkv_vocab_v20230424.txt")
with open(f,"r",encoding="utf-8") as r:
  v=[s.strip().split(" ") for s in r]
d=ast.literal_eval("{"+",".join(t[0]+":"+" ".join(t[1:-1]) for t in v)+"}")
d[0]="<|endoftext|>"
v,x={},bytes_to_unicode()
for i in range(len(d)):
  v["".join(x[j] for j in (d[i].encode("utf-8") if type(d[i])==str else d[i]))]=i
with open("vocab.json","w",encoding="utf-8") as w:
  json.dump(v,w,indent=2,ensure_ascii=False)
tkz=GPT2TokenizerFast("vocab.json","/dev/null")
w=WordPiece(v)
w.unk_token=d[0]
w.continuing_subword_prefix=""
tkz.backend_tokenizer.model=w
mdl=AutoModelForCausalLM.from_pretrained("RWKV/rwkv-4-world-3b",torch_dtype=torch.float16).to(0)
tgn=lambda x:tkz.decode(mdl.generate(tkz(x,return_tensors="pt")["input_ids"].to(mdl.device),max_new_tokens=333,do_sample=True,temperature=1.0,top_p=0.3,top_k=0)[0].tolist(),skip_special_tokens=True)
nlp=lambda txt:tgn(f"User: hi\n\nAssistant: Hi, I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.\n\nUser: {txt}\n\nAssistant:")
print(nlp("パソコンのキーボードは,なぜABC順・五十音順ではないのですか"))

WordPiece版GPT2TokenizerFastでトークナイザを置き換えつつ、「パソコンのキーボードは,なぜABC順・五十音順ではないのですか」と尋ねてみたところ、私(安岡孝一)の手元では以下の結果が出力された。

User: hi

Assistant: Hi, I am your assistant and I will provide expert full response in full details. Please feel free to ask any question and I will always answer it.

User: パソコンのキーボードは,なぜABC順・五十音順ではないのですか

Assistant: パソコンのキーボードは,英語の文字を表示するために使用されるため,英語の文字を表示するために使用されています。ABC順・五十音順とは,英語の文字を英語の発音順に並べたもので,英語の発音順に基づいてキーボードを設定するために使用されています。また,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発音順に基づいてキーボードを設定するために,英語の発

うーむ、出力が止まらなくなってしまったようだ。言語モデルとしては、旧版のRWKV/rwkv-4-world-3bより、やっぱり最新版のRWKV/v6-Finch-3B-HFの方がいいのかなあ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?