MCPcopy Create free account
hub / github.com/pytorch/pytorch / test_replace_uses

Method test_replace_uses

test/test_fx.py:2135–2159  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2133
2134 @skipIfNoTorchVision
2135 def test_replace_uses(self):
2136 rn18 = torchvision_models.resnet18()
2137
2138 class LowerReluTracer(torch.fx.Tracer):
2139 def is_leaf_module(self, m : torch.nn.Module, qualname : str):
2140 if isinstance(m, torch.nn.ReLU):
2141 return False
2142 return super().is_leaf_module(m, qualname)
2143
2144 rn18_traced = GraphModule(rn18, LowerReluTracer().trace(rn18))
2145
2146 to_erase = []
2147 for node in rn18_traced.graph.nodes:
2148 if node.op == 'call_function' and node.target in [torch.relu, torch.nn.functional.relu]:
2149 kwargs = node.kwargs.copy()
2150 # Neg doesn't have in-place
2151 kwargs.pop('inplace')
2152 with rn18_traced.graph.inserting_before(node):
2153 new_node = rn18_traced.graph.call_function(
2154 the_function=torch.neg, args=node.args, kwargs=node.kwargs)
2155 node.replace_all_uses_with(replace_with=new_node)
2156 to_erase.append(node)
2157
2158 for node in to_erase:
2159 rn18_traced.graph.erase_node(node)
2160
2161
2162 def test_replace_input(self):

Callers

nothing calls this directly

Calls 10

GraphModuleClass · 0.90
LowerReluTracerClass · 0.85
replace_all_uses_withMethod · 0.80
traceMethod · 0.45
copyMethod · 0.45
popMethod · 0.45
inserting_beforeMethod · 0.45
call_functionMethod · 0.45
appendMethod · 0.45
erase_nodeMethod · 0.45

Tested by

no test coverage detected