
以下为本文档的中文说明该技能专门用于为PyTorch算子添加无符号整数uint类型支持通过更新AT_DISPATCH宏来实现。其核心问题是PyTorch中许多算子默认只支持有符号整数和有符号浮点类型当用户需要使用uint16、uint32、uint64类型时需要手动扩展这些算子的类型覆盖范围。此技能提供了一种系统化的方法来完成这项工作。使用场景包括为现有的算子添加uint类型支持、用户提到需要“unsigned types”或“uint support”时、启用kUInt16/kUInt32/kUInt64在内核中的支持、以及处理需要扩展类型覆盖范围的算子实现。技能的核心方法是在AT_DISPATCH_V2宏中添加AT_EXPAND(AT_ALL_TYPES_AND_UINT)或者使用AT_EXPAND(AT_UINT_TYPES)来专门添加无符号整数类型。技能还提供了两种扩展策略一种是在现有调度上追加uint类型另一种是为uint类型创建专门的分支调度。此外技能还涵盖了如何处理不同uint类型之间的转换、如何在CUDA内核中正确处理uint类型的数学运算以及如何编写测试来验证新添加的uint支持是否正确。该技能将PyTorch内部类型系统的复杂知识简化为一套清晰的步骤是PyTorch贡献者和内部开发者的实用工具。Add Unsigned Integer (uint) Support to OperatorsThis skill helps add support for unsigned integer types (uint16, uint32, uint64) to PyTorch operators by updating their AT_DISPATCH macros.When to use this skillUse this skill when:Adding uint16, uint32, or uint64 support to an operatorUser mentions “unsigned types”, “uint support”, “barebones unsigned types”Enabling support for kUInt16, kUInt32, kUInt64 in kernelsWorking with operator implementations that need expanded type coverageQuick referenceAdd unsigned types to existing dispatch:// BeforeAT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_ALL_TYPES));// After (method 1: add unsigned types explicitly)AT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_ALL_TYPES),AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES));// After (method 2: use V2 integral types if AT_INTEGRAL_TYPES present)AT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_INTEGRAL_TYPES_V2),AT_EXPAND(AT_FLOATING_TYPES));Type group referenceUnsigned type groups:AT_BAREBONES_UNSIGNED_TYPES: kUInt16, kUInt32, kUInt64AT_INTEGRAL_TYPES_V2: AT_INTEGRAL_TYPES AT_BAREBONES_UNSIGNED_TYPESRelationship:AT_INTEGRAL_TYPES// kByte, kChar, kInt, kLong, kShortAT_BAREBONES_UNSIGNED_TYPES// kUInt16, kUInt32, kUInt64AT_INTEGRAL_TYPES_V2// INTEGRAL_TYPES BAREBONES_UNSIGNED_TYPESInstructionsStep 1: Determine if conversion to V2 is neededCheck if the file uses AT_DISPATCH_V2:If using old AT_DISPATCH:First convert to AT_DISPATCH_V2 using the at-dispatch-v2 skillThen proceed with adding uint supportIf already using AT_DISPATCH_V2:Proceed directly to Step 2Step 2: Analyze the current dispatch macroIdentify what type groups are currently in use:AT_DISPATCH_V2(dtype,op,AT_WRAP([](){// body}),AT_EXPAND(AT_ALL_TYPES),kHalf,kBFloat16);^^^^^^^^^^^^^^^^^^^^^^^^^Current type coverageCommon patterns:AT_EXPAND(AT_ALL_TYPES)→ includes AT_INTEGRAL_TYPES AT_FLOATING_TYPESAT_EXPAND(AT_INTEGRAL_TYPES)→ signed integers onlyAT_EXPAND(AT_FLOATING_TYPES)→ floating point typesStep 3: Choose the uint addition methodTwo approaches:Method 1: Add AT_BAREBONES_UNSIGNED_TYPES explicitlyUse when: You want to be explicit about adding uint supportAddAT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES)to the type listMethod 2: Substitute AT_INTEGRAL_TYPES with AT_INTEGRAL_TYPES_V2Use when: The dispatch already usesAT_EXPAND(AT_INTEGRAL_TYPES)More concise: replaces one type group with its supersetOnly applicable if AT_INTEGRAL_TYPES is presentStep 4: Apply the transformationMethod 1 example:// BeforeAT_DISPATCH_V2(dtype,min_values_cuda,AT_WRAP([](){kernel_implscalar_t(iter);}),AT_EXPAND(AT_ALL_TYPES),kBFloat16,kHalf,kBool);// After (add unsigned types)AT_DISPATCH_V2(dtype,min_values_cuda,AT_WRAP([](){kernel_implscalar_t(iter);}),AT_EXPAND(AT_ALL_TYPES),AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES),kBFloat16,kHalf,kBool);Method 2 example:// BeforeAT_DISPATCH_V2(dtype,integral_op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_INTEGRAL_TYPES));// After (substitute with V2)AT_DISPATCH_V2(dtype,integral_op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_INTEGRAL_TYPES_V2));Step 5: Handle AT_ALL_TYPES vs individual type groupsIf the dispatch usesAT_EXPAND(AT_ALL_TYPES):AT_ALL_TYPESAT_INTEGRAL_TYPESAT_FLOATING_TYPESTo add uint: addAT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES)to the listIf the dispatch separately lists INTEGRAL and FLOATING:// BeforeAT_EXPAND(AT_INTEGRAL_TYPES),AT_EXPAND(AT_FLOATING_TYPES)// After (Method 2 preferred)AT_EXPAND(AT_INTEGRAL_TYPES_V2),AT_EXPAND(AT_FLOATING_ TYPES)Step 6: Verify all dispatch sitesCheck the file for ALL dispatch macros that need uint support:Some operators have multiple dispatch sites (CPU, CUDA, different functions)Apply the transformation consistently across all sitesEnsure each gets the same type coverage updatesStep 7: Validate the changesCheck that:AT_DISPATCH_V2 format is used (not old AT_DISPATCH)Unsigned types are added via one of the two methodsAll relevant dispatch sites in the file are updatedType groups useAT_EXPAND()Arguments are properly formatted and comma-separatedCommon patternsPattern 1: AT_ALL_TYPES extras// BeforeAT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_ALL_TYPES),kHalf,kBFloat16);// AfterAT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_ALL_TYPES),AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES),kHalf,kBFloat16);Pattern 2: Separate INTEGRAL FLOATING// BeforeAT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_INTEGRAL_TYPES),AT_EXPAND(AT_FLOATING_TYPES));// AfterAT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_INTEGRAL_TYPES_V2),AT_EXPAND(AT_FLOATING_TYPES));Pattern 3: Old dispatch needs conversion first// Before (needs v2 conversion first)AT_DISPATCH_ALL_TYPES_AND2(kHalf,kBFloat16,dtype,op,[](){kernelscalar_t();});// After v2 conversionAT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_ALL_TYPES),kHalf,kBFloat16);// After adding uint supportAT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_ALL_TYPES),AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES),kHalf,kBFloat16);Multiple dispatch sites exampleFor a file with multiple functions:voidmin_values_kernel_cuda(TensorIteratoriter){AT_DISPATCH_V2(iter.dtype(),min_values_cuda,AT_WRAP([](){implscalar_t(iter);}),AT_EXPAND(AT_ALL_TYPES),AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES),kBFloat16,kHalf);// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^// Added uint support}voidmin_launch_kernel(TensorIteratoriter){AT_DISPATCH_V2(iter.input_dtype(),min_cuda,AT_WRAP([](){gpu_reduce_kernelscalar_t(iter);}),AT_EXPAND(AT_ALL_TYPES),AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES),kBFloat16,kHalf);// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^// Added uint support here too}Decision treeUse this decision tree to determine the approach:Is the file using AT_DISPATCH_V2? ├─ No → Use at-dispatch-v2 skill first, then continue └─ Yes └─ Does it use AT_EXPAND(AT_INTEGRAL_TYPES)? ├─ Yes → Replace with AT_EXPAND(AT_INTEGRAL_TYPES_V2) └─ No → Add AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES) to type listEdge casesCase 1: Dispatch with only floating typesIf the operator only supports floating point types, don’t add uint support:// Leave as-is - floating point only operatorAT_DISPATCH_V2(dtype,float_op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_FLOATING_TYPES),kHalf);Case 2: Complex types presentUnsigned types work alongside complex types:AT_DISPATCH_V2(dtype,op,AT_WRAP([](){kernelscalar_t();}),AT_EXPAND(AT_ALL_TYPES),AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES),AT_EXPAND(AT_COMPLEX_TYPES),kHalf,kBFloat16);Case 3: Already has uint supportCheck if uint types are already present:IfAT_INTEGRAL_TYPES_V2is used → already has uint supportIfAT_BAREBONES_UNSIGNED_TYPESis already in list → already has uint supportSkip the file if uint support is already presentWorkflowWhen asked to add uint support:Read the target fileCheck if using AT_DISPATCH_V2:If not → use at-dispatch-v2 skill firstIdentify all dispatch macro sitesFor each dispatch:Analyze current type groupsChoose method (add BAREBONES_UNSIGNED or upgrade to V2)Apply transformation with Edit toolShow the user the changesExplain what was modifiedImportant notesAlways check if v2 conversion is needed firstApply changes consistently across all dispatch sites in the fileMethod 2 (AT_INTEGRAL_TYPES_V2) is cleaner when applicableMethod 1 (explicit AT_BAREBONES_UNSIGNED_TYPES) is more explicitUnsigned types are: kUInt16, kUInt32, kUInt64 (not kByte which is uint8)Some operators may not semantically support unsigned types - use judgmentTestingAfter adding uint support, the operator should accept uint16, uint32, and uint64 tensors. The user is responsible for functional testing.3e:[“,,,L41”,null,{“content”:“$42”,“frontMatter”:{“name”:“add-uint-support”,“description”:“Add unsigned integer (uint) type support to PyTorch operators by updating AT_DISPATCH macros. Use when adding support for uint16, uint32, uint64 types to operators, kernels, or when user mentions enabling unsigned types, barebones unsigned types, or uint support.”}}]3f:[“KaTeX parse error: Expected }, got EOF at end of input: …,children:[[”,“div”,null,{“className”:“flex items-center justify-between border-b border-border bg-muted/30 px-4 py-2.5”,“children”:[[“KaTeX parse error: Expected }, got EOF at end of input: …,children:[”,“span”,null,{“className”:“truncate text-xs font-medium text-muted-foreground”,“children”:“同仓库更多 Skills”}]}],[“KaTeX parse error: Expected EOF, got } at position 88: …ldren:同仓库}]]}̲],[”,“div”,null,{“className”:“p-4 sm:p-5”,“children”:[[“,h2,null,id:related−skills−heading,className:text−2xlfont−semiboldtracking−normaltext−foreground,children:同仓库更多Skills],[,h2,null,{id:related-skills-heading,className:text-2xl font-semibold tracking-normal text-foreground,children:同仓库更多 Skills}],[,h2,null,id:related−skills−heading,className:text−2xlfont−semiboldtracking−normaltext−foreground,children:同仓库更多Skills],[”,“div”,null,{“className”:“mt-4 grid gap-3 sm:grid-cols-2”,“children”:[“L43,L43,L43,L44”,“L45,L45,L45,L46”,“L47,L47,L47,L48”]}]]}]]}]49:I[206516,[“/_next/static/chunks/051aanbhrv4br.js”,“/_next/static/chunks/0mizr60h7ayzt.js”,“/_next/static/chunks/0v9lm1dmbdoo-.js”,“/_next/static/chunks/0rxr1j1j3j-.r.js”,“/_next/static/chunks/02ftybezfvqjd.js”,“/_next/static/chunks/0.v9ksvnnj8ia.js”,“/_next/static/chunks/0bn6id96nx3k.js,“/_next/static/chunks/13ybnhn37c.tc.js”,“/_next/static/chunks/0_fnrdtruz8uf.js”,“/_next/static/chunks/0r6l15utt1mwb.js”,“/_next/static/chunks/0dm9a5into854.js”,/_next/static/chunks/07k6hqoibtcn.js”,“/next/static/chunks/0b4cao.4y…j.js”,“/_next/static/chunks/02i-n28z7kjd0.js”],“default”]