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,183 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tilework
4
+ # Immutable feature and device-property values for a backend.
5
+ # The value object returned by Tilework.capabilities. FIELDS is the single
6
+ # source of the key set, types, defaults and meanings; upper layers (ops.rb /
7
+ # dsl.rb / buffer.rb / nn.rb / tensor_core.rb) pick paths from it.
8
+ #
9
+ # Plain Hashes were dropped because of typos: caps[:tensro_core] on a Hash
10
+ # quietly reads nil ("unsupported"), but raises KeyError here. When adding a
11
+ # backend, FIELDS is what tells you what to declare.
12
+ #
13
+ # caps = Tilework.capabilities
14
+ # caps[:warp_size] # => 32 (KeyError for an unknown key)
15
+ # caps.warp_size # => 32 (preferred form)
16
+ class Capabilities
17
+ # type is a small validation vocabulary (:boolean / :boolean_or_nil /
18
+ # :integer_or_nil / :name_or_nil / :array / :array_or_nil / :any); :any
19
+ # means "not validated".
20
+ Field = Struct.new(:name, :type, :default, :doc)
21
+
22
+ FIELDS = [
23
+ Field.new(:tensor_core, :boolean, false,
24
+ "行列積専用ユニット (Tensor Core / SIMD group matrix) を使えるか"),
25
+ Field.new(:tf32, :boolean, false,
26
+ "FP32 バッファをそのまま Tensor Core に入力できるか (Ampere 以降)"),
27
+ Field.new(:fp16, :boolean, false,
28
+ "FP16 の Tensor Core 経路があるか"),
29
+ Field.new(:dialect, :boolean, true,
30
+ "CUDA 風方言のカーネル文字列 (compile の raw: false) を受け付けるか"),
31
+ Field.new(:fp16_storage, :boolean, true,
32
+ "生成カーネルが float16 を格納型として扱えるか (Ops#require_computable!)"),
33
+ Field.new(:bf16_storage, :boolean, false,
34
+ "生成カーネルが bfloat16 を格納型として扱えるか"),
35
+ Field.new(:gemm_dtypes, :array, [].freeze,
36
+ "委譲 GEMM が受け付ける入力 dtype"),
37
+ Field.new(:conv_dtypes, :array, [].freeze,
38
+ "委譲 convolution が受け付ける dtype"),
39
+ Field.new(:dtypes, :array, %i[float32 int32].freeze,
40
+ "Buffer.new で確保できる dtype (Buffer#check_dtype_supported!)"),
41
+ Field.new(:offload, :boolean, true,
42
+ "VRAM↔ディスクの退避経路があるか。素朴な to_a -> File.write 経路は常に使える"),
43
+ Field.new(:offload_async, :boolean, false,
44
+ "非同期転送 ABI (transfer_begin/poll/end) があるか"),
45
+ Field.new(:offload_transport, :name_or_nil, nil,
46
+ "退避に使う転送方式 (:posix / :direct など)"),
47
+ Field.new(:host_backing, :boolean, false,
48
+ "不揮発ファイルではない pageable CPU RAM への明示退避があるか"),
49
+ Field.new(:host_backing_async, :boolean, false,
50
+ "pageable host backing との転送を非同期に完了できるか"),
51
+ Field.new(:gemm_library, :name_or_nil, nil,
52
+ "GEMM を委譲するライブラリ名 (cuBLAS 等)。nil なら自前カーネル"),
53
+ Field.new(:gemm_batched, :boolean, false,
54
+ "同じ形の GEMM をまとめて投げられるか (multi-head attention が速くなる)"),
55
+ Field.new(:rng, :name_or_nil, nil,
56
+ "乱数生成を委譲するライブラリ (cuRAND 等)。nil なら Ruby 側で作る"),
57
+ Field.new(:fft, :name_or_nil, nil,
58
+ "FFT を委譲するライブラリ (cuFFT 等)。nil なら FFT は使えない"),
59
+ Field.new(:conv, :name_or_nil, nil,
60
+ "畳み込みを委譲するライブラリ (cuDNN 等)。nil なら畳み込みは使えない"),
61
+ Field.new(:conv_backward, :boolean, false,
62
+ "畳み込みの逆伝播があるか (学習に必須)"),
63
+ Field.new(:warp_size, :integer_or_nil, nil,
64
+ "ロックステップで動く最小スレッド束の幅 (CUDA の warpSize)"),
65
+ Field.new(:unified_memory, :boolean_or_nil, nil,
66
+ "ホストとデバイスでメモリを共有しているか"),
67
+ Field.new(:memory_budget_bytes, :integer_or_nil, nil,
68
+ "確保に使えるデバイスメモリのおおよその上限"),
69
+ Field.new(:compute_capability, :array_or_nil, nil,
70
+ "デバイスの世代 ([major, minor])。持たないバックエンドは nil"),
71
+ ].freeze
72
+
73
+ NAMES = FIELDS.map(&:name).freeze
74
+ BY_NAME = FIELDS.to_h { |f| [f.name, f] }.freeze
75
+ DEFAULTS = FIELDS.to_h { |f| [f.name, f.default] }.freeze
76
+
77
+ class << self
78
+ # Builds validated capability values.
79
+ #
80
+ # @api private
81
+ # @param overrides [Hash{Symbol => Object}] values overriding documented defaults
82
+ # @return [Capabilities]
83
+ # @raise [ArgumentError] for unknown keys or incompatible values
84
+ #
85
+ # The only entry point for a backend's declarations; unknown keys and type
86
+ # mismatches are rejected here so nothing silently reads as nil.
87
+ def build(**overrides)
88
+ validate!(overrides)
89
+ new(DEFAULTS.merge(overrides))
90
+ end
91
+
92
+ # Drops keys outside the contract. Inputs that may carry keys we did not
93
+ # define, such as the raw hash from a bridge's optional
94
+ # device_capabilities, pass through this before build.
95
+ def slice_known(hash)
96
+ hash.select { |k, _| BY_NAME.key?(k) }
97
+ end
98
+
99
+ private
100
+
101
+ def validate!(overrides)
102
+ overrides.each do |key, value|
103
+ field = BY_NAME[key] or
104
+ raise ArgumentError, "unknown capability #{key.inspect}; known keys are #{NAMES.join(', ')}"
105
+ next if valid_type?(field.type, value)
106
+
107
+ raise ArgumentError,
108
+ "capability #{key.inspect} expects #{field.type} but got #{value.inspect}"
109
+ end
110
+ end
111
+
112
+ def valid_type?(type, value)
113
+ case type
114
+ when :boolean then [true, false].include?(value)
115
+ when :boolean_or_nil then value.nil? || [true, false].include?(value)
116
+ when :integer_or_nil then value.nil? || value.is_a?(Integer)
117
+ # A library name or transport identifier. core returns Symbols like
118
+ # :cublas, but a bridge declaring its own may use a String.
119
+ when :name_or_nil then value.nil? || value.is_a?(Symbol) || value.is_a?(String)
120
+ when :array then value.is_a?(Array)
121
+ when :array_or_nil then value.nil? || value.is_a?(Array)
122
+ else true # :any
123
+ end
124
+ end
125
+ end
126
+
127
+ FIELDS.each do |field|
128
+ define_method(field.name) { @values[field.name] }
129
+ end
130
+
131
+ def initialize(values)
132
+ @values = values.freeze
133
+ freeze
134
+ end
135
+
136
+ # Looks up a capability by name.
137
+ #
138
+ # @param key [Symbol, String]
139
+ # @return [Object]
140
+ # @raise [KeyError] for an unknown capability
141
+ def [](key)
142
+ @values.fetch(key) do
143
+ raise KeyError, "unknown capability #{key.inspect}; known keys are #{NAMES.join(', ')}"
144
+ end
145
+ end
146
+
147
+ def fetch(key, *default, &block)
148
+ @values.fetch(key, *default, &block)
149
+ end
150
+
151
+ def key?(key)
152
+ BY_NAME.key?(key)
153
+ end
154
+
155
+ # Returns a new instance with validated overrides.
156
+ #
157
+ # @param overrides [Array<Hash{Symbol => Object}>]
158
+ # @return [Capabilities]
159
+ #
160
+ # Takes several Hashes like Hash#merge, so callers keep the call style from
161
+ # when this was a Hash.
162
+ def merge(*overrides)
163
+ self.class.build(**overrides.reduce(@values) { |acc, h| acc.merge(h) })
164
+ end
165
+
166
+ def to_h
167
+ @values.dup
168
+ end
169
+
170
+ def ==(other)
171
+ other.is_a?(Capabilities) && other.to_h == @values
172
+ end
173
+ alias eql? ==
174
+
175
+ def hash
176
+ @values.hash
177
+ end
178
+
179
+ def inspect
180
+ "#<Tilework::Capabilities #{@values.map { |k, v| "#{k}=#{v.inspect}" }.join(' ')}>"
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,66 @@
1
+ // tilework compat header (CUDA) — compiled by NVRTC.
2
+ // Expands the CUDA-flavored dialect's macro set into CUDA C++. This set is the
3
+ // de facto kernel ABI; keep it 1:1 with compat/metal.h (add a macro only if
4
+ // its semantics can be guaranteed identical on every backend).
5
+ //
6
+ // NVRTC provides built-ins like blockIdx / threadIdx, so no include is needed.
7
+
8
+ // extern "C" is required: with C++ name mangling cuModuleGetFunction(name)
9
+ // would not match the dialect's name: and fail.
10
+ #define GPU_KERNEL extern "C" __global__
11
+
12
+ // 32-bit unsigned integer, 1:1 with MSL's uint (CUDA has no `uint` spelling,
13
+ // so it expands to unsigned int).
14
+ #define GPU_UINT unsigned int
15
+ // Device-callable helper. Without the annotation it would be a host function
16
+ // that __global__ cannot call; this is the counterpart of Metal's inline.
17
+ #define GPU_FUNC __device__ inline
18
+
19
+ // Half precision, **as a storage type only** — arithmetic is done after
20
+ // widening to float (accumulation is always FP32). __half has float
21
+ // conversion operators from __CUDA_ARCH__ >= 530, so reads work with a
22
+ // (float) cast and writes through implicit conversion on assignment.
23
+ #include <cuda_fp16.h>
24
+ #define GPU_HALF __half
25
+ #include <cuda_bf16.h>
26
+ #define GPU_BFLOAT __nv_bfloat16
27
+ #define GPU_BFLOAT_TO_FLOAT(x) __bfloat162float(x)
28
+ #define GPU_FLOAT_TO_BFLOAT(x) __float2bfloat16(x)
29
+
30
+ // CUDA has no counterpart to Metal's implicit arguments
31
+ // (thread_position_in_grid etc.); built-in variables are readable anywhere,
32
+ // so the arguments expand to nothing. Dialect::CUDA_REWRITES removes
33
+ // `GPU_ARGS,` together with its comma, so only bare uses like `f(GPU_ARGS)`
34
+ // reach this macro.
35
+ #define GPU_ARGS
36
+
37
+ #define GLOBAL_ID_X ((int)(blockIdx.x * blockDim.x + threadIdx.x))
38
+ #define GLOBAL_ID_Y ((int)(blockIdx.y * blockDim.y + threadIdx.y))
39
+ #define LOCAL_ID_X ((int)threadIdx.x)
40
+ #define GROUP_ID_X ((int)blockIdx.x)
41
+ #define GPU_SHARED __shared__
42
+ // Metal expands this to threadgroup_barrier(mem_threadgroup) (same semantics)
43
+ #define GPU_BARRIER __syncthreads()
44
+
45
+ // MSL address-space qualifiers (device / constant) are **not** erased here.
46
+ // `#define device` is tempting, but CUDA's host_defines.h defines
47
+ // `__device__` as `__location__(device)`, so an empty `device` macro would
48
+ // collapse __device__ into __location__() and leave every later function
49
+ // without an execution-space annotation, failing compilation. Dialect::
50
+ // CUDA_REWRITES strips the qualifiers from the dialect source only (CUDA's
51
+ // own headers are untouched, so there is no side effect).
52
+
53
+ #ifndef INFINITY
54
+ #define INFINITY (__int_as_float(0x7f800000))
55
+ #endif
56
+
57
+ // MSL has clamp but CUDA does not, so provide it. min/max/exp/log/sqrt/sin/
58
+ // cos/tanh/floor/ceil/round/pow/abs/fabs resolve to CUDA built-ins.
59
+ template <typename T>
60
+ __device__ __forceinline__ T clamp(T x, T lo, T hi) {
61
+ return x < lo ? lo : (x > hi ? hi : x);
62
+ }
63
+
64
+ // CUDA has erff natively. MSL does not, so compat/metal.h approximates it
65
+ // (GPU_ERF is 1:1 across the headers).
66
+ #define GPU_ERF(x) erff(x)
@@ -0,0 +1,35 @@
1
+ // tilework compat header (HIP) — compiled by HIPRTC.
2
+ // Same macro set as compat/cuda.h; only the fp16 / bf16 headers and type
3
+ // names differ. See compat/cuda.h for the reasoning behind each macro.
4
+
5
+ #define GPU_KERNEL extern "C" __global__
6
+
7
+ #define GPU_UINT unsigned int
8
+ #define GPU_FUNC __device__ inline
9
+
10
+ #include <hip/hip_fp16.h>
11
+ #define GPU_HALF __half
12
+ #include <hip/hip_bf16.h>
13
+ #define GPU_BFLOAT __hip_bfloat16
14
+ #define GPU_BFLOAT_TO_FLOAT(x) __bfloat162float(x)
15
+ #define GPU_FLOAT_TO_BFLOAT(x) __float2bfloat16(x)
16
+
17
+ #define GPU_ARGS
18
+
19
+ #define GLOBAL_ID_X ((int)(blockIdx.x * blockDim.x + threadIdx.x))
20
+ #define GLOBAL_ID_Y ((int)(blockIdx.y * blockDim.y + threadIdx.y))
21
+ #define LOCAL_ID_X ((int)threadIdx.x)
22
+ #define GROUP_ID_X ((int)blockIdx.x)
23
+ #define GPU_SHARED __shared__
24
+ #define GPU_BARRIER __syncthreads()
25
+
26
+ #ifndef INFINITY
27
+ #define INFINITY (__int_as_float(0x7f800000))
28
+ #endif
29
+
30
+ template <typename T>
31
+ __device__ __forceinline__ T clamp(T x, T lo, T hi) {
32
+ return x < lo ? lo : (x > hi ? hi : x);
33
+ }
34
+
35
+ #define GPU_ERF(x) erff(x)
@@ -0,0 +1,45 @@
1
+ // tilework compat header (Metal)
2
+ // Expands the CUDA-flavored dialect's macro set into MSL. This set is the de
3
+ // facto kernel ABI; add a macro only if its semantics can be guaranteed
4
+ // identical on every backend.
5
+ #include <metal_stdlib>
6
+ using namespace metal;
7
+
8
+ // Half precision, as a storage type only (1:1 with cuda.h's GPU_HALF).
9
+ // MSL's half converts to and from float implicitly.
10
+ #define GPU_HALF half
11
+ #define GPU_BFLOAT bfloat
12
+ #define GPU_BFLOAT_TO_FLOAT(x) ((float)(x))
13
+ #define GPU_FLOAT_TO_BFLOAT(x) ((bfloat)(x))
14
+
15
+ // 32-bit unsigned integer (wrapping arithmetic). For computations that must
16
+ // give bit-identical answers on every backend, such as counter-based RNG.
17
+ #define GPU_UINT uint
18
+ // Device-callable helper. CUDA needs an execution-space annotation (without it
19
+ // the function is host-only and __global__ cannot call it); this absorbs that.
20
+ #define GPU_FUNC inline
21
+
22
+ #define GPU_KERNEL kernel
23
+ #define GPU_ARGS uint3 __gid [[thread_position_in_grid]], \
24
+ uint3 __lid [[thread_position_in_threadgroup]], \
25
+ uint3 __bid [[threadgroup_position_in_grid]]
26
+ #define GLOBAL_ID_X ((int)__gid.x)
27
+ #define GLOBAL_ID_Y ((int)__gid.y)
28
+ #define LOCAL_ID_X ((int)__lid.x)
29
+ #define GROUP_ID_X ((int)__bid.x)
30
+ #define GPU_SHARED threadgroup
31
+ // CUDA / HIP expand this to __syncthreads() (same semantics as a threadgroup barrier)
32
+ #define GPU_BARRIER threadgroup_barrier(mem_flags::mem_threadgroup)
33
+
34
+ // MSL has no erf (no counterpart to CUDA's erff), so approximate it with
35
+ // Abramowitz & Stegun 7.1.26 (max error 1.5e-7), well inside the 2e-3
36
+ // tolerance of its GELU (exact) caller.
37
+ inline float gpu_erf_approx(float x) {
38
+ float s = x < 0.0f ? -1.0f : 1.0f;
39
+ float ax = fabs(x);
40
+ float t = 1.0f / (1.0f + 0.3275911f * ax);
41
+ float poly = ((((1.061405429f * t - 1.453152027f) * t + 1.421413741f) * t
42
+ - 0.284496736f) * t + 0.254829592f) * t;
43
+ return s * (1.0f - poly * exp(-ax * ax));
44
+ }
45
+ #define GPU_ERF(x) gpu_erf_approx(x)