Apple Metal Performance Shashi | मूल, AI द्वारा अनुवादित
निम्नलिखित एक स्क्रिप्ट है जो PyTorch का उपयोग करके Metal (GPU) की प्रदर्शन परीक्षा करता है।
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 उपलब्ध है")
device = torch.device("mps")
else:
print("Metal उपलब्ध नहीं है, CPU का इस्तेमाल किया जा रहा है")
device = torch.device("cpu")
elif args.device == "cpu":
device = torch.device("cpu")
print("CPU का इस्तेमाल किया जा रहा है")
else:
print("गलत device दिया गया, CPU का इस्तेमाल किया जा रहा है")
device = torch.device("cpu")
# एक टेंसोर बनाएं और इसे निर्दिष्ट device पर ले जाएँ
x = torch.randn(5000, 5000, device=device)
y = torch.randn(5000, 5000, device=device)
# एक जटिल गणना करें
start_time = time.time()
result = torch.matmul(x, y)
for _ in range(10):
result = torch.matmul(result, y)
end_time = time.time()
# परिणाम को प्रिंट करें
print(result)
print(f"समय लिया गया: {end_time - start_time:.4f} seconds")
नतीजे दर्शाते हैं कि MPS CPU से काफी तेज़ है। MPS पर कार्यान्वयन समय केवल CPU समय का लगभग 0.2% है।
% python scripts/test_metal.py --device cpu
समय लिया गया: 2.8784 seconds
% python scripts/test_metal.py --device mps
समय लिया गया: 0.0061 seconds