Performance optimization is a crucial part of building efficient machine learning models. You can use the XProf profiling tool to measure the performance of your machine learning workloads. XProf lets you capture detailed traces of your model's execution on XLA devices. These traces can help you to identify performance bottlenecks, understand device utilization, and optimize your code.
এই নির্দেশিকাটি আপনার PyTorch XLA স্ক্রিপ্ট থেকে প্রোগ্রাম্যাটিকভাবে একটি ট্রেস ক্যাপচার করার এবং XProf ব্যবহার করে ভিজ্যুয়ালাইজ করার প্রক্রিয়া বর্ণনা করে।
প্রোগ্রাম্যাটিকভাবে একটি ট্রেস ক্যাপচার করুন
আপনার বিদ্যমান প্রশিক্ষণ স্ক্রিপ্টে কয়েকটি লাইন কোড যোগ করে আপনি একটি ট্রেস ক্যাপচার করতে পারেন। ট্রেস ক্যাপচার করার প্রাথমিক টুল হল torch_xla.debug.profiler মডিউল, যা সাধারণত xp নাম দিয়ে আমদানি করা হয়।
১. প্রোফাইলার সার্ভার শুরু করুন
ট্রেস ক্যাপচার করার আগে, আপনাকে প্রোফাইলার সার্ভারটি শুরু করতে হবে। এই সার্ভারটি আপনার স্ক্রিপ্টের ব্যাকগ্রাউন্ডে চলে এবং ট্রেস ডেটা সংগ্রহ করে। আপনি আপনার প্রধান এক্সিকিউশন ব্লকের শুরুতে xp.start_server(<port>) কল করে এটি শুরু করতে পারেন।
2. ট্রেস সময়কাল নির্ধারণ করুন
আপনি যে কোডটি প্রোফাইল করতে চান তা xp.start_trace() এবং xp.stop_trace() কলের মধ্যে মুড়ে দিন। start_trace ফাংশনটি একটি ডিরেক্টরিতে একটি পথ নিয়ে যায় যেখানে ট্রেস ফাইলগুলি সংরক্ষণ করা হয়।
সবচেয়ে প্রাসঙ্গিক ক্রিয়াকলাপগুলি ক্যাপচার করার জন্য প্রধান প্রশিক্ষণ লুপটি মোড়ানো একটি সাধারণ অভ্যাস।
import torch_xla.debug.profiler as xp
# The directory where the trace files are stored.
log_dir = '/root/logs/'
# Start tracing
xp.start_trace(log_dir)
# ... your training loop or other code to be profiled ...
train_mnist()
# Stop tracing
xp.stop_trace()
৩. কাস্টম ট্রেস লেবেল যোগ করুন
By default, the traces captured are low-level Pytorch XLA functions and can be hard to navigate. You can add custom labels to specific sections of your code using the xp.Trace() context manager. These labels will appear as named blocks in the profiler's timeline view, making it much easier to identify specific operations like data preparation, the forward pass, or the optimizer step.
নিম্নলিখিত উদাহরণটি দেখায় কিভাবে আপনি প্রশিক্ষণ ধাপের বিভিন্ন অংশে প্রসঙ্গ যোগ করতে পারেন।
def forward(self, x):
# This entire block will be labeled 'forward' in the trace
with xp.Trace('forward'):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2(x), 2))
x = x.view(-1, 7*7*64)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
# You can also nest context managers for more granular detail
for batch_idx, (data, target) in enumerate(train_loader):
with torch_xla.step():
with xp.Trace('train_step_data_prep_and_forward'):
optimizer.zero_grad()
data, target = data.to(device), target.to(device)
output = model(data)
with xp.Trace('train_step_loss_and_backward'):
loss = loss_fn(output, target)
loss.backward()
with xp.Trace('train_step_optimizer_step_host'):
optimizer.step()
সম্পূর্ণ উদাহরণ
নিম্নলিখিত উদাহরণটি দেখায় কিভাবে mnist_xla.py ফাইলের উপর ভিত্তি করে একটি PyTorch XLA স্ক্রিপ্ট থেকে একটি ট্রেস ক্যাপচার করতে হয়।
import torch
import torch.optim as optim
from torchvision import datasets, transforms
# PyTorch/XLA specific imports
import torch_xla
import torch_xla.core.xla_model as xm
import torch_xla.debug.profiler as xp
def train_mnist():
# ... (model definition and data loading code) ...
print("Starting training...")
# ... (training loop as defined in the previous section) ...
print("Training finished!")
if __name__ == '__main__':
# 1. Start the profiler server
server = xp.start_server(9012)
# 2. Start capturing the trace and define the output directory
xp.start_trace('/root/logs/')
# Run the training function that contains custom trace labels
train_mnist()
# 3. Stop the trace
xp.stop_trace()
ট্রেসটি কল্পনা করুন
আপনার স্ক্রিপ্টটি সম্পন্ন হলে, ট্রেস ফাইলগুলি আপনার নির্দিষ্ট ডিরেক্টরিতে সংরক্ষিত হয় (উদাহরণস্বরূপ, /root/logs/ )। আপনি XProf ব্যবহার করে এই ট্রেসটি কল্পনা করতে পারেন।
আপনি আপনার লগ ডিরেক্টরিতে নির্দেশ করে স্ট্যান্ডঅ্যালোন XProf কমান্ড ব্যবহার করে সরাসরি প্রোফাইলার UI চালু করতে পারেন:
$ xprof --port=8791 /root/logs/
Attempting to start XProf server:
Log Directory: /root/logs/
Port: 8791
Worker Service Address: 0.0.0.0:50051
Hide Capture Button: False
XProf at http://localhost:8791/ (Press CTRL+C to quit)
প্রোফাইলটি দেখতে আপনার ব্রাউজারে প্রদত্ত URL (যেমন, http://localhost:8791/) এ যান।
আপনি আপনার তৈরি করা কাস্টম লেবেলগুলি দেখতে এবং আপনার মডেলের বিভিন্ন অংশের কার্যকরকরণ সময় বিশ্লেষণ করতে সক্ষম হবেন।
আপনি যদি আপনার কাজের চাপ চালানোর জন্য Google ক্লাউড ব্যবহার করেন, তাহলে আমরা cloud-diagnostics-xprof টুলটি সুপারিশ করি। এটি XProf চালিত VM ব্যবহার করে একটি সুগম প্রোফাইল সংগ্রহ এবং দেখার অভিজ্ঞতা প্রদান করে।