使用Profiler()函数,报错RuntimeError: The output path of profiler only supports alphabets(a-zA-Z)

1 系统环境

硬件环境(Ascend/GPU/CPU): CPU
MindSpore版本: mindspore=2.2.12
执行模式(PyNative/ Graph):不限
Python版本: Python=3.9
操作系统平台: win11

2 报错信息

2.1 问题描述

在用mindspore_cpu_py39 2.2.12版本下,项目文件路径为E:\MindsporePrj\PreTrain_ms,使用如下脚本信息出现报错。

2.2 脚本信息

from mindspore.profiler import Profiler
profiler = Profiler()

2.3 报错信息

RuntimeError: The output path of profiler only supports alphabets(a-zA-Z), digit(0-9) or {'-', '_', '.', '/', '@'}, but got the absolute path= E:\MindsporePrj\PreTrain_ms\data复制

3 根因分析

报错的最终文件是mindspore\profiler\common\validator\validate_path.py

https://gitee.com/mindspore/mindspore/blob/master/mindspore/python/mindspore/profiler/common/validator/validate_path.py

Profiler() 创建的时候未提供任何参数,output_path: str = “./data”,

path默认值就是"./data"

在validate_and_normalize_path这个函数里面会把相对路径修改成绝对路径,然后在windows下面就是项目路径在加data

E:\MindsporePrj\PreTrain_ms\data

check_valid_character_of_path这个函数校验路径有效的时候,路径里面只能有alphabets(a-zA-Z), digit(0-9) or {‘-’, ‘_’, ‘.’, ‘/’}

并不包含windows下的 冒号 :

def check_valid_character_of_path(file_path):  
    """  
    Validates path.  
    
    The output path of profiler only supports alphabets(a-zA-Z), digit(0-9) or {'-', '_', '.', '/'}.  
    
    Note:  
        Chinese and other paths are not supported at present.  
    
    Args:  
        path (str):  Normalized Path.  
    
    Returns:  
        bool, whether valid.  
    """  
    re_path = r'^[/\\_a-zA-Z0-9-_.@]+$'  
    path_valid = re.fullmatch(re_path, file_path)  
    if not path_valid:  
        msg = "The output path of profiler only supports alphabets(a-zA-Z), " \  
                "digit(0-9) or {'-', '_', '.', '/', '@'}, but got the absolute path= " + file_path  
        raise RuntimeError(msg)  
    
def validate_and_normalize_path(  
        path,  
        check_absolute_path=False,  
        allow_parent_dir=True,  
):  
    """  
    Validates path and returns its normalized form.  
    
    If path has a valid scheme, treat path as url, otherwise consider path a  
    unix local path.  
    
    Note:  
        File scheme (rfc8089) is currently not supported.  
    
    Args:  
        path (str): Path to be normalized.  
        check_absolute_path (bool): Whether check path scheme is supported.  
        allow_parent_dir (bool): Whether allow parent dir in path.  
    
    Returns:  
        str, normalized path.  
    """  
    if not path:  
        raise RuntimeError("The path is invalid!")  
    
    path_str = str(path)  
    if not allow_parent_dir:  
        path_components = path_str.split("/")  
        if ".." in path_components:  
            raise RuntimeError("The parent path is not allowed!")  
    
    # path does not have valid schema, treat it as unix local path.  
    if check_absolute_path:  
        if not path_str.startswith("/"):  
            raise RuntimeError("The path is invalid!")  
    try:  
        # most unix systems allow  
        normalized_path = os.path.realpath(path)  
    except ValueError as err:  
        raise RuntimeError("The path is invalid!") from err  
    check_valid_character_of_path(normalized_path)  
    return normalized_path

4 解决方案

对windows做特殊处理

修改check_valid_character_of_path,增加:

def check_valid_character_of_path(file_path):  
    re_path = r'^[/\\_a-zA-Z0-9-_.@:]+$'  
    path_valid = re.fullmatch(re_path, file_path)  
    if not path_valid:  
        msg = "The output path of profiler only supports alphabets(a-zA-Z), " \  
                "digit(0-9) or {'-', '_', '.', '/', '@'}, but got the absolute path= " + file_path  
        raise RuntimeError(msg)  

修改之后就可以了