tilework 0.18.4
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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/lib/tilework/backend/cpu/dsl_codegen.rb +509 -0
- data/lib/tilework/backend/cpu/ops_codegen.rb +451 -0
- data/lib/tilework/backend/cpu.rb +156 -0
- data/lib/tilework/buffer.rb +753 -0
- data/lib/tilework/capabilities.rb +183 -0
- data/lib/tilework/compat/cuda.h +66 -0
- data/lib/tilework/compat/hip.h +35 -0
- data/lib/tilework/compat/metal.h +45 -0
- data/lib/tilework/conformance.rb +2296 -0
- data/lib/tilework/core_ext.rb +21 -0
- data/lib/tilework/dialect.rb +67 -0
- data/lib/tilework/dsl.rb +722 -0
- data/lib/tilework/dtype/bfloat.rb +31 -0
- data/lib/tilework/dtype/cast.rb +88 -0
- data/lib/tilework/dtype/half.rb +98 -0
- data/lib/tilework/graph/builder.rb +1073 -0
- data/lib/tilework/graph/fusion.rb +520 -0
- data/lib/tilework/graph/grad.rb +443 -0
- data/lib/tilework/graph/high_level.rb +266 -0
- data/lib/tilework/graph/kernels.rb +403 -0
- data/lib/tilework/graph/node.rb +27 -0
- data/lib/tilework/graph/op.rb +290 -0
- data/lib/tilework/graph/optim.rb +183 -0
- data/lib/tilework/graph/plan.rb +1086 -0
- data/lib/tilework/graph/pool.rb +178 -0
- data/lib/tilework/graph/region.rb +210 -0
- data/lib/tilework/graph/residency.rb +449 -0
- data/lib/tilework/graph/state.rb +64 -0
- data/lib/tilework/graph/tensor.rb +124 -0
- data/lib/tilework/graph.rb +213 -0
- data/lib/tilework/kernel.rb +163 -0
- data/lib/tilework/math/conv.rb +270 -0
- data/lib/tilework/math/fft.rb +172 -0
- data/lib/tilework/math/nn.rb +1103 -0
- data/lib/tilework/math/philox.rb +194 -0
- data/lib/tilework/math/random.rb +232 -0
- data/lib/tilework/math/tensor_core.rb +269 -0
- data/lib/tilework/ops.rb +682 -0
- data/lib/tilework/runtime/offload.rb +498 -0
- data/lib/tilework/runtime/submission.rb +163 -0
- data/lib/tilework/runtime/telemetry.rb +112 -0
- data/lib/tilework/scope.rb +33 -0
- data/lib/tilework/version.rb +5 -0
- data/lib/tilework.rb +345 -0
- metadata +92 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tilework
|
|
4
|
+
# Fast Fourier transforms provided by an available backend library.
|
|
5
|
+
#
|
|
6
|
+
# **Complex numbers are interleaved float32.** A complex array of length N
|
|
7
|
+
# is a `:float32` Buffer of length 2N laid out as `[re0, im0, re1, im1, ...]`.
|
|
8
|
+
# That is exactly cuFFT's `cufftComplex`, so a Buffer can be passed as is
|
|
9
|
+
# (a complex dtype would ripple through the whole Ops / DSL type system).
|
|
10
|
+
#
|
|
11
|
+
# Intended for audio and signal processing (spectrogram → MFCC →
|
|
12
|
+
# classification) and large-kernel convolution. **Not for a diffusion
|
|
13
|
+
# model's 3x3 convolutions** — FFT convolution only wins at kernel sizes of
|
|
14
|
+
# about 15–20 and up; small kernels are faster as direct (im2col + GEMM).
|
|
15
|
+
module FFT
|
|
16
|
+
R2C = 0
|
|
17
|
+
C2R = 1
|
|
18
|
+
C2C = 2
|
|
19
|
+
FORWARD = 1
|
|
20
|
+
INVERSE = -1
|
|
21
|
+
|
|
22
|
+
module_function
|
|
23
|
+
|
|
24
|
+
# @return [Boolean] whether the active backend exposes an FFT library
|
|
25
|
+
def available?
|
|
26
|
+
return false unless Tilework.backend
|
|
27
|
+
return false unless Tilework.bridge.respond_to?(:fft_available?)
|
|
28
|
+
|
|
29
|
+
Tilework.bridge.fft_available?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @return [Symbol, nil] backend FFT library identifier
|
|
33
|
+
def library
|
|
34
|
+
return nil unless available?
|
|
35
|
+
return Tilework.bridge.fft_library_name if Tilework.bridge.respond_to?(:fft_library_name)
|
|
36
|
+
|
|
37
|
+
:cufft
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Performs a real-to-complex transform.
|
|
41
|
+
#
|
|
42
|
+
# @param input [Buffer] real float32 values
|
|
43
|
+
# @param batch [Integer] number of equal-length signals
|
|
44
|
+
# @return [Buffer] interleaved float32 complex spectrum
|
|
45
|
+
# @raise [Error] if no FFT library is available
|
|
46
|
+
#
|
|
47
|
+
# Real -> complex (R2C). A real signal of length N gives a complex spectrum
|
|
48
|
+
# of length **N/2+1** (a real signal's spectrum is conjugate-symmetric, so
|
|
49
|
+
# the rest is redundant).
|
|
50
|
+
#
|
|
51
|
+
# spec = Tilework::FFT.forward(signal) # float32 of length 2*(N/2+1)
|
|
52
|
+
# Tilework::FFT.magnitude(spec) # magnitude spectrum (length N/2+1)
|
|
53
|
+
#
|
|
54
|
+
# batch: transforms several equal-length signals at once (for spectrograms).
|
|
55
|
+
def forward(input, batch: 1)
|
|
56
|
+
require_available!
|
|
57
|
+
n = input.length / batch
|
|
58
|
+
check_divisible!(input.length, batch, "input")
|
|
59
|
+
|
|
60
|
+
out = Buffer.new(:float32, 2 * (n / 2 + 1) * batch)
|
|
61
|
+
Tilework.bridge.fft(input.__handle__, out.__handle__, n, batch, R2C, FORWARD)
|
|
62
|
+
out
|
|
63
|
+
rescue StandardError
|
|
64
|
+
out&.free
|
|
65
|
+
raise
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Performs a complex-to-real inverse transform.
|
|
69
|
+
#
|
|
70
|
+
# @param input [Buffer] interleaved float32 complex spectrum
|
|
71
|
+
# @param n [Integer] original real signal length
|
|
72
|
+
# @param batch [Integer] number of signals
|
|
73
|
+
# @param normalize [Boolean] scale the inverse by `1 / n`
|
|
74
|
+
# @return [Buffer]
|
|
75
|
+
# @note The backend may modify the input buffer.
|
|
76
|
+
#
|
|
77
|
+
# cuFFT does not normalize, so 1/n is applied by default to make
|
|
78
|
+
# `inverse(forward(x)) == x` hold (the same convention as numpy's ifft).
|
|
79
|
+
# Pass normalize: false for raw cuFFT values.
|
|
80
|
+
#
|
|
81
|
+
# **The input complex buffer may be overwritten** (cuFFT's C2R behavior).
|
|
82
|
+
# Copy it first if it is needed afterwards.
|
|
83
|
+
def inverse(input, n, batch: 1, normalize: true)
|
|
84
|
+
require_available!
|
|
85
|
+
expected = 2 * (n / 2 + 1) * batch
|
|
86
|
+
unless input.length == expected
|
|
87
|
+
raise ArgumentError,
|
|
88
|
+
"expected #{expected} floats for n=#{n} batch=#{batch}, got #{input.length}"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
out = Buffer.new(:float32, n * batch)
|
|
92
|
+
Tilework.bridge.fft(input.__handle__, out.__handle__, n, batch, C2R, INVERSE)
|
|
93
|
+
normalize ? scaled(out, 1.0 / n) : out
|
|
94
|
+
rescue StandardError
|
|
95
|
+
out&.free
|
|
96
|
+
raise
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Performs a complex-to-complex transform.
|
|
100
|
+
#
|
|
101
|
+
# @param input [Buffer] interleaved float32 complex values
|
|
102
|
+
# @param direction [:forward, :inverse]
|
|
103
|
+
# @param batch [Integer]
|
|
104
|
+
# @param normalize [Boolean] scale inverse output by `1 / n`
|
|
105
|
+
# @return [Buffer]
|
|
106
|
+
def transform(input, direction: :forward, batch: 1, normalize: true)
|
|
107
|
+
require_available!
|
|
108
|
+
check_even!(input.length)
|
|
109
|
+
n = input.length / 2 / batch
|
|
110
|
+
check_divisible!(input.length / 2, batch, "input (complex)")
|
|
111
|
+
|
|
112
|
+
dir = direction == :inverse ? INVERSE : FORWARD
|
|
113
|
+
out = Buffer.new(:float32, input.length)
|
|
114
|
+
Tilework.bridge.fft(input.__handle__, out.__handle__, n, batch, C2C, dir)
|
|
115
|
+
dir == INVERSE && normalize ? scaled(out, 1.0 / n) : out
|
|
116
|
+
rescue StandardError
|
|
117
|
+
out&.free
|
|
118
|
+
raise
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Returns magnitudes for interleaved complex values.
|
|
122
|
+
#
|
|
123
|
+
# @param complex [Buffer] interleaved float32 complex values
|
|
124
|
+
# @return [Buffer] real float32 magnitudes
|
|
125
|
+
def magnitude(complex)
|
|
126
|
+
check_even!(complex.length)
|
|
127
|
+
n = complex.length / 2
|
|
128
|
+
magnitude_kernel.(complex, n, over: n)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# --- Internal ---
|
|
132
|
+
|
|
133
|
+
def magnitude_kernel
|
|
134
|
+
@magnitude_kernel ||= Tilework.kernel("fft_magnitude", out: 1) do |z, out, n|
|
|
135
|
+
i = global_id_x
|
|
136
|
+
if i < n
|
|
137
|
+
re = z[2 * i]
|
|
138
|
+
im = z[(2 * i) + 1]
|
|
139
|
+
out[i] = Math.sqrt((re * re) + (im * im))
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def scaled(buffer, factor)
|
|
145
|
+
scaled = buffer * factor
|
|
146
|
+
buffer.free
|
|
147
|
+
scaled
|
|
148
|
+
rescue StandardError
|
|
149
|
+
buffer.free
|
|
150
|
+
raise
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def require_available!
|
|
154
|
+
return if available?
|
|
155
|
+
|
|
156
|
+
raise Error, "no FFT library is available on the #{Tilework.backend.inspect} backend " \
|
|
157
|
+
"(cuFFT is used on :cuda)"
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def check_even!(length)
|
|
161
|
+
return if length.even?
|
|
162
|
+
|
|
163
|
+
raise ArgumentError, "a complex buffer must have an even length (got #{length})"
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def check_divisible!(total, batch, label)
|
|
167
|
+
return if (total % batch).zero?
|
|
168
|
+
|
|
169
|
+
raise ArgumentError, "#{label} length #{total} is not divisible by batch #{batch}"
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|