Skip to content

Commit 1b6455e

Browse files
Fixes for various warnings that appear in examples. (pyg-team#10357)
This PR addresses several warnings that appear in the example scripts, including: - UserWarnings triggered by unsafe tensor operations or missing configurations. - FutureWarnings indicating upcoming behavior changes in dependencies. By resolving these issues, we aim to improve the clarity of the output and ensure better compatibility with future library versions. No changes to core logic or results are introduced. ``` /workspace/examples/gcn.py:84: UserWarning: Converting a tensor with requires_grad=True to a scalar may lead to unexpected behavior. Consider using tensor.detach() first. (Triggered internally at /opt/pytorch/pytorch/torch/csrc/autograd/generated/python_variable_methods.cpp:835.) return float(loss) /workspace/examples/compile/gcn.py:65: UserWarning: Converting a tensor with requires_grad=True to a scalar may lead to unexpected behavior. Consider using tensor.detach() first. (Triggered internally at /opt/pytorch/pytorch/torch/csrc/autograd/generated/python_variable_methods.cpp:835.) usr/local/lib/python3.12/dist-packages/torch/cuda/memory.py:491: FutureWarning: torch.cuda.reset_max_memory_allocated now calls torch.cuda.reset_peak_memory_stats, which resets /all/ peak memory stats. warnings.warn( ``` Co-authored-by: Rishi Puri <riship@nvidia.com>
1 parent 8fdde68 commit 1b6455e

File tree

5 files changed

+8
-8
lines changed

5 files changed

+8
-8
lines changed

examples/compile/gcn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def train():
6262
loss = F.cross_entropy(out[data.train_mask], data.y[data.train_mask])
6363
loss.backward()
6464
optimizer.step()
65-
return float(loss)
65+
return float(loss.detach())
6666

6767

6868
@torch.no_grad()

examples/compile/gin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def train():
7373
loss = F.cross_entropy(out, data.y)
7474
loss.backward()
7575
optimizer.step()
76-
total_loss += float(loss) * data.num_graphs
76+
total_loss += float(loss.detach()) * data.num_graphs
7777
return total_loss / len(train_loader.dataset)
7878

7979

examples/llm/g_retriever.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def adjust_learning_rate(param_group, LR, epoch):
328328
step / len(train_loader) + epoch)
329329

330330
optimizer.step()
331-
epoch_loss = epoch_loss + float(loss)
331+
epoch_loss = epoch_loss + float(loss.detach())
332332

333333
if (step + 1) % 2 == 0:
334334
lr = optimizer.param_groups[0]['lr']
@@ -353,7 +353,7 @@ def adjust_learning_rate(param_group, LR, epoch):
353353

354354
# Clean up memory
355355
torch.cuda.empty_cache()
356-
torch.cuda.reset_max_memory_allocated()
356+
torch.cuda.reset_peak_memory_stats()
357357

358358
# Load best checkpoint if necessary
359359
if checkpointing and best_epoch != num_epochs - 1:

examples/llm/g_retriever_utils/minimal_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def adjust_learning_rate(param_group, LR, epoch):
212212
best_epoch = epoch
213213
save_params_dict(model, f'{model_save_name}_best_val_loss_ckpt.pt')
214214
torch.cuda.empty_cache()
215-
torch.cuda.reset_max_memory_allocated()
215+
torch.cuda.reset_peak_memory_stats()
216216

217217
if checkpointing and best_epoch != num_epochs - 1:
218218
print("Loading best checkpoint...")
@@ -343,7 +343,7 @@ def benchmark_models(models: List[Type[nn.Module]], model_names: List[str],
343343
model=pure_llm,
344344
dataset=dataset)
345345
torch.cuda.empty_cache()
346-
torch.cuda.reset_max_memory_allocated()
346+
torch.cuda.reset_peak_memory_stats()
347347
gc.collect()
348348
e2e_time = round(time.time() - since, 2)
349349
model_log["tuned_llm"]["prep_time"] = prep_time
@@ -386,7 +386,7 @@ def benchmark_models(models: List[Type[nn.Module]], model_names: List[str],
386386
tiny_llama=tiny_llama, dataset=dataset,
387387
model_save_name=root_dir + '/' + name, model=model)
388388
torch.cuda.empty_cache()
389-
torch.cuda.reset_max_memory_allocated()
389+
torch.cuda.reset_peak_memory_stats()
390390
gc.collect()
391391
e2e_time = round(time.time() - since, 2)
392392
model_log[name]["prep_time"] = prep_time

examples/llm/git_mol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def train(
108108
f'gitmol_pretrain_epoch{best_epoch}_val_loss{best_val_loss:4f}_ckpt.pt' # noqa: E501
109109
)
110110
torch.cuda.empty_cache()
111-
torch.cuda.reset_max_memory_allocated()
111+
torch.cuda.reset_peak_memory_stats()
112112

113113
# Test
114114
test_loss = eval(model, test_loader)

0 commit comments

Comments
 (0)