Skip to content

Module.to doesn't transfer parameters in ParameterList or ParameterDict #1179

Closed

Description

@shaltielshmid

When moving a ParameterList or ParameterDict Module, the registered values get moved but the field doesn't get the updated values.

For example, in the case below, the output would be Float32. If you would inspect the _internal_params list you would see that the parameter there is actually Float16.

This also means that the garbage collector will never dispose of the old parameter since they are both being referenced.

Example code:

private class TestModule1 : Module {
    public TestModule1() : base("TestModule1") {
        list.append(Parameter(torch.rand(10)));
    }
    public ParameterList list = new ParameterList();
}
private static void Test() {
    var m = new TestModule1(torch.randn(2, 2), true);
    m.to(ScalarType.Float16);
    
    Console.WriteLine(m.list[0].dtype);
}

Activity

  1. NiklasGustafsson commented on Dec 8, 2023

    @NiklasGustafsson

    I believe the solution to this is to override _to() in ParameterList, ParameterDict, ModuleList, and ModuleDict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions