| 39 | |
| 40 | |
| 41 | class XpuWorker(WorkerBase): |
| 42 | """ """ |
| 43 | |
| 44 | def __init__( |
| 45 | self, |
| 46 | fd_config: FDConfig, |
| 47 | local_rank: int, |
| 48 | rank: int, |
| 49 | ): |
| 50 | super().__init__( |
| 51 | fd_config=fd_config, |
| 52 | local_rank=local_rank, |
| 53 | rank=rank, |
| 54 | ) |
| 55 | pass |
| 56 | |
| 57 | def init_device(self): |
| 58 | """Initialize device and Construct model runner""" |
| 59 | self.max_chips_per_node = 16 if current_platform.is_iluvatar() else 8 |
| 60 | if paddle.is_compiled_with_xpu(): |
| 61 | # Set environment variable |
| 62 | self.device_ids = self.parallel_config.device_ids.split(",") |
| 63 | self.device = f"xpu:{self.local_rank % self.max_chips_per_node}" |
| 64 | paddle.device.set_device(self.device) |
| 65 | self.device_id = int(self.device_ids[self.local_rank % self.max_chips_per_node]) |
| 66 | assert ( |
| 67 | self.device_id is not None |
| 68 | ), f"device_id is none for rank {self.local_rank % self.max_chips_per_node}" |
| 69 | assert len(self.device_ids) > ( |
| 70 | self.local_rank % self.max_chips_per_node |
| 71 | ), f"device number must be greater than local rank, but get device number is {len(self.device_ids)}, rank is {self.local_rank % self.max_chips_per_node}" |
| 72 | paddle.set_default_dtype(self.model_config.dtype) |
| 73 | |
| 74 | gc.collect() |
| 75 | paddle.device.xpu.empty_cache() |
| 76 | else: |
| 77 | raise RuntimeError(f"Not support device type: {self.device_config.device}") |
| 78 | |
| 79 | if self.local_rank == 0: |
| 80 | report_usage_stats(self.fd_config) |
| 81 | |
| 82 | set_random_seed(self.fd_config.model_config.seed) |
| 83 | # Construct model runner |
| 84 | self.model_runner: XPUModelRunner = XPUModelRunner( |
| 85 | fd_config=self.fd_config, |
| 86 | device=self.device, |
| 87 | rank=self.rank, |
| 88 | device_id=self.device_id, |
| 89 | local_rank=self.local_rank, |
| 90 | ) |
| 91 | |
| 92 | def exist_prefill(self): |
| 93 | """ |
| 94 | check whether prefill stage exist |
| 95 | """ |
| 96 | return self.model_runner.exist_prefill() |
| 97 | |
| 98 | def determine_available_memory(self) -> int: |