File size: 12,059 Bytes
4f0238f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | #!/usr/bin/env python3
"""
Inference script for TouchGrass models.
Supports both 3B and 7B, CUDA and MPS backends.
"""
import argparse
import sys
from pathlib import Path
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from configs.touchgrass_3b_config import TOUCHGRASS_3B_CONFIG
from configs.touchgrass_7b_config import TOUCHGRASS_7B_CONFIG
def parse_args():
parser = argparse.ArgumentParser(description="Run inference with TouchGrass model")
parser.add_argument(
"--model_path",
type=str,
required=True,
help="Path to trained model checkpoint",
)
parser.add_argument(
"--model_size",
type=str,
choices=["3b", "7b"],
default="3b",
help="Model size for config",
)
parser.add_argument(
"--device",
type=str,
default="cuda",
choices=["cuda", "mps", "cpu"],
help="Device to run on",
)
parser.add_argument(
"--use_mps",
action="store_true",
help="Use MPS backend (Apple Silicon)",
)
parser.add_argument(
"--quantization",
type=str,
choices=[None, "int8", "int4"],
default=None,
help="Apply quantization (CUDA only)",
)
parser.add_argument(
"--flash_attention",
action="store_true",
help="Use Flash Attention 2 (CUDA only)",
)
parser.add_argument(
"--torch_compile",
action="store_true",
help="Use torch.compile",
)
parser.add_argument(
"--prompt",
type=str,
default=None,
help="Input prompt for generation",
)
parser.add_argument(
"--interactive",
action="store_true",
help="Run in interactive mode",
)
parser.add_argument(
"--instrument",
type=str,
default=None,
choices=["guitar", "piano", "drums", "vocals", "theory", "dj", "general"],
help="Instrument context for system prompt",
)
parser.add_argument(
"--skill_level",
type=str,
default="beginner",
choices=["beginner", "intermediate", "advanced"],
help="User skill level",
)
parser.add_argument(
"--max_new_tokens",
type=int,
default=200,
help="Maximum new tokens to generate",
)
parser.add_argument(
"--temperature",
type=float,
default=0.8,
help="Sampling temperature",
)
parser.add_argument(
"--top_p",
type=float,
default=0.9,
help="Top-p sampling",
)
parser.add_argument(
"--repetition_penalty",
type=float,
default=1.1,
help="Repetition penalty",
)
return parser.parse_args()
def get_system_prompt(instrument: str, skill_level: str) -> str:
"""Get system prompt based on instrument and skill level."""
base_prompt = """You are Touch Grass 🌿, a warm, encouraging, and knowledgeable music assistant.
You help people with:
- Learning instruments (guitar, bass, piano, keys, drums, vocals)
- Understanding music theory at any level
- Writing songs (lyrics, chord progressions, structure)
- Ear training and developing musicality
- DJ skills and music production
- Genre knowledge and music history
Your personality:
- Patient and encouraging — learning music is hard and takes time
- Adapt to the learner's level automatically — simpler for beginners, deeper for advanced
- When someone is frustrated, acknowledge it warmly before helping
- Use tabs, chord diagrams, and notation when helpful
- Make learning fun, not intimidating
- Celebrate small wins
When generating tabs use this format:
[TAB]
e|---------|
B|---------|
G|---------|
D|---------|
A|---------|
E|---------|
[/TAB]
When showing chord progressions use: [PROGRESSION]I - IV - V - I[/PROGRESSION]"""
# Instrument-specific additions
instrument_additions = {
"guitar": "\n\nYou specialize in guitar and bass. You know:\n- All chord shapes (open, barre, power chords)\n- Tablature and fingerpicking patterns\n- Strumming and picking techniques\n- Guitar-specific theory (CAGED system, pentatonic scales)",
"piano": "\n\nYou specialize in piano and keyboards. You know:\n- Hand position and fingerings\n- Sheet music reading\n- Scales and arpeggios\n- Chord voicings and inversions\n- Pedaling techniques",
"drums": "\n\nYou specialize in drums and percussion. You know:\n- Drum set setup and tuning\n- Basic grooves and fills\n- Reading drum notation\n- Rhythm and timing\n- Different drumming styles",
"vocals": "\n\nYou specialize in vocals and singing. You know:\n- Breathing techniques\n- Vocal warm-ups\n- Pitch and intonation\n- Vocal registers and range\n- Mic technique",
"theory": "\n\nYou specialize in music theory and composition. You know:\n- Harmony and chord progressions\n- Scales and modes\n- Rhythm and time signatures\n- Song structure\n- Ear training",
"dj": "\n\nYou specialize in DJing and production. You know:\n- Beatmatching and mixing\n- EQ and compression\n- DAW software\n- Sound design\n- Genre-specific techniques",
}
if instrument in instrument_additions:
base_prompt += instrument_additions[instrument]
# Skill level adjustment
if skill_level == "beginner":
base_prompt += "\n\nYou are speaking to a BEGINNER. Use simple language, avoid jargon, break concepts into small steps, and be extra encouraging."
elif skill_level == "advanced":
base_prompt += "\n\nYou are speaking to an ADVANCED musician. Use technical terms freely, dive deep into nuances, and challenge them with sophisticated concepts."
return base_prompt
def load_model_and_tokenizer(args):
"""Load model and tokenizer with appropriate optimizations."""
# Load config
if args.model_size == "3b":
config_dict = TOUCHGRASS_3B_CONFIG
else:
config_dict = TOUCHGRASS_7B_CONFIG
# Determine torch dtype
if args.device == "cuda" and torch.cuda.is_available():
dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
elif args.device == "mps":
dtype = torch.float32
else:
dtype = torch.float32
print(f"Loading model from {args.model_path}")
print(f"Device: {args.device}, Dtype: {dtype}")
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(
args.model_path,
trust_remote_code=True,
)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
# Load model
model = AutoModelForCausalLM.from_pretrained(
args.model_path,
torch_dtype=dtype,
trust_remote_code=True,
device_map="auto" if args.device != "cpu" else None,
)
# Move to device if not using device_map
if args.device == "cpu":
model = model.cpu()
elif args.device == "cuda" and not torch.cuda.is_available():
print("CUDA not available, falling back to CPU")
model = model.cpu()
# Apply optimizations
if args.flash_attention and args.device == "cuda":
print("Flash Attention 2 enabled")
# Note: Flash Attention requires specific model architecture support
if args.torch_compile and args.device != "mps":
print("Using torch.compile")
model = torch.compile(model, mode="reduce-overhead", fullgraph=True)
model.eval()
print(f"Model loaded successfully. Vocab size: {tokenizer.vocab_size}")
return model, tokenizer
def generate_response(
model,
tokenizer,
prompt: str,
system_prompt: str,
max_new_tokens: int = 200,
temperature: float = 0.8,
top_p: float = 0.9,
repetition_penalty: float = 1.1,
):
"""Generate response from model."""
# Format with system prompt
full_prompt = f"system\n{system_prompt}\nuser\n{prompt}\nassistant\n"
# Tokenize
inputs = tokenizer(
full_prompt,
return_tensors="pt",
truncation=True,
max_length=4096 - max_new_tokens,
)
# Move to model device
device = next(model.parameters()).device
inputs = {k: v.to(device) for k, v in inputs.items()}
# Generate
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)
# Extract only the new tokens (assistant response)
input_length = inputs["input_ids"].shape[1]
generated_tokens = outputs[0][input_length:]
# Decode
response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
# Clean up (stop at next system/user marker if present)
for marker in ["system", "user", "assistant"]:
if marker in response:
response = response.split(marker)[0].strip()
return response
def interactive_mode(model, tokenizer, args):
"""Run interactive chat mode."""
system_prompt = get_system_prompt(args.instrument or "general", args.skill_level)
print("\n" + "="*60)
print("Touch Grass 🌿 Interactive Mode")
print("="*60)
print(f"Instrument: {args.instrument or 'general'}")
print(f"Skill level: {args.skill_level}")
print("\nType your questions. Type 'quit' or 'exit' to end.")
print("="*60 + "\n")
while True:
try:
user_input = input("You: ").strip()
if user_input.lower() in ["quit", "exit", "q"]:
print("Goodbye! Keep making music! 🎵")
break
if not user_input:
continue
print("\nTouch Grass: ", end="", flush=True)
response = generate_response(
model,
tokenizer,
user_input,
system_prompt,
max_new_tokens=args.max_new_tokens,
temperature=args.temperature,
top_p=args.top_p,
repetition_penalty=args.repetition_penalty,
)
print(response)
print()
except KeyboardInterrupt:
print("\n\nInterrupted. Goodbye!")
break
except Exception as e:
print(f"\nError: {e}")
continue
def single_prompt_mode(model, tokenizer, args):
"""Run single prompt inference."""
if not args.prompt:
print("Error: --prompt is required for single prompt mode")
sys.exit(1)
system_prompt = get_system_prompt(args.instrument or "general", args.skill_level)
print(f"\nPrompt: {args.prompt}\n")
print("Generating...\n")
response = generate_response(
model,
tokenizer,
args.prompt,
system_prompt,
max_new_tokens=args.max_new_tokens,
temperature=args.temperature,
top_p=args.top_p,
repetition_penalty=args.repetition_penalty,
)
print(f"Touch Grass: {response}")
def main():
args = parse_args()
# Validate device
if args.device == "cuda" and not torch.cuda.is_available():
print("CUDA not available, falling back to CPU")
args.device = "cpu"
if args.use_mps and args.device != "mps":
args.device = "mps"
# Load model and tokenizer
model, tokenizer = load_model_and_tokenizer(args)
# Run inference
if args.interactive:
interactive_mode(model, tokenizer, args)
else:
single_prompt_mode(model, tokenizer, args)
if __name__ == "__main__":
main() |