Compare and contrast the two programs below. Gemini script: [code] ChatGPT script: [code]
- User
Compare and contrast the two programs below. Gemini script:
import json import pandas as pd from datasets import Dataset from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForLanguageModeling import torch # --- 1. Data Loading and Preparation --- # Load the full dataset with open("balanced_labeled_personality_conversations_cleaned.json", "r") as f: data = json.load(f) # Create a DataFrame and filter for our target traits df = pd.DataFrame(data) traits = ["witty", "sarcastic", "charming"] df = df[df['trait'].isin(traits)] # --- 2. Model and Tokenizer Setup for MoE --- # Use a real MoE model from DeepSeek. This one has 16B total parameters. # Note: This is a large model and will require significant GPU resources. model_checkpoint = "deepseek-ai/deepseek-moe-16b-base" # trust_remote_code is necessary for many new model architectures # torch_dtype=torch.bfloat16 is used for memory efficiency tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained( model_checkpoint, trust_remote_code=True, torch_dtype=torch.bfloat16, # device_map="auto" helps distribute the model across available GPUs device_map="auto" ) # It's good practice to add a padding token if the tokenizer doesn't have one if tokenizer.pad_token is None: tokenizer.pad_token = tokenizer.eos_token # --- 3. Formatting and Tokenization for Guided Specialization --- # We add a "control token" to the prompt to guide the MoE router. # The model will learn to associate "[EXPERT: WITTY]" with a witty response style. def format_conversation_for_moe(example): # The trait is converted to uppercase to make the control token distinct. trait_token = f"[EXPERT: {example['trait'].upper()}]" return f"{trait_token} <|user|> {example['dialogue']}\n<|assistant|>" # Apply the new formatting to the entire DataFrame print("Formatting dataset for MoE training...") df["text"] = df.apply(format_conversation_for_moe, axis=1) # Tokenization function remains largely the same def tokenize_data(data): return tokenizer( data["text"], truncation=True, # A longer max_length might be beneficial depending on your data max_length=512, padding="max_length", ) # Create a single dataset from the combined and formatted pandas DataFrame print("Creating and tokenizing the unified dataset...") full_dataset = Dataset.from_pandas(df[["text"]]) tokenized_dataset = full_dataset.map(tokenize_data, batched=True, remove_columns=["text"]) # --- 4. Training the Single MoE Model --- print("\n🚀 Training the unified MoE model...") # Training arguments are configured for the single training run. # Note: You will likely need to adjust batch size based on your GPU memory. # Using gradient_accumulation_steps can help simulate a larger batch size. training_args = TrainingArguments( output_dir="./moe_specialized_model", per_device_train_batch_size=1, # Lower this if you run out of memory gradient_accumulation_steps=4, # Effective batch size = 1 * 4 = 4 num_train_epochs=1, save_steps=500, save_total_limit=1, logging_steps=50, logging_dir="./logs_moe", # The aux_loss is crucial for MoE models to ensure expert load balancing label_names=["input_ids", "attention_mask"], report_to="none" # Disables reporting to services like wandb ) trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_dataset, tokenizer=tokenizer, data_collator=DataCollatorForLanguageModeling(tokenizer, mlm=False), ) # This single train() call fine-tunes the model on all personalities at once, # guided by the control tokens. trainer.train() print("✅ Finished training the unified MoE model.") # Save the final model and tokenizer final_model_path = "./moe_specialized_model/final" model.save_pretrained(final_model_path) tokenizer.save_pretrained(final_model_path) print(f"✅ Model saved to {final_model_path}") # --- 5. Inference with the Specialized MoE Model --- from transformers import pipeline def generate_moe_response(trait, user_input, model, tokenizer): """ Generates a response from the single, specialized MoE model. """ trait = trait.lower() if trait not in traits: return f"Error: Trait '{trait}' is not valid. Use one of {traits}." # Construct the prompt with the correct control token to guide the model trait_token = f"[EXPERT: {trait.upper()}]" prompt = f"{trait_token} <|user|> {user_input}\n<|assistant|>" # Create the pipeline on the fly pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device_map="auto") result = pipe(prompt, max_new_tokens=100, do_sample=True, temperature=0.7) return result[0]["generated_text"] # Example Usage (after training is complete) print("\n--- Testing Inference ---") # You would load the final model before running inference like this: # final_model_path = "./moe_specialized_model/final" # loaded_tokenizer = AutoTokenizer.from_pretrained(final_model_path) # loaded_model = AutoModelForCausalLM.from_pretrained(final_model_path, device_map="auto", torch_dtype=torch.bfloat16) # For demonstration, we use the model already in memory sarcastic_response = generate_moe_response("sarcastic", "What do you think about online dating?", model, tokenizer) print(f"\nSarcastic Expert Response:\n{sarcastic_response}") charming_response = generate_moe_response("charming", "I had a really rough day.", model, tokenizer) print(f"\nCharming Expert Response:\n{charming_response}") - Assistant
- User
My goal is to build an conversational MoE model where the experts where the experts specialize in different converational traits (charming, witty, sarcastic). At inference, the user can select a trait and the expert associated with that trait will be the only one to be activated. Which of the provided scripts is better for this stated purpose?
- Assistant
- User
Could you update the chatGPT script. If possible, keep expert prompting. This way the experts learn to generate trait specific responses from labeled data and prompting.
- Assistant
- User
Explain this from notes and tips: Tokenizer model compatibility: The explicit control token [EXPERT: TRAIT] should be treated as a known special token or at least a regular token by your tokenizer and model. If needed, you can add those tokens explicitly via tokenizer.add_tokens([...]) before training.
- Assistant
- User
Yes please!
- Assistant
- User
- Assistant
- User
Please do so!
- Assistant
- User
Would it be possible to add multi-trait blending at inference (like witty=0.6, charming=0.3. sarcastic=0.1)? Thus, the weighted output of each expert is combined in a final output.
- Assistant
- User
Please move forward with the second option: Ensemble multiple single-expert generations (post-hoc blending).
- Assistant
- User
Yes please!
- Assistant
- User
- Assistant