| 52 | |
| 53 | |
| 54 | class HpuWorker(WorkerBase): |
| 55 | def __init__( |
| 56 | self, |
| 57 | fd_config: FDConfig, |
| 58 | local_rank: int, |
| 59 | rank: int, |
| 60 | ): |
| 61 | super().__init__( |
| 62 | fd_config=fd_config, |
| 63 | local_rank=local_rank, |
| 64 | rank=rank, |
| 65 | ) |
| 66 | pass |
| 67 | |
| 68 | def init_device(self): |
| 69 | """ |
| 70 | Initialize device and construct model runner |
| 71 | """ |
| 72 | if paddle.is_compiled_with_custom_device("intel_hpu"): |
| 73 | # Set environment variable |
| 74 | self.device_ids = self.parallel_config.device_ids.split(",") |
| 75 | logger.info( |
| 76 | f"Using Intel HPU device with local rank => device id: {int(self.device_ids[self.local_rank])} as module id" |
| 77 | ) |
| 78 | intel_hpus_module_id = int(self.device_ids[self.local_rank]) |
| 79 | self.device = f"intel_hpu:{intel_hpus_module_id}" |
| 80 | paddle.device.set_device(self.device) |
| 81 | paddle.set_default_dtype(self.model_config.dtype) |
| 82 | |
| 83 | gc.collect() |
| 84 | paddle.device.cuda.empty_cache() |
| 85 | else: |
| 86 | raise RuntimeError(f"Not support device type: {self.device_config.device}") |
| 87 | |
| 88 | if self.local_rank == 0: |
| 89 | report_usage_stats(self.fd_config) |
| 90 | |
| 91 | set_random_seed(self.fd_config.model_config.seed) |
| 92 | # Construct model runner |
| 93 | self.model_runner: HPUModelRunner = HPUModelRunner( |
| 94 | fd_config=self.fd_config, |
| 95 | device=self.device, |
| 96 | device_id=self.device_ids[self.local_rank], |
| 97 | rank=self.rank, |
| 98 | local_rank=self.local_rank, |
| 99 | ) |
| 100 | |
| 101 | def exist_prefill(self): |
| 102 | """ |
| 103 | check whether prefill stage exist |
| 104 | """ |
| 105 | return self.model_runner.exist_prefill() |
| 106 | |
| 107 | def determine_available_memory(self) -> int: |
| 108 | """ |
| 109 | Profiles the peak memory usage of the model to determine how much |
| 110 | memory can be used for KV cache without OOMs. |
| 111 | |