Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/hf_profiling_blog_pt1/compiled_rmsnorm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os

import torch
import torch.profiler
from torch.profiler import ProfilerActivity

from torch_neuronx.profiling import NeuronConfig, NeuronProfiler, ProfileMode

OUTPUT_DIR = "./profile_compiled_rmsnorm"
os.makedirs(OUTPUT_DIR, exist_ok=True)

os.environ["NEURON_RT_VISIBLE_CORES"] = "0"
os.environ["NEURON_RT_INSPECT_ENABLE"] = "1"
os.environ["NEURON_RT_INSPECT_DEVICE_PROFILE"] = "1"
os.environ["NEURON_RT_INSPECT_OUTPUT_DIR"] = OUTPUT_DIR
os.environ["XLA_IR_DEBUG"] = "1"
os.environ["XLA_HLO_DEBUG"] = "1"
os.environ["NEURON_FRAMEWORK_DEBUG"] = "1"

device = torch.device("neuron")
x = torch.randn(4, 512, dtype=torch.bfloat16, device=device)
weight = torch.ones(512, dtype=torch.bfloat16, device=device)


@torch.compile(backend="neuron", fullgraph=True)
def naive_compiled(t):
x_sq = t * t
mean_sq = x_sq.mean(dim=-1, keepdim=True)
rms = torch.rsqrt(mean_sq + 1e-6)
return t * rms * weight


nc = NeuronConfig(
modes=[ProfileMode.DEVICE, ProfileMode.RUNTIME],
profile_output_dir=OUTPUT_DIR,
capture_enabled_for_nc="0",
correlate_device_time=True,
)
exporter = NeuronProfiler(nc)

with torch.profiler.profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.PrivateUse1],
experimental_config=nc,
record_shapes=False,
on_trace_ready=exporter.export_trace,
schedule=torch.profiler.schedule(wait=4, warmup=1, active=1),
) as prof:
for _ in range(6):
with torch.no_grad(), torch.profiler.record_function("rmsnorm"):
_ = naive_compiled(x)
torch.neuron.synchronize()
prof.step()

print(prof.key_averages().table(sort_by="self_privateuse1_time_total", row_limit=1))
53 changes: 53 additions & 0 deletions examples/hf_profiling_blog_pt1/naive_rmsnorm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os

import torch
import torch.profiler
from torch.profiler import ProfilerActivity

from torch_neuronx.profiling import NeuronConfig, NeuronProfiler, ProfileMode

OUTPUT_DIR = "./profile_naive_rmsnorm"
os.makedirs(OUTPUT_DIR, exist_ok=True)

os.environ["NEURON_RT_VISIBLE_CORES"] = "0"
os.environ["NEURON_RT_INSPECT_ENABLE"] = "1"
os.environ["NEURON_RT_INSPECT_DEVICE_PROFILE"] = "1"
os.environ["NEURON_RT_INSPECT_OUTPUT_DIR"] = OUTPUT_DIR
os.environ["XLA_IR_DEBUG"] = "1"
os.environ["XLA_HLO_DEBUG"] = "1"
os.environ["NEURON_FRAMEWORK_DEBUG"] = "1"

device = torch.device("neuron")
x = torch.randn(4, 512, dtype=torch.bfloat16, device=device)
weight = torch.ones(512, dtype=torch.bfloat16, device=device)


def rms_norm_fn(t):
x_sq = t * t
mean_sq = x_sq.mean(dim=-1, keepdim=True)
rms = torch.rsqrt(mean_sq + 1e-6)
return t * rms * weight


nc = NeuronConfig(
modes=[ProfileMode.DEVICE, ProfileMode.RUNTIME],
profile_output_dir=OUTPUT_DIR,
capture_enabled_for_nc="0",
correlate_device_time=True,
)
exporter = NeuronProfiler(nc)

with torch.profiler.profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.PrivateUse1],
experimental_config=nc,
record_shapes=False,
on_trace_ready=exporter.export_trace,
schedule=torch.profiler.schedule(wait=4, warmup=1, active=1),
) as prof:
for _ in range(6):
with torch.no_grad(), torch.profiler.record_function("rmsnorm"):
_ = rms_norm_fn(x)
torch.neuron.synchronize()
prof.step()

print(prof.key_averages().table(sort_by="self_privateuse1_time_total", row_limit=4))
50 changes: 50 additions & 0 deletions examples/hf_profiling_blog_pt1/torch_rmsnorm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os

import torch
import torch.profiler
from torch.profiler import ProfilerActivity

from torch_neuronx.profiling import NeuronConfig, NeuronProfiler, ProfileMode

OUTPUT_DIR = "./profile_torch_rmsnorm"
os.makedirs(OUTPUT_DIR, exist_ok=True)

os.environ["NEURON_RT_VISIBLE_CORES"] = "0"
os.environ["NEURON_RT_INSPECT_ENABLE"] = "1"
os.environ["NEURON_RT_INSPECT_DEVICE_PROFILE"] = "1"
os.environ["NEURON_RT_INSPECT_OUTPUT_DIR"] = OUTPUT_DIR
os.environ["XLA_IR_DEBUG"] = "1"
os.environ["XLA_HLO_DEBUG"] = "1"
os.environ["NEURON_FRAMEWORK_DEBUG"] = "1"

device = torch.device("neuron")
x = torch.randn(4, 512, dtype=torch.bfloat16, device=device)
weight = torch.ones(512, dtype=torch.bfloat16, device=device)


def rms_norm_fn(t):
return torch.rms_norm(t, [512], weight)


nc = NeuronConfig(
modes=[ProfileMode.DEVICE, ProfileMode.RUNTIME],
profile_output_dir=OUTPUT_DIR,
capture_enabled_for_nc="0",
correlate_device_time=True,
)
exporter = NeuronProfiler(nc)

with torch.profiler.profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.PrivateUse1],
experimental_config=nc,
record_shapes=False,
on_trace_ready=exporter.export_trace,
schedule=torch.profiler.schedule(wait=4, warmup=1, active=1),
) as prof:
for _ in range(6):
with torch.no_grad(), torch.profiler.record_function("rmsnorm"):
_ = rms_norm_fn(x)
torch.neuron.synchronize()
prof.step()

print(prof.key_averages().table(sort_by="self_privateuse1_time_total", row_limit=1))
Loading