Apple Metal Performance Shaders | أصلي، ترجم بواسطة AI
يأسفل هو كود لتجربة أداء (GPU) باستخدام PyTorch.
import torch
import argparse
import time
parser = argparse.ArgumentParser(description="Test torch with MPS or CPU.")
parser.add_argument("--device", type=str, default="mps", choices=["mps", "cpu"], help="Device to use (mps or cpu)")
args = parser.parse_args()
if args.device == "mps":
if torch.backends.mps.is_available():
print("Metal is available")
device = torch.device("mps")
else:
print("Metal is not available, using CPU instead")
device = torch.device("cpu")
elif args.device == "cpu":
device = torch.device("cpu")
print("Using CPU")
else:
print("Invalid device specified, using CPU instead")
device = torch.device("cpu")
# Create a tensor and move it to the specified device
x = torch.randn(5000, 5000, device=device)
y = torch.randn(5000, 5000, device=device)
# Perform a more complex computation
start_time = time.time()
result = torch.matmul(x, y)
for _ in range(10):
result = torch.matmul(result, y)
end_time = time.time()
# Print the result
print(result)
print(f"Time taken: {end_time - start_time:.4f} seconds")
نتائج التجربة تبيّن أن MPS أسرع بكثير من CPU. زمن التنفيذ على MPS لا يتجاوز 0.2% من زمن التنفيذ على CPU.
% python scripts/test_metal.py --device cpu
Time taken: 2.8784 seconds
% python scripts/test_metal.py --device mps
Time taken: 0.0061 seconds