Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inference for GCU #14142

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion tools/infer/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def init_args():
parser.add_argument("--use_xpu", type=str2bool, default=False)
parser.add_argument("--use_npu", type=str2bool, default=False)
parser.add_argument("--use_mlu", type=str2bool, default=False)
parser.add_argument(
"--use_gcu",
type=str2bool,
default=False,
help="Use Enflame GCU(General Compute Unit)",
)
parser.add_argument("--ir_optim", type=str2bool, default=True)
parser.add_argument("--use_tensorrt", type=str2bool, default=False)
parser.add_argument("--min_subgraph_size", type=int, default=15)
Expand Down Expand Up @@ -293,6 +299,34 @@ def create_predictor(args, mode, logger):
config.enable_custom_device("mlu")
elif args.use_xpu:
config.enable_xpu(10 * 1024 * 1024)
elif args.use_gcu: # for Enflame GCU(General Compute Unit)
assert paddle.device.is_compiled_with_custom_device("gcu"), (
"Args use_gcu cannot be set as True while your paddle "
"is not compiled with gcu! \nPlease try: \n"
"\t1. Install paddle-custom-gcu to run model on GCU. \n"
"\t2. Set use_gcu as False in args to run model on CPU."
)
import paddle_custom_device.gcu.passes as gcu_passes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we first check whether the paddle_custom_device package is present? If it’s not, remind the user to install it first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion.


gcu_passes.setUp()
if args.precision == "fp16":
config.enable_custom_device(
"gcu", 0, paddle.inference.PrecisionType.Half
)
gcu_passes.set_exp_enable_mixed_precision_ops(config)
else:
config.enable_custom_device("gcu")

if paddle.framework.use_pir_api():
config.enable_new_ir(True)
config.enable_new_executor(True)
kPirGcuPasses = gcu_passes.inference_passes(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to build passes based on "PaddleOCR" or "PaddleXXX"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to optimize performance based on hardware features and even network structure, and the processing here is to increase this flexibility.

use_pir=True, name="PaddleOCR"
)
config.enable_custom_passes(kPirGcuPasses, True)
else:
pass_builder = config.pass_builder()
gcu_passes.append_passes_for_legacy_ir(pass_builder, "PaddleOCR")
else:
config.disable_gpu()
if args.enable_mkldnn:
Expand All @@ -309,7 +343,8 @@ def create_predictor(args, mode, logger):
# enable memory optim
config.enable_memory_optim()
config.disable_glog_info()
config.delete_pass("conv_transpose_eltwiseadd_bn_fuse_pass")
if not args.use_gcu: # for Enflame GCU(General Compute Unit)
config.delete_pass("conv_transpose_eltwiseadd_bn_fuse_pass")
config.delete_pass("matmul_transpose_reshape_fuse_pass")
if mode == "rec" and args.rec_algorithm == "SRN":
config.delete_pass("gpu_cpu_map_matmul_v2_to_matmul_pass")
Expand Down
10 changes: 8 additions & 2 deletions tools/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def merge_config(config, opts):
return config


def check_device(use_gpu, use_xpu=False, use_npu=False, use_mlu=False):
def check_device(use_gpu, use_xpu=False, use_npu=False, use_mlu=False, use_gcu=False):
"""
Log error and exit when set use_gpu=true in paddlepaddle
cpu version.
Expand Down Expand Up @@ -154,6 +154,9 @@ def check_device(use_gpu, use_xpu=False, use_npu=False, use_mlu=False):
if use_mlu and not paddle.device.is_compiled_with_mlu():
print(err.format("use_mlu", "mlu", "mlu", "use_mlu"))
sys.exit(1)
if use_gcu and not paddle.device.is_compiled_with_custom_device("gcu"):
print(err.format("use_gcu", "gcu", "gcu", "use_gcu"))
sys.exit(1)
except Exception as e:
pass

Expand Down Expand Up @@ -799,6 +802,7 @@ def preprocess(is_train=False):
use_xpu = config["Global"].get("use_xpu", False)
use_npu = config["Global"].get("use_npu", False)
use_mlu = config["Global"].get("use_mlu", False)
use_gcu = config["Global"].get("use_gcu", False)

alg = config["Architecture"]["algorithm"]
assert alg in [
Expand Down Expand Up @@ -853,9 +857,11 @@ def preprocess(is_train=False):
device = "npu:{0}".format(os.getenv("FLAGS_selected_npus", 0))
elif use_mlu:
device = "mlu:{0}".format(os.getenv("FLAGS_selected_mlus", 0))
elif use_gcu: # Use Enflame GCU(General Compute Unit)
device = "gcu:{0}".format(os.getenv("FLAGS_selected_gcus", 0))
else:
device = "gpu:{}".format(dist.ParallelEnv().dev_id) if use_gpu else "cpu"
check_device(use_gpu, use_xpu, use_npu, use_mlu)
check_device(use_gpu, use_xpu, use_npu, use_mlu, use_gcu)

device = paddle.set_device(device)

Expand Down