Skip to content

[jit] Values of constants can't be accessed #30019

@driazati

Description

@driazati

Constant values are inlined in the graph without names, so they can't be accessed after compilation. This is a problem since a lot of nn.Modules are scripted using constants instead of attributes, so users have no way of accessing these values once they have scripted a module.

class M(nn.Module):
    __constants__ = ['my_const']
    def __init__(self):
        super().__init__()
        self.my_const = 2

    def forward(self, x):
        return x + self.my_const


sm = torch.jit.script(M())

# AttributeError: 'RecursiveScriptModule' object has no attribute 'my_const'
print(sm.my_const)

An easy fix would be to delete all the __constants__ arrays in torch/nn so everything would be made into an attribute.

cc @suo

Metadata

Metadata

Assignees

Labels

oncall: jitAdd this issue/PR to JIT oncall triage queue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions