|
| 1 | +import torch |
| 2 | +import os |
| 3 | + |
| 4 | +from transformers import AutoTokenizer, GenerationConfig |
| 5 | + |
| 6 | +from neuronx_distributed_inference.models.config import MultimodalVisionNeuronConfig, OnDeviceSamplingConfig |
| 7 | +from neuronx_distributed_inference.models.mllama.modeling_mllama import MllamaInferenceConfig, NeuronMllamaForCausalLM |
| 8 | +from neuronx_distributed_inference.utils.hf_adapter import load_pretrained_config, HuggingFaceGenerationAdapter |
| 9 | +from neuronx_distributed_inference.models.mllama.model_wrapper_mllama import NUM_IMAGE_PER_PROMPT |
| 10 | +from neuronx_distributed_inference.models.mllama.utils import create_vision_mask, get_image, get_image_tensors, add_instruct |
| 11 | +from neuronx_distributed_inference.modules.generation.sampling import prepare_sampling_params |
| 12 | +from neuronx_distributed_inference.utils.benchmark import benchmark_sampling |
| 13 | + |
| 14 | +# TODO : Either read from os_environment var or from arg_parser. |
| 15 | +checkpoint = "meta" |
| 16 | +model_variant = "11B" |
| 17 | +model_path = f"/home/ubuntu/models/Llama-3.2-{model_variant}-Vision-Instruct-{checkpoint}/" |
| 18 | +traced_model_path = f"/home/ubuntu/workplace/traced_models/Llama-3.2-{model_variant}-Vision-Instruct-{checkpoint}/" |
| 19 | + |
| 20 | +torch.manual_seed(0) |
| 21 | + |
| 22 | + |
| 23 | +def run_llama_generate(): |
| 24 | + # Initialize configs and tokenizer. |
| 25 | + batch_size = 1 |
| 26 | + num_img_per_prompt = 1 |
| 27 | + max_context_length = 1024 |
| 28 | + seq_len = 2048 |
| 29 | + |
| 30 | + generation_config = GenerationConfig.from_pretrained(model_path) |
| 31 | + generation_config_kwargs = { |
| 32 | + "top_k": 1, |
| 33 | + } |
| 34 | + generation_config.update(**generation_config_kwargs) |
| 35 | + |
| 36 | + on_device_sampling_config=OnDeviceSamplingConfig( |
| 37 | + dynamic=True, |
| 38 | + ) |
| 39 | + |
| 40 | + neuron_config = MultimodalVisionNeuronConfig( |
| 41 | + tp_degree=32, |
| 42 | + batch_size=batch_size, |
| 43 | + max_context_length=max_context_length, |
| 44 | + seq_len=seq_len, |
| 45 | + on_device_sampling_config=on_device_sampling_config, |
| 46 | + enable_bucketing=True, |
| 47 | + sequence_parallel_enabled=False, |
| 48 | + fused_qkv=False, |
| 49 | + async_mode=False, |
| 50 | + ) |
| 51 | + config = MllamaInferenceConfig( |
| 52 | + neuron_config, |
| 53 | + load_config=load_pretrained_config(model_path), |
| 54 | + ) |
| 55 | + |
| 56 | + tokenizer = AutoTokenizer.from_pretrained(model_path, padding_side="right") |
| 57 | + tokenizer.pad_token = tokenizer.eos_token |
| 58 | + |
| 59 | + |
| 60 | + # Generate outputs. |
| 61 | + image = get_image("dog.jpg") |
| 62 | + batch_image = [[image] * num_img_per_prompt] * batch_size |
| 63 | + pixel_values, aspect_ratios, num_chunks, has_image = get_image_tensors(config, batch_image) |
| 64 | + |
| 65 | + prompt = add_instruct("What is in this image? Tell me a story", has_image) |
| 66 | + batch_prompt = [prompt] * batch_size |
| 67 | + |
| 68 | + if not os.path.exists(traced_model_path): |
| 69 | + # Compile and save model. |
| 70 | + print("\nCompiling and saving model...") |
| 71 | + model = NeuronMllamaForCausalLM(model_path, config) |
| 72 | + model.compile(traced_model_path) |
| 73 | + tokenizer.save_pretrained(traced_model_path) |
| 74 | + |
| 75 | + # Load from compiled checkpoint. |
| 76 | + print("\nLoading model from compiled checkpoint...") |
| 77 | + model = NeuronMllamaForCausalLM(traced_model_path) |
| 78 | + model.load(traced_model_path) |
| 79 | + tokenizer = AutoTokenizer.from_pretrained(traced_model_path) |
| 80 | + |
| 81 | + print("\nGenerating outputs...") |
| 82 | + print(f"Prompts: {batch_prompt}") |
| 83 | + |
| 84 | + inputs = tokenizer(batch_prompt, padding=True, return_tensors="pt", add_special_tokens=False) |
| 85 | + |
| 86 | + vision_token_id = tokenizer("<|image|>", add_special_tokens=False).input_ids[0] |
| 87 | + vision_mask = create_vision_mask(inputs.input_ids, vision_token_id) |
| 88 | + |
| 89 | + generation_model = HuggingFaceGenerationAdapter(model) |
| 90 | + |
| 91 | + # Test Sampling Parameters |
| 92 | + sampling_params = prepare_sampling_params(batch_size=batch_size, top_k=[1], top_p=[1.0], temperature=[1.0]) |
| 93 | + outputs = generation_model.generate( |
| 94 | + inputs.input_ids, |
| 95 | + generation_config=generation_config, |
| 96 | + attention_mask=inputs.attention_mask, |
| 97 | + max_length=model.config.neuron_config.max_length, |
| 98 | + sampling_params=sampling_params, |
| 99 | + pixel_values=pixel_values, |
| 100 | + aspect_ratios=aspect_ratios, |
| 101 | + vision_mask =vision_mask, |
| 102 | + num_chunks=num_chunks, |
| 103 | + has_image=has_image, |
| 104 | + max_new_tokens=512, |
| 105 | + ) |
| 106 | + output_tokens = tokenizer.batch_decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False) |
| 107 | + |
| 108 | + print("Generated outputs:") |
| 109 | + for i, output_token in enumerate(output_tokens): |
| 110 | + print(f"Output {i}: {output_token}") |
| 111 | + |
| 112 | + |
| 113 | + # Test with text-only input |
| 114 | + pixel_values, aspect_ratios, num_chunks, has_image = get_image_tensors(config, [[]] * batch_size) |
| 115 | + |
| 116 | + prompt = add_instruct("what is the recipe of mayonnaise in two sentences?", has_image) |
| 117 | + batch_prompt = [prompt] * batch_size |
| 118 | + inputs = tokenizer(batch_prompt, padding=True, return_tensors="pt") |
| 119 | + |
| 120 | + sampling_params = prepare_sampling_params(batch_size=batch_size, top_k=[1], top_p=[1.0], temperature=[1.0]) |
| 121 | + outputs = generation_model.generate( |
| 122 | + inputs.input_ids, |
| 123 | + generation_config=generation_config, |
| 124 | + attention_mask=inputs.attention_mask, |
| 125 | + max_length=model.config.neuron_config.max_length, |
| 126 | + sampling_params=sampling_params, |
| 127 | + pixel_values=pixel_values, |
| 128 | + aspect_ratios=aspect_ratios, |
| 129 | + vision_mask=vision_mask, |
| 130 | + num_chunks=num_chunks, |
| 131 | + has_image=has_image, |
| 132 | + max_new_tokens=512, |
| 133 | + ) |
| 134 | + output_tokens = tokenizer.batch_decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False) |
| 135 | + |
| 136 | + print("Generated outputs:") |
| 137 | + for i, output_token in enumerate(output_tokens): |
| 138 | + print(f"Output {i}: {output_token}") |
| 139 | + |
| 140 | + print("\nPerformance Benchmarking!") |
| 141 | + benchmark_sampling(model=model, draft_model=None, generation_config=generation_config, target="all", image=True) |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + run_llama_generate() |
| 145 | + |
0 commit comments