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