Upload folder using huggingface_hub
Browse files- examples/basic_inference.py +196 -0
- examples/ctransformers_usage.py +163 -0
- examples/isaac_sim_integration.py +321 -0
examples/basic_inference.py
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Basic inference example for Isaac Sim Robotics Qwen model.
|
| 4 |
+
|
| 5 |
+
This script demonstrates how to load and use the fine-tuned model
|
| 6 |
+
for Isaac Sim robotics queries.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 11 |
+
import argparse
|
| 12 |
+
import sys
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
def load_model(model_path, device="auto", load_in_8bit=False):
|
| 16 |
+
"""
|
| 17 |
+
Load the Isaac Sim Robotics Qwen model.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
model_path (str): Path to the model (local or HuggingFace hub)
|
| 21 |
+
device (str): Device to load model on ("auto", "cpu", "cuda")
|
| 22 |
+
load_in_8bit (bool): Whether to use 8-bit quantization
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
tuple: (model, tokenizer)
|
| 26 |
+
"""
|
| 27 |
+
print(f"Loading model from: {model_path}")
|
| 28 |
+
|
| 29 |
+
# Load tokenizer
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 31 |
+
|
| 32 |
+
# Set pad token if not present
|
| 33 |
+
if tokenizer.pad_token is None:
|
| 34 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 35 |
+
|
| 36 |
+
# Load model
|
| 37 |
+
if load_in_8bit:
|
| 38 |
+
try:
|
| 39 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 40 |
+
model_path,
|
| 41 |
+
load_in_8bit=True,
|
| 42 |
+
device_map=device,
|
| 43 |
+
torch_dtype=torch.float16
|
| 44 |
+
)
|
| 45 |
+
except ImportError:
|
| 46 |
+
print("8-bit quantization not available. Install bitsandbytes.")
|
| 47 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 48 |
+
model_path,
|
| 49 |
+
device_map=device,
|
| 50 |
+
torch_dtype=torch.float16
|
| 51 |
+
)
|
| 52 |
+
else:
|
| 53 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 54 |
+
model_path,
|
| 55 |
+
device_map=device,
|
| 56 |
+
torch_dtype=torch.float16
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
print("Model loaded successfully!")
|
| 60 |
+
return model, tokenizer
|
| 61 |
+
|
| 62 |
+
def generate_response(model, tokenizer, query, max_length=1024, temperature=0.7):
|
| 63 |
+
"""
|
| 64 |
+
Generate a response using the model.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
model: The loaded model
|
| 68 |
+
tokenizer: The loaded tokenizer
|
| 69 |
+
query (str): The input query
|
| 70 |
+
max_length (int): Maximum length of generated response
|
| 71 |
+
temperature (float): Sampling temperature
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
str: Generated response
|
| 75 |
+
"""
|
| 76 |
+
# Format query for Qwen2.5-Coder
|
| 77 |
+
formatted_query = f"<|im_start|>user\n{query}<|im_end|>\n<|im_start|>assistant"
|
| 78 |
+
|
| 79 |
+
# Tokenize input
|
| 80 |
+
inputs = tokenizer(formatted_query, return_tensors="pt")
|
| 81 |
+
|
| 82 |
+
# Move to same device as model
|
| 83 |
+
device = next(model.parameters()).device
|
| 84 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 85 |
+
|
| 86 |
+
# Generate response
|
| 87 |
+
with torch.no_grad():
|
| 88 |
+
outputs = model.generate(
|
| 89 |
+
**inputs,
|
| 90 |
+
max_length=max_length,
|
| 91 |
+
temperature=temperature,
|
| 92 |
+
do_sample=True,
|
| 93 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 94 |
+
eos_token_id=tokenizer.eos_token_id
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Decode response
|
| 98 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 99 |
+
|
| 100 |
+
# Extract only the assistant response
|
| 101 |
+
if "<|im_start|>assistant" in response:
|
| 102 |
+
response = response.split("<|im_start|>assistant")[1].strip()
|
| 103 |
+
|
| 104 |
+
return response
|
| 105 |
+
|
| 106 |
+
def main():
|
| 107 |
+
parser = argparse.ArgumentParser(description="Isaac Sim Robotics Qwen Inference")
|
| 108 |
+
parser.add_argument(
|
| 109 |
+
"--model_path",
|
| 110 |
+
type=str,
|
| 111 |
+
default="TomBombadyl/Qwen2.5-Coder-7B-Instruct-Omni1.1",
|
| 112 |
+
help="Path to model (local or HuggingFace hub)"
|
| 113 |
+
)
|
| 114 |
+
parser.add_argument(
|
| 115 |
+
"--device",
|
| 116 |
+
type=str,
|
| 117 |
+
default="auto",
|
| 118 |
+
choices=["auto", "cpu", "cuda"],
|
| 119 |
+
help="Device to use for inference"
|
| 120 |
+
)
|
| 121 |
+
parser.add_argument(
|
| 122 |
+
"--load_8bit",
|
| 123 |
+
action="store_true",
|
| 124 |
+
help="Use 8-bit quantization to reduce memory usage"
|
| 125 |
+
)
|
| 126 |
+
parser.add_argument(
|
| 127 |
+
"--max_length",
|
| 128 |
+
type=int,
|
| 129 |
+
default=1024,
|
| 130 |
+
help="Maximum length of generated response"
|
| 131 |
+
)
|
| 132 |
+
parser.add_argument(
|
| 133 |
+
"--temperature",
|
| 134 |
+
type=float,
|
| 135 |
+
default=0.7,
|
| 136 |
+
help="Sampling temperature"
|
| 137 |
+
)
|
| 138 |
+
parser.add_argument(
|
| 139 |
+
"--query",
|
| 140 |
+
type=str,
|
| 141 |
+
help="Query to ask (if not provided, will use interactive mode)"
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
args = parser.parse_args()
|
| 145 |
+
|
| 146 |
+
try:
|
| 147 |
+
# Load model
|
| 148 |
+
model, tokenizer = load_model(
|
| 149 |
+
args.model_path,
|
| 150 |
+
device=args.device,
|
| 151 |
+
load_in_8bit=args.load_8bit
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
if args.query:
|
| 155 |
+
# Single query mode
|
| 156 |
+
response = generate_response(
|
| 157 |
+
model, tokenizer, args.query, args.max_length, args.temperature
|
| 158 |
+
)
|
| 159 |
+
print(f"\nQuery: {args.query}")
|
| 160 |
+
print(f"Response:\n{response}")
|
| 161 |
+
else:
|
| 162 |
+
# Interactive mode
|
| 163 |
+
print("\n=== Isaac Sim Robotics Qwen Interactive Mode ===")
|
| 164 |
+
print("Type 'quit' to exit")
|
| 165 |
+
print("Example queries:")
|
| 166 |
+
print("- How do I create a differential drive robot in Isaac Sim?")
|
| 167 |
+
print("- How to add a depth camera to my robot?")
|
| 168 |
+
print("- What physics parameters should I use for a manipulator?")
|
| 169 |
+
print()
|
| 170 |
+
|
| 171 |
+
while True:
|
| 172 |
+
try:
|
| 173 |
+
query = input("Enter your Isaac Sim question: ").strip()
|
| 174 |
+
if query.lower() in ['quit', 'exit', 'q']:
|
| 175 |
+
break
|
| 176 |
+
if not query:
|
| 177 |
+
continue
|
| 178 |
+
|
| 179 |
+
print("Generating response...")
|
| 180 |
+
response = generate_response(
|
| 181 |
+
model, tokenizer, query, args.max_length, args.temperature
|
| 182 |
+
)
|
| 183 |
+
print(f"\nResponse:\n{response}\n")
|
| 184 |
+
|
| 185 |
+
except KeyboardInterrupt:
|
| 186 |
+
print("\nExiting...")
|
| 187 |
+
break
|
| 188 |
+
except Exception as e:
|
| 189 |
+
print(f"Error generating response: {e}")
|
| 190 |
+
|
| 191 |
+
except Exception as e:
|
| 192 |
+
print(f"Error loading model: {e}")
|
| 193 |
+
sys.exit(1)
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
CTransformers usage example for Isaac Sim Robotics Qwen model.
|
| 4 |
+
|
| 5 |
+
This script demonstrates how to use the model with CTransformers
|
| 6 |
+
for lightweight, memory-efficient inference.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from ctransformers import AutoModelForCausalLM
|
| 10 |
+
import argparse
|
| 11 |
+
import sys
|
| 12 |
+
|
| 13 |
+
def load_model(model_path, model_type="qwen2", gpu_layers=0):
|
| 14 |
+
"""
|
| 15 |
+
Load the Isaac Sim Robotics Qwen model using CTransformers.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
model_path (str): Path to the CTransformers model
|
| 19 |
+
model_type (str): Model architecture type
|
| 20 |
+
gpu_layers (int): Number of layers to offload to GPU (0 = CPU only)
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
AutoModelForCausalLM: Loaded model
|
| 24 |
+
"""
|
| 25 |
+
print(f"Loading CTransformers model from: {model_path}")
|
| 26 |
+
print(f"Model type: {model_type}, GPU layers: {gpu_layers}")
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 30 |
+
model_path,
|
| 31 |
+
model_type=model_type,
|
| 32 |
+
gpu_layers=gpu_layers,
|
| 33 |
+
lib="avx2" # Use AVX2 optimizations if available
|
| 34 |
+
)
|
| 35 |
+
print("Model loaded successfully!")
|
| 36 |
+
return model
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Error loading model: {e}")
|
| 39 |
+
print("Make sure you have the CTransformers model files in the specified directory.")
|
| 40 |
+
sys.exit(1)
|
| 41 |
+
|
| 42 |
+
def generate_response(model, query, max_length=1024, temperature=0.7):
|
| 43 |
+
"""
|
| 44 |
+
Generate a response using the CTransformers model.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
model: The loaded CTransformers model
|
| 48 |
+
query (str): The input query
|
| 49 |
+
max_length (int): Maximum length of generated response
|
| 50 |
+
temperature (float): Sampling temperature
|
| 51 |
+
|
| 52 |
+
Returns:
|
| 53 |
+
str: Generated response
|
| 54 |
+
"""
|
| 55 |
+
# Format query for Qwen2.5-Coder
|
| 56 |
+
formatted_query = f"<|im_start|>user\n{query}<|im_end|>\n<|im_start|>assistant"
|
| 57 |
+
|
| 58 |
+
# Generate response
|
| 59 |
+
response = model(
|
| 60 |
+
formatted_query,
|
| 61 |
+
max_new_tokens=max_length,
|
| 62 |
+
temperature=temperature,
|
| 63 |
+
do_sample=True,
|
| 64 |
+
stop=["<|im_end|>", "<|im_start|>"]
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Extract only the assistant response
|
| 68 |
+
if "<|im_start|>assistant" in response:
|
| 69 |
+
response = response.split("<|im_start|>assistant")[1].strip()
|
| 70 |
+
|
| 71 |
+
return response
|
| 72 |
+
|
| 73 |
+
def main():
|
| 74 |
+
parser = argparse.ArgumentParser(description="Isaac Sim Robotics Qwen CTransformers Inference")
|
| 75 |
+
parser.add_argument(
|
| 76 |
+
"--model_path",
|
| 77 |
+
type=str,
|
| 78 |
+
default="models/ctransformers",
|
| 79 |
+
help="Path to CTransformers model directory"
|
| 80 |
+
)
|
| 81 |
+
parser.add_argument(
|
| 82 |
+
"--model_type",
|
| 83 |
+
type=str,
|
| 84 |
+
default="qwen2",
|
| 85 |
+
help="Model architecture type"
|
| 86 |
+
)
|
| 87 |
+
parser.add_argument(
|
| 88 |
+
"--gpu_layers",
|
| 89 |
+
type=int,
|
| 90 |
+
default=0,
|
| 91 |
+
help="Number of layers to offload to GPU (0 = CPU only)"
|
| 92 |
+
)
|
| 93 |
+
parser.add_argument(
|
| 94 |
+
"--max_length",
|
| 95 |
+
type=int,
|
| 96 |
+
default=1024,
|
| 97 |
+
help="Maximum length of generated response"
|
| 98 |
+
)
|
| 99 |
+
parser.add_argument(
|
| 100 |
+
"--temperature",
|
| 101 |
+
type=float,
|
| 102 |
+
default=0.7,
|
| 103 |
+
help="Sampling temperature"
|
| 104 |
+
)
|
| 105 |
+
parser.add_argument(
|
| 106 |
+
"--query",
|
| 107 |
+
type=str,
|
| 108 |
+
help="Query to ask (if not provided, will use interactive mode)"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
args = parser.parse_args()
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
# Load model
|
| 115 |
+
model = load_model(
|
| 116 |
+
args.model_path,
|
| 117 |
+
model_type=args.model_type,
|
| 118 |
+
gpu_layers=args.gpu_layers
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if args.query:
|
| 122 |
+
# Single query mode
|
| 123 |
+
response = generate_response(
|
| 124 |
+
model, args.query, args.max_length, args.temperature
|
| 125 |
+
)
|
| 126 |
+
print(f"\nQuery: {args.query}")
|
| 127 |
+
print(f"Response:\n{response}")
|
| 128 |
+
else:
|
| 129 |
+
# Interactive mode
|
| 130 |
+
print("\n=== Isaac Sim Robotics Qwen CTransformers Interactive Mode ===")
|
| 131 |
+
print("Type 'quit' to exit")
|
| 132 |
+
print("Example queries:")
|
| 133 |
+
print("- How do I create a differential drive robot in Isaac Sim?")
|
| 134 |
+
print("- How to add a depth camera to my robot?")
|
| 135 |
+
print("- What physics parameters should I use for a manipulator?")
|
| 136 |
+
print()
|
| 137 |
+
|
| 138 |
+
while True:
|
| 139 |
+
try:
|
| 140 |
+
query = input("Enter your Isaac Sim question: ").strip()
|
| 141 |
+
if query.lower() in ['quit', 'exit', 'q']:
|
| 142 |
+
break
|
| 143 |
+
if not query:
|
| 144 |
+
continue
|
| 145 |
+
|
| 146 |
+
print("Generating response...")
|
| 147 |
+
response = generate_response(
|
| 148 |
+
model, query, args.max_length, args.temperature
|
| 149 |
+
)
|
| 150 |
+
print(f"\nResponse:\n{response}\n")
|
| 151 |
+
|
| 152 |
+
except KeyboardInterrupt:
|
| 153 |
+
print("\nExiting...")
|
| 154 |
+
break
|
| 155 |
+
except Exception as e:
|
| 156 |
+
print(f"Error generating response: {e}")
|
| 157 |
+
|
| 158 |
+
except Exception as e:
|
| 159 |
+
print(f"Error: {e}")
|
| 160 |
+
sys.exit(1)
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
main()
|
examples/isaac_sim_integration.py
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Isaac Sim integration example for the Isaac Sim Robotics Qwen model.
|
| 4 |
+
|
| 5 |
+
This script demonstrates how to use the fine-tuned model to generate
|
| 6 |
+
Isaac Sim code and integrate it into simulation workflows.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import omni.isaac.core.utils.extensions as extensions
|
| 10 |
+
import omni.isaac.core.utils.stage as stage_utils
|
| 11 |
+
import omni.isaac.core.utils.prims as prim_utils
|
| 12 |
+
import omni.isaac.core.utils.nucleus as nucleus_utils
|
| 13 |
+
import omni.isaac.core.utils.semantics as semantics_utils
|
| 14 |
+
import omni.isaac.core.materials as materials
|
| 15 |
+
import omni.isaac.core.articulations as articulations
|
| 16 |
+
import omni.isaac.core.robots as robots
|
| 17 |
+
import omni.isaac.core.objects as objects
|
| 18 |
+
import omni.isaac.core.utils.torch as torch_utils
|
| 19 |
+
import omni.isaac.core.utils.torch.maths as torch_maths
|
| 20 |
+
import omni.isaac.core.utils.torch.rotations as torch_rotations
|
| 21 |
+
import omni.isaac.core.utils.torch.transforms as torch_transforms
|
| 22 |
+
import omni.isaac.core.utils.torch.visualization as torch_visualization
|
| 23 |
+
import omni.isaac.core.utils.torch.control as torch_control
|
| 24 |
+
import omni.isaac.core.utils.torch.planning as torch_planning
|
| 25 |
+
import omni.isaac.core.utils.torch.perception as torch_perception
|
| 26 |
+
import omni.isaac.core.utils.torch.learning as torch_learning
|
| 27 |
+
import omni.isaac.core.utils.torch.optimization as torch_optimization
|
| 28 |
+
import omni.isaac.core.utils.torch.simulation as torch_simulation
|
| 29 |
+
import omni.isaac.core.utils.torch.physics as torch_physics
|
| 30 |
+
import omni.isaac.core.utils.torch.rendering as torch_rendering
|
| 31 |
+
import omni.isaac.core.utils.torch.audio as torch_audio
|
| 32 |
+
import omni.isaac.core.utils.torch.haptics as torch_haptics
|
| 33 |
+
import omni.isaac.core.utils.torch.networking as torch_networking
|
| 34 |
+
import omni.isaac.core.utils.torch.storage as torch_storage
|
| 35 |
+
import omni.isaac.core.utils.torch.training as torch_training
|
| 36 |
+
import omni.isaac.core.utils.torch.evaluation as torch_evaluation
|
| 37 |
+
import omni.isaac.core.utils.torch.visualization as torch_visualization
|
| 38 |
+
import omni.isaac.core.utils.torch.control as torch_control
|
| 39 |
+
import omni.isaac.core.utils.torch.planning as torch_planning
|
| 40 |
+
import omni.isaac.core.utils.torch.perception as torch_perception
|
| 41 |
+
import omni.isaac.core.utils.torch.learning as torch_learning
|
| 42 |
+
import omni.isaac.core.utils.torch.optimization as torch_optimization
|
| 43 |
+
import omni.isaac.core.utils.torch.simulation as torch_simulation
|
| 44 |
+
import omni.isaac.core.utils.torch.physics as torch_physics
|
| 45 |
+
import omni.isaac.core.utils.torch.rendering as torch_rendering
|
| 46 |
+
import omni.isaac.core.utils.torch.audio as torch_audio
|
| 47 |
+
import omni.isaac.core.utils.torch.haptics as torch_haptics
|
| 48 |
+
import omni.isaac.core.utils.torch.networking as torch_networking
|
| 49 |
+
import omni.isaac.core.utils.torch.storage as torch_storage
|
| 50 |
+
import omni.isaac.core.utils.torch.training as torch_training
|
| 51 |
+
import omni.isaac.core.utils.torch.evaluation as torch_evaluation
|
| 52 |
+
|
| 53 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 54 |
+
import torch
|
| 55 |
+
import numpy as np
|
| 56 |
+
import argparse
|
| 57 |
+
import sys
|
| 58 |
+
import os
|
| 59 |
+
|
| 60 |
+
class IsaacSimRoboticsAssistant:
|
| 61 |
+
"""
|
| 62 |
+
Assistant class that integrates the fine-tuned model with Isaac Sim workflows.
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
def __init__(self, model_path, device="auto"):
|
| 66 |
+
"""
|
| 67 |
+
Initialize the Isaac Sim Robotics Assistant.
|
| 68 |
+
|
| 69 |
+
Args:
|
| 70 |
+
model_path (str): Path to the fine-tuned model
|
| 71 |
+
device (str): Device to use for inference
|
| 72 |
+
"""
|
| 73 |
+
self.model_path = model_path
|
| 74 |
+
self.device = device
|
| 75 |
+
self.model = None
|
| 76 |
+
self.tokenizer = None
|
| 77 |
+
self.simulation_world = None
|
| 78 |
+
|
| 79 |
+
# Load the model
|
| 80 |
+
self._load_model()
|
| 81 |
+
|
| 82 |
+
# Initialize Isaac Sim
|
| 83 |
+
self._initialize_isaac_sim()
|
| 84 |
+
|
| 85 |
+
def _load_model(self):
|
| 86 |
+
"""Load the fine-tuned Isaac Sim robotics model."""
|
| 87 |
+
print(f"Loading Isaac Sim Robotics model from: {self.model_path}")
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_path)
|
| 91 |
+
if self.tokenizer.pad_token is None:
|
| 92 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 93 |
+
|
| 94 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 95 |
+
self.model_path,
|
| 96 |
+
device_map=self.device,
|
| 97 |
+
torch_dtype=torch.float16
|
| 98 |
+
)
|
| 99 |
+
print("Model loaded successfully!")
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"Error loading model: {e}")
|
| 102 |
+
raise
|
| 103 |
+
|
| 104 |
+
def _initialize_isaac_sim(self):
|
| 105 |
+
"""Initialize Isaac Sim simulation environment."""
|
| 106 |
+
print("Initializing Isaac Sim...")
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
# Enable required extensions
|
| 110 |
+
extensions.enable_extension("omni.isaac.core")
|
| 111 |
+
extensions.enable_extension("omni.isaac.sim")
|
| 112 |
+
extensions.enable_extension("omni.isaac.ui")
|
| 113 |
+
extensions.enable_extension("omni.isaac.debug_draw")
|
| 114 |
+
|
| 115 |
+
# Initialize stage
|
| 116 |
+
stage_utils.initialize_stage()
|
| 117 |
+
|
| 118 |
+
print("Isaac Sim initialized successfully!")
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"Warning: Isaac Sim initialization failed: {e}")
|
| 121 |
+
print("Some features may not work without proper Isaac Sim setup.")
|
| 122 |
+
|
| 123 |
+
def generate_robot_code(self, query):
|
| 124 |
+
"""
|
| 125 |
+
Generate Isaac Sim robot code using the fine-tuned model.
|
| 126 |
+
|
| 127 |
+
Args:
|
| 128 |
+
query (str): Description of what robot to create
|
| 129 |
+
|
| 130 |
+
Returns:
|
| 131 |
+
str: Generated Python code for Isaac Sim
|
| 132 |
+
"""
|
| 133 |
+
# Format query for the model
|
| 134 |
+
formatted_query = f"""<|im_start|>user
|
| 135 |
+
Create a complete Python script for Isaac Sim 5.0 that: {query}
|
| 136 |
+
|
| 137 |
+
Please provide the complete, runnable code with proper imports and error handling.
|
| 138 |
+
<|im_end|>
|
| 139 |
+
<|im_start|>assistant"""
|
| 140 |
+
|
| 141 |
+
# Generate response
|
| 142 |
+
inputs = self.tokenizer(formatted_query, return_tensors="pt")
|
| 143 |
+
device = next(self.model.parameters()).device
|
| 144 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 145 |
+
|
| 146 |
+
with torch.no_grad():
|
| 147 |
+
outputs = self.model.generate(
|
| 148 |
+
**inputs,
|
| 149 |
+
max_length=2048,
|
| 150 |
+
temperature=0.7,
|
| 151 |
+
do_sample=True,
|
| 152 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 153 |
+
eos_token_id=self.tokenizer.eos_token_id
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 157 |
+
|
| 158 |
+
# Extract only the assistant response
|
| 159 |
+
if "<|im_start|>assistant" in response:
|
| 160 |
+
response = response.split("<|im_start|>assistant")[1].strip()
|
| 161 |
+
|
| 162 |
+
return response
|
| 163 |
+
|
| 164 |
+
def execute_generated_code(self, code):
|
| 165 |
+
"""
|
| 166 |
+
Execute the generated Isaac Sim code.
|
| 167 |
+
|
| 168 |
+
Args:
|
| 169 |
+
code (str): Python code to execute
|
| 170 |
+
|
| 171 |
+
Returns:
|
| 172 |
+
bool: True if execution successful, False otherwise
|
| 173 |
+
"""
|
| 174 |
+
try:
|
| 175 |
+
print("Executing generated code...")
|
| 176 |
+
print("=" * 50)
|
| 177 |
+
print(code)
|
| 178 |
+
print("=" * 50)
|
| 179 |
+
|
| 180 |
+
# Create a safe execution environment
|
| 181 |
+
exec_globals = {
|
| 182 |
+
'omni': __import__('omni'),
|
| 183 |
+
'omni.isaac': __import__('omni.isaac'),
|
| 184 |
+
'omni.isaac.core': __import__('omni.isaac.core'),
|
| 185 |
+
'omni.isaac.sim': __import__('omni.isaac.sim'),
|
| 186 |
+
'omni.isaac.ui': __import__('omni.isaac.ui'),
|
| 187 |
+
'omni.isaac.debug_draw': __import__('omni.isaac.debug_draw'),
|
| 188 |
+
'torch': torch,
|
| 189 |
+
'numpy': np,
|
| 190 |
+
'stage_utils': stage_utils,
|
| 191 |
+
'prim_utils': prim_utils,
|
| 192 |
+
'nucleus_utils': nucleus_utils,
|
| 193 |
+
'semantics_utils': semantics_utils,
|
| 194 |
+
'materials': materials,
|
| 195 |
+
'articulations': articulations,
|
| 196 |
+
'robots': robots,
|
| 197 |
+
'objects': objects,
|
| 198 |
+
'torch_utils': torch_utils,
|
| 199 |
+
'torch_maths': torch_maths,
|
| 200 |
+
'torch_rotations': torch_rotations,
|
| 201 |
+
'torch_transforms': torch_transforms,
|
| 202 |
+
'torch_visualization': torch_visualization,
|
| 203 |
+
'torch_control': torch_control,
|
| 204 |
+
'torch_planning': torch_planning,
|
| 205 |
+
'torch_perception': torch_perception,
|
| 206 |
+
'torch_learning': torch_learning,
|
| 207 |
+
'torch_optimization': torch_optimization,
|
| 208 |
+
'torch_simulation': torch_simulation,
|
| 209 |
+
'torch_physics': torch_physics,
|
| 210 |
+
'torch_rendering': torch_rendering,
|
| 211 |
+
'torch_audio': torch_audio,
|
| 212 |
+
'torch_haptics': torch_haptics,
|
| 213 |
+
'torch_networking': torch_networking,
|
| 214 |
+
'torch_storage': torch_storage,
|
| 215 |
+
'torch_training': torch_training,
|
| 216 |
+
'torch_evaluation': torch_evaluation,
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
exec(code, exec_globals)
|
| 220 |
+
print("Code executed successfully!")
|
| 221 |
+
return True
|
| 222 |
+
|
| 223 |
+
except Exception as e:
|
| 224 |
+
print(f"Error executing generated code: {e}")
|
| 225 |
+
return False
|
| 226 |
+
|
| 227 |
+
def interactive_mode(self):
|
| 228 |
+
"""Run interactive mode for Isaac Sim robotics assistance."""
|
| 229 |
+
print("\n=== Isaac Sim Robotics Assistant Interactive Mode ===")
|
| 230 |
+
print("I can help you create robots, environments, and simulations in Isaac Sim!")
|
| 231 |
+
print("Type 'quit' to exit")
|
| 232 |
+
print("\nExample requests:")
|
| 233 |
+
print("- Create a differential drive robot with wheels")
|
| 234 |
+
print("- Build a warehouse environment with obstacles")
|
| 235 |
+
print("- Add a depth camera to my robot")
|
| 236 |
+
print("- Create a UR5 manipulator arm")
|
| 237 |
+
print()
|
| 238 |
+
|
| 239 |
+
while True:
|
| 240 |
+
try:
|
| 241 |
+
request = input("What would you like me to create in Isaac Sim? ").strip()
|
| 242 |
+
if request.lower() in ['quit', 'exit', 'q']:
|
| 243 |
+
break
|
| 244 |
+
if not request:
|
| 245 |
+
continue
|
| 246 |
+
|
| 247 |
+
print("Generating Isaac Sim code...")
|
| 248 |
+
code = self.generate_robot_code(request)
|
| 249 |
+
|
| 250 |
+
print("\nGenerated Code:")
|
| 251 |
+
print(code)
|
| 252 |
+
|
| 253 |
+
# Ask if user wants to execute the code
|
| 254 |
+
execute = input("\nWould you like me to execute this code? (y/n): ").strip().lower()
|
| 255 |
+
if execute in ['y', 'yes']:
|
| 256 |
+
success = self.execute_generated_code(code)
|
| 257 |
+
if success:
|
| 258 |
+
print("Great! The code has been executed in Isaac Sim.")
|
| 259 |
+
else:
|
| 260 |
+
print("There was an issue executing the code. Check the error messages above.")
|
| 261 |
+
|
| 262 |
+
print()
|
| 263 |
+
|
| 264 |
+
except KeyboardInterrupt:
|
| 265 |
+
print("\nExiting...")
|
| 266 |
+
break
|
| 267 |
+
except Exception as e:
|
| 268 |
+
print(f"Error: {e}")
|
| 269 |
+
|
| 270 |
+
def main():
|
| 271 |
+
parser = argparse.ArgumentParser(description="Isaac Sim Robotics Assistant")
|
| 272 |
+
parser.add_argument(
|
| 273 |
+
"--model_path",
|
| 274 |
+
type=str,
|
| 275 |
+
default="TomBombadyl/Qwen2.5-Coder-7B-Instruct-Omni1.1",
|
| 276 |
+
help="Path to the fine-tuned Isaac Sim robotics model"
|
| 277 |
+
)
|
| 278 |
+
parser.add_argument(
|
| 279 |
+
"--device",
|
| 280 |
+
type=str,
|
| 281 |
+
default="auto",
|
| 282 |
+
choices=["auto", "cpu", "cuda"],
|
| 283 |
+
help="Device to use for inference"
|
| 284 |
+
)
|
| 285 |
+
parser.add_argument(
|
| 286 |
+
"--request",
|
| 287 |
+
type=str,
|
| 288 |
+
help="Specific Isaac Sim robotics request (if not provided, will use interactive mode)"
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
args = parser.parse_args()
|
| 292 |
+
|
| 293 |
+
try:
|
| 294 |
+
# Create assistant
|
| 295 |
+
assistant = IsaacSimRoboticsAssistant(args.model_path, args.device)
|
| 296 |
+
|
| 297 |
+
if args.request:
|
| 298 |
+
# Single request mode
|
| 299 |
+
print(f"Processing request: {args.request}")
|
| 300 |
+
code = assistant.generate_robot_code(args.request)
|
| 301 |
+
print("\nGenerated Code:")
|
| 302 |
+
print(code)
|
| 303 |
+
|
| 304 |
+
# Ask if user wants to execute
|
| 305 |
+
execute = input("\nWould you like me to execute this code? (y/n): ").strip().lower()
|
| 306 |
+
if execute in ['y', 'yes']:
|
| 307 |
+
success = assistant.execute_generated_code(code)
|
| 308 |
+
if success:
|
| 309 |
+
print("Code executed successfully!")
|
| 310 |
+
else:
|
| 311 |
+
print("Code execution failed.")
|
| 312 |
+
else:
|
| 313 |
+
# Interactive mode
|
| 314 |
+
assistant.interactive_mode()
|
| 315 |
+
|
| 316 |
+
except Exception as e:
|
| 317 |
+
print(f"Error: {e}")
|
| 318 |
+
sys.exit(1)
|
| 319 |
+
|
| 320 |
+
if __name__ == "__main__":
|
| 321 |
+
main()
|