Skip to content

Commit 120ade1

Browse files
committed
lazy import paddle to avoid conflicts with HPI
1 parent 1f35270 commit 120ade1

30 files changed

Lines changed: 122 additions & 57 deletions

File tree

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repos:
2222
- id: clang-format
2323
name: clang-format
2424
description: Format files with ClangFormat
25-
entry: bash .clang_format.hook -i
25+
entry: bash .precommit/clang_format.hook -i
2626
language: system
2727
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|cuh|proto)$
2828
# For Python files
@@ -46,8 +46,8 @@ repos:
4646
# check license
4747
- repo: local
4848
hooks:
49-
- id: check-license
50-
name: Check License
51-
entry: python .check_license.py
49+
- id: check-custom
50+
name: Check Custom
51+
entry: python .precommit/check_custom.py
5252
language: python
5353
files: \.py$
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,23 @@
3131
"""
3232

3333

34-
def check_license(file_path):
34+
def check(file_path):
3535
with open(file_path, "r") as f:
3636
content = f.read()
37-
if not content.startswith(LICENSE_TEXT):
38-
print(f"License header missing in {file_path}")
39-
return False
37+
if not content.startswith(LICENSE_TEXT):
38+
print(f"License header missing in {file_path}")
39+
return False
40+
if "import paddle" in content or "from paddle import " in content:
41+
print(f"Please using `lazy_paddle` instead `paddle` when import in {file_path}")
42+
return False
4043
return True
4144

4245

4346
def main():
4447
files = sys.argv[1:]
4548
all_files_valid = True
4649
for file in files:
47-
if not check_license(file):
50+
if not check(file):
4851
all_files_valid = False
4952
if not all_files_valid:
5053
sys.exit(1)

paddlex/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from .utils.lazy_loader import LazyLoader
16+
import sys
17+
18+
sys.modules["lazy_paddle"] = LazyLoader("lazy_paddle", globals(), "paddle")
19+
1520
import os
1621

1722
from . import version

paddlex/inference/components/paddle_predictor/predictor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
import os
1616
from abc import abstractmethod
17-
import numpy as np
18-
import paddle
19-
from paddle.inference import Config, create_predictor
17+
import lazy_paddle as paddle
2018
import numpy as np
2119

2220
from ..base import BaseComponent
@@ -44,6 +42,8 @@ def __init__(self, model_dir, model_prefix, option):
4442

4543
def _create(self, model_dir, model_prefix, option):
4644
"""_create"""
45+
from lazy_paddle.inference import Config, create_predictor
46+
4747
use_pir = (
4848
hasattr(paddle.framework, "use_pir_api") and paddle.framework.use_pir_api()
4949
)

paddlex/inference/components/task_related/text_rec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from PIL import Image
2222
import cv2
2323
import math
24-
import paddle
24+
import lazy_paddle as paddle
2525
import json
2626
import tempfile
2727
from tokenizers import Tokenizer as TokenizerFast

paddlex/modules/base/predictor/utils/paddle_inference_predictor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16-
import paddle
17-
from paddle.inference import Config, create_predictor
16+
import lazy_paddle as paddle
1817

1918
from .....utils import logging
2019

@@ -34,6 +33,8 @@ def __init__(self, model_dir, model_prefix, option, delete_pass=[]):
3433

3534
def _create(self, model_dir, model_prefix, option, delete_pass):
3635
"""_create"""
36+
from lazy_paddle.inference import Config, create_predictor
37+
3738
use_pir = (
3839
hasattr(paddle.framework, "use_pir_api") and paddle.framework.use_pir_api()
3940
)

paddlex/modules/base/trainer/train_deamon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import threading
2121
from abc import ABC, abstractmethod
2222
from pathlib import Path
23-
import paddle
23+
import lazy_paddle as paddle
2424

2525
from ..build_model import build_model
2626
from ....utils.file_interface import write_json_file

paddlex/modules/base/utils/topk_eval.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import os
1717
import json
1818
import argparse
19-
from paddle import nn
20-
import paddle
19+
import lazy_paddle as paddle
2120

2221
from ....utils import logging
2322

@@ -34,7 +33,7 @@ def parse_args():
3433
return args
3534

3635

37-
class AvgMetrics(nn.Layer):
36+
class AvgMetrics(paddle.nn.Layer):
3837
"""Average metrics"""
3938

4039
def __init__(self):

paddlex/modules/image_classification/trainer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import json
1616
import shutil
17-
import paddle
17+
import lazy_paddle as paddle
1818
from pathlib import Path
1919

2020
from ..base import BaseTrainer, BaseTrainDeamon

0 commit comments

Comments
 (0)