-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throwing this up as a pr to make it easier to view --------- Co-authored-by: archana-ramalingam <[email protected]> Co-authored-by: Ian <[email protected]>
- Loading branch information
1 parent
e051c37
commit aead69f
Showing
20 changed files
with
1,412 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
sharktank/sharktank/examples/validate_direct_mixtral_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# Copyright 2024 Advanced Micro Devices, Inc | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
import sys | ||
|
||
import torch | ||
|
||
from sharktank.layers import * | ||
from sharktank.types import * | ||
from sharktank.models.mixtral.mixtral import * | ||
|
||
|
||
def main(args: list[str]): | ||
from ..utils import cli | ||
|
||
torch.no_grad().__enter__() | ||
|
||
parser = cli.create_parser() | ||
cli.add_input_dataset_options(parser) | ||
args = cli.parse(parser) | ||
|
||
dataset = cli.get_input_dataset(args) | ||
hp = configs.LlamaHParams.from_gguf_props(dataset.properties) | ||
llama_config = LlamaModelConfig(hp) | ||
llama_config.kv_cache_type = "direct" | ||
llama_config.activation_dtype = torch.float16 | ||
model = PagedMixtralModelV1(dataset.root_theta, llama_config) | ||
|
||
# bs ("batch size") == 1 | ||
cache_state = model.cache.allocate(bs=1) | ||
|
||
start_index = 0 | ||
tokens = torch.tensor( | ||
[ | ||
[ | ||
1, | ||
1059, | ||
31871, | ||
1217, | ||
322, | ||
266, | ||
3682, | ||
6075, | ||
31902, | ||
13, | ||
31849, | ||
31871, | ||
0, | ||
0, | ||
0, | ||
0, | ||
] | ||
+ 48 * [0], | ||
] | ||
) | ||
assert tokens.shape[1] % model.cache.block_seq_stride == 0 | ||
seq_block_ids = torch.tensor( | ||
[ | ||
[127, 0, 0, 0], | ||
] | ||
) | ||
|
||
# Important: Do not use a sequence length of 0 for empty batch slots | ||
# as it will cause softmax to nan due to a mask of all -inf. This then | ||
# propagates and causes badness. | ||
seq_lens = torch.tensor([12]) | ||
|
||
attention_mask = model.attention_mask( | ||
model.input_mask(seq_lens, tokens.shape[1]), | ||
) | ||
|
||
print(f"Step {start_index}") | ||
logits = model.prefill( | ||
tokens, | ||
attention_mask=attention_mask, | ||
seq_block_ids=seq_block_ids, | ||
cache_state=cache_state, | ||
) | ||
# TODO: Normalize the output of extract_tokens_from_logits into tensor [bs, 1]. | ||
tokens = torch.tensor(model.extract_tokens_from_logits(logits, seq_lens)).unsqueeze( | ||
1 | ||
) | ||
print(f" : tokens = {tokens}") | ||
|
||
# Decode a step. | ||
print("Decoding...") | ||
print(tokens.shape, tokens) | ||
start_positions = torch.tensor([12]) | ||
seq_lens = seq_lens + 1 | ||
decode_attention_mask = model.decode_attention_mask( | ||
model.input_mask( | ||
seq_lens, | ||
seq_block_ids.shape[1] * model.cache.block_seq_stride, | ||
), | ||
) | ||
logits = model.decode( | ||
tokens, | ||
attention_mask=decode_attention_mask, | ||
start_positions=start_positions, | ||
seq_block_ids=seq_block_ids, | ||
cache_state=cache_state, | ||
) | ||
tokens = torch.tensor(model.extract_tokens_from_logits(logits, [1])).unsqueeze(1) | ||
print(f" : tokens = {tokens}") | ||
|
||
def save_prefill_module(model): | ||
from iree.compiler.extras.fx_importer import FxImporter | ||
from iree.compiler.ir import AsmState | ||
|
||
importer = FxImporter() | ||
|
||
print("Generating FX graph") | ||
|
||
class InferenceModule(torch.nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
self.add_module("prefill", model) | ||
|
||
def forward(self, tokens, attention_mask, seq_block_ids, *cache_state): | ||
return self.prefill.prefill( | ||
tokens, | ||
attention_mask=attention_mask, | ||
seq_block_ids=seq_block_ids, | ||
cache_state=list(cache_state), | ||
) | ||
|
||
infmod = InferenceModule() | ||
prog = torch.export.export( | ||
infmod, (tokens, attention_mask, seq_block_ids) + tuple(cache_state) | ||
) | ||
|
||
print(f"FX prog:", prog) | ||
importer.import_program(prog, func_name="prefill") | ||
output_file = "/tmp/prefill.mlirbc" | ||
print("Saving to:", output_file) | ||
with open(output_file, "wb") as f: | ||
importer.module_op.write_bytecode(f) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main(sys.argv[1:])) |
48 changes: 48 additions & 0 deletions
48
sharktank/sharktank/examples/validate_mixtral_ref_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2024 Advanced Micro Devices, Inc | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
import sys | ||
|
||
import torch | ||
|
||
from sharktank.layers import * | ||
from sharktank.types import * | ||
from sharktank.models.mixtral.mixtral_ref import * | ||
|
||
|
||
def main(args: list[str]): | ||
from ..utils import cli | ||
|
||
torch.no_grad().__enter__() | ||
|
||
parser = cli.create_parser() | ||
cli.add_input_dataset_options(parser) | ||
args = cli.parse(parser) | ||
|
||
dataset = cli.get_input_dataset(args) | ||
hp = configs.LlamaHParams.from_gguf_props(dataset.properties) | ||
ref_llama_config = RefLlamaModelConfig(hp) | ||
ref_llama_config.activation_dtype = torch.float16 | ||
model = DirectCacheMixtralModelV1(dataset.root_theta, ref_llama_config) | ||
|
||
kv_cache = model.create_cache(bs=1) | ||
start_index = 0 | ||
next_tokens = [1, 1059, 31871, 1217, 322, 266, 3682, 6075, 31902, 13, 31849, 31871] | ||
print(f"Step {start_index}") | ||
tokens = model.forward( | ||
torch.tensor([next_tokens]), start_index=start_index, local_kv_cache=kv_cache | ||
) | ||
print(f" : tokens = {tokens}") | ||
|
||
# Decode a step. | ||
print("Decoding...") | ||
print(tokens.shape, tokens) | ||
decode_token = model.forward(tokens, start_index=12, local_kv_cache=kv_cache) | ||
print(f" : decode tokens = {decode_token}") | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main(sys.argv[1:])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2024 Advanced Micro Devices, Inc | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
from typing import Optional | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
from .base import Theta, ThetaLayer | ||
from .linear import LinearLayer | ||
|
||
__all__ = [ | ||
"FFN", | ||
] | ||
|
||
|
||
class FFN(ThetaLayer): | ||
def __init__( | ||
self, | ||
theta: Theta, | ||
): | ||
super().__init__(theta) | ||
|
||
self.add_module("ffn_gate", LinearLayer(theta("ffn_gate"))) | ||
self.add_module("ffn_up", LinearLayer(theta("ffn_up"))) | ||
self.add_module("ffn_down", LinearLayer(theta("ffn_down"))) | ||
|
||
def forward( | ||
self, | ||
h: torch.Tensor, | ||
): | ||
ffn_gate = F.silu(self.ffn_gate(h)) | ||
ffn_up = self.ffn_up(h) | ||
ffn_down = self.ffn_down(ffn_gate * ffn_up) | ||
return ffn_down |
Oops, something went wrong.