nbxtensor 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nbxtensor/THIRD_PARTY_NOTICES.md +287 -0
- nbxtensor/__init__.py +17 -0
- nbxtensor/backend/__init__.py +1 -0
- nbxtensor/backend/capabilities.py +207 -0
- nbxtensor/backend/cpu/__init__.py +1 -0
- nbxtensor/backend/cpu/blas.py +102 -0
- nbxtensor/backend/cpu/pool.py +204 -0
- nbxtensor/backend/cpu_backend.py +162 -0
- nbxtensor/backend/device_facts.py +105 -0
- nbxtensor/backend/device_files.py +129 -0
- nbxtensor/backend/device_names.py +18 -0
- nbxtensor/backend/devices/amd/cdna.yml +228 -0
- nbxtensor/backend/devices/amd/cdna2.yml +228 -0
- nbxtensor/backend/devices/amd/cdna3.yml +227 -0
- nbxtensor/backend/devices/apple/apple_m1.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m1_max.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m1_pro.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m1_ultra.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m2.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m2_max.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m2_pro.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m2_ultra.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m3.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m3_max.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m3_pro.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m3_ultra.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m4.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m4_max.yml +266 -0
- nbxtensor/backend/devices/apple/apple_m4_pro.yml +308 -0
- nbxtensor/backend/devices/apple/apple_m5.yml +269 -0
- nbxtensor/backend/devices/apple/apple_m5_max.yml +269 -0
- nbxtensor/backend/devices/apple/apple_m5_pro.yml +269 -0
- nbxtensor/backend/devices/apple/apple_m5_ultra.yml +269 -0
- nbxtensor/backend/devices/apple/apple_m6.yml +269 -0
- nbxtensor/backend/devices/apple/apple_silicon.yml +263 -0
- nbxtensor/backend/devices/nvidia/ampere.yml +214 -0
- nbxtensor/backend/devices/nvidia/hopper.yml +213 -0
- nbxtensor/backend/devices/nvidia/volta.yml +411 -0
- nbxtensor/backend/faults.py +151 -0
- nbxtensor/backend/host_memory.py +185 -0
- nbxtensor/backend/launcher.py +2350 -0
- nbxtensor/backend/launcher_contract.py +429 -0
- nbxtensor/backend/metal_backend.py +661 -0
- nbxtensor/backend/metal_device.py +1274 -0
- nbxtensor/backend/process_memory.py +73 -0
- nbxtensor/backend/replay_graph.py +322 -0
- nbxtensor/backend/runtime.py +409 -0
- nbxtensor/backend/triton_ext_driver.py +735 -0
- nbxtensor/dtype/__init__.py +1 -0
- nbxtensor/dtype/convert.py +107 -0
- nbxtensor/dtype/dtypes.yml +70 -0
- nbxtensor/dtype/itemsize.py +166 -0
- nbxtensor/dtype/nbx_dtype.py +72 -0
- nbxtensor/dtype/operands.py +158 -0
- nbxtensor/dtype/outputs.py +64 -0
- nbxtensor/dtype/policy.py +155 -0
- nbxtensor/dtype/policy.yml +51 -0
- nbxtensor/dtype/promotion.py +112 -0
- nbxtensor/host.py +55 -0
- nbxtensor/io/__init__.py +1 -0
- nbxtensor/io/read_rate.py +197 -0
- nbxtensor/io/safetensors.py +314 -0
- nbxtensor/memory/__init__.py +9 -0
- nbxtensor/memory/allocator.py +1988 -0
- nbxtensor/memory/arena.py +75 -0
- nbxtensor/memory/device_memory.py +150 -0
- nbxtensor/memory/device_transfer.py +154 -0
- nbxtensor/memory/layout.py +43 -0
- nbxtensor/memory/memory_pool.py +191 -0
- nbxtensor/memory/metal.py +96 -0
- nbxtensor/memory/oom_advice.py +91 -0
- nbxtensor/memory/pointer_tables.py +323 -0
- nbxtensor/memory/pool.py +507 -0
- nbxtensor/memory/shadow.py +43 -0
- nbxtensor/memory/slab.py +273 -0
- nbxtensor/memory/trace_replay.py +122 -0
- nbxtensor/memory/traces/sana-video-2b-720p.16g.2026-11-02.tsv +13610 -0
- nbxtensor/memory/vmm.py +118 -0
- nbxtensor/ops/__init__.py +1 -0
- nbxtensor/ops/_autotune_policy.py +90 -0
- nbxtensor/ops/_common.py +32 -0
- nbxtensor/ops/_configs.py +1041 -0
- nbxtensor/ops/_cost.py +231 -0
- nbxtensor/ops/abs.py +17 -0
- nbxtensor/ops/adaptive_avg_pool2d.py +161 -0
- nbxtensor/ops/add.py +96 -0
- nbxtensor/ops/addcdiv.py +29 -0
- nbxtensor/ops/addcmul.py +29 -0
- nbxtensor/ops/addmv_op.py +64 -0
- nbxtensor/ops/addr.py +68 -0
- nbxtensor/ops/all_reduce.py +83 -0
- nbxtensor/ops/amax.py +40 -0
- nbxtensor/ops/any_reduce.py +83 -0
- nbxtensor/ops/arange_op.py +27 -0
- nbxtensor/ops/argmax.py +107 -0
- nbxtensor/ops/argmin.py +93 -0
- nbxtensor/ops/atan2.py +71 -0
- nbxtensor/ops/attention_wrappers.py +1648 -0
- nbxtensor/ops/autotune_bucket.py +137 -0
- nbxtensor/ops/autotune_cache.py +430 -0
- nbxtensor/ops/autotune_certified.py +1262 -0
- nbxtensor/ops/autotune_refusals.py +380 -0
- nbxtensor/ops/autotune_settings.py +58 -0
- nbxtensor/ops/avg_pool2d.py +73 -0
- nbxtensor/ops/baddbmm_op.py +132 -0
- nbxtensor/ops/batch_norm.py +163 -0
- nbxtensor/ops/binary_strided.py +196 -0
- nbxtensor/ops/bitwise_and.py +26 -0
- nbxtensor/ops/bitwise_left_shift.py +47 -0
- nbxtensor/ops/bitwise_not.py +24 -0
- nbxtensor/ops/bitwise_or.py +26 -0
- nbxtensor/ops/bitwise_right_shift.py +47 -0
- nbxtensor/ops/bucketize.py +37 -0
- nbxtensor/ops/cat_op.py +87 -0
- nbxtensor/ops/celu.py +20 -0
- nbxtensor/ops/clamp.py +24 -0
- nbxtensor/ops/clamp_min.py +22 -0
- nbxtensor/ops/conv2d.py +265 -0
- nbxtensor/ops/conv2d_m8n8k4.py +165 -0
- nbxtensor/ops/conv_depthwise2d.py +154 -0
- nbxtensor/ops/conv_transpose2d.py +148 -0
- nbxtensor/ops/conv_wrappers.py +1482 -0
- nbxtensor/ops/copy.py +18 -0
- nbxtensor/ops/copy_op.py +39 -0
- nbxtensor/ops/cos.py +17 -0
- nbxtensor/ops/count_nonzero.py +64 -0
- nbxtensor/ops/cross_entropy.py +67 -0
- nbxtensor/ops/cummax.py +109 -0
- nbxtensor/ops/cummin.py +105 -0
- nbxtensor/ops/cumsum.py +244 -0
- nbxtensor/ops/decode_attn_vec.py +162 -0
- nbxtensor/ops/depthwise_conv2d.py +132 -0
- nbxtensor/ops/dequant_gemv.py +190 -0
- nbxtensor/ops/dft.py +67 -0
- nbxtensor/ops/dispatch.py +1228 -0
- nbxtensor/ops/div.py +75 -0
- nbxtensor/ops/dot_op.py +67 -0
- nbxtensor/ops/dropout.py +23 -0
- nbxtensor/ops/dtype_convert.py +42 -0
- nbxtensor/ops/elementwise_launch.py +81 -0
- nbxtensor/ops/elementwise_wrappers.py +2315 -0
- nbxtensor/ops/elu.py +20 -0
- nbxtensor/ops/embedding.py +69 -0
- nbxtensor/ops/eq.py +32 -0
- nbxtensor/ops/erf.py +18 -0
- nbxtensor/ops/exp.py +17 -0
- nbxtensor/ops/exp2.py +18 -0
- nbxtensor/ops/fft_op.py +125 -0
- nbxtensor/ops/fill_op.py +20 -0
- nbxtensor/ops/flash_attention.py +307 -0
- nbxtensor/ops/flash_attention_m8n8k4.py +384 -0
- nbxtensor/ops/flash_decode.py +78 -0
- nbxtensor/ops/flip_op.py +60 -0
- nbxtensor/ops/floor.py +90 -0
- nbxtensor/ops/full_op.py +24 -0
- nbxtensor/ops/fused_moe.py +417 -0
- nbxtensor/ops/fused_moe_m8n8k4.py +99 -0
- nbxtensor/ops/fused_upsample_conv.py +413 -0
- nbxtensor/ops/gather_op.py +52 -0
- nbxtensor/ops/ge.py +32 -0
- nbxtensor/ops/gelu.py +31 -0
- nbxtensor/ops/gemv_vec.py +80 -0
- nbxtensor/ops/glu.py +43 -0
- nbxtensor/ops/grid_sampler.py +145 -0
- nbxtensor/ops/group_norm_tiled.py +67 -0
- nbxtensor/ops/groupnorm.py +123 -0
- nbxtensor/ops/gt.py +32 -0
- nbxtensor/ops/hardsigmoid.py +18 -0
- nbxtensor/ops/hardswish.py +19 -0
- nbxtensor/ops/im2col.py +57 -0
- nbxtensor/ops/index_add.py +95 -0
- nbxtensor/ops/index_put_op.py +92 -0
- nbxtensor/ops/index_select.py +121 -0
- nbxtensor/ops/index_wrappers.py +770 -0
- nbxtensor/ops/isfinite.py +20 -0
- nbxtensor/ops/isinf.py +20 -0
- nbxtensor/ops/isnan.py +18 -0
- nbxtensor/ops/launch_keys.py +1054 -0
- nbxtensor/ops/launch_observer.py +54 -0
- nbxtensor/ops/layernorm.py +71 -0
- nbxtensor/ops/le.py +35 -0
- nbxtensor/ops/leaky_relu.py +19 -0
- nbxtensor/ops/lerp.py +76 -0
- nbxtensor/ops/linalg_wrappers.py +1370 -0
- nbxtensor/ops/linspace_op.py +47 -0
- nbxtensor/ops/log.py +17 -0
- nbxtensor/ops/log_sigmoid.py +23 -0
- nbxtensor/ops/logical_and.py +26 -0
- nbxtensor/ops/logical_not.py +24 -0
- nbxtensor/ops/logical_or.py +26 -0
- nbxtensor/ops/logical_xor.py +37 -0
- nbxtensor/ops/loss_wrappers.py +282 -0
- nbxtensor/ops/lt.py +32 -0
- nbxtensor/ops/masked_fill.py +21 -0
- nbxtensor/ops/matmul.py +377 -0
- nbxtensor/ops/matmul_m8n8k4.py +235 -0
- nbxtensor/ops/max_pool2d.py +74 -0
- nbxtensor/ops/max_pool3d.py +93 -0
- nbxtensor/ops/max_reduce.py +91 -0
- nbxtensor/ops/maximum.py +18 -0
- nbxtensor/ops/mean.py +40 -0
- nbxtensor/ops/min_reduce.py +91 -0
- nbxtensor/ops/minimum.py +18 -0
- nbxtensor/ops/mish.py +22 -0
- nbxtensor/ops/mma_m8n8k4.py +152 -0
- nbxtensor/ops/moe_align.py +195 -0
- nbxtensor/ops/moe_decode_vec.py +374 -0
- nbxtensor/ops/moe_device_residency.py +141 -0
- nbxtensor/ops/moe_wrappers.py +345 -0
- nbxtensor/ops/mse_loss.py +79 -0
- nbxtensor/ops/mul.py +51 -0
- nbxtensor/ops/nan_to_num.py +23 -0
- nbxtensor/ops/ne.py +32 -0
- nbxtensor/ops/neg.py +17 -0
- nbxtensor/ops/nllloss.py +124 -0
- nbxtensor/ops/nonzero.py +33 -0
- nbxtensor/ops/nonzero_op.py +62 -0
- nbxtensor/ops/norm_wrappers.py +579 -0
- nbxtensor/ops/ones_op.py +20 -0
- nbxtensor/ops/oracles/__init__.py +6 -0
- nbxtensor/ops/oracles/conv2d_fp64.py +450 -0
- nbxtensor/ops/pad_op.py +159 -0
- nbxtensor/ops/paged_decode_attn.py +215 -0
- nbxtensor/ops/pixel_shuffle.py +144 -0
- nbxtensor/ops/pixel_unshuffle.py +72 -0
- nbxtensor/ops/pow.py +137 -0
- nbxtensor/ops/prod.py +81 -0
- nbxtensor/ops/rand_op.py +52 -0
- nbxtensor/ops/random_wrappers.py +366 -0
- nbxtensor/ops/reciprocal.py +17 -0
- nbxtensor/ops/reduction_wrappers.py +845 -0
- nbxtensor/ops/relu.py +18 -0
- nbxtensor/ops/remainder.py +46 -0
- nbxtensor/ops/repeat_interleave.py +48 -0
- nbxtensor/ops/repeat_op.py +132 -0
- nbxtensor/ops/residual_chain.py +374 -0
- nbxtensor/ops/rmsnorm.py +68 -0
- nbxtensor/ops/rope.py +142 -0
- nbxtensor/ops/rsqrt.py +17 -0
- nbxtensor/ops/scaled_softmax.py +96 -0
- nbxtensor/ops/scatter_op.py +139 -0
- nbxtensor/ops/screen_oracle.py +479 -0
- nbxtensor/ops/selu.py +22 -0
- nbxtensor/ops/shape_wrappers.py +1032 -0
- nbxtensor/ops/sigmoid.py +19 -0
- nbxtensor/ops/silu.py +27 -0
- nbxtensor/ops/sin.py +17 -0
- nbxtensor/ops/softmax.py +169 -0
- nbxtensor/ops/softplus.py +27 -0
- nbxtensor/ops/sort_op.py +304 -0
- nbxtensor/ops/spectral_wrappers.py +481 -0
- nbxtensor/ops/sqrt.py +17 -0
- nbxtensor/ops/stack_op.py +84 -0
- nbxtensor/ops/std.py +113 -0
- nbxtensor/ops/strided_copy.py +200 -0
- nbxtensor/ops/sub.py +35 -0
- nbxtensor/ops/sum.py +40 -0
- nbxtensor/ops/tan.py +19 -0
- nbxtensor/ops/tanh.py +21 -0
- nbxtensor/ops/threshold.py +19 -0
- nbxtensor/ops/tiling.yml +91 -0
- nbxtensor/ops/tiling_policy.py +43 -0
- nbxtensor/ops/tiling_sizes.py +370 -0
- nbxtensor/ops/topk.py +267 -0
- nbxtensor/ops/trace_op.py +64 -0
- nbxtensor/ops/tril.py +64 -0
- nbxtensor/ops/triu.py +62 -0
- nbxtensor/ops/unfold_op.py +37 -0
- nbxtensor/ops/unique_op.py +97 -0
- nbxtensor/ops/upsample_bilinear2d.py +106 -0
- nbxtensor/ops/upsample_nearest2d.py +52 -0
- nbxtensor/ops/upsample_nearest3d.py +61 -0
- nbxtensor/ops/var.py +136 -0
- nbxtensor/ops/vector_norm.py +216 -0
- nbxtensor/ops/weight_norm.py +104 -0
- nbxtensor/ops/where.py +39 -0
- nbxtensor/ops/wrappers.py +825 -0
- nbxtensor/ops/zeros_op.py +20 -0
- nbxtensor/quant/__init__.py +1 -0
- nbxtensor/quant/quantized_tensor.py +187 -0
- nbxtensor/random/__init__.py +1 -0
- nbxtensor/random/mt_randn.py +154 -0
- nbxtensor/random/rng_pin.py +59 -0
- nbxtensor/random/rng_stream.py +118 -0
- nbxtensor/random/seeded_draw.py +223 -0
- nbxtensor/shape/__init__.py +157 -0
- nbxtensor/tensor.py +1867 -0
- nbxtensor-0.0.1.dist-info/METADATA +379 -0
- nbxtensor-0.0.1.dist-info/RECORD +291 -0
- nbxtensor-0.0.1.dist-info/WHEEL +4 -0
- nbxtensor-0.0.1.dist-info/licenses/LICENSE +189 -0
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
"""Normalisation wrappers: layer, RMS, group and batch norm, weight norm, vector norms. Cut out of wrappers.py by family (M2, 2026-10-29), unchanged; shared launch layer and run context stay in wrappers.py."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import triton
|
|
7
|
+
from nbxtensor.ops.autotune_bucket import bucket_of as _bucket_of
|
|
8
|
+
from nbxtensor.ops import launch_keys as _lk
|
|
9
|
+
from nbxtensor.tensor import NBXTensor
|
|
10
|
+
from nbxtensor.dtype.nbx_dtype import NBXDtype, dtype_size
|
|
11
|
+
from nbxtensor.memory.allocator import DeviceAllocator
|
|
12
|
+
from nbxtensor.backend.runtime import set_device
|
|
13
|
+
from nbxtensor.shape import MAX_NDIM as _NBX_MAX_NDIM, broadcast_shapes, max_linear_offset
|
|
14
|
+
from nbxtensor.dtype.itemsize import canonical_name
|
|
15
|
+
from nbxtensor.dtype.promotion import binary_result, float_math_input
|
|
16
|
+
from nbxtensor.dtype.nbx_dtype import parse_dtype
|
|
17
|
+
from nbxtensor.memory.allocator import DeviceOOMError
|
|
18
|
+
from nbxtensor.backend.capabilities import fa_min_tile as _fa_min_tile
|
|
19
|
+
from nbxtensor.backend.capabilities import reduce_tile_max as _reduce_tile_max
|
|
20
|
+
from nbxtensor.backend.faults import device_fault_buffer, device_fault_code_cached, fault_channel
|
|
21
|
+
from nbxtensor.tensor import _upload_int64_array as _nbx_upload_int64, _saturating_cast as _nbx_saturating_cast
|
|
22
|
+
from nbxtensor.ops._configs import sdpa_block_ceiling as _sdpa_block_ceiling
|
|
23
|
+
from nbxtensor.ops._configs import sdpa_launch_meta as _sdpa_launch_meta
|
|
24
|
+
from nbxtensor.ops._configs import matrix_unit as _matrix_unit, matrix_unit_mm_tile as _matrix_unit_mm_tile
|
|
25
|
+
from nbxtensor.ops._configs import largest_tile_within_smem as _largest_tile_within_smem
|
|
26
|
+
from nbxtensor.backend import launcher as _nbx_launcher
|
|
27
|
+
from nbxtensor.backend.launcher import install as _install_launcher
|
|
28
|
+
from nbxtensor.ops.relu import relu_forward_kernel
|
|
29
|
+
from nbxtensor.ops.silu import silu_forward_kernel
|
|
30
|
+
from nbxtensor.ops.gelu import gelu_forward_kernel
|
|
31
|
+
from nbxtensor.ops.sigmoid import sigmoid_forward_kernel
|
|
32
|
+
from nbxtensor.ops.tanh import tanh_forward_kernel
|
|
33
|
+
from nbxtensor.ops.hardsigmoid import hardsigmoid_forward_kernel
|
|
34
|
+
from nbxtensor.ops.hardswish import hardswish_forward_kernel
|
|
35
|
+
from nbxtensor.ops.leaky_relu import leaky_relu_forward_kernel
|
|
36
|
+
from nbxtensor.ops.elu import elu_forward_kernel
|
|
37
|
+
from nbxtensor.ops.mish import mish_forward_kernel
|
|
38
|
+
from nbxtensor.ops.selu import selu_forward_kernel
|
|
39
|
+
from nbxtensor.ops.neg import neg_forward_kernel
|
|
40
|
+
from nbxtensor.ops.exp import exp_forward_kernel
|
|
41
|
+
from nbxtensor.ops.sin import sin_forward_kernel
|
|
42
|
+
from nbxtensor.ops.cos import cos_forward_kernel
|
|
43
|
+
from nbxtensor.ops.rsqrt import rsqrt_forward_kernel
|
|
44
|
+
from nbxtensor.ops.sqrt import sqrt_forward_kernel
|
|
45
|
+
from nbxtensor.ops.abs import abs_forward_kernel
|
|
46
|
+
from nbxtensor.ops.log import log_forward_kernel
|
|
47
|
+
from nbxtensor.ops.reciprocal import reciprocal_forward_kernel
|
|
48
|
+
from nbxtensor.ops.pow import pow_forward_kernel
|
|
49
|
+
from nbxtensor.ops.clamp import clamp_forward_kernel
|
|
50
|
+
from nbxtensor.ops.erf import erf_forward_kernel
|
|
51
|
+
from nbxtensor.ops.copy import copy_forward_kernel
|
|
52
|
+
from nbxtensor.ops.add import add_forward_kernel, add_scalar_kernel, add_scalar_dev_kernel, add_bias_broadcast_kernel
|
|
53
|
+
from nbxtensor.ops.binary_strided import add_strided_nd_kernel, mul_strided_nd_kernel, sub_strided_nd_kernel, div_strided_nd_kernel, add_scalar_strided_nd_kernel, mul_scalar_strided_nd_kernel
|
|
54
|
+
from nbxtensor.ops.mul import mul_forward_kernel, mul_scalar_kernel, mul_scalar_dev_kernel
|
|
55
|
+
from nbxtensor.ops.div import div_forward_kernel, div_scalar_kernel, div_scalar_dev_kernel, int_div_kernel
|
|
56
|
+
from nbxtensor.ops.sub import sub_forward_kernel, rsub_forward_kernel
|
|
57
|
+
from nbxtensor.ops.where import where_forward_kernel
|
|
58
|
+
from nbxtensor.ops.maximum import maximum_forward_kernel
|
|
59
|
+
from nbxtensor.ops.minimum import minimum_forward_kernel
|
|
60
|
+
from nbxtensor.ops.masked_fill import masked_fill_forward_kernel
|
|
61
|
+
from nbxtensor.ops.gt import gt_forward_kernel, gt_scalar_kernel
|
|
62
|
+
from nbxtensor.ops.ge import ge_forward_kernel, ge_scalar_kernel
|
|
63
|
+
from nbxtensor.ops.lt import lt_forward_kernel, lt_scalar_kernel
|
|
64
|
+
from nbxtensor.ops.eq import eq_forward_kernel, eq_scalar_kernel
|
|
65
|
+
from nbxtensor.ops.ne import ne_forward_kernel, ne_scalar_kernel
|
|
66
|
+
from nbxtensor.ops.layernorm import layer_norm_forward_kernel
|
|
67
|
+
from nbxtensor.ops.rmsnorm import rms_norm_forward_kernel
|
|
68
|
+
from nbxtensor.ops.softmax import softmax_forward_kernel, softmax_forward_looped_kernel
|
|
69
|
+
from nbxtensor.ops.matmul import matmul_kernel, addmm_kernel
|
|
70
|
+
from nbxtensor.ops._autotune_policy import NBX_FORCE_FP32_ACCUM
|
|
71
|
+
from nbxtensor.ops.fused_moe import fused_moe_kernel, silu_and_mul_kernel, silu_mul_split_kernel
|
|
72
|
+
from nbxtensor.ops.dtype_convert import bf16_to_fp16_kernel
|
|
73
|
+
from nbxtensor.ops.embedding import embedding_kernel, EMBEDDING_OOB
|
|
74
|
+
from nbxtensor.ops.sum import sum_forward_kernel
|
|
75
|
+
from nbxtensor.ops.mean import mean_forward_kernel
|
|
76
|
+
from nbxtensor.ops.amax import amax_forward_kernel
|
|
77
|
+
from nbxtensor.ops.glu import glu_forward_kernel
|
|
78
|
+
from nbxtensor.ops.le import le_forward_kernel, le_scalar_kernel
|
|
79
|
+
from nbxtensor.ops.softplus import softplus_forward_kernel
|
|
80
|
+
from nbxtensor.ops.dropout import dropout_inference_kernel
|
|
81
|
+
from nbxtensor.ops.upsample_nearest2d import upsample_nearest2d_kernel
|
|
82
|
+
from nbxtensor.ops.groupnorm import group_norm_forward_kernel
|
|
83
|
+
from nbxtensor.ops.index_select import index_select_kernel, INDEX_SELECT_OOB
|
|
84
|
+
from nbxtensor.ops.triu import triu_kernel, triu_batch_kernel
|
|
85
|
+
from nbxtensor.ops.tril import tril_kernel, tril_batch_kernel
|
|
86
|
+
from nbxtensor.ops.argmax import argmax_kernel_1, argmax_kernel_2, argmax_kernel_inner
|
|
87
|
+
from nbxtensor.ops.argmin import argmin_kernel_1, argmin_kernel_2, argmin_kernel
|
|
88
|
+
from nbxtensor.ops.conv2d import conv2d_forward_kernel
|
|
89
|
+
from nbxtensor.ops.depthwise_conv2d import depthwise_conv2d_kernel
|
|
90
|
+
from nbxtensor.ops.batch_norm import batch_norm_forward_kernel
|
|
91
|
+
from nbxtensor.ops.cumsum import scan_part_sum_kernel, add_base_sum_kernel
|
|
92
|
+
from nbxtensor.ops.scatter_op import scatter_kernel, scatter_add_kernel, scatter_reduce_amax_kernel, scatter_reduce_amin_kernel
|
|
93
|
+
from nbxtensor.ops.gather_op import gather_kernel
|
|
94
|
+
from nbxtensor.ops.avg_pool2d import avg_pool2d_forward_kernel
|
|
95
|
+
from nbxtensor.ops.max_pool2d import max_pool2d_forward_kernel
|
|
96
|
+
from nbxtensor.ops.max_pool3d import max_pool3d_forward_kernel
|
|
97
|
+
from nbxtensor.ops.cross_entropy import cross_entropy_loss_forward_kernel
|
|
98
|
+
from nbxtensor.ops.exp2 import exp2_forward_kernel
|
|
99
|
+
from nbxtensor.ops.tan import tan_forward_kernel
|
|
100
|
+
from nbxtensor.ops.celu import celu_forward_kernel
|
|
101
|
+
from nbxtensor.ops.log_sigmoid import log_sigmoid_forward_kernel
|
|
102
|
+
from nbxtensor.ops.isfinite import isfinite_forward_kernel
|
|
103
|
+
from nbxtensor.ops.isinf import isinf_forward_kernel
|
|
104
|
+
from nbxtensor.ops.isnan import isnan_forward_kernel
|
|
105
|
+
from nbxtensor.ops.nan_to_num import nan_to_num_forward_kernel
|
|
106
|
+
from nbxtensor.ops.threshold import threshold_forward_kernel
|
|
107
|
+
from nbxtensor.ops.floor import floor_forward_kernel, ceil_forward_kernel, round_forward_kernel, trunc_forward_kernel
|
|
108
|
+
from nbxtensor.ops.addcdiv import addcdiv_forward_kernel
|
|
109
|
+
from nbxtensor.ops.addcmul import addcmul_forward_kernel
|
|
110
|
+
from nbxtensor.ops.all_reduce import all_kernel_mid, all_kernel_result, all_kernel_dim
|
|
111
|
+
from nbxtensor.ops.any_reduce import any_kernel_mid, any_kernel_result, any_kernel_dim
|
|
112
|
+
from nbxtensor.ops.bitwise_and import bitwise_and_forward_kernel
|
|
113
|
+
from nbxtensor.ops.bitwise_or import bitwise_or_forward_kernel
|
|
114
|
+
from nbxtensor.ops.bitwise_not import bitwise_not_forward_kernel
|
|
115
|
+
from nbxtensor.ops.logical_and import logical_and_forward_kernel
|
|
116
|
+
from nbxtensor.ops.logical_or import logical_or_forward_kernel
|
|
117
|
+
from nbxtensor.ops.logical_not import logical_not_forward_kernel
|
|
118
|
+
from nbxtensor.ops.lerp import lerp_tensor_forward_kernel, lerp_scalar_head_kernel, lerp_scalar_tail_kernel
|
|
119
|
+
from nbxtensor.ops.dot_op import dot_kernel_small, dot_kernel_partial, dot_kernel_reduce
|
|
120
|
+
from nbxtensor.ops.flip_op import flip_1d_kernel, flip_strided_kernel
|
|
121
|
+
from nbxtensor.ops.cat_op import cat_copy_kernel_4
|
|
122
|
+
from nbxtensor.ops.fill_op import fill_kernel
|
|
123
|
+
from nbxtensor.ops.arange_op import arange_kernel
|
|
124
|
+
from nbxtensor.ops.prod import prod_kernel_mid, prod_kernel_result, prod_kernel
|
|
125
|
+
from nbxtensor.ops.min_reduce import min_kernel_mid, min_kernel_result, min_kernel
|
|
126
|
+
from nbxtensor.ops.max_reduce import max_kernel_mid, max_kernel_result, max_kernel
|
|
127
|
+
from nbxtensor.ops.baddbmm_op import baddbmm_kernel
|
|
128
|
+
from nbxtensor.ops.addmv_op import addmv_kernel
|
|
129
|
+
from nbxtensor.ops.mse_loss import mse_loss_partial_kernel, mse_loss_reduce_kernel, mse_loss_none_kernel
|
|
130
|
+
from nbxtensor.ops.nllloss import nll_loss_forward_kernel
|
|
131
|
+
from nbxtensor.ops.std import std_map_kernel, std_reduce_kernel, std_dim_kernel
|
|
132
|
+
from nbxtensor.ops.var import var_kernel_1, var_kernel_2, var_welford_kernel
|
|
133
|
+
from nbxtensor.ops.index_add import index_add_gather_kernel
|
|
134
|
+
from nbxtensor.ops.index_put_op import index_put_kernel, INDEX_PUT_OOB
|
|
135
|
+
from nbxtensor.ops.sort_op import (radix_sort_histogram_kernel,
|
|
136
|
+
radix_sort_scatter_kernel,
|
|
137
|
+
radix_sort_tile_counts_kernel,
|
|
138
|
+
radix_sort_tile_prefix_kernel)
|
|
139
|
+
from nbxtensor.ops.rope import rope_forward_kernel
|
|
140
|
+
from nbxtensor.ops.pixel_shuffle import (
|
|
141
|
+
pixel_shuffle_kernel,
|
|
142
|
+
pixel_shuffle_broadcast_aware_kernel,
|
|
143
|
+
)
|
|
144
|
+
from nbxtensor.ops.pixel_unshuffle import pixel_unshuffle_kernel
|
|
145
|
+
from nbxtensor.ops.upsample_bilinear2d import upsample_bilinear2d_kernel
|
|
146
|
+
from nbxtensor.ops.adaptive_avg_pool2d import adaptive_avg_pool2d_kernel
|
|
147
|
+
from nbxtensor.ops.conv_depthwise2d import conv_depthwise2d_kernel
|
|
148
|
+
from nbxtensor.ops.scaled_softmax import scaled_softmax_kernel
|
|
149
|
+
from nbxtensor.ops.vector_norm import (
|
|
150
|
+
l2_norm_kernel, l1_norm_kernel, linf_norm_kernel, l0_norm_kernel, lp_norm_kernel,
|
|
151
|
+
l2_norm_pass1_kernel, l2_norm_pass2_kernel,
|
|
152
|
+
linf_norm_pass1_kernel, linf_norm_pass2_kernel,
|
|
153
|
+
)
|
|
154
|
+
from nbxtensor.ops.addr import addr_kernel
|
|
155
|
+
from nbxtensor.ops.logical_xor import logical_xor_kernel
|
|
156
|
+
from nbxtensor.ops.bitwise_left_shift import bitwise_left_shift_kernel
|
|
157
|
+
from nbxtensor.ops.bitwise_right_shift import bitwise_right_shift_kernel
|
|
158
|
+
from nbxtensor.ops.rand_op import rand_kernel, randn_kernel
|
|
159
|
+
from nbxtensor.ops.clamp_min import clamp_min_forward_kernel
|
|
160
|
+
from nbxtensor.ops.conv_transpose2d import conv_transpose2d_kernel
|
|
161
|
+
from nbxtensor.ops.cummax import scan_part_max_kernel
|
|
162
|
+
from nbxtensor.ops.cummin import scan_part_min_kernel
|
|
163
|
+
from nbxtensor.ops.count_nonzero import count_nonzero_kernel
|
|
164
|
+
from nbxtensor.ops.trace_op import trace_kernel
|
|
165
|
+
from nbxtensor.ops.linspace_op import linspace_kernel
|
|
166
|
+
from nbxtensor.ops.full_op import full_kernel
|
|
167
|
+
from nbxtensor.ops.stack_op import stack_copy_kernel
|
|
168
|
+
from nbxtensor.ops.repeat_op import repeat_1d_kernel
|
|
169
|
+
from nbxtensor.ops.upsample_nearest3d import upsample_nearest3d_kernel
|
|
170
|
+
from nbxtensor.ops.weight_norm import weight_norm_kernel_first, weight_norm_kernel_last
|
|
171
|
+
from nbxtensor.ops.repeat_interleave import repeat_interleave_tensor_kernel
|
|
172
|
+
from nbxtensor.ops.elementwise_launch import ew_meta as _ew_meta, ew_grid as _ew_grid
|
|
173
|
+
from nbxtensor.ops import tiling_sizes as _ts
|
|
174
|
+
import math
|
|
175
|
+
from nbxtensor.ops import elementwise_wrappers as _ew
|
|
176
|
+
from nbxtensor.ops import random_wrappers as _rnd
|
|
177
|
+
from nbxtensor.ops import reduction_wrappers as _red
|
|
178
|
+
from nbxtensor.ops import wrappers as _core
|
|
179
|
+
_WNORM_BM, _WNORM_BN = 64, 256 # weight_norm
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
# ===========================================================================
|
|
183
|
+
# NORMALIZATION WRAPPERS
|
|
184
|
+
# ===========================================================================
|
|
185
|
+
|
|
186
|
+
def native_layer_norm(x, normalized_shape, weight, bias, eps=1e-5):
|
|
187
|
+
"""LayerNorm matching ATen native_layer_norm signature.
|
|
188
|
+
|
|
189
|
+
Returns (output, mean, rstd).
|
|
190
|
+
"""
|
|
191
|
+
# Flatten to [batch, feat] for the kernel
|
|
192
|
+
feat_dim = 1
|
|
193
|
+
for s in normalized_shape:
|
|
194
|
+
feat_dim *= s
|
|
195
|
+
batch_dim = x.numel() // feat_dim
|
|
196
|
+
|
|
197
|
+
x_2d = x.contiguous().view(batch_dim, feat_dim)
|
|
198
|
+
output_2d = NBXTensor.empty_like(x_2d)
|
|
199
|
+
mean = NBXTensor.empty(batch_dim, dtype=NBXDtype.float32, device=x.device)
|
|
200
|
+
inv_std = NBXTensor.empty(batch_dim, dtype=NBXDtype.float32, device=x.device)
|
|
201
|
+
|
|
202
|
+
has_weight = weight is not None
|
|
203
|
+
has_bias = bias is not None and has_weight
|
|
204
|
+
|
|
205
|
+
# rows per program and warps: the certified configuration of this key (workstream 1), the rows' bucket from the
|
|
206
|
+
# launcher's own `bucket_of` — the key the derived census forms (`launch_keys.layer_norm_launches`)
|
|
207
|
+
grid = lambda META: (triton.cdiv(batch_dim, META['BLOCK_SIZE_BATCH']),)
|
|
208
|
+
set_device(x_2d)
|
|
209
|
+
layer_norm_forward_kernel[grid](
|
|
210
|
+
x_2d, weight if has_weight else x_2d, bias if has_bias else x_2d,
|
|
211
|
+
mean, inv_std, output_2d,
|
|
212
|
+
batch_dim, feat_dim, _lk.bucket_of("M", batch_dim), _lk.softmax_feat_bucket(feat_dim),
|
|
213
|
+
x_2d.stride(0), x_2d.stride(1),
|
|
214
|
+
output_2d.stride(0), output_2d.stride(1),
|
|
215
|
+
eps,
|
|
216
|
+
scale_by_weight=has_weight,
|
|
217
|
+
add_bias=has_bias,
|
|
218
|
+
save_stats=True,
|
|
219
|
+
)
|
|
220
|
+
return output_2d.view_as(x), mean, inv_std
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def layer_norm_wrapper(x, normalized_shape, weight=None, bias=None, eps=1e-5,
|
|
224
|
+
cudnn_enable=True) :
|
|
225
|
+
"""aten::layer_norm — the high-level op returns ONLY the normalized output
|
|
226
|
+
(native_layer_norm also returns mean/rstd). The triton runtime stores a
|
|
227
|
+
tuple result as out_0=the-whole-tuple when the graph declares a single
|
|
228
|
+
output, so a downstream op (dropout) then receives a tuple. Return just the
|
|
229
|
+
output tensor here."""
|
|
230
|
+
out = native_layer_norm(x, normalized_shape, weight, bias, eps)
|
|
231
|
+
return out[0] if isinstance(out, (tuple, list)) else out
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def rms_norm(x, weight, eps=1e-6, epsilon=None, out_dtype=None):
|
|
235
|
+
"""RMSNorm wrapper. `out_dtype`: the dtype the output is stored in (default: x's).
|
|
236
|
+
|
|
237
|
+
The kernel widens every input load to fp32 and stores in the output buffer's dtype, so a
|
|
238
|
+
half input needs no materialised fp32 copy before the call and an fp32 output no cast
|
|
239
|
+
after it: the dtype engine's fp32-internal wrap asks for the output dtype instead (the
|
|
240
|
+
copy lever, 2026-09-07 — 48 + 48 copies a token on TinyLlama). The rounding at the store
|
|
241
|
+
is the one the materialised path applied at its cast back.
|
|
242
|
+
|
|
243
|
+
When `x.contiguous()` materializes a new tensor (input is a strided
|
|
244
|
+
view such as the NHWC permute pattern in DC-AE VAEs), the rms_norm
|
|
245
|
+
kernel can safely write its output back into that fresh contiguous
|
|
246
|
+
buffer in place: each program block reads its full BLOCK_SIZE_BATCH
|
|
247
|
+
x BLOCK_SIZE_FEAT tile via `tl.load` before any `tl.store`, so
|
|
248
|
+
input==output is per-tile safe and no other Python consumer sees
|
|
249
|
+
the materialized copy. This saves an 8 GiB transient on the
|
|
250
|
+
Sana 4Kpx VAE rms_norm::24 (post-pixel_shuffle NHWC chain).
|
|
251
|
+
"""
|
|
252
|
+
if epsilon is not None:
|
|
253
|
+
eps = epsilon
|
|
254
|
+
x = _core._ensure_cuda(x)
|
|
255
|
+
weight = _core._ensure_cuda(weight)
|
|
256
|
+
feat_dim = x.shape[-1]
|
|
257
|
+
batch_dim = x.numel() // feat_dim
|
|
258
|
+
|
|
259
|
+
x_contig = x.contiguous()
|
|
260
|
+
x_2d = x_contig.view(batch_dim, feat_dim)
|
|
261
|
+
out_dt = out_dtype if out_dtype is not None else x_2d.nbx_dtype
|
|
262
|
+
if x_contig is not x and out_dt == x_2d.nbx_dtype:
|
|
263
|
+
# contiguous() allocated a fresh buffer with no other holder —
|
|
264
|
+
# write rms_norm output directly into it (in-place) instead of
|
|
265
|
+
# paying for a second 8 GiB allocation.
|
|
266
|
+
output_2d = x_2d
|
|
267
|
+
else:
|
|
268
|
+
output_2d = NBXTensor.empty(x_2d.shape, dtype=out_dt, device=x_2d.device)
|
|
269
|
+
|
|
270
|
+
has_weight = weight is not None
|
|
271
|
+
|
|
272
|
+
# rows per program and warps: the certified configuration of this key (workstream 1) — the key the derived census
|
|
273
|
+
# forms (`launch_keys.rms_norm_launches`)
|
|
274
|
+
grid = lambda META: (triton.cdiv(batch_dim, META['BLOCK_SIZE_BATCH']),)
|
|
275
|
+
rms_norm_forward_kernel[grid](
|
|
276
|
+
x_2d, weight if has_weight else x_2d,
|
|
277
|
+
output_2d,
|
|
278
|
+
batch_dim, feat_dim, _lk.bucket_of("M", batch_dim), _lk.softmax_feat_bucket(feat_dim),
|
|
279
|
+
x_2d.stride(0), x_2d.stride(1),
|
|
280
|
+
output_2d.stride(0), output_2d.stride(1),
|
|
281
|
+
eps,
|
|
282
|
+
scale_by_weight=has_weight,
|
|
283
|
+
SATURATE_F16=_nbx_saturating_cast(NBXDtype.float32, out_dt),
|
|
284
|
+
)
|
|
285
|
+
return output_2d.view_as(x)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
# NOT `_nbx_widens_on_load`: the kernel widening an fp16 INPUT on load is another
|
|
289
|
+
# compilation than the fp32 input the certified rows ran it with — 12–15 % of the
|
|
290
|
+
# elements differ at T5's shapes (PixArt-XL-2, the machine-set gate of 2026-09-08),
|
|
291
|
+
# invisible on TinyLlama whose stream reaches rms_norm in fp32. The dtype engine's
|
|
292
|
+
# wrap pre-casts as it always did; `out_dtype` stays for callers that store fp32.
|
|
293
|
+
rms_norm._nbx_widens_on_load = False
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def norm_wrapper(x, p=2, dim=None, keepdim=False):
|
|
297
|
+
"""aten::norm(self, p, dim, keepdim) — Lp norm reduction over ``dim``.
|
|
298
|
+
|
|
299
|
+
The DAC codec's only norm is weight-norm: ``||v|| = sqrt(sum(v*v, dim))``
|
|
300
|
+
with p=2 over a single dim. Composed from existing triton kernels
|
|
301
|
+
(mul + sum + sqrt) — R33-pure, no new @triton.jit needed; sum_wrapper
|
|
302
|
+
accumulates in fp32 so the reduction over a long row does not overflow fp16.
|
|
303
|
+
Other p raise ZERO-FALLBACK (named follow-up) rather than mis-computing.
|
|
304
|
+
"""
|
|
305
|
+
pv = _core._to_scalar(p) if isinstance(p, NBXTensor) else (2 if p is None else p)
|
|
306
|
+
if float(pv) != 2.0:
|
|
307
|
+
raise NotImplementedError(
|
|
308
|
+
f"aten::norm p={pv} unwired (DAC weight-norm uses p=2) — "
|
|
309
|
+
"follow-up P-TRITON-NORM-GENERAL-P.")
|
|
310
|
+
return _ew.sqrt_wrapper(_red.sum_wrapper(_ew.mul(x, x), dim=dim, keepdim=keepdim))
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def group_norm_wrapper(x, num_groups: int, weight, bias, eps=1e-5):
|
|
314
|
+
"""GroupNorm. Returns (output, mean, rstd) matching ATen native_group_norm.
|
|
315
|
+
|
|
316
|
+
Friendly signature (x, num_groups, weight, bias, eps) for direct callers.
|
|
317
|
+
The graph-level `aten::native_group_norm` op uses a longer signature
|
|
318
|
+
(input, weight, bias, N, C, HxW, num_groups, eps) — see
|
|
319
|
+
`native_group_norm_wrapper` below for the adapter.
|
|
320
|
+
"""
|
|
321
|
+
x = x.contiguous()
|
|
322
|
+
N = x.shape[0]
|
|
323
|
+
C = x.shape[1]
|
|
324
|
+
HxW = x.numel() // (N * C)
|
|
325
|
+
group_size = C // num_groups
|
|
326
|
+
|
|
327
|
+
y = NBXTensor.empty_like(x)
|
|
328
|
+
mean = NBXTensor.empty((N, num_groups), dtype=NBXDtype.float32, device=x.device)
|
|
329
|
+
rstd = NBXTensor.empty((N, num_groups), dtype=NBXDtype.float32, device=x.device)
|
|
330
|
+
|
|
331
|
+
grid = (N * num_groups,)
|
|
332
|
+
set_device(x)
|
|
333
|
+
# the chunk along hidden = group_size * HxW and the warps: the certified configuration of this key (chantier 1),
|
|
334
|
+
# the key the derived census forms (`launch_keys.group_norm_launches`)
|
|
335
|
+
hidden = group_size * HxW
|
|
336
|
+
group_norm_forward_kernel[grid](
|
|
337
|
+
x, y,
|
|
338
|
+
weight if weight is not None else x,
|
|
339
|
+
bias if bias is not None else x,
|
|
340
|
+
mean, rstd,
|
|
341
|
+
group_size, C, HxW, num_groups, eps,
|
|
342
|
+
_lk.bucket_of("M", N * num_groups), _lk.softmax_feat_bucket(hidden),
|
|
343
|
+
scale_by_weight=(weight is not None),
|
|
344
|
+
add_bias=(bias is not None),
|
|
345
|
+
)
|
|
346
|
+
return y, mean, rstd
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def native_group_norm_wrapper(input, weight, bias, N, C, HxW, num_groups, eps):
|
|
350
|
+
"""Adapter for ATen's low-level `aten::native_group_norm`.
|
|
351
|
+
|
|
352
|
+
The op is dispatched with 8 positional args (input, weight, bias, N, C,
|
|
353
|
+
HxW, num_groups, eps). We recompute N/C/HxW inside the kernel wrapper
|
|
354
|
+
from `input.shape`, so we just forward the friendly call — dropping the
|
|
355
|
+
redundant N/C/HxW scalars.
|
|
356
|
+
"""
|
|
357
|
+
return group_norm_wrapper(input, int(num_groups), weight, bias, float(eps))
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
# ===========================================================================
|
|
361
|
+
# BATCH NORM WRAPPER — Extracted from FlagGems
|
|
362
|
+
# ===========================================================================
|
|
363
|
+
|
|
364
|
+
def batch_norm_wrapper(
|
|
365
|
+
x, weight, bias,
|
|
366
|
+
running_mean, running_var,
|
|
367
|
+
training: bool = False, momentum: float = 0.1, eps: float = 1e-5,
|
|
368
|
+
) :
|
|
369
|
+
"""BatchNorm / InstanceNorm forward (kernel from FlagGems).
|
|
370
|
+
|
|
371
|
+
training=False → inference batch_norm using running_mean/running_var.
|
|
372
|
+
training=True → statistics computed from the input — covers batch_norm
|
|
373
|
+
training AND instance_norm / AdaIN, which the tracer emits
|
|
374
|
+
as cudnn_batch_norm(training=True) with no affine and no
|
|
375
|
+
running buffers (it reshapes [N,C,L]→[1,N*C,L] first, so
|
|
376
|
+
each instance-channel normalises over the spatial axis).
|
|
377
|
+
Absent weight/bias/running reach the kernel as null pointers, handled by its
|
|
378
|
+
`if *_pointer:` guards (weight→1, bias→0, running update skipped). Never
|
|
379
|
+
substitute the input tensor for an absent buffer — that silently corrupted
|
|
380
|
+
instance_norm (kernel read x as the affine scale and overwrote x with the
|
|
381
|
+
momentum-blended running stats).
|
|
382
|
+
"""
|
|
383
|
+
if not training and running_mean is None:
|
|
384
|
+
raise ValueError(
|
|
385
|
+
"batch_norm eval mode (training=False) requires running_mean and "
|
|
386
|
+
"running_var; got None. For statistics-from-input use training=True."
|
|
387
|
+
)
|
|
388
|
+
if x.ndim == 2:
|
|
389
|
+
x = x.unsqueeze(-1)
|
|
390
|
+
elif x.ndim >= 4:
|
|
391
|
+
x = x.flatten(2, -1)
|
|
392
|
+
# x is now [N, C, spatial_dim]
|
|
393
|
+
N, C, spatial = x.shape
|
|
394
|
+
|
|
395
|
+
output = NBXTensor.empty_like(x)
|
|
396
|
+
mean_out = NBXTensor.empty(C, dtype=NBXDtype.float32, device=x.device)
|
|
397
|
+
inv_std_out = NBXTensor.empty(C, dtype=NBXDtype.float32, device=x.device)
|
|
398
|
+
|
|
399
|
+
grid = (C,)
|
|
400
|
+
xc = x.contiguous()
|
|
401
|
+
set_device(xc)
|
|
402
|
+
# Pass null pointers (None) — NOT x — for absent weight/bias/running. The
|
|
403
|
+
# kernel guards each with `if *_pointer:`; substituting x defeated the guard
|
|
404
|
+
# (kernel read x as the affine scale/shift and wrote running stats back into
|
|
405
|
+
# the input), which silently corrupted instance_norm (AdaIN has no affine
|
|
406
|
+
# nor running stats). batch_norm callers still pass real buffers unchanged.
|
|
407
|
+
batch_norm_forward_kernel[grid](
|
|
408
|
+
xc,
|
|
409
|
+
weight.contiguous() if weight is not None else None,
|
|
410
|
+
bias.contiguous() if bias is not None else None,
|
|
411
|
+
mean_out, inv_std_out, output,
|
|
412
|
+
running_mean if running_mean is not None else None,
|
|
413
|
+
running_var if running_var is not None else None,
|
|
414
|
+
N, spatial,
|
|
415
|
+
xc.stride(0), xc.stride(1), xc.stride(2),
|
|
416
|
+
output.stride(0), output.stride(1), output.stride(2),
|
|
417
|
+
momentum, eps,
|
|
418
|
+
is_train=training,
|
|
419
|
+
)
|
|
420
|
+
return output
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def vector_norm_wrapper(x, ord: float = 2.0, dim=None, keepdim=False) :
|
|
424
|
+
"""Vector norm — dispatches by ord to L0/L1/L2/Linf/Lp kernels. Triton kernel compute."""
|
|
425
|
+
import math as _math
|
|
426
|
+
|
|
427
|
+
if dim is None:
|
|
428
|
+
dim_list = list(range(x.ndim))
|
|
429
|
+
elif isinstance(dim, int):
|
|
430
|
+
dim_list = [dim % x.ndim]
|
|
431
|
+
else:
|
|
432
|
+
dim_list = [d % x.ndim for d in dim]
|
|
433
|
+
|
|
434
|
+
# Compute M (outer), N (reduction), shape manipulation
|
|
435
|
+
shape = list(x.shape)
|
|
436
|
+
N = 1
|
|
437
|
+
for d in dim_list:
|
|
438
|
+
N *= shape[d]
|
|
439
|
+
M = x.numel() // N
|
|
440
|
+
|
|
441
|
+
# Move reduction dims to last, flatten
|
|
442
|
+
# Build permutation: non-reduced dims first, reduced dims last
|
|
443
|
+
non_reduced = [i for i in range(x.ndim) if i not in dim_list]
|
|
444
|
+
perm = non_reduced + dim_list
|
|
445
|
+
x_perm = x.permute(perm).contiguous()
|
|
446
|
+
x_2d = x_perm.reshape(M, N)
|
|
447
|
+
|
|
448
|
+
out_shape = [shape[i] for i in range(x.ndim)]
|
|
449
|
+
for d in dim_list:
|
|
450
|
+
out_shape[d] = 1
|
|
451
|
+
out = NBXTensor.empty(M, dtype=NBXDtype.float32, device=x.device)
|
|
452
|
+
|
|
453
|
+
grid, _bm, _bn = _ew._reduction_tile(M)
|
|
454
|
+
|
|
455
|
+
if ord == 2 or ord == 2.0:
|
|
456
|
+
set_device(x_2d)
|
|
457
|
+
l2_norm_kernel[grid](x_2d, out, M, N, BLOCK_M=_bm, BLOCK_N=_bn, num_warps=4)
|
|
458
|
+
elif ord == float('inf'):
|
|
459
|
+
set_device(x_2d)
|
|
460
|
+
linf_norm_kernel[grid](x_2d, out, M, N, BLOCK_M=_bm, BLOCK_N=_bn, num_warps=4)
|
|
461
|
+
elif ord == 0 or ord == 0.0:
|
|
462
|
+
set_device(x_2d)
|
|
463
|
+
l0_norm_kernel[grid](x_2d, out, M, N, BLOCK_M=_bm, BLOCK_N=_bn, num_warps=4)
|
|
464
|
+
elif ord == 1 or ord == 1.0:
|
|
465
|
+
set_device(x_2d)
|
|
466
|
+
l1_norm_kernel[grid](x_2d, out, M, N, BLOCK_M=_bm, BLOCK_N=_bn, num_warps=4)
|
|
467
|
+
else:
|
|
468
|
+
set_device(x_2d)
|
|
469
|
+
lp_norm_kernel[grid](x_2d, out, M, N, ord, BLOCK_M=_bm, BLOCK_N=_bn, num_warps=4)
|
|
470
|
+
|
|
471
|
+
result = out.to(x.dtype).view(out_shape)
|
|
472
|
+
if not keepdim:
|
|
473
|
+
for d in sorted(dim_list, reverse=True):
|
|
474
|
+
result = result.squeeze(d)
|
|
475
|
+
return result
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def weight_norm_interface_wrapper(
|
|
479
|
+
v, g, dim: int = 0
|
|
480
|
+
) -> tuple:
|
|
481
|
+
"""Weight normalization: w = g * v / ||v||.
|
|
482
|
+
|
|
483
|
+
Returns (output, norm) matching aten::_weight_norm_interface.
|
|
484
|
+
"""
|
|
485
|
+
v = v.contiguous()
|
|
486
|
+
g = g.contiguous()
|
|
487
|
+
output = NBXTensor.empty_like(v)
|
|
488
|
+
norm = NBXTensor.empty_like(g)
|
|
489
|
+
eps = 1e-45
|
|
490
|
+
|
|
491
|
+
if dim == 0:
|
|
492
|
+
M = v.shape[0]
|
|
493
|
+
N = math.prod(v.shape[1:])
|
|
494
|
+
_wbm, _wbn = _red._clamp_reduce_tile(_WNORM_BM, _WNORM_BN)
|
|
495
|
+
grid = (triton.cdiv(M, _wbm),)
|
|
496
|
+
set_device(output)
|
|
497
|
+
weight_norm_kernel_first[grid](output, norm, v, g, M, N, eps,
|
|
498
|
+
BLOCK_M=_wbm, BLOCK_N=_wbn,
|
|
499
|
+
num_warps=4)
|
|
500
|
+
elif dim == v.ndim - 1:
|
|
501
|
+
M = math.prod(v.shape[:-1])
|
|
502
|
+
N = v.shape[dim]
|
|
503
|
+
_wbm, _wbn = _red._clamp_reduce_tile(_WNORM_BM, _WNORM_BN)
|
|
504
|
+
grid = (triton.cdiv(N, _wbn),)
|
|
505
|
+
set_device(output)
|
|
506
|
+
weight_norm_kernel_last[grid](output, norm, v, g, M, N, eps,
|
|
507
|
+
BLOCK_M=_wbm, BLOCK_N=_wbn,
|
|
508
|
+
num_warps=4)
|
|
509
|
+
else:
|
|
510
|
+
raise ValueError(f"weight_norm only supports dim=0 or dim={v.ndim - 1}, got {dim}")
|
|
511
|
+
|
|
512
|
+
return output, norm
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def normal_wrapper(*args, **kwargs) :
|
|
516
|
+
"""normal(mean, std, size) → N(mean, std) via Triton randn + transform."""
|
|
517
|
+
from nbxtensor.ops.rand_op import randn_kernel
|
|
518
|
+
# ATen: normal(float mean, float std, SymInt[] size, ...)
|
|
519
|
+
mean = float(args[0]) if len(args) > 0 else kwargs.get('mean', 0.0)
|
|
520
|
+
std = float(args[1]) if len(args) > 1 else kwargs.get('std', 1.0)
|
|
521
|
+
size = args[2] if len(args) > 2 else kwargs.get('size', [])
|
|
522
|
+
if isinstance(mean, NBXTensor):
|
|
523
|
+
mean = mean.item()
|
|
524
|
+
if isinstance(std, NBXTensor):
|
|
525
|
+
std = std.item()
|
|
526
|
+
dtype = kwargs.get('dtype', NBXDtype.float32)
|
|
527
|
+
device = kwargs.get('device', None)
|
|
528
|
+
if device is None:
|
|
529
|
+
device = str('cuda')
|
|
530
|
+
output = _ew._stream_draw("normal", size, dtype, device)
|
|
531
|
+
if output is None:
|
|
532
|
+
output = NBXTensor.empty(size, dtype=dtype, device=device)
|
|
533
|
+
n = output.numel()
|
|
534
|
+
if n == 0:
|
|
535
|
+
return output
|
|
536
|
+
seed = _rnd._draw_seed()
|
|
537
|
+
set_device(output)
|
|
538
|
+
randn_kernel[_ew._1d_grid(n)](output, n, seed, **_ew_meta(n))
|
|
539
|
+
# Transform N(0,1) → N(mean, std)
|
|
540
|
+
if std != 1.0:
|
|
541
|
+
output.mul_(std)
|
|
542
|
+
if mean != 0.0:
|
|
543
|
+
output.add_(mean)
|
|
544
|
+
return output
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
def group_norm_statistics(x, num_groups: int, eps: float):
|
|
548
|
+
"""(mean, rstd) [N, G] fp32 of `x` [N, C, ...] over each group — the first pass of an exact tiled group norm
|
|
549
|
+
(`core/strategies/tiled_norm.py`), over the WHOLE input, without its normalised tensor."""
|
|
550
|
+
from nbxtensor.ops.group_norm_tiled import group_norm_stats_kernel, TILED_NORM_BLOCK, TILED_NORM_WARPS
|
|
551
|
+
x = x.contiguous()
|
|
552
|
+
N, C = int(x.shape[0]), int(x.shape[1])
|
|
553
|
+
if C % int(num_groups):
|
|
554
|
+
raise ValueError(f"tiled group norm: {C} channels in {num_groups} groups")
|
|
555
|
+
HW = x.numel() // (N * C)
|
|
556
|
+
mean = NBXTensor.empty((N, int(num_groups)), dtype=NBXDtype.float32, device=x.device)
|
|
557
|
+
rstd = NBXTensor.empty((N, int(num_groups)), dtype=NBXDtype.float32, device=x.device)
|
|
558
|
+
set_device(x)
|
|
559
|
+
group_norm_stats_kernel[(N * int(num_groups),)](x, mean, rstd, C // int(num_groups), C, HW, int(num_groups),
|
|
560
|
+
float(eps), BLOCK_SIZE=TILED_NORM_BLOCK, num_warps=TILED_NORM_WARPS)
|
|
561
|
+
return mean, rstd
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
def group_norm_apply(x, weight, bias, num_groups: int, mean, rstd):
|
|
565
|
+
"""`aten::native_group_norm`'s three outputs for a slice `x` given the whole input's statistics — the second pass;
|
|
566
|
+
the output in `x`'s dtype, as the engine's group norm writes it (`group_norm_wrapper`)."""
|
|
567
|
+
from nbxtensor.ops.group_norm_tiled import group_norm_apply_kernel, TILED_NORM_BLOCK, TILED_NORM_WARPS
|
|
568
|
+
x = x.contiguous()
|
|
569
|
+
N, C = int(x.shape[0]), int(x.shape[1])
|
|
570
|
+
HW = x.numel() // (N * C)
|
|
571
|
+
if mean._device != "cuda" or getattr(mean, "_device_idx", None) != getattr(x, "_device_idx", None):
|
|
572
|
+
mean, rstd = mean.to_cuda(x._device_idx), rstd.to_cuda(x._device_idx)
|
|
573
|
+
y = NBXTensor.empty_like(x)
|
|
574
|
+
set_device(x)
|
|
575
|
+
group_norm_apply_kernel[(N * int(num_groups),)](
|
|
576
|
+
x, y, weight if weight is not None else x, bias if bias is not None else x, mean, rstd,
|
|
577
|
+
C // int(num_groups), C, HW, int(num_groups), scale_by_weight=weight is not None, add_bias=bias is not None,
|
|
578
|
+
BLOCK_SIZE=TILED_NORM_BLOCK, num_warps=TILED_NORM_WARPS)
|
|
579
|
+
return y, mean, rstd
|
nbxtensor/ops/ones_op.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Ones — pure @triton.jit kernel.
|
|
2
|
+
|
|
3
|
+
Fills a tensor with ones. Specialization of fill.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import triton
|
|
7
|
+
import triton.language as tl
|
|
8
|
+
|
|
9
|
+
@triton.jit
|
|
10
|
+
def ones_kernel(
|
|
11
|
+
output_ptr,
|
|
12
|
+
n_elements,
|
|
13
|
+
BLOCK_SIZE: tl.constexpr,
|
|
14
|
+
):
|
|
15
|
+
"""Fill output with ones."""
|
|
16
|
+
pid = tl.program_id(0).to(tl.int64)
|
|
17
|
+
offset = pid.to(tl.int64) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) # 64-bit from the program id: the product itself wraps past 2^31 elements (register 58)
|
|
18
|
+
mask = offset < n_elements
|
|
19
|
+
one = tl.full([BLOCK_SIZE], 1.0, dtype=output_ptr.type.element_ty)
|
|
20
|
+
tl.store(output_ptr + offset, one, mask=mask)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"""Float64 references for the autotune correctness screen.
|
|
2
|
+
|
|
3
|
+
Pure numpy, no torch, no Triton: a reference that shares an implementation with
|
|
4
|
+
the thing it checks is not a reference. Each module here answers one kernel
|
|
5
|
+
family from its live arguments and says nothing otherwise.
|
|
6
|
+
"""
|