Skip to content
Open
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
16 changes: 0 additions & 16 deletions paddlex/inference/models/common/static_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,6 @@ def _create(
# TODO(changdazhou): use a black list instead
if self._model_name == "PP-DocLayoutV3":
config.delete_pass("matmul_add_act_fuse_pass")
# ROCm does not support fused_conv2d_add_act kernel, delete the fuse passes
if paddle.is_compiled_with_rocm():
config.delete_pass("conv2d_add_act_fuse_pass")
config.delete_pass("conv2d_add_fuse_pass")
elif self._option.device_type == "npu":
config.enable_custom_device("npu", self._option.device_id)
if hasattr(config, "enable_new_ir"):
Expand Down Expand Up @@ -457,11 +453,6 @@ def _create(
config.disable_mkldnn()
if hasattr(config, "enable_new_executor"):
config.enable_new_executor()
# XXX: is_compiled_with_rocm() must be True on dcu platform ?
if paddle.is_compiled_with_rocm():
# Delete unsupported passes in dcu
config.delete_pass("conv2d_add_act_fuse_pass")
config.delete_pass("conv2d_add_fuse_pass")
elif self._option.device_type == "iluvatar_gpu":
config.enable_custom_device("iluvatar_gpu", int(self._option.device_id))
if hasattr(config, "enable_new_ir"):
Expand All @@ -486,20 +477,13 @@ def _create(
if hasattr(config, "enable_new_executor"):
config.enable_new_executor()
config.set_optimization_level(3)
if paddle.is_compiled_with_rocm():
config.delete_pass("conv2d_add_act_fuse_pass")
config.delete_pass("conv2d_add_fuse_pass")
config.enable_memory_optim()
for del_p in self._option.delete_pass:
config.delete_pass(del_p)

# Disable paddle inference logging
if not DEBUG:
config.disable_glog_info()
# ROCm does not support fused_conv2d_add_act kernel, delete the fuse passes
if paddle.is_compiled_with_rocm():
config.delete_pass("conv2d_add_act_fuse_pass")
config.delete_pass("conv2d_add_fuse_pass")

predictor = paddle.inference.create_predictor(config)

Expand Down
3 changes: 3 additions & 0 deletions paddlex/inference/models/common/transformers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def device_guard(device="cpu", dev_id=0):
paddle.set_device(device)
elif device in ["gpu", "xpu", "npu"]:
paddle.set_device("{}:{}".format(device, dev_id))
elif device == "dcu":
# DCU/ROCm: Paddle does not accept "dcu" as device name; use "gpu" instead
paddle.set_device("gpu:{}".format(dev_id))
try:
yield
finally:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@
import paddle
import paddle.nn as nn

# ROCm compatibility: Paddle HIP wheel (<=3.4.0.dev20260408) did not register
# phi::bfloat16 for the layer_norm kernel. This shim casts through FP32 on ROCm.
# Remove once PaddlePaddle/Paddle PR (add bfloat16 to HIP layer_norm kernel) merges.
if paddle.is_compiled_with_rocm():
import paddle.nn.functional as _F_compat
_orig_ln_fwd = paddle.nn.LayerNorm.forward
def _rocm_bf16_safe_ln_fwd(self, input):
if input.dtype == paddle.bfloat16:
x32 = paddle.cast(input, paddle.float32)
w = getattr(self, 'weight', None)
b = getattr(self, 'bias', None)
w32 = paddle.cast(w, paddle.float32) if w is not None else None
b32 = paddle.cast(b, paddle.float32) if b is not None else None
out = _F_compat.layer_norm(x32, self._normalized_shape, w32, b32, self._epsilon)
return paddle.cast(out, paddle.bfloat16)
return _orig_ln_fwd(self, input)
paddle.nn.LayerNorm.forward = _rocm_bf16_safe_ln_fwd
del _F_compat, _orig_ln_fwd, _rocm_bf16_safe_ln_fwd

from ....common.transformers.transformers.model_outputs import (
CausalLMOutputWithCrossAttentions,
ModelOutput,
Expand All @@ -65,9 +84,6 @@ class PaddleOCRVLForConditionalGeneration(Ernie4_5PretrainedModel):
_tied_weights_keys = ["lm_head.weight"]
config_class = PaddleOCRVLConfig
_no_split_modules = ["Ernie4_5DecoderLayer", "SiglipEncoderLayer"]
# Keep visual encoder in fp32 for ROCm stability (MIOpen bf16 conv has bugs)
# This also improves precision for vision processing
_keep_in_fp32_modules = ["visual", "mlp_AR"]
base_model_prefix = ""

def __init__(self, config):
Expand Down
2 changes: 1 addition & 1 deletion paddlex/inference/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def is_bfloat16_available(device):
device_type, _ = parse_device(device)
return (
"npu" in get_device_type() or paddle.amp.is_bfloat16_supported()
) and device_type in ("gpu", "npu", "xpu", "mlu", "metax_gpu", "iluvatar_gpu")
) and device_type in ("gpu", "dcu", "npu", "xpu", "mlu", "metax_gpu", "iluvatar_gpu")


def is_float16_available(device):
Expand Down