@@ -1666,8 +1666,18 @@ def custom_forward(pe_embedder):
16661666 # Process through double stream blocks
16671667 capture_idx = 0
16681668 for bid , block in enumerate (self .double_stream_blocks ):
1669+ checkpoint_this_block = self .training and should_checkpoint_block (
1670+ bid ,
1671+ self .gradient_checkpointing ,
1672+ self .gradient_checkpointing_interval ,
1673+ self .gradient_checkpointing_segment_stride ,
1674+ )
16691675 if musubi_offload_active and musubi_manager .is_managed_block (block_id ):
1670- musubi_manager .stream_in (block , hidden_states .device )
1676+ musubi_manager .stream_in (
1677+ block ,
1678+ hidden_states .device ,
1679+ checkpointed = checkpoint_this_block ,
1680+ )
16711681 # TREAD routing for this layer
16721682 if use_routing :
16731683 # Check if this layer should use routing
@@ -1698,12 +1708,7 @@ def custom_forward(pe_embedder):
16981708 )
16991709
17001710 # Process through the block with optional gradient checkpointing
1701- if self .training and should_checkpoint_block (
1702- bid ,
1703- self .gradient_checkpointing ,
1704- self .gradient_checkpointing_interval ,
1705- self .gradient_checkpointing_segment_stride ,
1706- ):
1711+ if checkpoint_this_block :
17071712
17081713 def create_custom_forward (module , return_dict = None ):
17091714 def custom_forward (* inputs ):
@@ -1797,8 +1802,18 @@ def custom_forward(*inputs):
17971802
17981803 # 7. Process through single stream blocks
17991804 for bid , block in enumerate (self .single_stream_blocks ):
1805+ checkpoint_this_block = self .training and should_checkpoint_block (
1806+ len (self .double_stream_blocks ) + bid ,
1807+ self .gradient_checkpointing ,
1808+ self .gradient_checkpointing_interval ,
1809+ self .gradient_checkpointing_segment_stride ,
1810+ )
18001811 if musubi_offload_active and musubi_manager .is_managed_block (block_id ):
1801- musubi_manager .stream_in (block , hidden_states .device )
1812+ musubi_manager .stream_in (
1813+ block ,
1814+ hidden_states .device ,
1815+ checkpointed = checkpoint_this_block ,
1816+ )
18021817 # TREAD routing for single stream layers
18031818 if use_routing :
18041819 # Check if this layer should use routing
@@ -1835,12 +1850,7 @@ def custom_forward(*inputs):
18351850 hidden_states = torch .cat ([hidden_states , cur_llama_embedding ], dim = 1 )
18361851
18371852 # Process through the block with optional gradient checkpointing
1838- if self .training and should_checkpoint_block (
1839- len (self .double_stream_blocks ) + bid ,
1840- self .gradient_checkpointing ,
1841- self .gradient_checkpointing_interval ,
1842- self .gradient_checkpointing_segment_stride ,
1843- ):
1853+ if checkpoint_this_block :
18441854
18451855 def create_custom_forward (module , return_dict = None ):
18461856 def custom_forward (* inputs ):
0 commit comments