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

Add LLaVA support, modify generate function #820

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
449 changes: 449 additions & 0 deletions demos/LLaVA.ipynb

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions tests/acceptance/test_hooked_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,77 @@ def test_all_pythia_models_exist():
f"Could not download model '{model}' from Huggingface."
" Maybe the name was changed or the model has been removed."
)


@pytest.mark.parametrize(
"input_type,return_type",
[
("str", "input"),
("str", "str"),
("str", "tokens"),
("str", "embeds"),
("tokens", "input"),
("tokens", "str"),
("tokens", "tokens"),
("tokens", "embeds"),
("embeds", "input"),
("embeds", "str"),
("embeds", "tokens"),
("embeds", "embeds"),
],
)
def test_different_inputs_for_generation(
input_type, return_type, print_output=False, max_new_tokens=3
):
from typing import List

device = "cuda" if torch.cuda.is_available() else "cpu"
hooked_llm = HookedTransformer.from_pretrained("gpt2", device=device)

hooked_llm.eval()
for text_input in [
"What is the meaning of life?",
["AI will destroy world", "AI will save us"],
]:
is_batched = False if isinstance(text_input, str) else True

tokens_input = hooked_llm.to_tokens(text_input)
embeddings_input = hooked_llm.embed(tokens_input)

if input_type == "str":
model_input = text_input
elif input_type == "tokens":
model_input = tokens_input
elif input_type == "embeds":
model_input = embeddings_input
else:
raise ValueError(f"Unknown input_type: {input_type}")

output = hooked_llm.generate(
input=model_input, max_new_tokens=max_new_tokens, return_type=return_type, verbose=False
)

if return_type == "str" or (return_type == "input" and input_type == "str"):
if is_batched:
assert isinstance(output, List), f"Expected list output but got {type(output)}"
assert isinstance(
output[0], str
), f"Expected list of strings but got list of {type(output[0])}"
else:
assert isinstance(output, str), f"Expected string output but got {type(output)}"
elif return_type == "tokens" or (return_type == "input" and input_type == "tokens"):
assert isinstance(
output, torch.Tensor
), f"Expected tensor output but got {type(output)}"
assert output.ndim == 2, f"Expected 2D tensor but got {output.ndim}D"
elif return_type == "embeds" or (return_type == "input" and input_type == "embeds"):
assert isinstance(
output, torch.Tensor
), f"Expected tensor output but got {type(output)}"
assert output.ndim == 3, f"Expected 3D tensor but got {output.ndim}D"

if print_output:
print(f"Input type: {input_type}, return type: {return_type}, output:\n{output}")

if print_output:
print()
Loading
Loading