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,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../tilework"
4
+
5
+ # Opt-in core extension. Adds Array#to_gpu only when "tilework/core_ext" is
6
+ # required (a plain require "tilework" does not touch core classes).
7
+ class Array
8
+ def to_gpu(dtype: nil)
9
+ Tilework::Buffer.from(self, dtype: dtype)
10
+ end
11
+ end
12
+
13
+ # Top-level with_gpu (an alias of Tilework.run). Defined private like other
14
+ # Kernel methods so it is not visible with an explicit receiver.
15
+ module Kernel
16
+ private
17
+
18
+ def with_gpu(*inputs, &block)
19
+ Tilework.run(*inputs, &block)
20
+ end
21
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tilework
4
+ # Expands the CUDA-flavored dialect into the backend's shader language.
5
+ # The approach is to prepend the compat header (compat/<backend>.h) and let
6
+ # each backend's preprocessor do the actual expansion.
7
+ module Dialect
8
+ HEADERS = {
9
+ metal: File.expand_path("compat/metal.h", __dir__),
10
+ cuda: File.expand_path("compat/cuda.h", __dir__),
11
+ hip: File.expand_path("compat/hip.h", __dir__),
12
+ }.freeze
13
+
14
+ # The dialect's macro set follows the Metal way of writing, so exactly two
15
+ # spots are not valid C++ (NVRTC) syntax. Header macros cannot absorb them,
16
+ # so on the CUDA path only, the source itself is minimally rewritten.
17
+ #
18
+ # 1. `f(GPU_ARGS, device const float* a)` — in Metal this expands to three
19
+ # implicit arguments, which have no CUDA counterpart. GPU_ARGS can be an
20
+ # empty macro, but the comma written in the source would remain as
21
+ # `f( , ...)`, so the comma is dropped here too.
22
+ # 2. `constant int& n` — removing `constant` leaves the reference `int& n`,
23
+ # but dispatch passes scalars as value bytes (kernel.rb#convert_args), so
24
+ # as cuLaunchKernel kernelParams it must be the by-value `const int n`. A
25
+ # reference still compiles and silently corrupts the value, so it is
26
+ # rewritten to by-value here.
27
+ #
28
+ # MSL address-space qualifiers (device / constant) are also dropped here.
29
+ # cuda.h cannot `#define device`: CUDA's host_defines.h defines
30
+ # `__device__` as `__location__(device)`, so an empty `device` macro breaks
31
+ # __device__ and leaves every function without an execution-space
32
+ # annotation. Rewriting only the dialect source avoids that side effect.
33
+ CPP_DIALECT_REWRITES = [
34
+ [/\bGPU_ARGS\s*,\s*/, ""].freeze,
35
+ # `constant int& n` -> `const int n` (by value). Must run before the
36
+ # bare `constant` removal.
37
+ [/\bconstant\s+([A-Za-z_]\w*)\s*&/, 'const \1'].freeze,
38
+ [/\b(?:device|constant)\s+/, ""].freeze,
39
+ ].freeze
40
+
41
+ module_function
42
+
43
+ def expand(src)
44
+ "#{compat_header}\n#line 1 \"kernel\"\n#{rewrite(src)}"
45
+ end
46
+
47
+ # Unknown backends (such as :custom in tests) are treated as the Metal dialect.
48
+ def header_key
49
+ HEADERS.key?(Tilework.backend) ? Tilework.backend : :metal
50
+ end
51
+
52
+ def compat_header
53
+ key = header_key
54
+ # Memoized per backend. With a single key, a header read once under
55
+ # :metal would stay after switching to :cuda and silently generate the
56
+ # wrong source.
57
+ @compat_headers ||= {}
58
+ @compat_headers[key] ||= File.read(HEADERS.fetch(key))
59
+ end
60
+
61
+ def rewrite(src)
62
+ return src unless %i[cuda hip].include?(header_key)
63
+
64
+ CPP_DIALECT_REWRITES.reduce(src) { |acc, (pattern, replacement)| acc.gsub(pattern, replacement) }
65
+ end
66
+ end
67
+ end