|
| 1 | +# Copyright 2024 Advanced Micro Devices, Inc |
| 2 | +# |
| 3 | +# Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +# See https://llvm.org/LICENSE.txt for license information. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | + |
| 7 | +import sys |
| 8 | + |
| 9 | +import torch |
| 10 | + |
| 11 | +from sharktank.layers import * |
| 12 | +from sharktank.types import * |
| 13 | +from sharktank.models.mixtral.mixtral import * |
| 14 | + |
| 15 | + |
| 16 | +def main(args: list[str]): |
| 17 | + from ..utils import cli |
| 18 | + |
| 19 | + torch.no_grad().__enter__() |
| 20 | + |
| 21 | + parser = cli.create_parser() |
| 22 | + cli.add_input_dataset_options(parser) |
| 23 | + args = cli.parse(parser) |
| 24 | + |
| 25 | + dataset = cli.get_input_dataset(args) |
| 26 | + hp = configs.LlamaHParams.from_gguf_props(dataset.properties) |
| 27 | + llama_config = LlamaModelConfig(hp) |
| 28 | + llama_config.kv_cache_type = "direct" |
| 29 | + llama_config.activation_dtype = torch.float16 |
| 30 | + model = PagedMixtralModelV1(dataset.root_theta, llama_config) |
| 31 | + |
| 32 | + # bs ("batch size") == 1 |
| 33 | + cache_state = model.cache.allocate(bs=1) |
| 34 | + |
| 35 | + start_index = 0 |
| 36 | + tokens = torch.tensor( |
| 37 | + [ |
| 38 | + [ |
| 39 | + 1, |
| 40 | + 1059, |
| 41 | + 31871, |
| 42 | + 1217, |
| 43 | + 322, |
| 44 | + 266, |
| 45 | + 3682, |
| 46 | + 6075, |
| 47 | + 31902, |
| 48 | + 13, |
| 49 | + 31849, |
| 50 | + 31871, |
| 51 | + 0, |
| 52 | + 0, |
| 53 | + 0, |
| 54 | + 0, |
| 55 | + ] |
| 56 | + + 48 * [0], |
| 57 | + ] |
| 58 | + ) |
| 59 | + assert tokens.shape[1] % model.cache.block_seq_stride == 0 |
| 60 | + seq_block_ids = torch.tensor( |
| 61 | + [ |
| 62 | + [127, 0, 0, 0], |
| 63 | + ] |
| 64 | + ) |
| 65 | + |
| 66 | + # Important: Do not use a sequence length of 0 for empty batch slots |
| 67 | + # as it will cause softmax to nan due to a mask of all -inf. This then |
| 68 | + # propagates and causes badness. |
| 69 | + seq_lens = torch.tensor([12]) |
| 70 | + |
| 71 | + attention_mask = model.attention_mask( |
| 72 | + model.input_mask(seq_lens, tokens.shape[1]), |
| 73 | + ) |
| 74 | + |
| 75 | + print(f"Step {start_index}") |
| 76 | + logits = model.prefill( |
| 77 | + tokens, |
| 78 | + attention_mask=attention_mask, |
| 79 | + seq_block_ids=seq_block_ids, |
| 80 | + cache_state=cache_state, |
| 81 | + ) |
| 82 | + # TODO: Normalize the output of extract_tokens_from_logits into tensor [bs, 1]. |
| 83 | + tokens = torch.tensor(model.extract_tokens_from_logits(logits, seq_lens)).unsqueeze( |
| 84 | + 1 |
| 85 | + ) |
| 86 | + print(f" : tokens = {tokens}") |
| 87 | + |
| 88 | + # Decode a step. |
| 89 | + print("Decoding...") |
| 90 | + print(tokens.shape, tokens) |
| 91 | + start_positions = torch.tensor([12]) |
| 92 | + seq_lens = seq_lens + 1 |
| 93 | + decode_attention_mask = model.decode_attention_mask( |
| 94 | + model.input_mask( |
| 95 | + seq_lens, |
| 96 | + seq_block_ids.shape[1] * model.cache.block_seq_stride, |
| 97 | + ), |
| 98 | + ) |
| 99 | + logits = model.decode( |
| 100 | + tokens, |
| 101 | + attention_mask=decode_attention_mask, |
| 102 | + start_positions=start_positions, |
| 103 | + seq_block_ids=seq_block_ids, |
| 104 | + cache_state=cache_state, |
| 105 | + ) |
| 106 | + tokens = torch.tensor(model.extract_tokens_from_logits(logits, [1])).unsqueeze(1) |
| 107 | + print(f" : tokens = {tokens}") |
| 108 | + |
| 109 | + def save_prefill_module(model): |
| 110 | + from iree.compiler.extras.fx_importer import FxImporter |
| 111 | + from iree.compiler.ir import AsmState |
| 112 | + |
| 113 | + importer = FxImporter() |
| 114 | + |
| 115 | + print("Generating FX graph") |
| 116 | + |
| 117 | + class InferenceModule(torch.nn.Module): |
| 118 | + def __init__(self): |
| 119 | + super().__init__() |
| 120 | + self.add_module("prefill", model) |
| 121 | + |
| 122 | + def forward(self, tokens, attention_mask, seq_block_ids, *cache_state): |
| 123 | + return self.prefill.prefill( |
| 124 | + tokens, |
| 125 | + attention_mask=attention_mask, |
| 126 | + seq_block_ids=seq_block_ids, |
| 127 | + cache_state=list(cache_state), |
| 128 | + ) |
| 129 | + |
| 130 | + infmod = InferenceModule() |
| 131 | + prog = torch.export.export( |
| 132 | + infmod, (tokens, attention_mask, seq_block_ids) + tuple(cache_state) |
| 133 | + ) |
| 134 | + |
| 135 | + print(f"FX prog:", prog) |
| 136 | + importer.import_program(prog, func_name="prefill") |
| 137 | + output_file = "/tmp/prefill.mlirbc" |
| 138 | + print("Saving to:", output_file) |
| 139 | + with open(output_file, "wb") as f: |
| 140 | + importer.module_op.write_bytecode(f) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + sys.exit(main(sys.argv[1:])) |
0 commit comments