@@ -827,100 +827,6 @@ def validate(self, fgraph):
827
827
raise InconsistencyError ("Trying to reintroduce a removed node" )
828
828
829
829
830
- class NodeFinder (Bookkeeper ):
831
- def __init__ (self ):
832
- self .fgraph = None
833
- self .d = {}
834
-
835
- def on_attach (self , fgraph ):
836
- if hasattr (fgraph , "get_nodes" ):
837
- raise AlreadyThere ("NodeFinder is already present" )
838
-
839
- if self .fgraph is not None and self .fgraph != fgraph :
840
- raise Exception ("A NodeFinder instance can only serve one FunctionGraph." )
841
-
842
- self .fgraph = fgraph
843
- fgraph .get_nodes = partial (self .query , fgraph )
844
- Bookkeeper .on_attach (self , fgraph )
845
-
846
- def clone (self ):
847
- return type (self )()
848
-
849
- def on_detach (self , fgraph ):
850
- """
851
- Should remove any dynamically added functionality
852
- that it installed into the function_graph
853
- """
854
- if self .fgraph is not fgraph :
855
- raise Exception (
856
- "This NodeFinder instance was not attached to the provided fgraph."
857
- )
858
- self .fgraph = None
859
- del fgraph .get_nodes
860
- Bookkeeper .on_detach (self , fgraph )
861
-
862
- def on_import (self , fgraph , node , reason ):
863
- try :
864
- self .d .setdefault (node .op , []).append (node )
865
- except TypeError : # node.op is unhashable
866
- return
867
- except Exception as e :
868
- print ("OFFENDING node" , type (node ), type (node .op ), file = sys .stderr ) # noqa: T201
869
- try :
870
- print ("OFFENDING node hash" , hash (node .op ), file = sys .stderr ) # noqa: T201
871
- except Exception :
872
- print ("OFFENDING node not hashable" , file = sys .stderr ) # noqa: T201
873
- raise e
874
-
875
- def on_prune (self , fgraph , node , reason ):
876
- try :
877
- nodes = self .d [node .op ]
878
- except TypeError : # node.op is unhashable
879
- return
880
- nodes .remove (node )
881
- if not nodes :
882
- del self .d [node .op ]
883
-
884
- def query (self , fgraph , op ):
885
- try :
886
- all = self .d .get (op , [])
887
- except TypeError :
888
- raise TypeError (
889
- f"{ op } in unhashable and cannot be queried by the optimizer"
890
- )
891
- all = list (all )
892
- return all
893
-
894
-
895
- class PrintListener (Feature ):
896
- def __init__ (self , active = True ):
897
- self .active = active
898
-
899
- def on_attach (self , fgraph ):
900
- if self .active :
901
- print ("-- attaching to: " , fgraph ) # noqa: T201
902
-
903
- def on_detach (self , fgraph ):
904
- """
905
- Should remove any dynamically added functionality
906
- that it installed into the function_graph
907
- """
908
- if self .active :
909
- print ("-- detaching from: " , fgraph ) # noqa: T201
910
-
911
- def on_import (self , fgraph , node , reason ):
912
- if self .active :
913
- print (f"-- importing: { node } , reason: { reason } " ) # noqa: T201
914
-
915
- def on_prune (self , fgraph , node , reason ):
916
- if self .active :
917
- print (f"-- pruning: { node } , reason: { reason } " ) # noqa: T201
918
-
919
- def on_change_input (self , fgraph , node , i , r , new_r , reason = None ):
920
- if self .active :
921
- print (f"-- changing ({ node } .inputs[{ i } ]) from { r } to { new_r } " ) # noqa: T201
922
-
923
-
924
830
class PreserveVariableAttributes (Feature ):
925
831
"""
926
832
This preserve some variables attributes and tag during optimization.
0 commit comments