pytorch / pytorch/pytorch

[Doc] SyntaxError in torch.profiler example due to fused statements on a single line

Open Beginner friendly
#190,204 1 comment 0 reactions 0 assignees View on GitHub
bot-triaged module: docs triaged
Dominant language
Python
Stars
103k
Forks
29.5k
PR merge metrics
PR metrics pending

Description

## Description

The initial code block example in the `torch.profiler` documentation overview contains a formatting error where the execution block (`code_to_profile()`) and the evaluation block (`print(...)`) are concatenated directly on the same line without a newline or semicolon delimiter.
Attempting to run this exact documentation snippet results in an immediate `SyntaxError`.

## Reproduction Script

```python
import torch

def code_to_profile():
pass

with torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU
]) as p:
code_to_profile()print(p.key_averages().table(sort_by="self_cuda_time_total", row_limit=-1))

```

## Actual Output

```
File "", line 10
code_to_profile()print(p.key_averages().table(sort_by="self_cuda_time_total", row_limit=-1))
^
SyntaxError: invalid syntax

```

## Expected Behavior

Distinct Python statements must be separated by standard newlines to satisfy the language's grammar constraints. The example should execute cleanly when copied.

## Impact

The code example fails to parse, causing script execution failure exactly at the conclusion of the tracing block. This creates unnecessary friction for developers trying to learn the profiler API.

## Environment

```
PyTorch: 2.13.0
Python: 3.12.13

```

## Suggested Fix

* Move the `print` command to its own distinct line outside the indentation scope of the context manager block in the documentation source.

```python
with torch.profiler.profile(...) as p:
code_to_profile()

print(p.key_averages().table(sort_by="self_cuda_time_total", row_limit=-1))

```

cc @svekars @sekyondaMeta @AlannaBurke

Contributor guide

Open the contributing guide

Research direction

Locate the initial code block in the torch.profiler documentation overview and compare it with the reproduction shown in the issue. Verify the corrected snippet parses and runs cleanly when copied, with the execution and evaluation statements separated as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.