Skip to content

Commit

Permalink
Re-land: Fix model initialization. (pytorch#6182)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysiraichi authored Dec 15, 2023
1 parent e123f92 commit 38e3644
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions benchmarks/torchbench_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import torch
import torch.nn as nn
import torch.utils._pytree as pytree
from torch._dynamo.testing import collect_results, reduce_to_scalar_loss
from torch._dynamo.utils import clone_inputs
import types
Expand Down Expand Up @@ -143,7 +144,6 @@ def is_compatible(self, dummy_benchmark_model, benchmark_experiment):
break
if matched:
return False

return True


Expand Down Expand Up @@ -193,12 +193,23 @@ def load_benchmark(self):
# workaround "RuntimeError: not allowed to set torch.backends.cudnn flags"
# torch.backends.__allow_nonbracketed_mutation_flag = True

if self.benchmark_experiment.accelerator == "cpu":
device = "cpu"
elif self.benchmark_experiment.accelerator == "cuda" and not self.benchmark_experiment.xla:
device = "cuda"
else:
device = str(self.benchmark_experiment.get_device())
benchmark = benchmark_cls(
test=self.benchmark_experiment.test,
device=self.benchmark_experiment.accelerator,
batch_size=self.benchmark_experiment.batch_size,
)

self.module, self.example_inputs = benchmark.get_module()

# Move the initialized model to XLA device.
if self.benchmark_experiment.xla:
device = self.benchmark_experiment.get_device()
self.module = self.module.to(device)
self.example_inputs = pytree.tree_map_only(torch.Tensor,
lambda t: t.to(device),
self.example_inputs)

self.benchmark_experiment.batch_size = benchmark.batch_size

return benchmark_cls(
test=self.benchmark_experiment.test,
Expand Down

0 comments on commit 38e3644

Please sign in to comment.