Skip to content
Merged
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
18 changes: 8 additions & 10 deletions autoparallel/compute_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,23 @@ def estimate_strategy_runtime_cost(node, strategy):
args = tree_map_only(torch.fx.Node, lambda x: x.meta["val"], node.args)
kwargs = tree_map_only(torch.fx.Node, lambda x: x.meta["val"], node.kwargs)

fake_mode = torch._guards.detect_fake_mode(args)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this bc we are already under a fake mode now, but we weren't in the initial autop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's right, now the whole AutoParallel is running under fake mode, so we can remove it

Copy link
Member

Choose a reason for hiding this comment

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

this is removed because we're already running this in a fake mode?


if len(kwargs) > 0:
for k, v in kwargs.items():
assert not isinstance(v, torch.Tensor), f"{node} {v}"
args_sizes_strides = tuple(
_get_sharded_shape_stride(spec) for spec in strategy.input_specs
)

flat_args, treespec = tree_flatten(args)
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we just call tree_map_only(Tensor, torch.empty) here instead of doing the for loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to get the size from the args_sizes_strides (which comes from the spec), so I think we might need this indirection.

But if there is a cleaner way of doing this I'm happy to change the code!

new_flat_args = []
counter = 0
args = list(args)
for i, arg in enumerate(args):
if isinstance(arg, torch.Tensor):
with fake_mode:
sizes, strides = args_sizes_strides[counter]
args[i] = torch.empty_strided(
sizes, strides, device=arg.device, dtype=arg.dtype
)
for x in flat_args:
if isinstance(x, torch.Tensor):
sizes, strides = args_sizes_strides[counter]
x = torch.empty_strided(sizes, strides, device=x.device, dtype=x.dtype)
counter += 1
new_flat_args.append(x)
args = treespec.unflatten(new_flat_args)

# TODO: maybe cache the flop_counter to avoid recreating it
# all the time
Expand Down