fix and modify this code so when i run it with accelerate launch it uses all 8 intel…
- User
fix and modify this code so when i run it with accelerate launch it uses all 8 intel 1550 xpu s on this machine
import gradio as gr import torch from transformers import AutoTokenizer, AutoModelForCausalLM
Initialize the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-14B-Instruct")
Load the model from safetensors format
model = AutoModelForCausalLM.from_pretrained( "/scratch-1/q14", # Update this path to your actual model path torch_dtype=torch.float16, device_map="auto", # This will distribute across all available GPUs automatically trust_remote_code=True ).eval()
Function to generate text using the model
def generate_text(prompt, max_tokens=50, temperature=0.7, top_p=0.9): # Prepare the input prompt messages = [ {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."}, {"role": "user", "content": prompt} ]
code# Tokenize the input prompt input_text = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) inputs = tokenizer(input_text, return_tensors="pt").to("xpu") # Move input to XPU # Generate output using model with torch.no_grad(): outputs = model.generate( inputs["input_ids"], max_length=max_tokens + len(inputs["input_ids"][0]), temperature=temperature, top_p=top_p, repetition_penalty=1.05, pad_token_id=tokenizer.eos_token_id ) # Decode the generated tokens generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) return generated_textCreate the Gradio interface
iface = gr.Interface( fn=generate_text, inputs=[ gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"), gr.Slider(minimum=1, maximum=512, value=50, label="Max Tokens"), gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Temperature"), gr.Slider(minimum=0.1, maximum=1.0, value=0.9, label="Top P") ], outputs=gr.Textbox(label="Generated Text"), title="Qwen 2.5 14B Instruct Text Generator", description="Generate text using the Qwen 2.5 14B Instruct model with transformers.", )
Launch the interface
iface.launch(share=True)
the issue was when i accelerate launch g7.py
- Assistant