|
| 1 | +import torch |
| 2 | + |
| 3 | +from lightx2v.common.offload.manager import WeightAsyncStreamManager |
| 4 | +from lightx2v.models.networks.longcat_image.infer.transformer_infer import LongCatImageTransformerInfer |
| 5 | +from lightx2v_platform.base.global_var import AI_DEVICE |
| 6 | + |
| 7 | +torch_device_module = getattr(torch, AI_DEVICE) |
| 8 | + |
| 9 | + |
| 10 | +class LongCatImageOffloadTransformerInfer(LongCatImageTransformerInfer): |
| 11 | + """Offload transformer inference for LongCat Image model. |
| 12 | +
|
| 13 | + Supports block-level offload with double-buffer async prefetch for both |
| 14 | + double-stream blocks and single-stream blocks. |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, config): |
| 18 | + super().__init__(config) |
| 19 | + if self.config.get("cpu_offload", False): |
| 20 | + offload_granularity = self.config.get("offload_granularity", "block") |
| 21 | + if offload_granularity == "block": |
| 22 | + self.infer_func = self.infer_with_blocks_offload |
| 23 | + if offload_granularity != "model": |
| 24 | + self.offload_manager_double = WeightAsyncStreamManager(offload_granularity=offload_granularity) |
| 25 | + self.offload_manager_single = WeightAsyncStreamManager(offload_granularity=offload_granularity) |
| 26 | + |
| 27 | + def infer_with_blocks_offload(self, blocks, pre_infer_out): |
| 28 | + """Run transformer inference with block-level offload. |
| 29 | +
|
| 30 | + Two-phase approach: first process all double blocks, then all single blocks, |
| 31 | + each with their own offload manager and cuda buffers. |
| 32 | + """ |
| 33 | + hidden_states = pre_infer_out.hidden_states |
| 34 | + encoder_hidden_states = pre_infer_out.encoder_hidden_states |
| 35 | + temb = pre_infer_out.temb |
| 36 | + image_rotary_emb = pre_infer_out.image_rotary_emb |
| 37 | + |
| 38 | + # For I2I task: concatenate output latents with input image latents |
| 39 | + output_seq_len = None |
| 40 | + if pre_infer_out.input_image_latents is not None: |
| 41 | + output_seq_len = pre_infer_out.output_seq_len |
| 42 | + hidden_states = torch.cat([hidden_states, pre_infer_out.input_image_latents], dim=0) |
| 43 | + |
| 44 | + # Stage 1: double blocks offload |
| 45 | + # wait for default stream |
| 46 | + current_stream = torch_device_module.current_stream() |
| 47 | + self.offload_manager_double.compute_stream.wait_stream(current_stream) |
| 48 | + for block_idx in range(len(blocks.double_blocks)): |
| 49 | + self.block_idx = block_idx |
| 50 | + |
| 51 | + if self.offload_manager_double.need_init_first_buffer: |
| 52 | + self.offload_manager_double.init_first_buffer(blocks.double_blocks) |
| 53 | + |
| 54 | + self.offload_manager_double.prefetch_weights((block_idx + 1) % len(blocks.double_blocks), blocks.double_blocks) |
| 55 | + |
| 56 | + with torch_device_module.stream(self.offload_manager_double.compute_stream): |
| 57 | + encoder_hidden_states, hidden_states = self.infer_double_stream_block( |
| 58 | + self.offload_manager_double.cuda_buffers[0], |
| 59 | + hidden_states, |
| 60 | + encoder_hidden_states, |
| 61 | + temb, |
| 62 | + image_rotary_emb, |
| 63 | + ) |
| 64 | + |
| 65 | + self.offload_manager_double.swap_blocks() |
| 66 | + |
| 67 | + # Stage 2: single blocks offload |
| 68 | + # wait for double stream |
| 69 | + self.offload_manager_single.compute_stream.wait_stream(self.offload_manager_double.compute_stream) |
| 70 | + for block_idx in range(len(blocks.single_blocks)): |
| 71 | + self.block_idx = block_idx |
| 72 | + |
| 73 | + if self.offload_manager_single.need_init_first_buffer: |
| 74 | + self.offload_manager_single.init_first_buffer(blocks.single_blocks) |
| 75 | + |
| 76 | + self.offload_manager_single.prefetch_weights((block_idx + 1) % len(blocks.single_blocks), blocks.single_blocks) |
| 77 | + |
| 78 | + with torch_device_module.stream(self.offload_manager_single.compute_stream): |
| 79 | + encoder_hidden_states, hidden_states = self.infer_single_stream_block( |
| 80 | + self.offload_manager_single.cuda_buffers[0], |
| 81 | + hidden_states, |
| 82 | + encoder_hidden_states, |
| 83 | + temb, |
| 84 | + image_rotary_emb, |
| 85 | + ) |
| 86 | + |
| 87 | + self.offload_manager_single.swap_blocks() |
| 88 | + |
| 89 | + # For I2I task: only return output image latents |
| 90 | + if output_seq_len is not None: |
| 91 | + hidden_states = hidden_states[:output_seq_len] |
| 92 | + |
| 93 | + return hidden_states |
0 commit comments