在国产芯片集群上,dtensor 如何与 HCCL 通信库对接?

解读

面试官真正想问的是:

  1. 你是否理解 Keras 3 在分布式场景下“设备无关”的设计思路;
  2. 你是否知道昇腾(Ascend)/海光等国产芯片的集合通信库 HCCL(Huawei Collective Communication Library)在 TensorFlow 2.x 生态里的位置;
  3. 你能否把 DTensor 的“通信后端插件”机制与 HCCL 的 CCL 接口桥接起来,让 Keras 模型在国产集群上零代码迁移且性能达标。
    一句话:不是问“HCCL 怎么用”,而是问“怎么让 Keras 的 DTensor 自动落到 HCCL 上”。

知识点

  1. DTensor 的架构

    • 前端:Python API,通过 dtensor.mesh 与 dtensor.call_with_layout 把张量切分逻辑与设备网格绑定。
    • 中间层:DTensor 的 C++ core 只认“通信运行时句柄”(Rendezvous),不依赖 NCCL/RCCL/HCCL 具体实现。
    • 后端:TensorFlow 的 CollectiveOps 在运行时根据设备类型选择 CollectiveExecutor。
  2. HCCL 在 TF 侧的现状

    • 华为官方提供的 libhccl.so 已包装成 tf_custom_collective 插件,注册名 HcclCollectiveExecutor。
    • 该插件通过 REGISTER_COLLECTIVE_EXECUTOR 宏把 HcclAllReduce、HcclBroadcast 等算子注入到 TF 的 CollectiveRegistry。
  3. DTensor 与 HCCL 的对接点

    • DTensor 在生成 CollectiveReduce 或 AllGather 算子时,调用 GetCollectiveExecutorHandle(device);
    • 若设备是 ASCEND:0,TF 会查到优先级最高的 HcclCollectiveExecutor,于是后续通信走 HCCL;
    • 用户层无需改 Keras 代码,只要保证进程环境变量 HCCL_WHITELIST=ALL 与 ASCEND_VISIBLE_DEVICES 正确即可。
  4. 集群启动细节(国产芯片场景常见)

    • 多机采用 hccl_controller 生成 ranktable.json,每个进程启动前 export HCCL_RANK_TABLE_FILE=ranktable.json;
    • DTensor 的 mesh 维度与 HCCL 的 world_size 必须一致,否则 HcclGroup 初始化会报 HCOM_E_NOT_IN_GROUP;
    • 若使用 Keras 3 的 tf.distribute.experimental.DTensorStrategy,需在 __init__ 里把 communication_options.implementation 显式设为 HCCL,避免 TF 默认选 NCCL(x86 环境残留)导致段错误。

答案

步骤一:确认 TF 已加载 HCCL 插件

python -c "from tensorflow.python.platform import _pywrap_mlir; print('hccl' in _pywrap_mlir.get_registered_collective_ops())"

输出 True 表示插件注册成功。

步骤二:写一份 Keras 3 模型,用 DTensor 做数据并行

import keras, tensorflow as tf, os
from keras.dtensor import dtensor_api as dt

# 1. 根据 ranktable.json 设置本地设备
os.environ['ASCEND_VISIBLE_DEVICES'] = '0'
os.environ['HCCL_RANK_TABLE_FILE']   = 'ranktable.json'

# 2. 构造 1-D 网格,对应 HCCL world
mesh = dt.Mesh.from_string('data=8')   # 8卡并行
layout = dt.Layout.batch_sharded(mesh, 'data', keras.backend.image_data_format())

# 3. 把模型权重、输入数据都转成 DTensor
inputs  = keras.Input(shape=(224,224,3))
x       = keras.layers.Conv2D(32,3, activation='relu')(inputs)
outputs = keras.layers.Dense(1000)(x)
model   = keras.Model(inputs, outputs)

# 4. 在 Strategy 作用域内编译,DTensor 自动选 HCCL
strategy = tf.distribute.experimental.DTensorStrategy(mesh=mesh,
                             communication_options=tf.distribute.experimental.CommunicationOptions(implementation='HCCL'))
with strategy.scope():
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
    model.fit(dataset.shard(...), epochs=10)

核心只有一行:把 implementation 设成 HCCL,DTensor 在底层就会调用 HcclCollectiveExecutor,完成梯度 AllReduce。

步骤三:启动脚本(以 2 机 8 卡为例)

# 节点 0
export HCCL_RANK=0
python train.py

# 节点 1
export HCCL_RANK=1
python train.py

HCCL 会自动建立 RoCE v2 或 PCIe 拓扑,DTensor 层透明。

拓展思考

  1. 若 HCCL 插件版本低于 TF 2.15,不支持 fp8 AllReduce,可在 DTensor 里通过 dt.reduction.RaiseToFp32 插入类型转换,保证数值稳定。
  2. 当集群规模 >128 卡时,HCCL 的 HCOM_GROUP 默认只建 8 个子组,需在 DTensorStrategy 里把 communication_options.params['group_size']='128' 调大,否则会出现 HCOM_E_GROUP_EXCEED。
  3. 未来 Keras 3 计划把 communication_options 暴露到 keras.distribute 命名空间,届时可直接 keras.distribute.set_communication('HCCL'),实现纯 Keras API 切换,无需再写 TF 的 DTensorStrategy。