CANN/cannbot-skills动态张量列表构造方案

发布时间:2026/7/4 8:16:14
CANN/cannbot-skills动态张量列表构造方案 DYNAMIC TensorList 构造方案【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills适用条件当 input/output 的 param_type 为 DYNAMIC 时子 agent 按需读取本文件。与 02 的关系本文件描述 DYNAMIC 专属的构造/校验/diversity 逻辑。通用步骤dtype 解析、属性采样、const input、编排代码见 02-step5a-mapper.md本文件不重复。空 tensor 处理已移至 5c见 04-step5c-merge-expand.md。1. 数据来源DYNAMIC 构造的全部约束来自S2P1_operator_model.json的对应字段子 agent 不得从源码重新推断信息operator_model 字段tensor count 控制方式inputs[*].tensor_countdtype 合法值inputs[*].dtype.valuesndim 范围inputs[*].rank.min / rank.max列表级 shape 约束inputs[*].shape.constraints输出 count 推导outputs[*].tensor_count.derived_from输出 shape 规则outputs[*].shape.ruleshape.input输出 dtype 来源outputs[*].dtype.sourcedtype.input输出 rank 来源outputs[*].rank.sourcerank.input2. 输入构造2.1 tensor_count 生成读operator_model.inputs[*].tensor_count按两种模式处理硬件约束Ascend C DYNAMIC TensorList 最多容纳 50 个子 tensor。若operator_model中tensor_count.max 50实际采样上限取min(max, 50)。模式 Aparam 控制有 param/min/max 字段count 范围 [min, min(max, 50)]。优先从 case dict 读取网络用例预设值否则由rng.randint(min, min(max, 50))生成。跨 case 覆盖由 §4 diversity check 监督。if {name}_count in case: {name}_count case[{name}_count] else: {name}_count rng.randint({min}, min({max}, 50))模式 Bderived_from有 derived_from 字段{name}_count {target}_count # 直接赋值不随机2.2 逐子 tensor ndim 生成{name}_ndims [rng.randint({rank_min}, {rank_max}) for _ in range({name}_count)]各子 tensor 的 ndim 可独立选择除非有 same_rank_within_list 约束。2.3 逐子 tensor shape 构造约束各维度 0单 tensor numel 10,000,000维度间比值 32totalElements 不超过 tiling 约束上限从 S5_mapping_spec.md 读取。若有 shape 构造参数用_decompose分解。若无 shape 构造参数按以下算法动态生成。# {name}无 shape 构造参数动态生成 # 约束numel MAX_NUMEL各维度 2维度间比值 RATIO_MAX MAX_NUMEL 10_000_000 RATIO_MAX 32 {name}_shapes [] for i in range({name}_count): ndim {name}_ndims[i] if ndim 0: {name}_shapes.append(()) # 标量 tensor else: target rng.randint(2 ** ndim, MAX_NUMEL) gm int(target ** (1.0 / ndim)) lo max(2, gm // int(RATIO_MAX ** 0.5)) hi max(lo 1, min(gm * int(RATIO_MAX ** 0.5), MAX_NUMEL)) dims [rng.randint(lo, hi) for _ in range(ndim)] rng.shuffle(dims) prod math.prod(dims) if prod MAX_NUMEL: scale (MAX_NUMEL / prod) ** (1.0 / ndim) dims [max(2, int(d * scale)) for d in dims] while math.prod(dims) MAX_NUMEL: idx dims.index(max(dims)) dims[idx] max(2, dims[idx] - 1) {name}_shapes.append(tuple(dims))2.4 列表级约束处理读operator_model.inputs[*].shape.constraints逐条处理constraint type含义处理方式same_dtype_within_list列表内所有子 tensor dtype 相同使用同一个 dtype_val步骤 1 已解析天然满足same_shape_within_list列表内所有子 tensor shape 相同生成 1 个 shape所有子 tensor 复用sync_with与另一个 DYNAMIC 输入同步count/shape/dtype 复制目标输入的值{name}_count {target}_count {name}_shapes list({target}_shapes) {name}_dtype {target}_dtype2.5 组装格式DYNAMIC 输入的值为list[dict]每个 dict 描述一个子 tensor# DYNAMIC 输入组装 inputs[{name}] [ {shape: {name}_shapes[i], dtype: dtype_val} for i in range({name}_count) ]3. L1 输入校验逐 DYNAMIC 输入校验每条约束翻译为 assert 风格检查# tensor_count 范围param 模式 if not ({min} len(t[inputs][{name}]) min({max}, 50)): errors.append(f{name} count {len(...)} out of [{min}, {max}]) # 逐子 tensor ndim 范围 for i, sub in enumerate(t[inputs][{name}]): if not ({rank_min} len(sub[shape]) {rank_max}): errors.append(f{name}[{i}] ndim {len(sub[shape])} out of [{rank_min}, {rank_max}]) # same_dtype_within_list 约束 dtypes set(sub[dtype] for sub in t[inputs][{name}]) if len(dtypes) 1: errors.append(f{name} has inconsistent dtypes: {dtypes}) # sync_with 约束若有 if len(t[inputs][{name}]) ! len(t[inputs][{sync_target}]): errors.append(f{name} count ! {sync_target} count)输出校验见 02-step5a-mapper.md §6 输出校验节通用不按 param_type 派发。4. diversity check4.1 检查项DYNAMIC 输入需检查两项覆盖率检查项范围来源覆盖要求tensor_count 覆盖率operator_model tensor_count.min / min(max, 50)min 和 min(max, 50) 至少各出现 1 次逐子 tensor ndim 覆盖率operator_model rank.min/max[min, max] 内每个值至少出现 1 次4.2 代码模板counts [len(c[tensors][inputs][{name}]) for c in mapped_cases] count_min, count_max count_range[{name}] missing_counts [v for v in range(count_min, count_max 1) if v not in counts] if missing_counts: print(f[DIVERSITY] {name} tensor_count: MISSING {missing_counts}) all_ndims [len(sub[shape]) for c in mapped_cases for sub in c[tensors][inputs][{name}]] rank_min, rank_max rank_range[{name}] missing_ndims [v for v in range(rank_min, rank_max 1) if v not in all_ndims] if missing_ndims: print(f[DIVERSITY] {name} ndim: MISSING {missing_ndims})sync_with 输入标注sync_with {target} (skipped)不单独报 MISSING。【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考