For a self-defined nn.Module, we can easily convert between cpu and cuda by means of .to(device) if our variable is defined as :
class MyModule(nn.Module):
  def __init__():
       var1 = nn.Parameters()
       var2 = nn.register_buffer()

However, sometimes, it’s can NOT work if in run-time code, such as :
class MyModule(nn.Module):
 def __init__(n):
      for i in range(n):
          var1 = nn.Parameters()
          var2 = nn.register_buffer()

OR：
class MyModule(nn.Module):
  def get(self):
       var1 = nn.Parameters()
       var2 = nn.register_buffer()
       return var1, var2

In the above 2 cases, the var1 and var2 can NOT be converted automatically.
How to elegantly solve the problems?