| 32 | |
| 33 | |
| 34 | class GcuWorker(WorkerBase): |
| 35 | """ """ |
| 36 | |
| 37 | def __init__( |
| 38 | self, |
| 39 | fd_config: FDConfig, |
| 40 | local_rank: int, |
| 41 | rank: int, |
| 42 | ): |
| 43 | super().__init__( |
| 44 | fd_config=fd_config, |
| 45 | local_rank=local_rank, |
| 46 | rank=rank, |
| 47 | ) |
| 48 | pass |
| 49 | |
| 50 | def init_device(self): |
| 51 | """Initialize device and Construct model runner""" |
| 52 | if paddle.is_compiled_with_custom_device("gcu"): |
| 53 | # Set environment variable |
| 54 | self.device_ids = self.parallel_config.device_ids.split(",") |
| 55 | self.device = f"gcu:{self.local_rank}" |
| 56 | paddle.device.set_device(self.device) |
| 57 | paddle.set_default_dtype(self.model_config.dtype) |
| 58 | logger.info(f"GcuWorker init_device:{self.device}, device_ids:{self.device_ids}") |
| 59 | |
| 60 | gc.collect() |
| 61 | else: |
| 62 | raise RuntimeError(f"Not support device type: {self.device_config.device}") |
| 63 | |
| 64 | if self.local_rank == 0: |
| 65 | report_usage_stats(self.fd_config) |
| 66 | |
| 67 | set_random_seed(self.fd_config.model_config.seed) |
| 68 | # Construct model runner |
| 69 | self.model_runner: GCUModelRunner = GCUModelRunner( |
| 70 | fd_config=self.fd_config, |
| 71 | device=self.device, |
| 72 | device_id=self.device_ids[self.local_rank], |
| 73 | rank=self.rank, |
| 74 | local_rank=self.local_rank, |
| 75 | ) |
| 76 | |
| 77 | def exist_prefill(self): |
| 78 | """ |
| 79 | check whether prefill stage exist |
| 80 | """ |
| 81 | return self.model_runner.exist_prefill() |
| 82 | |
| 83 | def determine_available_memory(self) -> int: |
| 84 | """ |
| 85 | Profiles the peak memory usage of the model to determine how much |
| 86 | memory can be used for KV cache without OOMs. |
| 87 | |
| 88 | The engine will first conduct a profiling of the existing memory usage. |
| 89 | Then, it calculate the maximum possible number of GCU and CPU blocks |
| 90 | that can be allocated with the remaining free memory. |
| 91 | |