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,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tilework
|
|
4
|
+
# Profiling event collection and resource accounting.
|
|
5
|
+
# Instrumentation is called from Ruby-side Kernel / Buffer /
|
|
6
|
+
# Tilework.synchronize, so it works on every backend without bridge changes.
|
|
7
|
+
#
|
|
8
|
+
# Event collection (#instrument) is active only inside a Tilework.profile
|
|
9
|
+
# block and is a pass-through otherwise (not even Process.clock_gettime runs
|
|
10
|
+
# outside one). Counting live buffers/pipelines in Resources is always on,
|
|
11
|
+
# since leak detection and observability need it outside profile blocks too.
|
|
12
|
+
module Telemetry
|
|
13
|
+
KEY = :gpu_telemetry_events
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def collector
|
|
18
|
+
Thread.current[KEY]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def enabled?
|
|
22
|
+
!collector.nil?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Times the block as an event tied to name/label. Outside Tilework.profile it
|
|
26
|
+
# does not measure and just returns the block's value. extra (bytes:,
|
|
27
|
+
# threads:, cache_hit:, ...) is per-event-kind detail.
|
|
28
|
+
def instrument(name, label: nil, **extra)
|
|
29
|
+
return yield unless enabled?
|
|
30
|
+
|
|
31
|
+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
32
|
+
result = yield
|
|
33
|
+
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
|
|
34
|
+
collector << { name: name, label: label, duration: duration }.merge(extra)
|
|
35
|
+
result
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def start!
|
|
39
|
+
raise Error, "Tilework.profile blocks cannot be nested" if enabled?
|
|
40
|
+
|
|
41
|
+
Thread.current[KEY] = []
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def finish!
|
|
45
|
+
events = collector
|
|
46
|
+
Thread.current[KEY] = nil
|
|
47
|
+
events
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Always counts live buffers / bytes and cached pipelines. Handles are keyed
|
|
51
|
+
# by object_id: every alloc returns a new handle object on any backend, so it
|
|
52
|
+
# identifies them uniquely and Hash#delete is naturally idempotent against a
|
|
53
|
+
# double free (a backend that reuses integer handles just re-enters the next
|
|
54
|
+
# alloc as a fresh entry).
|
|
55
|
+
# No Mutex: untrack_buffer is also called from GC finalizers (buffer.rb),
|
|
56
|
+
# which run under trap-context-like restrictions where Mutex#synchronize
|
|
57
|
+
# raises ThreadError.
|
|
58
|
+
module Resources
|
|
59
|
+
@buffers = {} # handle.object_id => bytesize
|
|
60
|
+
|
|
61
|
+
class << self
|
|
62
|
+
def track_buffer(handle, bytesize)
|
|
63
|
+
@buffers[handle.object_id] = bytesize
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def untrack_buffer(handle)
|
|
67
|
+
@buffers.delete(handle.object_id)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def snapshot
|
|
71
|
+
{ live_buffers: @buffers.size, live_bytes: @buffers.each_value.sum, cached_pipelines: Kernel.cache_size }
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Aggregated events returned by {Tilework.profile}.
|
|
77
|
+
class Report
|
|
78
|
+
attr_reader :events
|
|
79
|
+
|
|
80
|
+
def initialize(events)
|
|
81
|
+
@events = events
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Groups events by name and label.
|
|
85
|
+
# @return [Hash]
|
|
86
|
+
def summary
|
|
87
|
+
@summary ||= events.group_by { |e| [e[:name], e[:label]] }.transform_values do |group|
|
|
88
|
+
{
|
|
89
|
+
count: group.size,
|
|
90
|
+
total_duration: group.sum { |e| e[:duration] },
|
|
91
|
+
total_bytes: group.sum { |e| e[:bytes] || 0 },
|
|
92
|
+
cache_hits: group.count { |e| e[:cache_hit] },
|
|
93
|
+
}
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Formats a compact profiling table.
|
|
98
|
+
# @return [String]
|
|
99
|
+
def to_s
|
|
100
|
+
return "Tilework.profile: (no events recorded)" if events.empty?
|
|
101
|
+
|
|
102
|
+
header = format("%-24s %8s %12s %14s", "event", "count", "ms", "bytes")
|
|
103
|
+
rows = summary.map do |(name, label), s|
|
|
104
|
+
event_label = [name, label].compact.join(":")
|
|
105
|
+
bytes = s[:total_bytes].positive? ? s[:total_bytes].to_s : "-"
|
|
106
|
+
format("%-24s %8d %12.3f %14s", event_label, s[:count], s[:total_duration] * 1000, bytes)
|
|
107
|
+
end
|
|
108
|
+
([header] + rows).join("\n")
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tilework
|
|
4
|
+
# Resource scope for with_gpu (Tilework.run). Tracks Buffers allocated inside
|
|
5
|
+
# the block on a thread-local stack and frees them together when it ends.
|
|
6
|
+
module Scope
|
|
7
|
+
KEY = :gpu_scope_stack
|
|
8
|
+
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def stack
|
|
12
|
+
Thread.current[KEY] ||= []
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def open
|
|
16
|
+
scope = []
|
|
17
|
+
stack.push(scope)
|
|
18
|
+
scope
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def close(scope)
|
|
22
|
+
stack.delete_if { |s| s.equal?(scope) }
|
|
23
|
+
scope.each(&:free)
|
|
24
|
+
nil
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Called from Buffer#initialize. Does nothing when no scope is active.
|
|
28
|
+
def register(buffer)
|
|
29
|
+
stack.last&.push(buffer)
|
|
30
|
+
buffer
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
data/lib/tilework.rb
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "tilework/version"
|
|
4
|
+
require_relative "tilework/runtime/telemetry"
|
|
5
|
+
require_relative "tilework/dtype/half"
|
|
6
|
+
require_relative "tilework/dtype/bfloat"
|
|
7
|
+
require_relative "tilework/dtype/cast"
|
|
8
|
+
require_relative "tilework/math/philox"
|
|
9
|
+
require_relative "tilework/math/random"
|
|
10
|
+
require_relative "tilework/math/fft"
|
|
11
|
+
require_relative "tilework/math/conv"
|
|
12
|
+
require_relative "tilework/capabilities"
|
|
13
|
+
require_relative "tilework/dialect"
|
|
14
|
+
require_relative "tilework/scope"
|
|
15
|
+
require_relative "tilework/buffer"
|
|
16
|
+
require_relative "tilework/kernel"
|
|
17
|
+
require_relative "tilework/dsl"
|
|
18
|
+
require_relative "tilework/ops"
|
|
19
|
+
require_relative "tilework/math/tensor_core"
|
|
20
|
+
require_relative "tilework/runtime/submission"
|
|
21
|
+
require_relative "tilework/runtime/offload"
|
|
22
|
+
require_relative "tilework/math/nn"
|
|
23
|
+
require_relative "tilework/graph"
|
|
24
|
+
|
|
25
|
+
module Tilework
|
|
26
|
+
class Error < StandardError; end
|
|
27
|
+
class DeviceError < Error; end
|
|
28
|
+
class CompileError < Error; end
|
|
29
|
+
class LaunchError < Error; end
|
|
30
|
+
# Raised when an operation requires a buffer that has been offloaded.
|
|
31
|
+
class NotResidentError < Error; end
|
|
32
|
+
|
|
33
|
+
# The maximum number of kernel arguments supported by the Metal backend.
|
|
34
|
+
MAX_KERNEL_ARGS = 31
|
|
35
|
+
|
|
36
|
+
class << self
|
|
37
|
+
attr_reader :backend
|
|
38
|
+
|
|
39
|
+
# Registers a backend bridge.
|
|
40
|
+
#
|
|
41
|
+
# @api private
|
|
42
|
+
# @param name [Symbol, String] backend identifier
|
|
43
|
+
# @param bridge [Module] bridge implementing Tilework's backend ABI
|
|
44
|
+
# @return [Module] the registered bridge
|
|
45
|
+
def register_backend(name, bridge)
|
|
46
|
+
(@registry ||= {})[name.to_sym] = bridge
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Selects a backend and loads its gem on demand.
|
|
50
|
+
#
|
|
51
|
+
# @param name [Symbol, String] `:cpu`, `:metal`, `:cuda`, or a registered backend
|
|
52
|
+
# @return [Symbol] the selected backend
|
|
53
|
+
# @raise [DeviceError] if the backend cannot be loaded or registered
|
|
54
|
+
def backend=(name)
|
|
55
|
+
name = name.to_sym
|
|
56
|
+
unless (@registry ||= {}).key?(name)
|
|
57
|
+
begin
|
|
58
|
+
require "tilework/backend/#{name}"
|
|
59
|
+
rescue LoadError => e
|
|
60
|
+
raise DeviceError, "backend #{name.inspect} is not installed; add gem \"tilework-#{name}\" " \
|
|
61
|
+
"to your Gemfile (#{e.message})"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
next_bridge = @registry[name] or
|
|
65
|
+
raise DeviceError, "tilework/backend/#{name} loaded but never called Tilework.register_backend(#{name.inspect}, ...)"
|
|
66
|
+
release_backend_cache! unless @bridge.equal?(next_bridge)
|
|
67
|
+
@bridge = next_bridge
|
|
68
|
+
@backend = name
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Returns the active backend bridge.
|
|
72
|
+
#
|
|
73
|
+
# @return [Module]
|
|
74
|
+
# @raise [DeviceError] if no backend has been selected
|
|
75
|
+
def bridge
|
|
76
|
+
@bridge or raise DeviceError, "no backend selected; set Tilework.backend = :metal first"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Installs a custom backend bridge.
|
|
80
|
+
#
|
|
81
|
+
# @api private
|
|
82
|
+
# @param mod [Module] a bridge implementing Tilework's backend ABI
|
|
83
|
+
# @return [Module]
|
|
84
|
+
def bridge=(mod)
|
|
85
|
+
release_backend_cache! unless @bridge.equal?(mod)
|
|
86
|
+
@bridge = mod
|
|
87
|
+
@backend = :custom
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Whether the active bridge prefers direct host execution for supported operations.
|
|
91
|
+
#
|
|
92
|
+
# @api private
|
|
93
|
+
# @return [Boolean]
|
|
94
|
+
def host_fallback?
|
|
95
|
+
bridge.respond_to?(:host_fallback?) && bridge.host_fallback?
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Returns the active device's display name.
|
|
99
|
+
#
|
|
100
|
+
# @return [String]
|
|
101
|
+
def device_name
|
|
102
|
+
bridge.device_name
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Describes the active backend's supported features and device properties.
|
|
106
|
+
# Upper layers pick paths from this one value, keeping the branching in one
|
|
107
|
+
# place. Do not probe Tilework.bridge.respond_to? individually — this is
|
|
108
|
+
# more reliable than aligning respond_to? behavior across backends, and it
|
|
109
|
+
# doubles as a user-facing report.
|
|
110
|
+
#
|
|
111
|
+
# Tilework.capabilities
|
|
112
|
+
# # => { tensor_core: true, tf32: true, offload: true, offload_async: false,
|
|
113
|
+
# # offload_transport: :posix, gemm_library: nil, warp_size: 32,
|
|
114
|
+
# # unified_memory: true, memory_budget_bytes: 7_400_000_000,
|
|
115
|
+
# # compute_capability: [8, 7] }
|
|
116
|
+
#
|
|
117
|
+
# Unimplemented features honestly answer false / nil; branches rely on that.
|
|
118
|
+
# Capabilities::FIELDS in lib/tilework/capabilities.rb is the single source
|
|
119
|
+
# of the key set, types, defaults and meanings; this only writes the
|
|
120
|
+
# backend's declarations on top of it.
|
|
121
|
+
def capabilities
|
|
122
|
+
base = Capabilities.build(
|
|
123
|
+
offload_async: Offload.async?, # has transfer_begin/poll/end
|
|
124
|
+
offload_transport: Offload.transport,
|
|
125
|
+
host_backing: Offload::HostBacking.available?,
|
|
126
|
+
# HIP's first implementation is a pageable synchronous copy; it does not
|
|
127
|
+
# declare async until the staging ABI is measured.
|
|
128
|
+
host_backing_async: backend && bridge.respond_to?(:host_backing_async_available?) && bridge.host_backing_async_available?,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
case backend
|
|
132
|
+
when :cuda
|
|
133
|
+
base.merge(cuda_capabilities.merge(delegated_library_capabilities,
|
|
134
|
+
bf16_storage: true, dtypes: base[:dtypes] + %i[float16 bfloat16]))
|
|
135
|
+
when :metal
|
|
136
|
+
# SIMD group matrix is unsupported, so tensor_core is false. float16
|
|
137
|
+
# storage is verified on Apple Silicon by conformance / metal_test.
|
|
138
|
+
base.merge(delegated_library_capabilities,
|
|
139
|
+
warp_size: 32, unified_memory: true, bf16_storage: true,
|
|
140
|
+
dtypes: base[:dtypes] + %i[float16 bfloat16])
|
|
141
|
+
when :hip
|
|
142
|
+
base.merge(hip_capabilities.merge(delegated_library_capabilities,
|
|
143
|
+
bf16_storage: true, dtypes: base[:dtypes] + %i[float16 bfloat16]))
|
|
144
|
+
when :cpu
|
|
145
|
+
# :cpu's generators (lib/tilework/backend/cpu/*) accept neither
|
|
146
|
+
# CUDA-flavored dialect strings nor float16 storage (they only emit
|
|
147
|
+
# plain Ruby source).
|
|
148
|
+
base.merge(warp_size: 1, unified_memory: true, dialect: false, fp16_storage: false,
|
|
149
|
+
bf16_storage: true, dtypes: base[:dtypes] + [:bfloat16])
|
|
150
|
+
else
|
|
151
|
+
base
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Creates a buffer from scalar values or an Array.
|
|
156
|
+
#
|
|
157
|
+
# @param values [Array<Numeric>, Array<Array<Numeric>>] values to transfer
|
|
158
|
+
# @param dtype [:float32, :float16, :int32, nil] storage dtype; inferred when omitted
|
|
159
|
+
# @return [Buffer]
|
|
160
|
+
def [](*values, dtype: nil)
|
|
161
|
+
array = values.length == 1 && values.first.is_a?(Array) ? values.first : values
|
|
162
|
+
Buffer.from(array, dtype: dtype)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Compiles source into a reusable kernel.
|
|
166
|
+
#
|
|
167
|
+
# @param src [String] Tilework dialect source, or native source when `raw: true`
|
|
168
|
+
# @param name [String, Symbol] kernel entry-point name
|
|
169
|
+
# @param raw [Boolean] bypass dialect expansion for backend-native source
|
|
170
|
+
# @return [Kernel]
|
|
171
|
+
# @raise [CompileError] if the active backend cannot compile the source
|
|
172
|
+
def compile(src, name:, raw: false)
|
|
173
|
+
if !capabilities[:dialect] && !raw
|
|
174
|
+
raise CompileError,
|
|
175
|
+
"the #{backend.inspect} backend does not support dialect kernel strings; " \
|
|
176
|
+
"use Tilework.kernel { } (DSL), Buffer operators (Ops), or pass raw: true with backend-native source"
|
|
177
|
+
end
|
|
178
|
+
Kernel.compile(src, name: name, raw: raw)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Defines a Ruby-block kernel specialized from its call arguments.
|
|
182
|
+
#
|
|
183
|
+
# @param name [String] generated kernel name
|
|
184
|
+
# @param out [Integer, nil] output argument index; inferred when omitted
|
|
185
|
+
# @param out_dtype [Symbol, nil] output dtype; inferred when omitted
|
|
186
|
+
# @yield kernel body in the supported Tilework DSL
|
|
187
|
+
# @return [DSLKernel]
|
|
188
|
+
def kernel(name = "dsl_kernel", out: nil, out_dtype: nil, &block)
|
|
189
|
+
DSLKernel.new(name, block, out: out, out_dtype: out_dtype)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Runs the block with kernel launches enqueued instead of waited on, and
|
|
193
|
+
# returns a {Submission::Job} whose `#wait` synchronizes once.
|
|
194
|
+
#
|
|
195
|
+
# job = Tilework.submit { (a * 2 + b).relu }
|
|
196
|
+
# out = job.wait
|
|
197
|
+
#
|
|
198
|
+
# Ordering on the device is unchanged (everything goes to the same queue),
|
|
199
|
+
# so the values match a synchronous eager run. Only the host-side waiting
|
|
200
|
+
# moves: one wait at the boundary instead of one per launch. Operations
|
|
201
|
+
# that must wait anyway — whole-buffer reductions, attention, delegated
|
|
202
|
+
# library calls, and reading back to the host — stay synchronous inside the
|
|
203
|
+
# block; see {Submission}.
|
|
204
|
+
#
|
|
205
|
+
# `Buffer#free` called inside the block is deferred until the job
|
|
206
|
+
# completes, so a temporary buffer freed in an `ensure` is not handed back
|
|
207
|
+
# while a launched kernel still reads it. Nesting is a no-op: an inner
|
|
208
|
+
# `submit` does not introduce a second boundary.
|
|
209
|
+
#
|
|
210
|
+
# @yield code to enqueue
|
|
211
|
+
# @return [Submission::Job]
|
|
212
|
+
def submit(&block)
|
|
213
|
+
Submission.run(&block)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Waits for all work submitted to the active backend.
|
|
217
|
+
#
|
|
218
|
+
# @return [nil]
|
|
219
|
+
# @raise [LaunchError] if work submitted asynchronously failed; the call
|
|
220
|
+
# that enqueued it had already returned, so this is where it surfaces
|
|
221
|
+
def synchronize
|
|
222
|
+
Telemetry.instrument(:synchronize) { bridge.synchronize }
|
|
223
|
+
nil
|
|
224
|
+
rescue RuntimeError => e
|
|
225
|
+
raise LaunchError, "asynchronous work failed: #{e.message}"
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# Collects backend activity while the block runs.
|
|
229
|
+
#
|
|
230
|
+
# @yield code to profile
|
|
231
|
+
# @return [Telemetry::Report]
|
|
232
|
+
# @raise [Error] if called while another profile is active
|
|
233
|
+
def profile
|
|
234
|
+
Telemetry.start! # raises here when nested (finish! is not called yet)
|
|
235
|
+
begin
|
|
236
|
+
yield
|
|
237
|
+
ensure
|
|
238
|
+
events = Telemetry.finish!
|
|
239
|
+
end
|
|
240
|
+
Telemetry::Report.new(events)
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# Runs a block with Array inputs converted to buffers and outputs converted back.
|
|
244
|
+
#
|
|
245
|
+
# Buffers allocated inside the block are freed when it returns, including on errors.
|
|
246
|
+
# Do not retain a buffer except through the block return value.
|
|
247
|
+
#
|
|
248
|
+
# @param inputs [Array, Buffer] input values
|
|
249
|
+
# @yieldparam buffers [Buffer] converted inputs
|
|
250
|
+
# @return [Object] the block result, with buffers converted to nested Arrays
|
|
251
|
+
def run(*inputs)
|
|
252
|
+
scope = Scope.open
|
|
253
|
+
args = inputs.map { |v| v.is_a?(Array) ? Buffer.from(v) : v }
|
|
254
|
+
unwrap(yield(*args))
|
|
255
|
+
ensure
|
|
256
|
+
Scope.close(scope)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
private
|
|
260
|
+
|
|
261
|
+
# Compiled pipelines belong to the bridge that created them. Keeping the
|
|
262
|
+
# cache across a backend change both retains the old backend's native
|
|
263
|
+
# modules and risks handing one backend another backend's handle for an
|
|
264
|
+
# identical source string. Synchronize before dropping the cache because
|
|
265
|
+
# an async launch may still be using a cached pipeline.
|
|
266
|
+
def release_backend_cache!
|
|
267
|
+
return unless @bridge && defined?(Kernel) && Kernel.cache_size.positive?
|
|
268
|
+
|
|
269
|
+
Telemetry.instrument(:synchronize) { @bridge.synchronize }
|
|
270
|
+
Kernel.clear_cache!
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Takes raw device facts from the bridge's optional query and translates
|
|
274
|
+
# them to contract keys. Without the query, only warp_size is filled with
|
|
275
|
+
# a known constant (CUDA's warpSize is always 32).
|
|
276
|
+
def cuda_capabilities
|
|
277
|
+
probe = bridge.respond_to?(:device_capabilities) ? bridge.device_capabilities : nil
|
|
278
|
+
return { warp_size: 32 } if probe.nil?
|
|
279
|
+
|
|
280
|
+
major, = probe[:compute_capability]
|
|
281
|
+
# The bridge returns raw device facts without knowing the contract's key
|
|
282
|
+
# set; keys outside it are dropped here (Capabilities.build would reject them).
|
|
283
|
+
Capabilities.slice_known(probe).merge(
|
|
284
|
+
# Tensor Cores from Volta (SM 70). TF32 from Ampere (SM 80), taking FP32
|
|
285
|
+
# buffers directly as input.
|
|
286
|
+
tensor_core: major >= 7,
|
|
287
|
+
tf32: major >= 8,
|
|
288
|
+
# FP16 Tensor Cores from Volta (SM 70).
|
|
289
|
+
fp16: major >= 7,
|
|
290
|
+
)
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
# HIP's gfx names are not CUDA compute capabilities. Only measured values the
|
|
294
|
+
# bridge returns go into the contract; matrix instructions / TF32 are not
|
|
295
|
+
# declared until an execution probe exists.
|
|
296
|
+
def hip_capabilities
|
|
297
|
+
probe = bridge.respond_to?(:device_capabilities) ? bridge.device_capabilities : nil
|
|
298
|
+
matrix_core = bridge.respond_to?(:matrix_core_available?) && bridge.matrix_core_available?
|
|
299
|
+
return { compute_capability: nil, tf32: false, tensor_core: matrix_core, fp16: matrix_core } if probe.nil?
|
|
300
|
+
|
|
301
|
+
Capabilities.slice_known(probe).merge(
|
|
302
|
+
compute_capability: nil,
|
|
303
|
+
tf32: false,
|
|
304
|
+
tensor_core: matrix_core,
|
|
305
|
+
fp16: matrix_core,
|
|
306
|
+
)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Delegation (GEMM/RNG/FFT/convolution) is not CUDA-specific like SM
|
|
310
|
+
# generations; it follows from respond_to? queries to the bridge alone, so
|
|
311
|
+
# the same expression is right on :cuda and :metal alike.
|
|
312
|
+
def delegated_library_capabilities
|
|
313
|
+
{
|
|
314
|
+
gemm_library: TensorCore.gemm_library? ? TensorCore.gemm_library_name : nil,
|
|
315
|
+
gemm_batched: TensorCore.gemm_batched?,
|
|
316
|
+
gemm_dtypes: if !TensorCore.gemm_library?
|
|
317
|
+
[]
|
|
318
|
+
elsif bridge.respond_to?(:gemm_supported_dtypes)
|
|
319
|
+
bridge.gemm_supported_dtypes
|
|
320
|
+
else
|
|
321
|
+
%i[float32 float16]
|
|
322
|
+
end,
|
|
323
|
+
rng: Random.library,
|
|
324
|
+
fft: FFT.library,
|
|
325
|
+
conv: Conv.library,
|
|
326
|
+
conv_backward: Conv.backward_available?,
|
|
327
|
+
conv_dtypes: if !Conv.library
|
|
328
|
+
[]
|
|
329
|
+
elsif bridge.respond_to?(:conv_supported_dtypes)
|
|
330
|
+
bridge.conv_supported_dtypes
|
|
331
|
+
else
|
|
332
|
+
%i[float32 float16]
|
|
333
|
+
end,
|
|
334
|
+
}
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def unwrap(value)
|
|
338
|
+
case value
|
|
339
|
+
when Buffer then value.to_nested_a # same as to_a for a 1D shape
|
|
340
|
+
when Array then value.map { |v| unwrap(v) }
|
|
341
|
+
else value
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: tilework
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.18.4
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Kihaya Sugiura
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: 'The core of tilework: pure Ruby, no native extension, ships the :cpu
|
|
14
|
+
backend as the conformance oracle for the tilework-metal / tilework-cuda / tilework-hip
|
|
15
|
+
backend gems.'
|
|
16
|
+
email:
|
|
17
|
+
- kihayas@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- LICENSE
|
|
23
|
+
- lib/tilework.rb
|
|
24
|
+
- lib/tilework/backend/cpu.rb
|
|
25
|
+
- lib/tilework/backend/cpu/dsl_codegen.rb
|
|
26
|
+
- lib/tilework/backend/cpu/ops_codegen.rb
|
|
27
|
+
- lib/tilework/buffer.rb
|
|
28
|
+
- lib/tilework/capabilities.rb
|
|
29
|
+
- lib/tilework/compat/cuda.h
|
|
30
|
+
- lib/tilework/compat/hip.h
|
|
31
|
+
- lib/tilework/compat/metal.h
|
|
32
|
+
- lib/tilework/conformance.rb
|
|
33
|
+
- lib/tilework/core_ext.rb
|
|
34
|
+
- lib/tilework/dialect.rb
|
|
35
|
+
- lib/tilework/dsl.rb
|
|
36
|
+
- lib/tilework/dtype/bfloat.rb
|
|
37
|
+
- lib/tilework/dtype/cast.rb
|
|
38
|
+
- lib/tilework/dtype/half.rb
|
|
39
|
+
- lib/tilework/graph.rb
|
|
40
|
+
- lib/tilework/graph/builder.rb
|
|
41
|
+
- lib/tilework/graph/fusion.rb
|
|
42
|
+
- lib/tilework/graph/grad.rb
|
|
43
|
+
- lib/tilework/graph/high_level.rb
|
|
44
|
+
- lib/tilework/graph/kernels.rb
|
|
45
|
+
- lib/tilework/graph/node.rb
|
|
46
|
+
- lib/tilework/graph/op.rb
|
|
47
|
+
- lib/tilework/graph/optim.rb
|
|
48
|
+
- lib/tilework/graph/plan.rb
|
|
49
|
+
- lib/tilework/graph/pool.rb
|
|
50
|
+
- lib/tilework/graph/region.rb
|
|
51
|
+
- lib/tilework/graph/residency.rb
|
|
52
|
+
- lib/tilework/graph/state.rb
|
|
53
|
+
- lib/tilework/graph/tensor.rb
|
|
54
|
+
- lib/tilework/kernel.rb
|
|
55
|
+
- lib/tilework/math/conv.rb
|
|
56
|
+
- lib/tilework/math/fft.rb
|
|
57
|
+
- lib/tilework/math/nn.rb
|
|
58
|
+
- lib/tilework/math/philox.rb
|
|
59
|
+
- lib/tilework/math/random.rb
|
|
60
|
+
- lib/tilework/math/tensor_core.rb
|
|
61
|
+
- lib/tilework/ops.rb
|
|
62
|
+
- lib/tilework/runtime/offload.rb
|
|
63
|
+
- lib/tilework/runtime/submission.rb
|
|
64
|
+
- lib/tilework/runtime/telemetry.rb
|
|
65
|
+
- lib/tilework/scope.rb
|
|
66
|
+
- lib/tilework/version.rb
|
|
67
|
+
homepage: https://github.com/kihaya/tilework
|
|
68
|
+
licenses:
|
|
69
|
+
- MIT
|
|
70
|
+
metadata:
|
|
71
|
+
rubygems_mfa_required: 'true'
|
|
72
|
+
post_install_message:
|
|
73
|
+
rdoc_options: []
|
|
74
|
+
require_paths:
|
|
75
|
+
- lib
|
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - ">="
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '3.1'
|
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
|
+
requirements:
|
|
83
|
+
- - ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
86
|
+
requirements: []
|
|
87
|
+
rubygems_version: 3.5.22
|
|
88
|
+
signing_key:
|
|
89
|
+
specification_version: 4
|
|
90
|
+
summary: Backend-agnostic core (Buffer / Kernel / DSL / Ops) plus the :cpu reference
|
|
91
|
+
backend
|
|
92
|
+
test_files: []
|