Skip to content

fix eager run for cuda #6429

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

Merged
merged 1 commit into from
Oct 22, 2024
Merged
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
12 changes: 6 additions & 6 deletions examples/models/llama/runner/eager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def __init__(self, args):
use_kv_cache=args.use_kv_cache,
**params,
)
super().__init__(tokenizer_path=args.tokenizer_path, model_args=model_args)
manager: LLMEdgeManager = _prepare_for_llama_export("llama", args)
self.model = (
manager.model.eval().to(device="cuda")
if torch.cuda.is_available()
else manager.model.eval().to(device="cpu")
super().__init__(
tokenizer_path=args.tokenizer_path,
model_args=model_args,
device="cuda" if torch.cuda.is_available() else "cpu",
)
manager: LLMEdgeManager = _prepare_for_llama_export("llama", args)
self.model = manager.model.eval().to(device=self.device)

def forward(
self,
Expand Down
22 changes: 15 additions & 7 deletions examples/models/llama/runner/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ def next_token(logits: torch.Tensor, temperature: float, top_p: float) -> int:


class LlamaRunner(ABC):
def __init__(self, tokenizer_path: str, model_args: ModelArgs):
def __init__(self, tokenizer_path: str, model_args: ModelArgs, device: str = "cpu"):
self.params = model_args
self.tokenizer = get_tokenizer(tokenizer_path)
assert model_args.vocab_size == self.tokenizer.n_words
self.device = device

@abstractmethod
def forward(
Expand All @@ -73,9 +74,9 @@ def generate( # noqa: C901
) -> List[int]:
# prefill
logits = self.forward(
tokens=torch.tensor([prompt_tokens], dtype=torch.long),
tokens=torch.tensor([prompt_tokens], dtype=torch.long, device=self.device),
input_pos=(
torch.tensor([0], dtype=torch.long)
torch.tensor([0], dtype=torch.long, device=self.device)
if self.params.use_kv_cache
else None
),
Expand All @@ -87,14 +88,21 @@ def generate( # noqa: C901
while len(tokens) < self.params.max_seq_len:
if self.params.use_kv_cache:
logits = self.forward(
tokens=torch.tensor([[current_token]], dtype=torch.long),
input_pos=torch.tensor([len(tokens) - 1], dtype=torch.long),
tokens=torch.tensor(
[[current_token]], dtype=torch.long, device=self.device
),
input_pos=torch.tensor(
[len(tokens) - 1], dtype=torch.long, device=self.device
),
)
else:
logits = self.forward(tokens=torch.tensor([tokens], dtype=torch.long))
logits = self.forward(
tokens=torch.tensor([tokens], dtype=torch.long, device=self.device),
)
current_token = next_token(logits, temperature, top_p)
if current_token == self.tokenizer.eos_id or (
hasattr(self, "stop_tokens") and current_token in self.stop_tokens
hasattr(self.tokenizer, "stop_tokens")
and current_token in self.tokenizer.stop_tokens
):
break
tokens.append(current_token)
Expand Down
Loading