Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] RWKV LM #207

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored RWKV and merged from main
  • Loading branch information
Kyle1668 committed Apr 22, 2023
commit 15baa83ecd69a2b414efa99e0654dd54dc1a1a1f
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "python",
"request": "launch",
"module": "elk",
"args": ["elicit", "RWKV", "imdb", "--max_examples=5"],
"args": ["elicit", "rwkv", "imdb", "--max_examples=5"],
"env": {
"CUDA_VISIBLE_DEVICES": "5",
},
Expand Down
11 changes: 9 additions & 2 deletions elk/rwkv_lm/rwkv_hf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import torch
from rwkv.model import RWKV
from huggingface_hub import hf_hub_download
from transformers import AutoTokenizer, GPT2TokenizerFast, PreTrainedModel, PretrainedConfig
from transformers.modeling_outputs import CausalLMOutput

os.environ["RWKV_JIT_ON"] = '1'
os.environ["RWKV_CUDA_ON"] = '0'
Expand Down Expand Up @@ -29,9 +31,14 @@ def forward(
position_ids=None,
head_mask=None,
labels=None,
output_hidden_states=None
):
_, state = self.model.forward(input_ids, None)
return state
inputs = input_ids.detach().cpu()
token, states = self.model.forward(inputs, None)
mock_embedding_state = states[0].clone()
output_states = [mock_embedding_state] + states
response = CausalLMOutput(logits=token, hidden_states=output_states)
return response

# @staticmethod
# def from_pretrained(pretrained_model_name_or_path):
Expand Down
5 changes: 3 additions & 2 deletions elk/utils/hf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
PretrainedConfig,
PreTrainedModel,
PreTrainedTokenizerBase,
GPT2TokenizerFast
)
from ..rwkv_lm.rwkv_hf import RWKVModel, RWKVConfig, RWKVTokenizer
from ..rwkv_lm.rwkv_hf import RWKVModel, RWKVConfig

# Ordered by preference
_DECODER_ONLY_SUFFIXES = [
Expand Down Expand Up @@ -42,7 +43,7 @@ def instantiate_model(model_str: str, **kwargs) -> PreTrainedModel:
def instantiate_tokenizer(model_str: str, **kwargs) -> PreTrainedTokenizerBase:
"""Instantiate a tokenizer, using the fast one iff it exists."""
if model_str.startswith("rwkv"):
return RWKVTokenizer()
return GPT2TokenizerFast(tokenizer_file="elk/rwkv_lm/20B_tokenizer.json")

try:
return AutoTokenizer.from_pretrained(model_str, use_fast=True, **kwargs)
Expand Down