Skip to content
Open
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
5 changes: 4 additions & 1 deletion graph_net/torch/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

torch._dynamo.config.capture_scalar_outputs = True
torch._dynamo.config.capture_dynamic_output_shape_ops = True
torch._dynamo.config.capture_sparse_compute = True
try:
torch._dynamo.config.capture_sparse_compute = True
except AttributeError:
pass
torch._dynamo.config.raise_on_ctx_manager_usage = False
torch._dynamo.config.allow_rnn = True

Expand Down
42 changes: 41 additions & 1 deletion graph_net/torch/sample_pass/backward_graph_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ def __call__(self):
module, forward_inputs = get_torch_module_and_inputs(
self.model_path, use_dummy_inputs=False, device=self.device
)
module.train()
module.eval()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

eval模式下不会生成反向图吧?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

model.eval() 不会禁用梯度计算,只有 torch.no_grad() / torch.inference_mode() 才会。eval 仅改变特定层的前向行为(dropout → identity,BatchNorm → 用 running stats 而非 batch stats),反向传播完全正常。而且使用 eval 模式反而更好

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

不行,反向图生成时,这些算子就应该用train模式

Comment thread
Dayuxiaoshui marked this conversation as resolved.

if self._is_pure_shape_graph(module):
Comment thread
Dayuxiaoshui marked this conversation as resolved.
Outdated
print(f"[Skip] Pure shape graph: {self.model_path}")
return

eval_forward_dir = os.path.join(
self.output_dir, "eval_forward", self.rel_model_path
)
if not os.path.exists(eval_forward_dir):
shutil.copytree(self.model_path, eval_forward_dir)

forward_inputs = [
inp.detach().clone() if isinstance(inp, torch.Tensor) else inp
for inp in forward_inputs
]
forward_inputs = self.set_requires_grad_for_forward_inputs(
self.model_path, module, forward_inputs
)
Expand Down Expand Up @@ -117,6 +125,38 @@ def _remove_none_from_output(self, gm):
gm.recompile()
return gm

def _is_pure_shape_graph(self, module):
"""Check if the graph only contains shape manipulation ops."""
shape_only_ops = {
torch.ops.aten.view,
torch.ops.aten.reshape,
torch.ops.aten.squeeze,
torch.ops.aten.unsqueeze,
torch.ops.aten.permute,
torch.ops.aten.transpose,
torch.ops.aten.expand,
torch.ops.aten.flatten,
torch.ops.aten.t,
"view",
"reshape",
"squeeze",
"unsqueeze",
"permute",
"transpose",
"expand",
"flatten",
"t",
}
for node in module.graph.nodes:
if node.op in {"placeholder", "output", "get_attr"}:
continue
if node.op == "call_function" and node.target in shape_only_ops:
continue
if node.op == "call_method" and node.target in shape_only_ops:
continue
return False
return True

def _requires_grad(self, name, tensor):
if not tensor.is_floating_point():
return False
Expand Down
Loading
Loading