|
| 1 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | + |
| 17 | +from paddleformers.trl.llm_utils import init_dist_env |
| 18 | + |
| 19 | +from fastdeploy.rl.rollout_config import RolloutModelConfig |
| 20 | +from fastdeploy.rl.rollout_model import RolloutModel |
| 21 | + |
| 22 | +_, ranks = init_dist_env() |
| 23 | + |
| 24 | +parser = argparse.ArgumentParser() |
| 25 | +parser.add_argument("--model_path", type=str, required=True, help="Path to the model directory") |
| 26 | +parser.add_argument("--baseline_path", type=str, required=True, help="Path to the baseline path") |
| 27 | +parser.add_argument("--quantization", type=str, default=None, help="Quantization") |
| 28 | +parser.add_argument("--enable_mm", action="store_true", required=False, help="Flags to enable multi-modal model") |
| 29 | +args = parser.parse_args() |
| 30 | + |
| 31 | +# base result |
| 32 | +model_path = args.model_path |
| 33 | + |
| 34 | +# Usage example: |
| 35 | +init_kwargs = { |
| 36 | + "model_name_or_path": model_path, |
| 37 | + "max_model_len": 32768, |
| 38 | + "tensor_parallel_size": ranks, |
| 39 | + "dynamic_load_weight": True, |
| 40 | + "load_strategy": "ipc_snapshot", |
| 41 | + "quantization": args.quantization, |
| 42 | +} |
| 43 | +if args.enable_mm: |
| 44 | + init_kwargs["enable_mm"] = True |
| 45 | + |
| 46 | + |
| 47 | +rollout_config = RolloutModelConfig(**init_kwargs) |
| 48 | +actor_eval_model = RolloutModel(rollout_config) |
| 49 | + |
| 50 | +content = "".join( |
| 51 | + sorted( |
| 52 | + [f"{k}\n" for k, v in actor_eval_model.state_dict().items()] |
| 53 | + + [f"{k}:{v}\n" for k, v in actor_eval_model.get_name_mappings_to_training().items()] |
| 54 | + ) |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +def compare_strings_line_by_line(a: str, b: str) -> bool: |
| 59 | + """ |
| 60 | + Compare two multiline strings line by line. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + True if all lines match exactly in order and content. |
| 64 | + False if any line differs or the number of lines is not equal. |
| 65 | + """ |
| 66 | + a_lines = a.splitlines() |
| 67 | + b_lines = b.splitlines() |
| 68 | + |
| 69 | + if len(a_lines) != len(b_lines): |
| 70 | + print(f"❌ Mismatch in number of lines: expected {len(a_lines)}, but got {len(b_lines)}.") |
| 71 | + return False |
| 72 | + |
| 73 | + for i, (line_a, line_b) in enumerate(zip(a_lines, b_lines)): |
| 74 | + if line_a != line_b: |
| 75 | + print(f"❌ Difference found on line {i + 1}:") |
| 76 | + print(f" Expected: {repr(line_a)}") |
| 77 | + print(f" Actual : {repr(line_b)}") |
| 78 | + return False |
| 79 | + |
| 80 | + print("✅ All lines match exactly.") |
| 81 | + return True |
| 82 | + |
| 83 | + |
| 84 | +with open(args.baseline_path, "r", encoding="utf-8") as f: |
| 85 | + baseline = f.read() |
| 86 | + assert compare_strings_line_by_line(baseline, content), ( |
| 87 | + "In the unittest of RL scenario, your modification " |
| 88 | + "caused inconsistency in the content before and after. Please fix it. " |
| 89 | + "Can request assistance from yuanlehome or gzy19990617 (github id)." |
| 90 | + ) |
0 commit comments