Skip to content

Commit d91168d

Browse files
committed
优化CI和示例写法
1 parent 2a7f452 commit d91168d

File tree

2 files changed

+38
-97
lines changed

2 files changed

+38
-97
lines changed

ci_scripts/hooks/pre-doc-compile.sh

Lines changed: 28 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ else
2020
echo "INFO: Tools directory ${TOOLS_DIR} already exists"
2121
fi
2222

23-
# Define API mapping files URLs (only main URLs, no backups)
23+
# Define API mapping files URLs
2424
API_ALIAS_MAPPING_URL="https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/api_alias_mapping.json"
2525
API_MAPPING_URL="https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/api_mapping.json"
2626
GLOBAL_VAR_URL="https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/global_var.py"
@@ -45,7 +45,23 @@ else
4545
CURL_PROXY_ARGS=""
4646
fi
4747

48-
# Download API mapping files with retry
48+
# Handle failure: copy cached file and exit
49+
handle_failure() {
50+
local cached_file="${APIMAPPING_ROOT}/cached_pytorch_api_mapping_cn.md"
51+
local target_file="${APIMAPPING_ROOT}/pytorch_api_mapping_cn.md"
52+
53+
if [ -f "$cached_file" ]; then
54+
echo "INFO: Copying cached file to target: $cached_file -> $target_file"
55+
cp "$cached_file" "$target_file"
56+
echo "INFO: Successfully copied cached file to $target_file"
57+
exit 0
58+
else
59+
echo "ERROR: Cached file not found at $cached_file"
60+
exit 1
61+
fi
62+
}
63+
64+
# Download file with retry and failure handling
4965
download_file() {
5066
local url=$1
5167
local dest=$2
@@ -69,91 +85,26 @@ download_file() {
6985
done
7086

7187
echo "ERROR: Failed to download ${filename} after $max_retries attempts"
72-
return 1
88+
handle_failure
7389
}
7490

75-
# Check if cached file exists
76-
check_cached_file() {
77-
local cached_file="${APIMAPPING_ROOT}/cached_pytorch_api_mapping_cn.md"
78-
if [ -f "$cached_file" ]; then
79-
echo "INFO: Cached file found at $cached_file"
80-
return 0
81-
else
82-
echo "INFO: No cached file found at $cached_file"
83-
return 1
84-
fi
85-
}
86-
87-
# Copy cached file to target if download fails
88-
copy_cached_file() {
89-
local cached_file="${APIMAPPING_ROOT}/cached_pytorch_api_mapping_cn.md"
90-
local target_file="${APIMAPPING_ROOT}/pytorch_api_mapping_cn.md"
91-
92-
if [ -f "$cached_file" ]; then
93-
echo "INFO: Copying cached file to target: $cached_file -> $target_file"
94-
cp "$cached_file" "$target_file"
95-
echo "INFO: Successfully copied cached file to $target_file"
96-
return 0
97-
else
98-
echo "ERROR: Cached file not found at $cached_file"
99-
return 1
100-
fi
101-
}
102-
103-
# Function to handle download failure
104-
handle_download_failure() {
105-
local filename=$1
106-
echo "INFO: Download or Execute $filename failed. Checking for cached file..."
107-
if check_cached_file; then
108-
if copy_cached_file; then
109-
echo "INFO: Successfully copied cached file. Exiting."
110-
exit 0
111-
else
112-
echo "ERROR: Failed to copy cached file to target"
113-
exit 1
114-
fi
115-
else
116-
echo "ERROR: Download failed and no cached file available"
117-
exit 1
118-
fi
119-
}
120-
121-
# Download API alias mapping file
122-
echo "INFO: Downloading API alias mapping file"
123-
if ! download_file "${API_ALIAS_MAPPING_URL}" "${TOOLS_DIR}/api_alias_mapping.json"; then
124-
handle_download_failure "API alias mapping"
125-
fi
126-
127-
# Download API mapping file
128-
echo "INFO: Downloading API mapping file"
129-
if ! download_file "${API_MAPPING_URL}" "${TOOLS_DIR}/api_mapping.json"; then
130-
handle_download_failure "API mapping"
131-
fi
132-
133-
# Download global variable file
134-
echo "INFO: Downloading global variable file"
135-
if ! download_file "${GLOBAL_VAR_URL}" "${TOOLS_DIR}/global_var.py"; then
136-
handle_download_failure "Global variable"
137-
fi
138-
139-
# Download attribute mapping file
140-
echo "INFO: Downloading attribute mapping file"
141-
if ! download_file "${ATTRIBUTE_MAPPING_URL}" "${TOOLS_DIR}/attribute_mapping.json"; then
142-
handle_download_failure "Attribute mapping"
143-
fi
91+
# Download all API mapping files
92+
download_file "${API_ALIAS_MAPPING_URL}" "${TOOLS_DIR}/api_alias_mapping.json"
93+
download_file "${API_MAPPING_URL}" "${TOOLS_DIR}/api_mapping.json"
94+
download_file "${GLOBAL_VAR_URL}" "${TOOLS_DIR}/global_var.py"
95+
download_file "${ATTRIBUTE_MAPPING_URL}" "${TOOLS_DIR}/attribute_mapping.json"
14496

145-
# If we get here, all files were downloaded successfully
14697
echo "INFO: All API mapping files successfully downloaded"
14798

148-
# Run the remaining scripts
99+
# Run the remaining scripts with failure handling
149100
echo "INFO: Running get_api_difference_info.py"
150101
if ! python "${APIMAPPING_ROOT}/tools/get_api_difference_info.py"; then
151-
handle_download_failure "get_api_difference_info.py"
102+
handle_failure
152103
fi
153104

154105
echo "INFO: Running generate_pytorch_api_mapping.py"
155106
if ! python "${APIMAPPING_ROOT}/tools/generate_pytorch_api_mapping.py"; then
156-
handle_download_failure "generate_pytorch_api_mapping.py"
107+
handle_failure
157108
fi
158109

159110
# Create backup of generated file
@@ -167,5 +118,5 @@ if [ -f "$GENERATED_FILE" ]; then
167118
echo "INFO: Successfully created backup file at $BACKUP_FILE"
168119
else
169120
echo "ERROR: Generated API mapping file not found at $GENERATED_FILE"
170-
exit 1
121+
handle_failure
171122
fi

docs/guides/model_convert/convert_from_pytorch/pytorch_api_mapping_cn.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,10 @@
3131
**简介:此类 API 没有转写成本,两者用法完全一致,** 只需将代码中所有前缀 ``torch.`` 替换为 ``paddle.`` 即可。(也可只在文件最上方插入一行 ``import paddle as torch``)。示例如下。
3232

3333
```python
34-
# PyTorch 写法
35-
torch.eye(5)
36-
torch.einsum('ii->i', x)
37-
torch.nn.Softplus(beta=0.5, threshold=15)
38-
39-
# Paddle 写法
40-
paddle.eye(5)
41-
paddle.einsum('ii->i', x)
42-
paddle.nn.Softplus(beta=0.5, threshold=15)
34+
# PyTorch 写法 -> Paddle 写法
35+
torch.eye(5) -> paddle.eye(5)
36+
torch.einsum('ii->i', x) -> paddle.einsum('ii->i', x)
37+
torch.nn.Softplus(beta=0.5, threshold=15) -> paddle.nn.Softplus(beta=0.5, threshold=15)
4338
```
4439

4540

@@ -51,17 +46,12 @@ paddle.nn.Softplus(beta=0.5, threshold=15)
5146
**简介:** 此类 API 的转写成本较低,只需对 API 调用方式进行改写,无需转写 API 参数部分。示例如下。
5247

5348
```python
54-
# PyTorch 写法
55-
torch.numel(x)
56-
out = x.matrix_exp()
57-
out = x.to_sparse(1)
58-
out = x.clamp_(-0.5, 0.5)
59-
60-
# Paddle 写法
61-
x.size
62-
out = paddle.linalg.matrix_exp(x)
63-
out = x.to_sparse_coo(1)
64-
out = x.clip_(-0.5, 0.5)
49+
# PyTorch 写法 -> Paddle 写法
50+
torch.numel(x) -> x.size # Tensor 方法转属性
51+
out = x.matrix_exp() -> paddle.linalg.matrix_exp(x) # 函数转方法
52+
out = x.to_sparse(1) -> x.to_sparse_coo(1) # 函数名不同
53+
out = x.clamp_(-0.5, 0.5) -> x.clip_(-0.5, 0.5) # 函数名不同
54+
6555
```
6656

6757
| 序号 | Pytorch 最新 release | Paddle develop | 映射分类 | 备注 |

0 commit comments

Comments
 (0)