1111
1212
1313class MockHook (StatelessHook ):
14- produces = {'foo' }
14+ _cls_produces = {'foo' }
15+
16+ def __init__ (self , id : str = None ):
17+ super ().__init__ ()
18+ self ._id = id
19+ self .__post_init__ ()
1520
1621 def __call__ (self , dg : DGraph , batch : DGBatch ) -> DGBatch :
1722 batch .edge_time *= 2
1823 return batch
1924
2025
2126class MockHookRequires (StatelessHook ):
22- requires = {'foo' }
27+ _cls_requires = {'foo' }
28+
29+ def __init__ (self , id : str = None ):
30+ super ().__init__ ()
31+ self ._id = id
32+ self .__post_init__ ()
2333
2434 def __call__ (self , dg : DGraph , batch : DGBatch ) -> DGBatch :
2535 return batch
2636
2737
28- class DeduplicationMockHook (StatelessHook ):
38+ class MockHookRequiresWoof (StatelessHook ):
39+ _cls_requires = {'foo_woof' }
40+
41+ def __init__ (self , id : str = None ):
42+ super ().__init__ ()
43+ self ._id = id
44+ self .__post_init__ ()
45+
2946 def __call__ (self , dg : DGraph , batch : DGBatch ) -> DGBatch :
3047 return batch
3148
3249
3350class MockHookWithState (StatefulHook ):
34- has_state : bool = True
35-
36- def __init__ (self ) -> None :
51+ def __init__ (self , id : str = None ) -> None :
52+ super ().__init__ ()
53+ self ._id = id
54+ self .has_state = True
3755 self .x = 0
3856
3957 def __call__ (self , dg : DGraph , batch : DGBatch ) -> DGBatch :
@@ -165,8 +183,8 @@ def test_resolve_hooks_by_key():
165183def test_resolve_hooks_no_solution_no_dag ():
166184 h1 = MockHook ()
167185 h2 = MockHook ()
168- h1 .requires , h1 .produces = {'x' }, {'y' }
169- h2 .requires , h2 .produces = {'y' }, {'x' }
186+ h1 ._requires , h1 ._produces = {'x' }, {'y' }
187+ h2 ._requires , h2 ._produces = {'y' }, {'x' }
170188
171189 # Cycle-like missing dependency
172190 hm = HookManager (keys = ['train' ])
@@ -210,8 +228,8 @@ def test_topo_sort_cached(dg, monkeypatch):
210228 hm = HookManager (keys = ['train' ])
211229
212230 h1 , h2 = MockHook (), MockHook ()
213- h1 .requires , h1 .produces = set (), {'x' }
214- h2 .requires , h2 .produces = {'x' }, {'y' }
231+ h1 ._requires , h1 ._produces = set (), {'x' }
232+ h2 ._requires , h2 ._produces = {'x' }, {'y' }
215233
216234 hm .register ('train' , h1 )
217235 hm .register ('train' , h2 )
@@ -237,7 +255,7 @@ def test_topo_sort_cached_invalidated(dg, monkeypatch):
237255 hm = HookManager (keys = ['train' ])
238256
239257 h1 = MockHook ()
240- h1 .requires , h1 .produces = set (), {'x' }
258+ h1 ._requires , h1 ._produces = set (), {'x' }
241259
242260 hm .register ('train' , h1 )
243261 call_count = {'n' : 0 }
@@ -266,8 +284,8 @@ def fake_topo_sort(hooks_list):
266284def test_topo_sort_no_solution_no_dag (dg ):
267285 h1 = MockHook ()
268286 h2 = MockHook ()
269- h1 .requires , h1 .produces = {'x' }, {'y' }
270- h2 .requires , h2 .produces = {'y' }, {'x' }
287+ h1 ._requires , h1 ._produces = {'x' }, {'y' }
288+ h2 ._requires , h2 ._produces = {'y' }, {'x' }
271289
272290 # Cycle-like missing dependency
273291 hm = HookManager (keys = ['train' ])
@@ -362,8 +380,8 @@ def test_activate_ctx():
362380
363381def test_topo_sort_neg_before_nbr ():
364382 mock_neg_hook , mock_nbr_hook = MockHook (), MockHook ()
365- mock_neg_hook .requires , mock_neg_hook .produces = set (), {'neg' }
366- mock_nbr_hook .requires , mock_nbr_hook .produces = set (), {'nbr_nids' }
383+ mock_neg_hook ._requires , mock_neg_hook ._produces = set (), {'neg' }
384+ mock_nbr_hook ._requires , mock_nbr_hook ._produces = set (), {'nbr_nids' }
367385
368386 # Register neg first in foo, nbr first in bar
369387 hm = HookManager (keys = ['foo' , 'bar' ])
@@ -381,25 +399,14 @@ def test_topo_sort_neg_before_nbr():
381399 assert bar_hooks .index (mock_neg_hook ) < bar_hooks .index (mock_nbr_hook )
382400
383401
384- def test_force_last_dedup_hook ():
385- # @TODO: Dedup hook is temporarily forced to run at the end.
386- # This test potentially needs to be updated once instance-level require is introduced to DGHook.
387- h1 = MockHook ()
388- h2 = MockHookRequires ()
389- h3 = DeduplicationMockHook ()
402+ def test_resolve_hooks_with_id_by_key ():
403+ h1 = MockHook (id = 'woof' ) # MockHook produces with _woof suffix
404+ h2 = MockHookRequiresWoof ()
390405
391- hm = HookManager (keys = ['train' , 'val' ])
392- hm .register ('train' , h3 )
406+ hm = HookManager (keys = ['train' ])
393407 hm .register ('train' , h2 )
394408 hm .register ('train' , h1 )
395- hm .register ('val' , h3 )
396- hm .register ('val' , h2 )
397- hm .register ('val' , h1 )
398409
399- hm .resolve_hooks ()
400- assert len (hm ._key_to_hooks ['train' ]) == 3
401- assert len (hm ._key_to_hooks ['val' ]) == 3
410+ hm .resolve_hooks ('train' )
411+ assert len (hm ._key_to_hooks ['train' ]) == 2
402412 assert hm ._key_to_hooks ['train' ].index (h1 ) < hm ._key_to_hooks ['train' ].index (h2 )
403- assert hm ._key_to_hooks ['val' ].index (h1 ) < hm ._key_to_hooks ['val' ].index (h2 )
404- assert hm ._key_to_hooks ['train' ].index (h2 ) < hm ._key_to_hooks ['train' ].index (h3 )
405- assert hm ._key_to_hooks ['val' ].index (h2 ) < hm ._key_to_hooks ['val' ].index (h3 )
0 commit comments