Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from sglang.srt.environ import envs
from sglang.srt.managers.io_struct import ProfileReq, ProfileReqOutput, ProfileReqType
from sglang.srt.model_executor.forward_batch_info import ForwardMode
from sglang.srt.platforms import current_platform
from sglang.srt.runtime_context import get_server_args
from sglang.srt.utils import is_mps, is_npu
from sglang.srt.utils.profile_merger import ProfileMerger
Expand Down Expand Up @@ -170,6 +171,15 @@ def _start_profile(
"CPU": torch.profiler.ProfilerActivity.CPU,
"GPU": torch.profiler.ProfilerActivity.CUDA,
}

if current_platform.is_out_of_tree():
if hasattr(
torch.profiler.ProfilerActivity,
current_platform.get_torch_profiler_activity_str(),
):
activity_map[current_platform.get_torch_profiler_activity_str()] = (
current_platform.get_torch_profiler_activity()
)
Comment on lines +175 to +182

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If get_torch_profiler_activity_str() returns None (or if it is not implemented), calling hasattr(torch.profiler.ProfilerActivity, None) will raise a TypeError. We should retrieve the values first and check if they are not None before checking attributes and updating the map.

Suggested change
if current_platform.is_out_of_tree():
if hasattr(
torch.profiler.ProfilerActivity,
current_platform.get_torch_profiler_activity_str(),
):
activity_map[current_platform.get_torch_profiler_activity_str()] = (
current_platform.get_torch_profiler_activity()
)
if current_platform.is_out_of_tree():
activity_str = current_platform.get_torch_profiler_activity_str()
activity = current_platform.get_torch_profiler_activity()
if activity_str and activity and hasattr(torch.profiler.ProfilerActivity, activity_str):
activity_map[activity_str] = activity

if hasattr(torch.profiler.ProfilerActivity, "XPU"):
activity_map["XPU"] = torch.profiler.ProfilerActivity.XPU
torchprof_activities = [
Expand Down
8 changes: 8 additions & 0 deletions python/sglang/srt/platforms/device_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ def get_cpu_architecture(cls) -> "CpuArchEnum":
return CpuArchEnum.ARM
return CpuArchEnum.UNSPECIFIED

def get_torch_profiler_activity_str(self) -> str:
"""[Planned] Return the torch profiler activity string."""
raise NotImplementedError

def get_torch_profiler_activity(self) -> torch.profiler.ProfilerActivity:
"""[Planned] Return the torch profiler activity."""
raise NotImplementedError
Comment on lines +254 to +260

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of raising NotImplementedError by default, it is safer to return None (using Optional). This prevents out-of-tree (OOT) platforms that do not require custom profiler activities from being forced to implement these methods, avoiding potential runtime crashes.

Suggested change
def get_torch_profiler_activity_str(self) -> str:
"""[Planned] Return the torch profiler activity string."""
raise NotImplementedError
def get_torch_profiler_activity(self) -> torch.profiler.ProfilerActivity:
"""[Planned] Return the torch profiler activity."""
raise NotImplementedError
def get_torch_profiler_activity_str(self) -> Optional[str]:
"""[Planned] Return the torch profiler activity string."""
return None
def get_torch_profiler_activity(self) -> Optional[torch.profiler.ProfilerActivity]:
"""[Planned] Return the torch profiler activity."""
return None


# ------------------------------------------------------------------
# Dunder helpers
# ------------------------------------------------------------------
Expand Down
17 changes: 17 additions & 0 deletions python/sglang/srt/utils/profile_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from sglang.srt.environ import envs
from sglang.srt.managers.io_struct import ProfileReqOutput
from sglang.srt.model_executor.forward_batch_info import ForwardBatch, ForwardMode
from sglang.srt.platforms import current_platform
from sglang.srt.runtime_context import get_server_args
from sglang.srt.utils import is_npu
from sglang.srt.utils.torch_npu_patch_utils import apply_torch_npu_patches
Expand Down Expand Up @@ -221,6 +222,16 @@ class _ProfilerBase(ABC):
@staticmethod
def create(activities, with_stack, record_shapes, **kwargs):
inners = []
if current_platform.is_out_of_tree():
if current_platform.get_torch_profiler_activity_str() in activities:
inners.append(
_ProfilerTorch(
**kwargs,
activities=activities,
with_stack=with_stack,
record_shapes=record_shapes,
)
)
Comment on lines +225 to +234

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent potential TypeError or issues when get_torch_profiler_activity_str() returns None, we should check that the returned activity string is not None before checking if it is in activities.

Suggested change
if current_platform.is_out_of_tree():
if current_platform.get_torch_profiler_activity_str() in activities:
inners.append(
_ProfilerTorch(
**kwargs,
activities=activities,
with_stack=with_stack,
record_shapes=record_shapes,
)
)
if current_platform.is_out_of_tree():
activity_str = current_platform.get_torch_profiler_activity_str()
if activity_str and activity_str in activities:
inners.append(
_ProfilerTorch(
**kwargs,
activities=activities,
with_stack=with_stack,
record_shapes=record_shapes,
)
)

if ("CPU" in activities) or ("GPU" in activities):
inners.append(
_ProfilerTorch(
Expand Down Expand Up @@ -291,6 +302,12 @@ def start(self):
"CPU": torch.profiler.ProfilerActivity.CPU,
"GPU": torch.profiler.ProfilerActivity.CUDA,
}

if current_platform.is_out_of_tree():
activity_map[current_platform.get_torch_profiler_activity_str()] = (
current_platform.get_torch_profiler_activity()
)
Comment on lines +306 to +309

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If get_torch_profiler_activity_str() returns None, this will set activity_map[None] = None, which can cause issues later. We should check that both the activity string and the activity itself are not None before adding them to the map.

Suggested change
if current_platform.is_out_of_tree():
activity_map[current_platform.get_torch_profiler_activity_str()] = (
current_platform.get_torch_profiler_activity()
)
if current_platform.is_out_of_tree():
activity_str = current_platform.get_torch_profiler_activity_str()
activity = current_platform.get_torch_profiler_activity()
if activity_str and activity:
activity_map[activity_str] = activity


torchprof_activities = [
activity_map[a] for a in self.activities if a in activity_map
]
Expand Down
Loading