What does model.to(device) return and where is the documentation for it?

240 views Asked by At

I found documentation for models but not the specefic api documentation for model.to().

ChatGPT had told me conflicting info. that it returns a copy of the model and also that it returns a reference to the model.

I'm trying to understand this code:

modelBigram0 = BigramLanguageModel(vocab_size)
modelBigram1 = modelBigram0.to(device)

If I had the documentation for model.to() this would help.

1

There are 1 answers

3
Odney On

The method modifies the module in-place. It is used to move the model between devices (cpu/gpu). The return value is a self-reference to the calling object.

The documentation is here as @Carcigenicate already pointed out in the comment.

Example usage according to documentation:

modelBigram0 = BigramLanguageModel(vocab_size)
modelBigram0.to(device)

There is no need to use two separate variables. In your example, both of them contain reference to the same object which is stored in the system memory or gpu memory only once.