在国产芯片集群上,dtensor 如何与 HCCL 通信库对接?
解读
面试官真正想问的是:
- 你是否理解 Keras 3 在分布式场景下“设备无关”的设计思路;
- 你是否知道昇腾(Ascend)/海光等国产芯片的集合通信库 HCCL(Huawei Collective Communication Library)在 TensorFlow 2.x 生态里的位置;
- 你能否把 DTensor 的“通信后端插件”机制与 HCCL 的 CCL 接口桥接起来,让 Keras 模型在国产集群上零代码迁移且性能达标。
一句话:不是问“HCCL 怎么用”,而是问“怎么让 Keras 的 DTensor 自动落到 HCCL 上”。
知识点
-
DTensor 的架构
- 前端:Python API,通过
dtensor.mesh与dtensor.call_with_layout把张量切分逻辑与设备网格绑定。 - 中间层:DTensor 的 C++ core 只认“通信运行时句柄”(
Rendezvous),不依赖 NCCL/RCCL/HCCL 具体实现。 - 后端:TensorFlow 的
CollectiveOps在运行时根据设备类型选择CollectiveExecutor。
- 前端:Python API,通过
-
HCCL 在 TF 侧的现状
- 华为官方提供的
libhccl.so已包装成tf_custom_collective插件,注册名HcclCollectiveExecutor。 - 该插件通过
REGISTER_COLLECTIVE_EXECUTOR宏把HcclAllReduce、HcclBroadcast等算子注入到 TF 的CollectiveRegistry。
- 华为官方提供的
-
DTensor 与 HCCL 的对接点
- DTensor 在生成
CollectiveReduce或AllGather算子时,调用GetCollectiveExecutorHandle(device); - 若设备是
ASCEND:0,TF 会查到优先级最高的HcclCollectiveExecutor,于是后续通信走 HCCL; - 用户层无需改 Keras 代码,只要保证进程环境变量
HCCL_WHITELIST=ALL与ASCEND_VISIBLE_DEVICES正确即可。
- DTensor 在生成
-
集群启动细节(国产芯片场景常见)
- 多机采用
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 层透明。
拓展思考
- 若 HCCL 插件版本低于 TF 2.15,不支持
fp8AllReduce,可在 DTensor 里通过dt.reduction.RaiseToFp32插入类型转换,保证数值稳定。 - 当集群规模 >128 卡时,HCCL 的
HCOM_GROUP默认只建 8 个子组,需在DTensorStrategy里把communication_options.params['group_size']='128'调大,否则会出现HCOM_E_GROUP_EXCEED。 - 未来 Keras 3 计划把
communication_options暴露到keras.distribute命名空间,届时可直接keras.distribute.set_communication('HCCL'),实现纯 Keras API 切换,无需再写 TF 的DTensorStrategy。