@@ -1653,7 +1653,7 @@ def forward(self, a, b):
16531653 enable_non_cpu_memory_planning = True ,
16541654 )
16551655
1656- for pipeline , program , gm in self ._get_executorch_program (
1656+ for pipeline , _program , gm in self ._get_executorch_program (
16571657 model , inputs , DeviceAwarePartitioner ("cuda:0" ), et_config
16581658 ):
16591659 with self .subTest (pipeline = pipeline ):
@@ -1683,16 +1683,6 @@ def forward(self, a, b):
16831683 expected_index = 0 ,
16841684 )
16851685
1686- # Verify buffer device mapping: CUDA placeholders should
1687- # have their memory planned on a CUDA buffer.
1688- self ._assert_nodes_buffer_device (
1689- cuda_ph ,
1690- program ,
1691- DeviceType .CUDA ,
1692- pipeline ,
1693- "Placeholder" ,
1694- )
1695-
16961686 def test_skip_d2h_for_method_outputs (self ):
16971687 """When skip_d2h_for_method_outputs=True, delegate outputs that feed
16981688 directly to the graph output should NOT get _d2h_copy nodes."""
@@ -1808,15 +1798,6 @@ def forward(self, a, b):
18081798 "Delegate getitem" ,
18091799 )
18101800
1811- # Verify buffer device mapping: both input and output
1812- # buffers should be on CUDA.
1813- self ._assert_nodes_buffer_device (
1814- cuda_ph ,
1815- program ,
1816- DeviceType .CUDA ,
1817- pipeline ,
1818- "Placeholder" ,
1819- )
18201801 self ._assert_nodes_buffer_device (
18211802 getitems ,
18221803 program ,
@@ -1876,16 +1857,9 @@ def forward(self, a, b):
18761857 f"device spec, got { len (cuda_ph )} " ,
18771858 )
18781859
1879- # Verify buffer device mapping: the CUDA placeholder's
1880- # buffer should be on CUDA, the CPU placeholder's buffer
1881- # should be on CPU.
1882- self ._assert_nodes_buffer_device (
1883- cuda_ph ,
1884- program ,
1885- DeviceType .CUDA ,
1886- pipeline ,
1887- "CUDA placeholder" ,
1888- )
1860+ # The method reads the caller's memory for b, while a keeps
1861+ # its planned buffer on the CPU.
1862+ self .assertIsNone (cuda_ph [0 ].meta ["spec" ].mem_id )
18891863 self ._assert_nodes_buffer_device (
18901864 cpu_ph ,
18911865 program ,
@@ -2386,7 +2360,7 @@ def forward(self, a, b):
23862360 ),
23872361 enable_non_cpu_memory_planning = True ,
23882362 )
2389- for pipeline , program , gm in self ._get_executorch_program (
2363+ for pipeline , _program , gm in self ._get_executorch_program (
23902364 model , inputs , DeviceAwarePartitioner ("cuda:0" ), et_config
23912365 ):
23922366 with self .subTest (pipeline = pipeline ):
@@ -2396,9 +2370,6 @@ def forward(self, a, b):
23962370 self .assertEqual (len (nodes .h2d_nodes ), 1 )
23972371 cuda_ph , _ = self ._collect_placeholders_by_device (gm )
23982372 self .assertEqual (sorted (n .name for n in cuda_ph ), ["a" , "b" ])
2399- self ._assert_nodes_buffer_device (
2400- cuda_ph , program , DeviceType .CUDA , pipeline , "Placeholder"
2401- )
24022373
24032374 def test_skip_h2d_copies_input_for_readers_off_device (self ):
24042375 """With skip_h2d_for_method_inputs=True, a method input that a device
@@ -2634,74 +2605,35 @@ def test_tensorspec_repr_includes_device(self):
26342605 self .assertIn ("device=" , repr_str )
26352606 self .assertIn ("CPU" , repr_str )
26362607
2637- def test_skipping_copies_requires_unallocated_graph_io (self ):
2638- """Skipping a boundary copy is only complete when the runtime also stops reserving its own
2639- buffer for that tensor.
2640-
2641- Memory planning allocates graph inputs and outputs by default. A planned input carries
2642- allocation info, and the runtime fills a planned input by copying the caller's memory into the
2643- buffer it reserved, so the copy the export asked to skip comes back at run time. For device
2644- memory that copy is also a host memcpy into a device pointer, which crashes.
2645-
2646- This pins the pairing the configuration needs, because the two settings live in different
2647- places and nothing else connects them.
2648- """
2608+ def test_inputs_left_on_device_are_not_planned (self ):
2609+ """The runtime fills a planned input by copying the caller's tensor into the buffer it
2610+ reserved, which it cannot do for device memory. So an input that skip_h2d_for_method_inputs
2611+ leaves on the device gets no buffer, whatever alloc_graph_input says, and the method reads
2612+ the caller's memory. The output still follows alloc_graph_output."""
26492613
26502614 class Model (torch .nn .Module ):
26512615 def forward (self , a , b ):
26522616 return torch .add (a , b )
26532617
2654- model = Model ()
2655- inputs = (torch .randn (2 , 2 ), torch .randn (2 , 2 ))
2656- skip_copies = PropagateDeviceConfig (
2657- skip_h2d_for_method_inputs = True ,
2658- skip_d2h_for_method_outputs = True ,
2659- )
2660-
2661- def planned_io (config : ExecutorchBackendConfig ):
2662- """Whether the program reserves its own buffer for each graph input and output."""
2663- lowered = to_edge_transform_and_lower (
2664- torch .export .export (model , inputs ),
2665- partitioner = [DeviceAwarePartitioner ("cuda:0" )],
2666- ).to_executorch (config )
2667- plan = lowered .executorch_program .execution_plan [0 ]
2668- planned = lambda indices : [ # noqa: E731
2669- getattr (plan .values [index ].val , "allocation_info" , None ) is not None
2670- for index in indices
2671- ]
2672- return planned (plan .inputs ), planned (plan .outputs )
2673-
2674- # Default planning reserves buffers, so the runtime copies into them despite the skip flags.
2675- planned_inputs , planned_outputs = planned_io (
2676- ExecutorchBackendConfig (
2677- propagate_device_config = skip_copies ,
2678- enable_non_cpu_memory_planning = True ,
2679- )
2680- )
2681- self .assertTrue (
2682- all (planned_inputs ),
2683- "graph inputs are expected to be planned by default, which is why the pairing below "
2684- f"is needed, but got { planned_inputs } " ,
2685- )
2686-
2687- # Asking planning to leave them alone is what actually removes the copy.
2688- planned_inputs , planned_outputs = planned_io (
2618+ lowered = to_edge_transform_and_lower (
2619+ torch .export .export (Model (), (torch .randn (2 , 2 ), torch .randn (2 , 2 ))),
2620+ partitioner = [DeviceAwarePartitioner ("cuda:0" )],
2621+ ).to_executorch (
26892622 ExecutorchBackendConfig (
2690- propagate_device_config = skip_copies ,
2691- enable_non_cpu_memory_planning = True ,
2692- memory_planning_pass = MemoryPlanningPass (
2693- alloc_graph_input = False , alloc_graph_output = False
2623+ propagate_device_config = PropagateDeviceConfig (
2624+ skip_h2d_for_method_inputs = True ,
2625+ skip_d2h_for_method_outputs = True ,
26942626 ),
2627+ enable_non_cpu_memory_planning = True ,
26952628 )
26962629 )
2697- self .assertFalse (
2698- any (planned_inputs ),
2699- f"no graph input should be planned when the caller provides it, got { planned_inputs } " ,
2700- )
2701- self .assertFalse (
2702- any (planned_outputs ),
2703- f"no graph output should be planned when it stays on the device, got { planned_outputs } " ,
2704- )
2630+ plan = lowered .executorch_program .execution_plan [0 ]
2631+ planned = lambda indices : [ # noqa: E731
2632+ getattr (plan .values [index ].val , "allocation_info" , None ) is not None
2633+ for index in indices
2634+ ]
2635+ self .assertEqual (planned (plan .inputs ), [False , False ])
2636+ self .assertEqual (planned (plan .outputs ), [True ])
27052637
27062638 def test_device_output_copied_to_host_is_planned (self ):
27072639 """A delegate output copied to the host is an intermediate tensor on the
0 commit comments