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.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/lib/tilework/backend/cpu/dsl_codegen.rb +509 -0
  4. data/lib/tilework/backend/cpu/ops_codegen.rb +451 -0
  5. data/lib/tilework/backend/cpu.rb +156 -0
  6. data/lib/tilework/buffer.rb +753 -0
  7. data/lib/tilework/capabilities.rb +183 -0
  8. data/lib/tilework/compat/cuda.h +66 -0
  9. data/lib/tilework/compat/hip.h +35 -0
  10. data/lib/tilework/compat/metal.h +45 -0
  11. data/lib/tilework/conformance.rb +2296 -0
  12. data/lib/tilework/core_ext.rb +21 -0
  13. data/lib/tilework/dialect.rb +67 -0
  14. data/lib/tilework/dsl.rb +722 -0
  15. data/lib/tilework/dtype/bfloat.rb +31 -0
  16. data/lib/tilework/dtype/cast.rb +88 -0
  17. data/lib/tilework/dtype/half.rb +98 -0
  18. data/lib/tilework/graph/builder.rb +1073 -0
  19. data/lib/tilework/graph/fusion.rb +520 -0
  20. data/lib/tilework/graph/grad.rb +443 -0
  21. data/lib/tilework/graph/high_level.rb +266 -0
  22. data/lib/tilework/graph/kernels.rb +403 -0
  23. data/lib/tilework/graph/node.rb +27 -0
  24. data/lib/tilework/graph/op.rb +290 -0
  25. data/lib/tilework/graph/optim.rb +183 -0
  26. data/lib/tilework/graph/plan.rb +1086 -0
  27. data/lib/tilework/graph/pool.rb +178 -0
  28. data/lib/tilework/graph/region.rb +210 -0
  29. data/lib/tilework/graph/residency.rb +449 -0
  30. data/lib/tilework/graph/state.rb +64 -0
  31. data/lib/tilework/graph/tensor.rb +124 -0
  32. data/lib/tilework/graph.rb +213 -0
  33. data/lib/tilework/kernel.rb +163 -0
  34. data/lib/tilework/math/conv.rb +270 -0
  35. data/lib/tilework/math/fft.rb +172 -0
  36. data/lib/tilework/math/nn.rb +1103 -0
  37. data/lib/tilework/math/philox.rb +194 -0
  38. data/lib/tilework/math/random.rb +232 -0
  39. data/lib/tilework/math/tensor_core.rb +269 -0
  40. data/lib/tilework/ops.rb +682 -0
  41. data/lib/tilework/runtime/offload.rb +498 -0
  42. data/lib/tilework/runtime/submission.rb +163 -0
  43. data/lib/tilework/runtime/telemetry.rb +112 -0
  44. data/lib/tilework/scope.rb +33 -0
  45. data/lib/tilework/version.rb +5 -0
  46. data/lib/tilework.rb +345 -0
  47. metadata +92 -0
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tilework
4
+ # IEEE-754 bfloat16 and Ruby Float conversion. BF16 is the upper 16 bits of
5
+ # binary32, rounded to nearest with ties to even.
6
+ module BFloat
7
+ BYTES = 2
8
+
9
+ module_function
10
+
11
+ def encode(array)
12
+ array.map { |value| from_f(value) }.pack("S<*")
13
+ end
14
+
15
+ def decode(bytes)
16
+ bytes.unpack("S<*").map { |bits| to_f(bits) }
17
+ end
18
+
19
+ def from_f(value)
20
+ bits = [value.to_f].pack("e").unpack1("L<")
21
+ # Canonicalize NaNs so rounding cannot turn a signaling NaN into Inf.
22
+ return ((bits >> 16) | 0x0040) & 0xFFFF if (bits & 0x7F80_0000) == 0x7F80_0000 && (bits & 0x007F_FFFF) != 0
23
+
24
+ ((bits + 0x7FFF + ((bits >> 16) & 1)) >> 16) & 0xFFFF
25
+ end
26
+
27
+ def to_f(bits)
28
+ [Integer(bits) << 16].pack("L<").unpack1("e")
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tilework
4
+ # dtype conversion on the GPU.
5
+ #
6
+ # The pure-Ruby Tilework::Half does per-element bit manipulation, so loading
7
+ # large weights is host-bound. The conversion is element-independent, so it
8
+ # can go to the GPU.
9
+ #
10
+ # **Both round to nearest even, so the results are bit-identical.**
11
+ module Cast
12
+ # source -> target. The kernels are written in the CUDA-flavored dialect,
13
+ # so the same source compiles on every backend with a compat header. A C
14
+ # cast to / from GPU_HALF becomes __half's converting constructor
15
+ # (= __float2half) on CUDA and half's implicit conversion in MSL — both
16
+ # round to nearest even.
17
+ CONVERSIONS = {
18
+ %i[float32 float16] => { in: "float", out: "GPU_HALF" },
19
+ %i[float16 float32] => { in: "GPU_HALF", out: "float" },
20
+ %i[float32 bfloat16] => { in: "float", out: "GPU_BFLOAT" },
21
+ %i[bfloat16 float32] => { in: "GPU_BFLOAT", out: "float" },
22
+ }.freeze
23
+
24
+ BLOCK = 256
25
+
26
+ module_function
27
+
28
+ # Being in the table is not enough: a backend that does not accept dialect
29
+ # kernels (:cpu), or cannot allocate the dtype, cannot convert. Without this
30
+ # capability gate Buffer#gpu_conversion? would answer yes and hit a
31
+ # CompileError.
32
+ def supported?(from, to)
33
+ return false unless CONVERSIONS.key?([from, to])
34
+
35
+ caps = Tilework.capabilities
36
+ caps[:dialect] && caps[:dtypes].include?(from) && caps[:dtypes].include?(to)
37
+ end
38
+
39
+ def convert(buffer, target)
40
+ # Inside a graph the result is an ordinary node output that returns to
41
+ # the pool. Allocating it directly would never borrow that slot back, so
42
+ # the pool would grow on every repeated conversion (e.g. a low-precision
43
+ # matmul's out_dtype).
44
+ out = Ops.allocate_output(target, buffer.length, buffer.shape)
45
+ if Tilework.host_fallback?
46
+ out.write(buffer.to_a)
47
+ return out
48
+ end
49
+ convert_into(buffer, out)
50
+ out
51
+ rescue StandardError
52
+ out&.free
53
+ raise
54
+ end
55
+
56
+ # Converts into an existing Buffer. Buffer#write uses it to fill fp16 via a
57
+ # temporary float32 buffer (it cannot create a new Buffer there).
58
+ def convert_into(src, dst)
59
+ spec = CONVERSIONS[[src.dtype, dst.dtype]] or
60
+ raise Error, "cannot convert #{src.dtype.inspect} to #{dst.dtype.inspect} " \
61
+ "(supported: #{CONVERSIONS.keys.map { |f, t| "#{f}->#{t}" }.join(', ')})"
62
+ unless supported?(src.dtype, dst.dtype)
63
+ raise Error, "the #{Tilework.backend.inspect} backend cannot convert " \
64
+ "#{src.dtype.inspect} to #{dst.dtype.inspect}"
65
+ end
66
+ unless src.length == dst.length
67
+ raise ArgumentError, "length mismatch: #{src.length} -> #{dst.length}"
68
+ end
69
+
70
+ kernel = Tilework.compile(source(spec), name: "cast_kernel")
71
+ Ops.launch_kernel(kernel, grid: (src.length / BLOCK.to_f).ceil, block: BLOCK,
72
+ args: [src, dst, src.length])
73
+ dst
74
+ end
75
+
76
+ def source(spec)
77
+ <<~KERNEL
78
+ GPU_KERNEL void cast_kernel(GPU_ARGS,
79
+ device const #{spec[:in]}* src,
80
+ device #{spec[:out]}* dst,
81
+ constant int& n) {
82
+ int i = GLOBAL_ID_X;
83
+ if (i < n) dst[i] = (#{spec[:out]})src[i];
84
+ }
85
+ KERNEL
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tilework
4
+ # Conversion between IEEE-754 binary16 (half precision) and Ruby Float.
5
+ #
6
+ # Ruby's Array#pack has no half-precision directive (f/e/g are 4 bytes,
7
+ # d/E/G are 8), so Buffer's "one pack directive per dtype" design does not
8
+ # work for float16. This is the only implementation of the conversion and is
9
+ # referenced from Buffer::DTYPES as encode/decode.
10
+ #
11
+ # **This is the reference implementation of the conversion** and runs without
12
+ # a GPU, so CI verifies it. Large arrays are converted by Buffer#cast with the
13
+ # GPU's __float2half / __half2float; both round to nearest even, so **the two
14
+ # must agree bit for bit**.
15
+ module Half
16
+ BYTES = 2
17
+
18
+ # binary16: 1 sign bit / 5 exponent bits (bias 15) / 10 mantissa bits
19
+ MANT_BITS = 10
20
+ EXP_BIAS = 15
21
+ # binary32: 8 exponent bits (bias 127) / 23 mantissa bits
22
+ F32_MANT_BITS = 23
23
+ F32_EXP_BIAS = 127
24
+
25
+ MAX_FINITE = 65_504.0 # largest finite half value
26
+ MIN_NORMAL = 2.0**-14 # smallest normal (about 6.104e-5)
27
+ MIN_SUBNORMAL = 2.0**-24 # smallest subnormal (about 5.96e-8)
28
+
29
+ module_function
30
+
31
+ # Array<Numeric> -> String (2 bytes per element, little-endian)
32
+ def encode(array)
33
+ array.map { |v| from_f(v) }.pack("S<*")
34
+ end
35
+
36
+ # String -> Array<Float>
37
+ def decode(bytes)
38
+ bytes.unpack("S<*").map { |bits| to_f(bits) }
39
+ end
40
+
41
+ # Float -> binary16 bit pattern (Integer), rounding to nearest even.
42
+ def from_f(value)
43
+ # "e" / "L<" pin the endianness (independent of the host)
44
+ bits = [value.to_f].pack("e").unpack1("L<")
45
+ sign = (bits >> 31) << 15
46
+ exp = (bits >> F32_MANT_BITS) & 0xFF
47
+ mant = bits & ((1 << F32_MANT_BITS) - 1)
48
+
49
+ # Inf / NaN. NaN keeps a nonzero mantissa (zero would turn it into Inf)
50
+ return sign | 0x7C00 | (mant.zero? ? 0 : 0x0200) if exp == 0xFF
51
+
52
+ new_exp = exp - F32_EXP_BIAS + EXP_BIAS # rebias to binary16
53
+
54
+ if new_exp >= 0x1F
55
+ sign | 0x7C00 # overflow -> Inf
56
+ elsif new_exp > 0
57
+ # Normal. Round the mantissa from 23 to 10 bits. If rounding up
58
+ # overflows it to 1<<10, the addition carries into the exponent (and
59
+ # reaching 0x1F there correctly gives Inf).
60
+ sign | ((new_exp << MANT_BITS) + round_shift(mant, F32_MANT_BITS - MANT_BITS))
61
+ elsif new_exp > -MANT_BITS - 1
62
+ # Subnormal. Restore the implicit 1 and round mant16 = full >> (14 - new_exp).
63
+ # Rounding up to 1<<10 yields the smallest normal as is.
64
+ full = mant | (1 << F32_MANT_BITS)
65
+ sign | round_shift(full, F32_MANT_BITS - MANT_BITS + 1 - new_exp)
66
+ else
67
+ sign # too small -> signed zero
68
+ end
69
+ end
70
+
71
+ # binary16 bit pattern -> Float
72
+ def to_f(bits)
73
+ sign = (bits >> 15).zero? ? 1.0 : -1.0
74
+ exp = (bits >> MANT_BITS) & 0x1F
75
+ mant = bits & ((1 << MANT_BITS) - 1)
76
+
77
+ case exp
78
+ when 0x1F then mant.zero? ? sign * Float::INFINITY : Float::NAN
79
+ when 0 then sign * mant * MIN_SUBNORMAL # subnormal (and ±0)
80
+ else sign * (1.0 + mant.fdiv(1 << MANT_BITS)) * 2.0**(exp - EXP_BIAS)
81
+ end
82
+ end
83
+
84
+ # Drops the low `shift` bits with round-to-nearest-even. Ties go to the
85
+ # even side; that is the "-even" part.
86
+ def round_shift(mant, shift)
87
+ return 0 if shift > F32_MANT_BITS + 1 # every bit is dropped
88
+
89
+ dropped = mant & ((1 << shift) - 1)
90
+ kept = mant >> shift
91
+ halfway = 1 << (shift - 1)
92
+
93
+ kept += 1 if dropped > halfway || (dropped == halfway && kept.odd?)
94
+ kept
95
+ end
96
+ private_class_method :round_shift
97
+ end
98
+ end