# \[Tune\] How to get a reference to the trainer's model

**URL:** <https://discuss.ray.io/t/tune-how-to-get-a-reference-to-the-trainers-model/2394>\
**Category:** Ray Tune\
**Created:** [June 2, 2021, 10:15pm UTC](https://discuss.ray.io/t/tune-how-to-get-a-reference-to-the-trainers-model/2394 "2021-06-02T22:15:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![valiantljk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/valiantljk/32/252_2.png) [@valiantljk](https://discuss.ray.io/u/valiantljk)\
**Post date:** [June 2, 2021, 10:15pm UTC](https://discuss.ray.io/t/tune-how-to-get-a-reference-to-the-trainers-model/2394/1 "2021-06-02T22:15:10Z")

</div>

Hi, I have Torchtrainer to setup the model, how can I get a reference to the ddp wrapped model?

```auto
 trainer = TorchTrainer(
            training_operator_cls=MyTrainOperator,
            use_gpu=True,
            config=trainer_config
)

```

It looks like TorchTrainer has a method to return the learned model, e.g.,

```auto
model = trainer.get_models()[0]

```

Is this model a reference to the trainer’s model so I can do some work with it on the driver side?

---

<div class="post-metadata">

**Author:** ![rliaw](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rliaw/32/24_2.png) [@rliaw](https://discuss.ray.io/u/rliaw)\
**Post date:** [June 2, 2021, 11:19pm UTC](https://discuss.ray.io/t/tune-how-to-get-a-reference-to-the-trainers-model/2394/2 "2021-06-02T23:19:53Z")

</div>

> [@valiantljk](#):
>
> Is this model a reference to the trainer’s model so I can do some work with it on the driver side?

Hmm, get\_models will return the right model from the training operator, yeah.

---

<div class="post-metadata">

**Author:** ![valiantljk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/valiantljk/32/252_2.png) [@valiantljk](https://discuss.ray.io/u/valiantljk)\
**Post date:** [June 3, 2021, 5:37pm UTC](https://discuss.ray.io/t/tune-how-to-get-a-reference-to-the-trainers-model/2394/3 "2021-06-03T17:37:01Z")

</div>

Hi @rliaw , I found it maybe wrong. It looks like it’s not the same reference. Thus I can’t access some internal state of the model outside torchtrainer.

For example

```auto
class CurrentModel(model):
    def __init__ (self):
           self.model = model
     def fit(self):
           config = {"model": self.model}
           trainer = TorchTrainer(
                   training_operator_cls= TrainOperator,
                   config = config)
          #now self.model and trainer's model are different reference to the different object, which might because in TorchTrainer, model is copied onto CUDA

class TrainOperator():
     def setup(self, config):
           self.model = self.register(config['model'])

```

In the above example, in `fit`, I can’t access the actual ddp wrapped model’s runtime state, e.g., some variables in the class.

Does it because in TrainOperator, self.mode is referred to a model that has been copied onto CUDA, not the original model that was passed in with config[‘model’]?

---

<div class="post-metadata">

**Author:** ![rliaw](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rliaw/32/24_2.png) [@rliaw](https://discuss.ray.io/u/rliaw)\
**Post date:** [June 4, 2021, 7:32am UTC](https://discuss.ray.io/t/tune-how-to-get-a-reference-to-the-trainers-model/2394/4 "2021-06-04T07:32:03Z")

</div>

Hmm, sorry can you provide a bit more context about what you’re trying to do?

This seems to be an interesting use case that we can better support.

---

<div class="post-metadata">

**Author:** ![valiantljk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/valiantljk/32/252_2.png) [@valiantljk](https://discuss.ray.io/u/valiantljk)\
**Post date:** [June 4, 2021, 6:57pm UTC](https://discuss.ray.io/t/tune-how-to-get-a-reference-to-the-trainers-model/2394/5 "2021-06-04T18:57:18Z")

</div>

When I tries to convert an existing code into distributed version using RaySGD, One way is to convert everything into a customized TrainOperator, another way is to keep some logic, e.g., metrics computing outside the TrainOperator. Initially I followed the later approach, which breaks as I posted. Now I just convert everything in the Trainoperator, seems working. But this brings a lot of coding effort in using RaySGD.

So basically, RaySGD or Ray assumes a clean boundary between driver and remote, which makes sense. But seems a bit inconvenience in converting an existing codebase.
