(*args, **kwargs)
| 171 | def decorator(func): |
| 172 | @wraps(func) |
| 173 | def wrapper(*args, **kwargs): |
| 174 | sig = inspect.signature(func) |
| 175 | bound_args = sig.bind(*args, **kwargs) |
| 176 | bound_args.apply_defaults() |
| 177 | fd_config = bound_args.arguments.get(config_arg_name, None) |
| 178 | model = bound_args.arguments.get(model_arg_name, None) |
| 179 | enable_cache, weight_cache_dir, _ = is_weight_cache_enabled(fd_config) |
| 180 | assert fd_config is not None, "fd_config cannot be None" |
| 181 | assert model is not None, "model cannot be None" |
| 182 | if enable_cache: |
| 183 | tp_weight_cache_dir = os.path.join( |
| 184 | weight_cache_dir, f"rank{str(fd_config.parallel_config.tensor_parallel_rank)}" |
| 185 | ) |
| 186 | context = multi_switch_config_context((fd_config.model_config, "model", tp_weight_cache_dir)) |
| 187 | else: |
| 188 | context = contextlib.nullcontext() |
| 189 | |
| 190 | with context: |
| 191 | result = func(*args, **kwargs) |
| 192 | |
| 193 | if envs.FD_ENABLE_MODEL_LOAD_CACHE: |
| 194 | if not ( |
| 195 | fd_config.quant_config is not None and getattr(fd_config.quant_config, "is_checkpoint_bf16", False) |
| 196 | ): |
| 197 | # Save cache only for dynamic quantization |
| 198 | return result |
| 199 | if weight_cache_dir is None: |
| 200 | return result |
| 201 | tp_weight_cache_dir = os.path.join( |
| 202 | weight_cache_dir, f"rank{str(fd_config.parallel_config.tensor_parallel_rank)}" |
| 203 | ) |
| 204 | if not os.path.exists(tp_weight_cache_dir): |
| 205 | logger.info(f"Saving model to {tp_weight_cache_dir}") |
| 206 | os.makedirs( |
| 207 | tp_weight_cache_dir, |
| 208 | exist_ok=True, |
| 209 | ) |
| 210 | _save_model(model.state_dict(), os.path.join(tp_weight_cache_dir, "cache.pdparams")) |
| 211 | else: |
| 212 | reason = "weights already cached" if envs.FD_ENABLE_MODEL_LOAD_CACHE else "cache disabled" |
| 213 | logger.info(f"Skip saving ,{reason}") |
| 214 | return result |
| 215 | |
| 216 | return wrapper |
| 217 |
nothing calls this directly
no test coverage detected