base_runner.py 2.98 KB
Newer Older
wangkx1's avatar
init  
wangkx1 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# scripts/base_runner.py
import subprocess
import os
from typing import Dict, Any, List, Tuple

class BaseRunner:
    """基础模型运行器"""
    
    def __init__(self, config: Dict[str, Any], env):
        self.config = config
        self.env = env
        self.base_config = self.config.get('base', {})
        self.common_config = self.base_config.get('common', {})
        
    def build_command(self, model_file: str, model_config: Dict[str, Any], 
                     batch_size: int) -> List[str]:
        """构建migraphx-driver命令"""
        
        # 基础命令
        cmd = [
            self.common_config.get('migraphx_driver', '/opt/dtk/bin/migraphx-driver'),
            "perf",
            model_file
        ]
        
        # FP16选项
        if self.common_config.get('fp16', True):
            cmd.append("--fp16")
        
        # 迭代次数
        iterations = model_config.get('iterations', self.common_config.get('iterations', 100))
        cmd.extend(["-n", str(iterations)])
        
        # 输入维度
        inputs = model_config.get('inputs', [])
        for input_config in inputs:
            input_name = input_config.get('name', 'x')
            shape = input_config.get('shape', [])
            
            # 将batch size插入到形状的第一个位置
            full_shape = [str(batch_size)] + [str(dim) for dim in shape]
            
            cmd.extend(["--input-dim", f"@{input_name}", *full_shape])
        
        # 额外参数
        extra_args = model_config.get('extra_args', [])
        if extra_args:
            cmd.extend(extra_args)
            
        return cmd
    
    def run_model(self, cmd: List[str], log_file: str) -> Tuple[bool, str]:
        """运行模型并记录日志"""
        
        try:
            # 执行命令并同时输出到终端和文件
            with open(log_file, 'w') as f:
                print(f"执行命令: {' '.join(cmd)}")
                
                # 运行命令,实时输出到终端和文件
                process = subprocess.Popen(
                    cmd,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT,
                    text=True,
                    bufsize=1,
                    universal_newlines=True
                )
                
                # 实时读取输出
                output_lines = []
                for line in process.stdout:
                    print(line, end='')
                    f.write(line)
                    output_lines.append(line)
                
                process.wait()
                
                output = ''.join(output_lines)
                success = (process.returncode == 0)
                
                return success, output
                
        except Exception as e:
            error_msg = f"执行命令失败: {e}"
            print(error_msg)
            with open(log_file, 'a') as f:
                f.write(f"\n错误: {error_msg}\n")
            return False, error_msg