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,481 @@
|
|
|
1
|
+
"""Spectral wrappers: FFTs (r2c, c2r, c2c), DFT matrices, STFT and its inverse. 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 conv_wrappers as _cnv
|
|
176
|
+
from nbxtensor.ops import elementwise_wrappers as _ew
|
|
177
|
+
from nbxtensor.ops import linalg_wrappers as _la
|
|
178
|
+
from nbxtensor.ops import wrappers as _core
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _triton_fft_rows(x_real, x_imag, inverse: bool) -> tuple:
|
|
182
|
+
"""The radix-2 FFT of every row along the last dim, all rows in ONE launch per step:
|
|
183
|
+
one bit-reversal, log2(N) butterfly stages and, for the inverse, one 1/N scale.
|
|
184
|
+
Returns (real, imag) in the input's shape. N must be a power of 2.
|
|
185
|
+
|
|
186
|
+
The rows were launched one at a time from the host — an STFT of a spoken sentence
|
|
187
|
+
is thousands of frames, so thousands of launches per stage: chatterbox's census
|
|
188
|
+
walk spent every py-spy sample inside that loop (2026-09-28), and every live
|
|
189
|
+
Triton run through `aten::stft` paid the same per-frame cost."""
|
|
190
|
+
from nbxtensor.ops.fft_op import bit_reverse_rows_kernel, fft_stage_rows_kernel, scale_kernel
|
|
191
|
+
|
|
192
|
+
N = x_real.shape[-1]
|
|
193
|
+
assert N > 0 and (N & (N - 1)) == 0, f"FFT size must be power of 2, got {N}"
|
|
194
|
+
orig_shape = x_real.shape
|
|
195
|
+
rows = x_real.numel() // N
|
|
196
|
+
xr = x_real.reshape(rows, N).contiguous()
|
|
197
|
+
xi = x_imag.reshape(rows, N).contiguous()
|
|
198
|
+
out_real = NBXTensor.empty_like(xr)
|
|
199
|
+
out_imag = NBXTensor.empty_like(xi)
|
|
200
|
+
log2n = N.bit_length() - 1
|
|
201
|
+
total = rows * N
|
|
202
|
+
set_device(xr)
|
|
203
|
+
bit_reverse_rows_kernel[_ew._1d_grid(total)](xr, xi, out_real, out_imag, N, total, log2n,
|
|
204
|
+
**_ew_meta(total))
|
|
205
|
+
pairs = rows * (N // 2)
|
|
206
|
+
for stage in range(1, log2n + 1):
|
|
207
|
+
fft_stage_rows_kernel[_ew._1d_grid(pairs)](out_real, out_imag, N, pairs, stage,
|
|
208
|
+
INVERSE=inverse, **_ew_meta(pairs))
|
|
209
|
+
if inverse:
|
|
210
|
+
# 1/N through the house scale kernel (an NBXTensor has no torch-style in-place
|
|
211
|
+
# multiply — found by the 2026-09-05 bit gate).
|
|
212
|
+
scale_kernel[_ew._1d_grid(total)](out_real, out_imag, total, 1.0 / N, **_ew_meta(total))
|
|
213
|
+
return out_real.reshape(orig_shape), out_imag.reshape(orig_shape)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _triton_fft_forward(x_real, x_imag) -> tuple:
|
|
217
|
+
"""Forward FFT of every row along the last dim. Returns (real, imag). N a power of 2."""
|
|
218
|
+
return _triton_fft_rows(x_real, x_imag, inverse=False)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _triton_ifft(x_real, x_imag) -> tuple:
|
|
222
|
+
"""Inverse FFT of every row along the last dim, scaled by 1/N. Returns (real, imag)."""
|
|
223
|
+
return _triton_fft_rows(x_real, x_imag, inverse=True)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _dft_rfft_matrices(N, N_bins, ref):
|
|
227
|
+
"""cos[k,n] and -sin[k,n] rfft basis matrices [N_bins, N] (R33-pure kernel).
|
|
228
|
+
Allocated on `ref`'s device."""
|
|
229
|
+
import math
|
|
230
|
+
from nbxtensor.ops.dft import dft_rfft_matrix_kernel
|
|
231
|
+
set_device(ref)
|
|
232
|
+
cos_mat = NBXTensor.empty((N_bins, N), dtype=NBXDtype.float32, device=ref.device)
|
|
233
|
+
nsin_mat = NBXTensor.empty((N_bins, N), dtype=NBXDtype.float32, device=ref.device)
|
|
234
|
+
total = N_bins * N
|
|
235
|
+
dft_rfft_matrix_kernel[_ew._1d_grid(total)](
|
|
236
|
+
cos_mat, nsin_mat, N_bins, N, 2.0 * math.pi / N, **_ew_meta(total))
|
|
237
|
+
return cos_mat, nsin_mat
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _idft_c2r_matrices(N, N_bins, ref):
|
|
241
|
+
"""irfft basis matrices [N_bins, N]: real_out = X_real @ C + X_imag @ S."""
|
|
242
|
+
import math
|
|
243
|
+
from nbxtensor.ops.dft import idft_c2r_matrix_kernel
|
|
244
|
+
set_device(ref)
|
|
245
|
+
C = NBXTensor.empty((N_bins, N), dtype=NBXDtype.float32, device=ref.device)
|
|
246
|
+
S = NBXTensor.empty((N_bins, N), dtype=NBXDtype.float32, device=ref.device)
|
|
247
|
+
total = N_bins * N
|
|
248
|
+
idft_c2r_matrix_kernel[_ew._1d_grid(total)](
|
|
249
|
+
C, S, N_bins, N, 2.0 * math.pi / N, 1.0 / N,
|
|
250
|
+
1 if (N % 2 == 0) else 0, **_ew_meta(total))
|
|
251
|
+
return C, S
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _dft_r2c(x, onesided):
|
|
255
|
+
"""Real frames [.., N] → (X_real, X_imag) [.., N_bins] via DFT matmul."""
|
|
256
|
+
N = x.shape[-1]
|
|
257
|
+
N_bins = N // 2 + 1 if onesided else N
|
|
258
|
+
cos_mat, nsin_mat = _dft_rfft_matrices(N, N_bins, x)
|
|
259
|
+
lead = tuple(x.shape[:-1])
|
|
260
|
+
M = 1
|
|
261
|
+
for d in lead:
|
|
262
|
+
M *= d
|
|
263
|
+
x2 = x.reshape(M, N)
|
|
264
|
+
if x2.nbx_dtype != NBXDtype.float32:
|
|
265
|
+
x2 = x2.to(NBXDtype.float32)
|
|
266
|
+
Xr = _la.matmul_wrapper(x2, cos_mat.transpose(0, 1).contiguous()) # [M, N_bins]
|
|
267
|
+
Xi = _la.matmul_wrapper(x2, nsin_mat.transpose(0, 1).contiguous()) # [M, N_bins]
|
|
268
|
+
return Xr.reshape(*lead, N_bins), Xi.reshape(*lead, N_bins)
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def _dft_c2r(x_complex, N):
|
|
272
|
+
"""Complex spectrum [.., N_bins] → real [.., N] via inverse-DFT matmul."""
|
|
273
|
+
N_bins = x_complex.shape[-1]
|
|
274
|
+
Xr = x_complex.real.contiguous()
|
|
275
|
+
Xi = x_complex.imag.contiguous()
|
|
276
|
+
C, S = _idft_c2r_matrices(N, N_bins, x_complex)
|
|
277
|
+
lead = tuple(Xr.shape[:-1])
|
|
278
|
+
M = 1
|
|
279
|
+
for d in lead:
|
|
280
|
+
M *= d
|
|
281
|
+
Xr2 = Xr.reshape(M, N_bins)
|
|
282
|
+
Xi2 = Xi.reshape(M, N_bins)
|
|
283
|
+
real_out = _ew.add(_la.matmul_wrapper(Xr2, C), _la.matmul_wrapper(Xi2, S)) # [M, N]
|
|
284
|
+
return real_out.reshape(*lead, N)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def fft_r2c_wrapper(x, dim=-1, norm: str = None,
|
|
288
|
+
onesided: bool = True) :
|
|
289
|
+
"""_fft_r2c: real-to-complex rfft → complex64 NBXTensor.
|
|
290
|
+
|
|
291
|
+
pow2 N: radix-2 butterfly (_triton_fft_forward), returning the FULL complex
|
|
292
|
+
pair (the old code dropped the imaginary part). non-pow2 N (e.g. iSTFT
|
|
293
|
+
n_fft=20): DFT-via-matmul (the standard small-N path). Model-agnostic — routes
|
|
294
|
+
on N, never on model identity. R33-pure."""
|
|
295
|
+
if isinstance(dim, (list, tuple)):
|
|
296
|
+
dim = dim[0] if dim else -1
|
|
297
|
+
x = _core._ensure_cuda(x).contiguous()
|
|
298
|
+
moved = (dim != -1 and dim != x.ndim - 1)
|
|
299
|
+
if moved:
|
|
300
|
+
x = x.transpose(dim, -1).contiguous()
|
|
301
|
+
N = x.shape[-1]
|
|
302
|
+
if N > 1 and (N & (N - 1)) == 0:
|
|
303
|
+
# power of 2 → radix-2 butterfly
|
|
304
|
+
x_real = x.to(NBXDtype.float32) if x.nbx_dtype != NBXDtype.float32 else x
|
|
305
|
+
x_imag = NBXTensor.zeros_like(x_real)
|
|
306
|
+
out_real, out_imag = _triton_fft_forward(x_real, x_imag)
|
|
307
|
+
if onesided:
|
|
308
|
+
half = N // 2 + 1
|
|
309
|
+
out_real = out_real.narrow(-1, 0, half).contiguous()
|
|
310
|
+
out_imag = out_imag.narrow(-1, 0, half).contiguous()
|
|
311
|
+
else:
|
|
312
|
+
# non-power-of-2 → DFT-via-matmul
|
|
313
|
+
out_real, out_imag = _dft_r2c(x, onesided)
|
|
314
|
+
result = _ew.complex_wrapper(out_real, out_imag)
|
|
315
|
+
if moved:
|
|
316
|
+
result = result.transpose(dim, -1).contiguous()
|
|
317
|
+
return result
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def fft_c2r_wrapper(x, dim: int = -1, norm: str = None,
|
|
321
|
+
last_dim_size: int = None) :
|
|
322
|
+
"""_fft_c2r: complex-to-real irfft → real, via inverse-DFT-via-matmul.
|
|
323
|
+
|
|
324
|
+
Hermitian-symmetric inverse rfft, correct for any N; the small-N / non-pow2
|
|
325
|
+
case (Kokoro iSTFT n_fft=20) is the primary use. Unified on the DFT-matmul
|
|
326
|
+
path: the prior pow2 radix-2 _triton_ifft path was broken (it splatted the
|
|
327
|
+
NBXTensor.zeros shape and called a non-existent NBXTensor.flip) and was never
|
|
328
|
+
reachable for the audio decoders, so no working path is lost. Model-agnostic,
|
|
329
|
+
R33-pure. fft_r2c keeps its working pow2 butterfly (+ non-pow2 DFT)."""
|
|
330
|
+
# aten::_fft_c2r passes dim as int[] and last_dim_size as a (Sym)int; take ints.
|
|
331
|
+
if isinstance(dim, (list, tuple)):
|
|
332
|
+
dim = dim[0] if dim else -1
|
|
333
|
+
if isinstance(last_dim_size, (list, tuple)):
|
|
334
|
+
last_dim_size = last_dim_size[0] if last_dim_size else None
|
|
335
|
+
x = _core._ensure_cuda(x)
|
|
336
|
+
moved = (dim != -1 and dim != x.ndim - 1)
|
|
337
|
+
if moved:
|
|
338
|
+
x = x.transpose(dim, -1).contiguous()
|
|
339
|
+
half = x.shape[-1]
|
|
340
|
+
N = last_dim_size if last_dim_size is not None else (half - 1) * 2
|
|
341
|
+
result = _dft_c2r(x, N)
|
|
342
|
+
if moved:
|
|
343
|
+
result = result.transpose(dim, -1).contiguous()
|
|
344
|
+
return result
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def fft_c2c_wrapper(x, dim: int = -1, norm: str = None,
|
|
348
|
+
forward: bool = True) :
|
|
349
|
+
"""_fft_c2c: complex-to-complex FFT."""
|
|
350
|
+
x = _core._ensure_cuda(x)
|
|
351
|
+
|
|
352
|
+
if dim != -1 and dim != x.ndim - 1:
|
|
353
|
+
x = x.transpose(dim, -1).contiguous()
|
|
354
|
+
|
|
355
|
+
N = x.shape[-1]
|
|
356
|
+
padded_N = _ew._next_power_of_2(N)
|
|
357
|
+
|
|
358
|
+
x_real = x.real.float().contiguous()
|
|
359
|
+
x_imag = x.imag.float().contiguous()
|
|
360
|
+
|
|
361
|
+
if padded_N != N:
|
|
362
|
+
x_real = x_real
|
|
363
|
+
x_imag = x_imag
|
|
364
|
+
|
|
365
|
+
if forward:
|
|
366
|
+
out_real, out_imag = _triton_fft_forward(x_real, x_imag)
|
|
367
|
+
else:
|
|
368
|
+
out_real, out_imag = _triton_ifft(x_real, x_imag)
|
|
369
|
+
|
|
370
|
+
result = out_real
|
|
371
|
+
|
|
372
|
+
if dim != -1 and dim != x.ndim - 1:
|
|
373
|
+
result = result.transpose(dim, -1).contiguous()
|
|
374
|
+
|
|
375
|
+
return result
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def fft_rfft_wrapper(x, n: int = None, dim: int = -1,
|
|
379
|
+
norm: str = None) :
|
|
380
|
+
"""rfft equivalent via Triton kernel."""
|
|
381
|
+
return fft_r2c_wrapper(x, dim=dim, norm=norm, onesided=True)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def fft_irfft_wrapper(x, n: int = None, dim: int = -1,
|
|
385
|
+
norm: str = None) :
|
|
386
|
+
"""irfft equivalent via Triton kernel."""
|
|
387
|
+
return fft_c2r_wrapper(x, dim=dim, norm=norm, last_dim_size=n)
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def stft_wrapper(x, n_fft, hop_length=None, win_length=None, window=None,
|
|
391
|
+
normalized=False, onesided=None, return_complex=True,
|
|
392
|
+
center=False, pad_mode="reflect") :
|
|
393
|
+
"""aten::stft — framing + window + rfft → complex spectrogram
|
|
394
|
+
[.., n_fft//2+1, num_frames]. Built on the existing rfft (fft_r2c_wrapper,
|
|
395
|
+
which already covers pow2 and non-pow2 N); framing via NBXTensor.unfold.
|
|
396
|
+
`center` is applied upstream by the traced graph (an explicit pad op), so the
|
|
397
|
+
op-level stft frames without re-padding. Model-agnostic, R33-pure.
|
|
398
|
+
|
|
399
|
+
Mirrors torch.stft: real input → onesided complex spectrogram. Used by the
|
|
400
|
+
chatterbox/CosyVoice s3gen vocoder (n_fft=16, hop=4)."""
|
|
401
|
+
if isinstance(n_fft, (list, tuple)):
|
|
402
|
+
n_fft = n_fft[0]
|
|
403
|
+
hop_length = (n_fft // 4) if hop_length in (None, 0) else hop_length
|
|
404
|
+
win_length = n_fft if win_length in (None, 0) else win_length
|
|
405
|
+
onesided = True if onesided is None else bool(onesided)
|
|
406
|
+
|
|
407
|
+
x = _core._ensure_cuda(x).contiguous()
|
|
408
|
+
squeezed = (x.ndim == 1)
|
|
409
|
+
if squeezed:
|
|
410
|
+
x = x.unsqueeze(0) # [1, signal]
|
|
411
|
+
|
|
412
|
+
# frame: [..., num_frames, win_length]
|
|
413
|
+
frames = x.unfold(-1, win_length, hop_length).contiguous()
|
|
414
|
+
if window is not None:
|
|
415
|
+
frames = _ew.mul(frames, window) # broadcast [win_length]
|
|
416
|
+
# centre a short window inside the n_fft buffer (uncommon; here win == n_fft)
|
|
417
|
+
if win_length < n_fft:
|
|
418
|
+
pad_total = n_fft - win_length
|
|
419
|
+
left = pad_total // 2
|
|
420
|
+
frames = constant_pad_nd(frames, [left, pad_total - left], 0.0)
|
|
421
|
+
|
|
422
|
+
spec = fft_r2c_wrapper(frames, dim=-1, onesided=onesided) # [..., num_frames, freq]
|
|
423
|
+
spec = spec.transpose(-1, -2).contiguous() # [..., freq, num_frames]
|
|
424
|
+
if squeezed:
|
|
425
|
+
spec = spec.squeeze(0)
|
|
426
|
+
return spec
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def istft_wrapper(x, n_fft, hop_length=None, win_length=None, window=None,
|
|
430
|
+
center=True, normalized=False, onesided=None, length=None,
|
|
431
|
+
return_complex=False) :
|
|
432
|
+
"""aten::istft — inverse STFT: irfft each frame + windowed overlap-add +
|
|
433
|
+
window-envelope normalisation. Built on fft_c2r_wrapper (irfft) and
|
|
434
|
+
unfold_backward_wrapper (the overlap-add scatter). Model-agnostic, R33-pure.
|
|
435
|
+
Used by the chatterbox/CosyVoice s3gen vocoder (n_fft=16, hop=4)."""
|
|
436
|
+
if isinstance(n_fft, (list, tuple)):
|
|
437
|
+
n_fft = n_fft[0]
|
|
438
|
+
hop_length = (n_fft // 4) if hop_length in (None, 0) else hop_length
|
|
439
|
+
win_length = n_fft if win_length in (None, 0) else win_length
|
|
440
|
+
|
|
441
|
+
x = _core._ensure_cuda(x)
|
|
442
|
+
squeezed = (x.ndim == 2)
|
|
443
|
+
if squeezed:
|
|
444
|
+
x = x.unsqueeze(0) # [1, freq, frames]
|
|
445
|
+
# [batch, freq, frames] -> [batch, frames, freq]
|
|
446
|
+
xt = x.transpose(-1, -2).contiguous()
|
|
447
|
+
n_frames = xt.shape[-2]
|
|
448
|
+
batch = xt.shape[0]
|
|
449
|
+
# irfft each frame -> real [batch, frames, n_fft]
|
|
450
|
+
frames = fft_c2r_wrapper(xt, dim=-1, last_dim_size=n_fft)
|
|
451
|
+
if window is not None:
|
|
452
|
+
frames = _ew.mul(frames, window) # synthesis window
|
|
453
|
+
|
|
454
|
+
sig_len = (n_frames - 1) * hop_length + win_length
|
|
455
|
+
# overlap-add the windowed frames back into the signal
|
|
456
|
+
signal = _cnv.unfold_backward_wrapper(frames, [batch, sig_len], 1, win_length, hop_length)
|
|
457
|
+
# window-energy envelope (sum of squared windows at each position) — torch.istft
|
|
458
|
+
# divides by this so overlapping windows reconstruct unit gain.
|
|
459
|
+
if window is not None:
|
|
460
|
+
win_sq = _ew.mul(window, window)
|
|
461
|
+
else:
|
|
462
|
+
win_sq = NBXTensor.ones((win_length,), dtype=frames.nbx_dtype, device=frames.device)
|
|
463
|
+
win_sq_b = win_sq.reshape([1, 1, win_length])
|
|
464
|
+
win_sq_frames = _ew.mul(NBXTensor.ones((batch, n_frames, win_length),
|
|
465
|
+
dtype=frames.nbx_dtype, device=frames.device),
|
|
466
|
+
win_sq_b)
|
|
467
|
+
win_env = _cnv.unfold_backward_wrapper(win_sq_frames, [batch, sig_len], 1, win_length, hop_length)
|
|
468
|
+
signal = _ew.div(signal, _ew.clamp(win_env, 1e-11, None))
|
|
469
|
+
|
|
470
|
+
if center:
|
|
471
|
+
pad = n_fft // 2
|
|
472
|
+
signal = signal.narrow(-1, pad, sig_len - 2 * pad)
|
|
473
|
+
if length is not None:
|
|
474
|
+
cur = signal.shape[-1]
|
|
475
|
+
if length <= cur:
|
|
476
|
+
signal = signal.narrow(-1, 0, length)
|
|
477
|
+
else:
|
|
478
|
+
signal = constant_pad_nd(signal, [0, length - cur], 0.0)
|
|
479
|
+
if squeezed:
|
|
480
|
+
signal = signal.squeeze(0)
|
|
481
|
+
return signal
|
nbxtensor/ops/sqrt.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Square root — pure @triton.jit kernel."""
|
|
2
|
+
|
|
3
|
+
import triton
|
|
4
|
+
import triton.language as tl
|
|
5
|
+
|
|
6
|
+
@triton.jit
|
|
7
|
+
def sqrt_forward_kernel(
|
|
8
|
+
input_ptr, output_ptr,
|
|
9
|
+
n_elements,
|
|
10
|
+
BLOCK_SIZE: tl.constexpr,
|
|
11
|
+
):
|
|
12
|
+
pid = tl.program_id(0).to(tl.int64)
|
|
13
|
+
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)
|
|
14
|
+
mask = offset < n_elements
|
|
15
|
+
|
|
16
|
+
x = tl.load(input_ptr + offset, mask=mask)
|
|
17
|
+
tl.store(output_ptr + offset, tl.sqrt(x.to(tl.float32)), mask=mask)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Stack — pure @triton.jit kernel.
|
|
2
|
+
|
|
3
|
+
Stack = unsqueeze + cat along new dim. Copies up to 4 input tensors
|
|
4
|
+
per kernel launch into the output tensor at their respective offsets.
|
|
5
|
+
Extracted from FlagGems (Apache 2.0).
|
|
6
|
+
|
|
7
|
+
Grid: (ceil(max_elements / BLOCK_X), num_tensors_in_batch)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import triton
|
|
11
|
+
import triton.language as tl
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@triton.jit
|
|
15
|
+
def stack_copy_kernel(
|
|
16
|
+
out_ptr,
|
|
17
|
+
in_ptr_a,
|
|
18
|
+
in_ptr_b,
|
|
19
|
+
in_ptr_c,
|
|
20
|
+
in_ptr_d,
|
|
21
|
+
dim_size_in_a,
|
|
22
|
+
dim_size_in_b,
|
|
23
|
+
dim_size_in_c,
|
|
24
|
+
dim_size_in_d,
|
|
25
|
+
dim_size_out,
|
|
26
|
+
dim_prod_post,
|
|
27
|
+
dim_offset_a,
|
|
28
|
+
dim_offset_b,
|
|
29
|
+
dim_offset_c,
|
|
30
|
+
dim_offset_d,
|
|
31
|
+
total_elements_a,
|
|
32
|
+
total_elements_b,
|
|
33
|
+
total_elements_c,
|
|
34
|
+
total_elements_d,
|
|
35
|
+
BLOCK_X: tl.constexpr,
|
|
36
|
+
):
|
|
37
|
+
"""Copies up to 4 tensors into stacked output at correct offsets.
|
|
38
|
+
|
|
39
|
+
pid_y selects which of the 4 tensors this program handles.
|
|
40
|
+
pid_x tiles over the elements of that tensor.
|
|
41
|
+
|
|
42
|
+
The output index is computed from the input flat index by decomposing
|
|
43
|
+
into (pre, in_dim, post) and inserting the stack dimension offset.
|
|
44
|
+
"""
|
|
45
|
+
pid_x = tl.program_id(0).to(tl.int64)
|
|
46
|
+
pid_y = tl.program_id(1).to(tl.int64)
|
|
47
|
+
|
|
48
|
+
if pid_y == 0:
|
|
49
|
+
in_ptr = in_ptr_a
|
|
50
|
+
elif pid_y == 1:
|
|
51
|
+
in_ptr = in_ptr_b
|
|
52
|
+
elif pid_y == 2:
|
|
53
|
+
in_ptr = in_ptr_c
|
|
54
|
+
else:
|
|
55
|
+
in_ptr = in_ptr_d
|
|
56
|
+
|
|
57
|
+
# The per-input scalars are selected OUTSIDE the pointer branches, by tl.where,
|
|
58
|
+
# which unifies their types: Triton specialises a Python int as int32 when it
|
|
59
|
+
# fits, int64 when it does not, and as a constexpr when it equals 1 — so one
|
|
60
|
+
# input past 2^31 elements beside a small one made the if/elif branches
|
|
61
|
+
# disagree (`Mismatched type for total_elements`, Open-Sora-v2 VAE decode,
|
|
62
|
+
# 2026-09-25), and a `.to(tl.int64)` inside the branches broke on the
|
|
63
|
+
# constexpr form ('int' object has no attribute 'to', Flex.1-alpha).
|
|
64
|
+
dim_offset = tl.where(pid_y == 0, dim_offset_a, tl.where(pid_y == 1, dim_offset_b, tl.where(pid_y == 2, dim_offset_c, dim_offset_d)))
|
|
65
|
+
total_elements = tl.where(pid_y == 0, total_elements_a, tl.where(pid_y == 1, total_elements_b, tl.where(pid_y == 2, total_elements_c, total_elements_d)))
|
|
66
|
+
|
|
67
|
+
block_start = pid_x * BLOCK_X
|
|
68
|
+
offsets = tl.arange(0, BLOCK_X)
|
|
69
|
+
idx = block_start + offsets
|
|
70
|
+
mask = idx < total_elements
|
|
71
|
+
|
|
72
|
+
# Decompose flat index into (pre_idx, post_idx) around the stack dim
|
|
73
|
+
pre_idx = idx // dim_prod_post
|
|
74
|
+
post_idx = idx % dim_prod_post
|
|
75
|
+
|
|
76
|
+
# Compute output index with stack dimension inserted
|
|
77
|
+
out_idx = (
|
|
78
|
+
pre_idx * dim_size_out * dim_prod_post
|
|
79
|
+
+ dim_offset * dim_prod_post
|
|
80
|
+
+ post_idx
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
data = tl.load(in_ptr + idx, mask=mask)
|
|
84
|
+
tl.store(out_ptr + out_idx, data, mask=mask)
|