@@ -1061,6 +1061,88 @@ def test_build_graph_custom_name(self):
10611061 )
10621062 self .assertEqual (graph .name , "loop_body" )
10631063
1064+ def test_build_graph_with_parent (self ):
1065+ """build_graph with parent sets root on the sub-builder."""
1066+ parent_graph = ir .Graph (
1067+ name = "main" ,
1068+ inputs = [],
1069+ outputs = [],
1070+ nodes = [],
1071+ opset_imports = {"" : 23 },
1072+ )
1073+ parent_builder = builder .GraphBuilder (parent_graph )
1074+
1075+ def body (op , x ):
1076+ self .assertIs (op .builder .parent , parent_builder )
1077+ self .assertIs (op .builder .root , parent_builder )
1078+ return op .Identity (x )
1079+
1080+ builder .build_graph (
1081+ body ,
1082+ inputs = [FLOAT [3 ]],
1083+ outputs = [FLOAT [3 ]],
1084+ parent = parent_builder ,
1085+ )
1086+
1087+ def test_subgraph_sets_parent_and_root (self ):
1088+ """GraphBuilder.subgraph() sets parent=self on the sub-builder."""
1089+ parent_graph = ir .Graph (
1090+ name = "main" ,
1091+ inputs = [],
1092+ outputs = [],
1093+ nodes = [],
1094+ opset_imports = {"" : 23 },
1095+ )
1096+ parent_builder = builder .GraphBuilder (parent_graph )
1097+
1098+ def body (op , x ):
1099+ self .assertIs (op .builder .parent , parent_builder )
1100+ self .assertIs (op .builder .root , parent_builder )
1101+ return op .Identity (x )
1102+
1103+ parent_builder .subgraph (body , inputs = [FLOAT [3 ]], outputs = [FLOAT [3 ]])
1104+
1105+ def test_build_graph_inherits_parent_scope_stack (self ):
1106+ """build_graph copies the parent's scope stack so nodes in the subgraph carry scoped names."""
1107+ parent_graph = ir .Graph (
1108+ name = "main" ,
1109+ inputs = [],
1110+ outputs = [],
1111+ nodes = [],
1112+ opset_imports = {"" : 23 },
1113+ )
1114+ parent_builder = builder .GraphBuilder (parent_graph )
1115+ parent_builder .push_module ("encoder" , "Encoder" )
1116+ parent_builder .push_module ("layers.0" , "TransformerBlock" )
1117+
1118+ subgraph = builder .build_graph (
1119+ lambda op , x : op .Relu (x ),
1120+ inputs = {"x" : FLOAT [3 , 4 ]},
1121+ outputs = {"y" : FLOAT [3 , 4 ]},
1122+ parent = parent_builder ,
1123+ )
1124+
1125+ # The single node created inside the subgraph should carry the
1126+ # parent's scope prefix in its name and metadata.
1127+ node = subgraph .node (0 )
1128+ self .assertIn ("encoder" , node .name )
1129+ self .assertIn ("layers.0" , node .name )
1130+ self .assertIn ("encoder" , node .metadata_props ["namespace" ])
1131+ self .assertIn ("TransformerBlock" , node .metadata_props ["namespace" ])
1132+
1133+ def test_root_graph_builder_is_its_own_root (self ):
1134+ """A top-level GraphBuilder has root == self."""
1135+ graph = ir .Graph (
1136+ name = "main" ,
1137+ inputs = [],
1138+ outputs = [],
1139+ nodes = [],
1140+ opset_imports = {"" : 23 },
1141+ )
1142+ gb = builder .GraphBuilder (graph )
1143+ self .assertIs (gb .root , gb )
1144+ self .assertIsNone (gb .parent )
1145+
10641146
10651147class PartitionInputsAttributesTest (unittest .TestCase ):
10661148 """Tests for GraphBuilder._partition_inputs_attributes."""
0 commit comments