Libtask.jl icon indicating copy to clipboard operation
Libtask.jl copied to clipboard

Questions About Semantics

Open willtebbutt opened this issue 9 months ago • 7 comments

There are several tests that surprised me, and I would like to know why the semantics chosen are the correct ones.

Firstly, https://github.com/TuringLang/Libtask.jl/blob/6264afd33f9441697d2f5ed4a75bbcc37f15feba/test/tape_copy.jl#L47 . This test stipulates that Dicts ought not to be deepcopyed when tasks are copied. Why is this?

Same thing here. Why are Refs not deepcopyed? https://github.com/TuringLang/Libtask.jl/blob/6264afd33f9441697d2f5ed4a75bbcc37f15feba/test/tape_copy.jl#L116

willtebbutt avatar Feb 27 '25 18:02 willtebbutt

. This test stipulates that Dicts ought not to be deepcopyed when tasks are copied. Why is this?

Same thing here. Why are Refs not deepcopyed?

IIRC, There isn't a reason for that other than we couldn't find a robust way to deepcopy them earlier.

yebai avatar Feb 27 '25 19:02 yebai

cc @KDr2

yebai avatar Feb 27 '25 19:02 yebai

Oh, interesting. I had wondered if there's some semantic reason -- the "ref atomic" test made me wonder if the point is that you want Refs to behave differently from other mutable types. Interested to see what @KDr2 's thoughts are.

willtebbutt avatar Feb 27 '25 19:02 willtebbutt

The current(default) behavior:

  • Ref is deep copied
  • Array is deep copied
  • Dict is NOT deep copied
  • All other types are NOT deep copied

This default behavior is set following the behavior of the old version of Libtask when we have our own Ref and Array type implemented in it.

However, this setting can be customized(except Ref and Array are always deep copied): ttask = TapedTask(f, 0; deepcopy_types=Dict) will make the Dict values deep copied.

KDr2 avatar Feb 28 '25 02:02 KDr2

Ahhh interesting, okay.

@yebai do you know if we rely on not deepcopying anywhere in AdvancedPS.jl / Turing.jl?

willtebbutt avatar Feb 28 '25 08:02 willtebbutt

I’d suggest we have the option to maintain the current behaviour to avoid surprises, but I am unaware of specific reasons for not deep copying everything by default

edit: VarInfo passed to models should not be deepcopied I think.

yebai avatar Feb 28 '25 09:02 yebai

edit: VarInfo passed to models should not be deepcopied I think.

Why is this? Given that it defines the state of each model, I would have thought you would definitely want a completely independent copy on each task. I must be missing something...

willtebbutt avatar Feb 28 '25 13:02 willtebbutt