Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PT] disable grad for extracted module #3266

Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions nncf/torch/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ def extract_bn(node: NNCFNode, model: NNCFNetwork) -> Optional[Union[nn.BatchNor
for name, _ in chain(extracted_bn.named_parameters(), extracted_bn.named_buffers()):
setattr(extracted_bn, name, deepcopy(getattr(bn_module, name)))
extracted_bn.eval()
extracted_bn.weight.requires_grad = False
extracted_bn.bias.requires_grad = False
return extracted_bn


Expand Down
17 changes: 9 additions & 8 deletions tests/torch/test_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def test_extract_model(model_cls, input_node_name, output_node_name):

model = wrap_model(model_cls().eval(), example_input=example_input, trace_parameters=True)
extracted_module = extract_model(model, [input_node_name], [output_node_name])
with torch.no_grad():
ret1 = model(example_input)
ret2 = extracted_module(example_input)
assert torch.any(torch.isclose(ret1, ret2))
ret1 = model(example_input)
ret2 = extracted_module(example_input)
assert not ret2.grad_fn
assert torch.any(torch.isclose(ret1, ret2))


@pytest.mark.parametrize(
Expand Down Expand Up @@ -122,10 +122,11 @@ def test_extract_model_for_node_with_fq(model_cls, input_node_name, output_node_
q_model = transformer.transform(layout)

extracted_module = extract_model(model, [input_node_name], [output_node_name])
with torch.no_grad():
ret1 = q_model(example_input)
ret2 = extracted_module(example_input)
assert torch.all(torch.isclose(ret1, ret2))

ret1 = q_model(example_input)
ret2 = extracted_module(example_input)
assert torch.all(torch.isclose(ret1, ret2))
assert not ret2.grad_fn

extracted_fn = extracted_module
if isinstance(extracted_fn, nn.Sequential):
Expand Down