ray-project / ray

Ray is an AI compute engine. Ray consists of a core distributed runtime and a set of AI Libraries for accelerating ML workloads.
https://ray.io
Apache License 2.0
33.75k stars 5.74k forks source link

[Core|Tune]: Static/Class variables with `tune.Trainable` #25947

Closed Rohan138 closed 2 years ago

Rohan138 commented 2 years ago

Description

Is it possible to use static/class variables or otherwise save attributes from an object without using Trainable.save? If not, is there some way to explicitly warn users against this using metaclasses?

Here's an example script that might fall under ray gotchas:

from ray import tune
from ray.rllib.utils.annotations import override
from typing import Dict

class Inner(tune.Trainable):
    b = 0
    @override(tune.Trainable)
    def setup(self, config: Dict):
        self.a = 0

    @override(tune.Trainable)
    def step(self):
        self.a += 1
        Inner.b += 1
        return {"inner": self.a, "inner_cls": Inner.b}

class Outer(tune.Trainable):
    @override(tune.Trainable)
    def setup(self, config: Dict):
        self.ctr = 0

    @override(tune.Trainable)
    def step(self):
        tune.run(Inner, stop={"training_iteration": 5})
        self.ctr += 1
        return {"outer": self.ctr}

def run(cls, stop):
    obj = cls()
    obj.setup(config={})
    for _ in range(stop):
        print(obj.step())

class PlainInner:
    b = 0
    def setup(self, config: Dict):
        self.a = 0

    def step(self):
        self.a += 1
        PlainInner.b += 1
        return {"inner": self.a, "inner_cls": PlainInner.b}

class PlainOuter:
    def setup(self, config: Dict):
        self.ctr = 0

    def step(self):
        run(PlainInner, 5)
        self.ctr += 1
        return {"outer": self.ctr}

if __name__=="__main__":
    tune.run(Outer, stop={"training_iteration": 5})
    run(PlainOuter, 5)

Outer.b is 5, but PlainOuter.b is 25.

Use case

No response

matthewdeng commented 2 years ago

Not sure if I understand the original question, but does this explain the behavior you're seeing?