cumo 0.5.11 → 0.7.0

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 (174) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +76 -1
  3. data/README.md +133 -0
  4. data/bench/cumo_shape_probe.rb +264 -0
  5. data/bench/cumo_sync_probe.rb +71 -8
  6. data/ext/cumo/cuda/cublas.c +0 -40
  7. data/ext/cumo/cuda/cudnn.c +48 -0
  8. data/ext/cumo/cuda/cudnn_impl.cpp +100 -63
  9. data/ext/cumo/cuda/memory_pool_impl_test.cpp +4 -1
  10. data/ext/cumo/cuda/nvrtc.c +1 -1
  11. data/ext/cumo/cuda/runtime.c +3 -4
  12. data/ext/cumo/cumo.c +34 -0
  13. data/ext/cumo/depend.erb +7 -0
  14. data/ext/cumo/extconf.rb +2 -0
  15. data/ext/cumo/include/cumo/bit_reduce_kernel.h +119 -13
  16. data/ext/cumo/include/cumo/check.h +51 -0
  17. data/ext/cumo/include/cumo/cuda/cudnn.h +47 -43
  18. data/ext/cumo/include/cumo/cuda/cumo_thrust.hpp +1 -0
  19. data/ext/cumo/include/cumo/cuda/runtime.h +28 -0
  20. data/ext/cumo/include/cumo/indexer.h +29 -8
  21. data/ext/cumo/include/cumo/intern.h +4 -1
  22. data/ext/cumo/include/cumo/narray.h +69 -3
  23. data/ext/cumo/include/cumo/ndloop.h +1 -0
  24. data/ext/cumo/include/cumo/reduce_kernel.h +133 -50
  25. data/ext/cumo/include/cumo/template.h +35 -10
  26. data/ext/cumo/include/cumo/template_kernel.h +96 -0
  27. data/ext/cumo/include/cumo/types/bit.h +3 -0
  28. data/ext/cumo/include/cumo/types/bit_kernel.h +3 -0
  29. data/ext/cumo/include/cumo/types/complex_macro.h +2 -0
  30. data/ext/cumo/include/cumo/types/complex_macro_kernel.h +2 -0
  31. data/ext/cumo/include/cumo/types/float_macro.h +17 -2
  32. data/ext/cumo/include/cumo/types/float_macro_kernel.h +17 -2
  33. data/ext/cumo/include/cumo/types/half_def.h +129 -0
  34. data/ext/cumo/include/cumo/types/half_def_kernel.h +28 -0
  35. data/ext/cumo/include/cumo/types/half_macro.h +188 -0
  36. data/ext/cumo/include/cumo/types/half_macro_kernel.h +198 -0
  37. data/ext/cumo/include/cumo/types/hfloat.h +22 -0
  38. data/ext/cumo/include/cumo/types/hfloat_kernel.h +16 -0
  39. data/ext/cumo/include/cumo/types/int_macro.h +20 -0
  40. data/ext/cumo/include/cumo/types/int_macro_kernel.h +20 -0
  41. data/ext/cumo/include/cumo/types/real_accum.h +2 -0
  42. data/ext/cumo/include/cumo/types/real_accum_kernel.h +2 -0
  43. data/ext/cumo/include/cumo/types/robj_macro.h +11 -3
  44. data/ext/cumo/include/cumo/types/xint_macro.h +3 -0
  45. data/ext/cumo/include/cumo/types/xint_macro_kernel.h +2 -0
  46. data/ext/cumo/include/cumo.h +2 -2
  47. data/ext/cumo/narray/data.c +70 -199
  48. data/ext/cumo/narray/data_kernel.cu +113 -0
  49. data/ext/cumo/narray/gen/def/bit.rb +2 -0
  50. data/ext/cumo/narray/gen/def/dcomplex.rb +2 -0
  51. data/ext/cumo/narray/gen/def/dfloat.rb +8 -0
  52. data/ext/cumo/narray/gen/def/hfloat.rb +47 -0
  53. data/ext/cumo/narray/gen/def/int16.rb +2 -0
  54. data/ext/cumo/narray/gen/def/int32.rb +2 -0
  55. data/ext/cumo/narray/gen/def/int64.rb +2 -0
  56. data/ext/cumo/narray/gen/def/int8.rb +2 -0
  57. data/ext/cumo/narray/gen/def/robject.rb +2 -0
  58. data/ext/cumo/narray/gen/def/scomplex.rb +2 -0
  59. data/ext/cumo/narray/gen/def/sfloat.rb +8 -0
  60. data/ext/cumo/narray/gen/def/uint16.rb +3 -1
  61. data/ext/cumo/narray/gen/def/uint32.rb +3 -1
  62. data/ext/cumo/narray/gen/def/uint64.rb +2 -0
  63. data/ext/cumo/narray/gen/def/uint8.rb +3 -1
  64. data/ext/cumo/narray/gen/erbpp2.rb +6 -0
  65. data/ext/cumo/narray/gen/narray_def.rb +5 -0
  66. data/ext/cumo/narray/gen/spec.rb +11 -0
  67. data/ext/cumo/narray/gen/tmpl/accum.c +1 -1
  68. data/ext/cumo/narray/gen/tmpl/accum_arg.c +1 -1
  69. data/ext/cumo/narray/gen/tmpl/accum_arg_kernel.cu +5 -5
  70. data/ext/cumo/narray/gen/tmpl/accum_binary_kernel.cu +14 -1
  71. data/ext/cumo/narray/gen/tmpl/accum_index.c +1 -1
  72. data/ext/cumo/narray/gen/tmpl/accum_index_kernel.cu +5 -5
  73. data/ext/cumo/narray/gen/tmpl/alloc_func.c +4 -7
  74. data/ext/cumo/narray/gen/tmpl/allocate.c +1 -2
  75. data/ext/cumo/narray/gen/tmpl/aref_cpu.c +1 -1
  76. data/ext/cumo/narray/gen/tmpl/batch_norm.c +27 -36
  77. data/ext/cumo/narray/gen/tmpl/batch_norm_backward.c +26 -35
  78. data/ext/cumo/narray/gen/tmpl/binary.c +3 -3
  79. data/ext/cumo/narray/gen/tmpl/binary2.c +1 -1
  80. data/ext/cumo/narray/gen/tmpl/binary_kernel.cu +69 -2
  81. data/ext/cumo/narray/gen/tmpl/binary_s.c +3 -3
  82. data/ext/cumo/narray/gen/tmpl/clip.c +9 -9
  83. data/ext/cumo/narray/gen/tmpl/cond_binary.c +3 -3
  84. data/ext/cumo/narray/gen/tmpl/cond_unary.c +2 -2
  85. data/ext/cumo/narray/gen/tmpl/conv.c +19 -25
  86. data/ext/cumo/narray/gen/tmpl/conv_grad_w.c +21 -27
  87. data/ext/cumo/narray/gen/tmpl/conv_transpose.c +21 -27
  88. data/ext/cumo/narray/gen/tmpl/cum.c +24 -1
  89. data/ext/cumo/narray/gen/tmpl/cum_kernel.cu +29 -4
  90. data/ext/cumo/narray/gen/tmpl/each.c +2 -3
  91. data/ext/cumo/narray/gen/tmpl/each_with_index.c +5 -21
  92. data/ext/cumo/narray/gen/tmpl/ewcomp.c +1 -1
  93. data/ext/cumo/narray/gen/tmpl/extract_cpu.c +1 -1
  94. data/ext/cumo/narray/gen/tmpl/extract_data.c +1 -1
  95. data/ext/cumo/narray/gen/tmpl/fill.c +2 -2
  96. data/ext/cumo/narray/gen/tmpl/fixed_batch_norm.c +18 -27
  97. data/ext/cumo/narray/gen/tmpl/float_accum_kernel.cu +42 -37
  98. data/ext/cumo/narray/gen/tmpl/format.c +1 -1
  99. data/ext/cumo/narray/gen/tmpl/format_to_a.c +1 -1
  100. data/ext/cumo/narray/gen/tmpl/frexp.c +1 -1
  101. data/ext/cumo/narray/gen/tmpl/gemm.c +78 -27
  102. data/ext/cumo/narray/gen/tmpl/init_class.c +0 -7
  103. data/ext/cumo/narray/gen/tmpl/init_upcast.c +6 -0
  104. data/ext/cumo/narray/gen/tmpl/inspect.c +1 -1
  105. data/ext/cumo/narray/gen/tmpl/lib.c +11 -1
  106. data/ext/cumo/narray/gen/tmpl/map_with_index.c +7 -14
  107. data/ext/cumo/narray/gen/tmpl/median.c +9 -47
  108. data/ext/cumo/narray/gen/tmpl/minmax.c +1 -1
  109. data/ext/cumo/narray/gen/tmpl/poly.c +2 -2
  110. data/ext/cumo/narray/gen/tmpl/pooling_backward.c +6 -18
  111. data/ext/cumo/narray/gen/tmpl/pooling_forward.c +6 -18
  112. data/ext/cumo/narray/gen/tmpl/pow.c +6 -6
  113. data/ext/cumo/narray/gen/tmpl/qsort.c +0 -1
  114. data/ext/cumo/narray/gen/tmpl/rand.c +20 -2
  115. data/ext/cumo/narray/gen/tmpl/rand_kernel.cu +13 -0
  116. data/ext/cumo/narray/gen/tmpl/rand_norm.c +4 -4
  117. data/ext/cumo/narray/gen/tmpl/rand_norm_kernel.cu +7 -5
  118. data/ext/cumo/narray/gen/tmpl/real_accum_kernel.cu +155 -96
  119. data/ext/cumo/narray/gen/tmpl/set2.c +1 -1
  120. data/ext/cumo/narray/gen/tmpl/sort.c +1 -1
  121. data/ext/cumo/narray/gen/tmpl/sort_index.c +6 -3
  122. data/ext/cumo/narray/gen/tmpl/store_array.c +21 -22
  123. data/ext/cumo/narray/gen/tmpl/store_bit.c +2 -2
  124. data/ext/cumo/narray/gen/tmpl/store_from.c +2 -2
  125. data/ext/cumo/narray/gen/tmpl/store_from_kernel.cu +37 -2
  126. data/ext/cumo/narray/gen/tmpl/to_a.c +1 -1
  127. data/ext/cumo/narray/gen/tmpl/unary.c +21 -4
  128. data/ext/cumo/narray/gen/tmpl/unary2.c +2 -2
  129. data/ext/cumo/narray/gen/tmpl/unary_ret2.c +1 -1
  130. data/ext/cumo/narray/gen/tmpl/unary_s.c +2 -2
  131. data/ext/cumo/narray/gen/tmpl_bit/allocate.c +2 -3
  132. data/ext/cumo/narray/gen/tmpl_bit/aref_cpu.c +1 -1
  133. data/ext/cumo/narray/gen/tmpl_bit/binary.c +1 -1
  134. data/ext/cumo/narray/gen/tmpl_bit/bit_count_cpu.c +1 -1
  135. data/ext/cumo/narray/gen/tmpl_bit/bit_reduce.c +4 -1
  136. data/ext/cumo/narray/gen/tmpl_bit/bit_stat.c +36 -0
  137. data/ext/cumo/narray/gen/tmpl_bit/bit_stat_kernel.cu +4 -0
  138. data/ext/cumo/narray/gen/tmpl_bit/each.c +2 -2
  139. data/ext/cumo/narray/gen/tmpl_bit/each_with_index.c +5 -20
  140. data/ext/cumo/narray/gen/tmpl_bit/extract.c +13 -27
  141. data/ext/cumo/narray/gen/tmpl_bit/extract_cpu.c +1 -1
  142. data/ext/cumo/narray/gen/tmpl_bit/fill.c +1 -1
  143. data/ext/cumo/narray/gen/tmpl_bit/format.c +1 -1
  144. data/ext/cumo/narray/gen/tmpl_bit/format_to_a.c +1 -1
  145. data/ext/cumo/narray/gen/tmpl_bit/inspect.c +1 -1
  146. data/ext/cumo/narray/gen/tmpl_bit/mask.c +38 -17
  147. data/ext/cumo/narray/gen/tmpl_bit/mask_kernel.cu +4 -4
  148. data/ext/cumo/narray/gen/tmpl_bit/store_array.c +10 -11
  149. data/ext/cumo/narray/gen/tmpl_bit/store_bit.c +1 -1
  150. data/ext/cumo/narray/gen/tmpl_bit/store_from.c +2 -2
  151. data/ext/cumo/narray/gen/tmpl_bit/to_a.c +1 -1
  152. data/ext/cumo/narray/gen/tmpl_bit/unary.c +1 -1
  153. data/ext/cumo/narray/gen/tmpl_bit/where.c +66 -13
  154. data/ext/cumo/narray/gen/tmpl_bit/where2.c +57 -20
  155. data/ext/cumo/narray/gen/tmpl_bit/where_kernel.cu +6 -6
  156. data/ext/cumo/narray/index.c +83 -26
  157. data/ext/cumo/narray/index_kernel.cu +15 -0
  158. data/ext/cumo/narray/math.c +2 -0
  159. data/ext/cumo/narray/narray.c +421 -178
  160. data/ext/cumo/narray/ndloop.c +158 -26
  161. data/ext/cumo/narray/sort_kernel.cu +39 -18
  162. data/ext/cumo/narray/struct.c +14 -17
  163. data/lib/cumo/cuda/cudnn.rb +1 -1
  164. data/lib/cumo/narray/extra.rb +130 -26
  165. data/test/bit_test.rb +290 -0
  166. data/test/cudnn_test.rb +208 -34
  167. data/test/cumo_test.rb +1 -0
  168. data/test/extra_test.rb +75 -7
  169. data/test/hfloat_test.rb +717 -0
  170. data/test/math_test.rb +4 -1
  171. data/test/narray_alt_coverage_test.rb +7 -0
  172. data/test/narray_test.rb +1865 -10
  173. data/test/test_helper.rb +73 -0
  174. metadata +15 -1
@@ -76,46 +76,6 @@ cumo_cuda_cublas_option_value(VALUE value, VALUE default_value)
76
76
  return value;
77
77
  }
78
78
 
79
- #if 0
80
- cublasOperation_t
81
- cumo_cuda_cublas_option_trans(VALUE trans)
82
- {
83
- int opt;
84
- char *ptr;
85
-
86
- switch(TYPE(trans)) {
87
- case T_NIL:
88
- case T_UNDEF:
89
- case T_FALSE:
90
- return CUBLAS_OP_N;
91
- case T_TRUE:
92
- return CUBLAS_OP_T;
93
- case T_FIXNUM:
94
- opt = FIX2INT(trans);
95
- if (opt >= CUBLAS_OP_N && opt <= CUBLAS_OP_C) {
96
- return opt;
97
- }
98
- break;
99
- case T_SYMBOL:
100
- trans = rb_sym2str(trans);
101
- case T_STRING:
102
- ptr = RSTRING_PTR(trans);
103
- if (RSTRING_LEN(trans) > 0) {
104
- switch(ptr[0]){
105
- case 'N': case 'n':
106
- return CUBLAS_OP_N;
107
- case 'T': case 't':
108
- return CUBLAS_OP_T;
109
- case 'C': case 'c':
110
- return CUBLAS_OP_C;
111
- }
112
- }
113
- break;
114
- }
115
- rb_raise(rb_eArgError, "invalid value for cublasOperation_t");
116
- return 0;
117
- }
118
- #endif
119
79
 
120
80
  void
121
81
  Init_cumo_cuda_cublas(void)
@@ -1,6 +1,9 @@
1
1
  #include "cumo/cuda/cudnn.h"
2
2
 
3
3
  #include <assert.h>
4
+ #include <stdlib.h>
5
+ #include <stdint.h>
6
+ #include <errno.h>
4
7
  #include <ruby.h>
5
8
  #include "cumo/narray.h"
6
9
  #include "cumo/template.h"
@@ -21,6 +24,35 @@ cumo_cuda_cudnn_check_status(cudnnStatus_t status)
21
24
  }
22
25
  }
23
26
 
27
+ static size_t cudnn_max_workspace_size = CUMO_CUDA_CUDNN_DEFAULT_MAX_WORKSPACE_SIZE;
28
+
29
+ size_t
30
+ cumo_cuda_cudnn_max_workspace_size()
31
+ {
32
+ return cudnn_max_workspace_size;
33
+ }
34
+
35
+ static void
36
+ init_max_workspace_size(void)
37
+ {
38
+ const char* env = getenv("CUMO_CUDNN_MAX_WORKSPACE_SIZE");
39
+ char* end = NULL;
40
+ unsigned long long v;
41
+
42
+ if (env == NULL || *env == '\0') return;
43
+ // strtoull takes a leading minus and wraps it, so reject the sign itself
44
+ if (*env == '-' || *env == '+') goto BAD;
45
+ errno = 0;
46
+ v = strtoull(env, &end, 10);
47
+ if (errno != 0 || end == env || *end != '\0' || v == 0 || v > (unsigned long long)SIZE_MAX) goto BAD;
48
+ cudnn_max_workspace_size = (size_t)v;
49
+ return;
50
+
51
+ BAD:
52
+ rb_warn("CUMO_CUDNN_MAX_WORKSPACE_SIZE=%s is not a positive byte count, using %"PRIuSIZE,
53
+ env, cudnn_max_workspace_size);
54
+ }
55
+
24
56
  // Lazily initialize cudnn handle, and cache it
25
57
  cudnnHandle_t
26
58
  cumo_cuda_cudnn_handle()
@@ -51,6 +83,20 @@ cumo_cuda_cudnn_handle()
51
83
 
52
84
  @return [Boolean] Returns true if cuDNN is available
53
85
  */
86
+ #ifdef CUDNN_FOUND
87
+ /*
88
+ Returns the ceiling cuDNN may use when it searches for a convolution
89
+ algorithm, set by CUMO_CUDNN_MAX_WORKSPACE_SIZE.
90
+
91
+ @return [Integer] bytes
92
+ */
93
+ static VALUE
94
+ rb_cudnn_max_workspace_size(VALUE self)
95
+ {
96
+ return SIZET2NUM(cumo_cuda_cudnn_max_workspace_size());
97
+ }
98
+ #endif // CUDNN_FOUND
99
+
54
100
  static VALUE
55
101
  rb_cudnn_available_p(VALUE self)
56
102
  {
@@ -76,6 +122,8 @@ Init_cumo_cuda_cudnn(void)
76
122
 
77
123
  rb_define_singleton_method(mCUDNN, "available?", rb_cudnn_available_p, 0);
78
124
  #ifdef CUDNN_FOUND
125
+ init_max_workspace_size();
126
+ rb_define_singleton_method(mCUDNN, "max_workspace_size", rb_cudnn_max_workspace_size, 0);
79
127
  rb_define_const(mCUDNN, "CUDNN_POOLING_MAX", INT2NUM(CUDNN_POOLING_MAX));
80
128
  rb_define_const(mCUDNN, "CUDNN_POOLING_MAX_DETERMINISTIC", INT2NUM(CUDNN_POOLING_MAX_DETERMINISTIC));
81
129
  rb_define_const(mCUDNN, "CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING", INT2NUM(CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING));
@@ -158,7 +158,8 @@ cumo_cuda_cudnn_CreateConvolutionDescriptor(
158
158
  size_t ndim,
159
159
  int* int_stride,
160
160
  int* int_pad,
161
- cudnnDataType_t cudnn_dtype) {
161
+ cudnnDataType_t compute_dtype,
162
+ cudnnMathType_t math_type) {
162
163
  cudnnStatus_t status = CUDNN_STATUS_SUCCESS;
163
164
  int int_dilation[CUMO_NA_MAX_DIMENSION];
164
165
  for (size_t idim = 0; idim < ndim; ++idim) {
@@ -178,7 +179,7 @@ cumo_cuda_cudnn_CreateConvolutionDescriptor(
178
179
  int_dilation[0],
179
180
  int_dilation[1],
180
181
  CUDNN_CROSS_CORRELATION,
181
- cudnn_dtype);
182
+ compute_dtype);
182
183
  } else {
183
184
  status = cudnnSetConvolutionNdDescriptor(
184
185
  *desc,
@@ -187,10 +188,14 @@ cumo_cuda_cudnn_CreateConvolutionDescriptor(
187
188
  int_stride,
188
189
  int_dilation,
189
190
  CUDNN_CROSS_CORRELATION,
190
- cudnn_dtype);
191
+ compute_dtype);
191
192
  }
193
+ if (status != CUDNN_STATUS_SUCCESS) return status;
192
194
 
193
- return status;
195
+ // Tensor cores are not reached under CUDNN_DEFAULT_MATH, and asking for
196
+ // them where they do not apply is not an error, so every dtype names what
197
+ // it wants.
198
+ return cudnnSetConvolutionMathType(*desc, math_type);
194
199
  }
195
200
 
196
201
  cudnnStatus_t
@@ -304,9 +309,49 @@ struct AlgoCacheKeyHash {
304
309
  }
305
310
  };
306
311
 
307
- using FwdAlgoCacheMap = std::unordered_map<AlgoCacheKey, std::pair<cudnnConvolutionFwdAlgo_t, size_t>, AlgoCacheKeyHash>;
308
- using BwdDataAlgoCacheMap = std::unordered_map<AlgoCacheKey, std::pair<cudnnConvolutionBwdDataAlgo_t, size_t>, AlgoCacheKeyHash>;
309
- using BwdFilterAlgoCacheMap = std::unordered_map<AlgoCacheKey, std::pair<cudnnConvolutionBwdFilterAlgo_t, size_t>, AlgoCacheKeyHash>;
312
+ // The math type belongs with the algorithm: the descriptor's own setting is
313
+ // only a permission, and cuDNN reports which one the chosen algorithm actually
314
+ // used. Handing that algorithm to the convolution under a different math type
315
+ // is rejected, or answers something else.
316
+ // A template cannot be declared with C linkage, and this file's exports are
317
+ // wrapped in extern "C".
318
+ extern "C++" {
319
+ template <typename Algo>
320
+ struct AlgoCacheEntry {
321
+ Algo algo;
322
+ size_t memory;
323
+ cudnnMathType_t math_type;
324
+ };
325
+ }
326
+
327
+ using FwdAlgoCacheMap = std::unordered_map<AlgoCacheKey, AlgoCacheEntry<cudnnConvolutionFwdAlgo_t>, AlgoCacheKeyHash>;
328
+ using BwdDataAlgoCacheMap = std::unordered_map<AlgoCacheKey, AlgoCacheEntry<cudnnConvolutionBwdDataAlgo_t>, AlgoCacheKeyHash>;
329
+ using BwdFilterAlgoCacheMap = std::unordered_map<AlgoCacheKey, AlgoCacheEntry<cudnnConvolutionBwdFilterAlgo_t>, AlgoCacheKeyHash>;
330
+
331
+ // Every search builds the same key. Leaving each of the three to do it by hand
332
+ // is how the device id came to be set in one of them and not the others.
333
+ static AlgoCacheKey
334
+ MakeAlgoCacheKey(
335
+ cumo_narray_t* nx, cumo_narray_t* nw, cumo_narray_t* ny,
336
+ int* int_stride, int* int_pad, size_t ndim,
337
+ cudnnDataType_t cudnn_dtype, size_t max_workspace_size)
338
+ {
339
+ auto key = AlgoCacheKey{};
340
+ cumo_cuda_runtime_check_status(cudaGetDevice(&(key.device_id)));
341
+ key.ndim = ndim;
342
+ for (size_t idim = 0; idim < ndim + 2; ++idim) {
343
+ key.x_shape[idim] = nx->shape[idim];
344
+ key.w_shape[idim] = nw->shape[idim];
345
+ key.y_shape[idim] = ny->shape[idim];
346
+ }
347
+ for (size_t idim = 0; idim < ndim; ++idim) {
348
+ key.pad[idim] = int_pad[idim];
349
+ key.stride[idim] = int_stride[idim];
350
+ }
351
+ key.dtype = cudnn_dtype;
352
+ key.max_workspace_size = max_workspace_size;
353
+ return key;
354
+ }
310
355
 
311
356
  static FwdAlgoCacheMap fwd_algo_cache_map_{};
312
357
  static BwdDataAlgoCacheMap bwd_data_algo_cache_map_{};
@@ -335,28 +380,19 @@ cumo_cuda_cudnn_FindConvolutionForwardAlgorithm(
335
380
  CumoGetNArray(w, nw);
336
381
  CumoGetNArray(y, ny);
337
382
 
338
- auto key = AlgoCacheKey{};
339
- cumo_cuda_runtime_check_status(cudaGetDevice(&(key.device_id)));
340
- key.ndim = ndim;
341
- for (size_t idim = 0; idim < ndim + 2; ++idim) {
342
- key.x_shape[idim] = nx->shape[idim];
343
- key.w_shape[idim] = nw->shape[idim];
344
- key.y_shape[idim] = ny->shape[idim];
345
- }
346
- for (size_t idim = 0; idim < ndim; ++idim) {
347
- key.pad[idim]= int_pad[idim];
348
- key.stride[idim]= int_stride[idim];
349
- }
350
- key.dtype = cudnn_dtype;
351
- key.max_workspace_size = max_workspace_size;
383
+ auto key = MakeAlgoCacheKey(nx, nw, ny, int_stride, int_pad, ndim,
384
+ cudnn_dtype, max_workspace_size);
352
385
 
353
386
  auto& algo_cache_map = fwd_algo_cache_map_;
354
387
  // TODO: thread-safe
355
388
  auto it = algo_cache_map.find(key);
356
389
  if (it != algo_cache_map.end()) {
357
- auto pair = it->second;
358
- perf_result->algo = pair.first;
359
- perf_result->memory = pair.second;
390
+ auto entry = it->second;
391
+ // clear the fields the search would have filled but the cache drops
392
+ *perf_result = {};
393
+ perf_result->algo = entry.algo;
394
+ perf_result->memory = entry.memory;
395
+ perf_result->mathType = entry.math_type;
360
396
  return CUDNN_STATUS_SUCCESS;
361
397
  }
362
398
 
@@ -382,10 +418,13 @@ cumo_cuda_cudnn_FindConvolutionForwardAlgorithm(
382
418
  max_workspace_size);
383
419
  cumo_cuda_runtime_free(workspace);
384
420
  if (status != CUDNN_STATUS_SUCCESS) return status;
385
- assert(returned_algo_count == 1);
421
+ // A search that answers success with nothing to report would leave
422
+ // perf_result untouched, and its algo and math type are used below.
423
+ if (returned_algo_count < 1) return CUDNN_STATUS_NOT_SUPPORTED;
424
+ if (perf_result->status != CUDNN_STATUS_SUCCESS) return perf_result->status;
386
425
 
387
426
  // TODO: thread-safe
388
- algo_cache_map[key] = {perf_result->algo, perf_result->memory};
427
+ algo_cache_map[key] = {perf_result->algo, perf_result->memory, perf_result->mathType};
389
428
  return status;
390
429
  }
391
430
 
@@ -412,27 +451,19 @@ cumo_cuda_cudnn_FindConvolutionBackwardDataAlgorithm(
412
451
  CumoGetNArray(w, nw);
413
452
  CumoGetNArray(y, ny);
414
453
 
415
- auto key = AlgoCacheKey{};
416
- key.ndim = ndim;
417
- for (size_t idim = 0; idim < ndim + 2; ++idim) {
418
- key.x_shape[idim] = nx->shape[idim];
419
- key.w_shape[idim] = nw->shape[idim];
420
- key.y_shape[idim] = ny->shape[idim];
421
- }
422
- for (size_t idim = 0; idim < ndim; ++idim) {
423
- key.pad[idim]= int_pad[idim];
424
- key.stride[idim]= int_stride[idim];
425
- }
426
- key.dtype = cudnn_dtype;
427
- key.max_workspace_size = max_workspace_size;
454
+ auto key = MakeAlgoCacheKey(nx, nw, ny, int_stride, int_pad, ndim,
455
+ cudnn_dtype, max_workspace_size);
428
456
 
429
457
  auto& algo_cache_map = bwd_data_algo_cache_map_;
430
458
  // TODO: thread-safe
431
459
  auto it = algo_cache_map.find(key);
432
460
  if (it != algo_cache_map.end()) {
433
- auto pair = it->second;
434
- perf_result->algo = pair.first;
435
- perf_result->memory = pair.second;
461
+ auto entry = it->second;
462
+ // clear the fields the search would have filled but the cache drops
463
+ *perf_result = {};
464
+ perf_result->algo = entry.algo;
465
+ perf_result->memory = entry.memory;
466
+ perf_result->mathType = entry.math_type;
436
467
  return CUDNN_STATUS_SUCCESS;
437
468
  }
438
469
 
@@ -458,10 +489,13 @@ cumo_cuda_cudnn_FindConvolutionBackwardDataAlgorithm(
458
489
  max_workspace_size);
459
490
  cumo_cuda_runtime_free(workspace);
460
491
  if (status != CUDNN_STATUS_SUCCESS) return status;
461
- assert(returned_algo_count == 1);
492
+ // A search that answers success with nothing to report would leave
493
+ // perf_result untouched, and its algo and math type are used below.
494
+ if (returned_algo_count < 1) return CUDNN_STATUS_NOT_SUPPORTED;
495
+ if (perf_result->status != CUDNN_STATUS_SUCCESS) return perf_result->status;
462
496
 
463
497
  // TODO: thread-safe
464
- algo_cache_map[key] = {perf_result->algo, perf_result->memory};
498
+ algo_cache_map[key] = {perf_result->algo, perf_result->memory, perf_result->mathType};
465
499
  return status;
466
500
  }
467
501
 
@@ -488,27 +522,19 @@ cumo_cuda_cudnn_FindConvolutionBackwardFilterAlgorithm(
488
522
  CumoGetNArray(gy, ngy);
489
523
  CumoGetNArray(gw, ngw);
490
524
 
491
- auto key = AlgoCacheKey{};
492
- key.ndim = ndim;
493
- for (size_t idim = 0; idim < ndim + 2; ++idim) {
494
- key.x_shape[idim] = nx->shape[idim];
495
- key.w_shape[idim] = ngw->shape[idim];
496
- key.y_shape[idim] = ngy->shape[idim];
497
- }
498
- for (size_t idim = 0; idim < ndim; ++idim) {
499
- key.pad[idim]= int_pad[idim];
500
- key.stride[idim]= int_stride[idim];
501
- }
502
- key.dtype = cudnn_dtype;
503
- key.max_workspace_size = max_workspace_size;
525
+ auto key = MakeAlgoCacheKey(nx, ngw, ngy, int_stride, int_pad, ndim,
526
+ cudnn_dtype, max_workspace_size);
504
527
 
505
528
  auto& algo_cache_map = bwd_filter_algo_cache_map_;
506
529
  // TODO: thread-safe
507
530
  auto it = algo_cache_map.find(key);
508
531
  if (it != algo_cache_map.end()) {
509
- auto pair = it->second;
510
- perf_result->algo = pair.first;
511
- perf_result->memory = pair.second;
532
+ auto entry = it->second;
533
+ // clear the fields the search would have filled but the cache drops
534
+ *perf_result = {};
535
+ perf_result->algo = entry.algo;
536
+ perf_result->memory = entry.memory;
537
+ perf_result->mathType = entry.math_type;
512
538
  return CUDNN_STATUS_SUCCESS;
513
539
  }
514
540
 
@@ -534,10 +560,13 @@ cumo_cuda_cudnn_FindConvolutionBackwardFilterAlgorithm(
534
560
  max_workspace_size);
535
561
  cumo_cuda_runtime_free(workspace);
536
562
  if (status != CUDNN_STATUS_SUCCESS) return status;
537
- assert(returned_algo_count == 1);
563
+ // A search that answers success with nothing to report would leave
564
+ // perf_result untouched, and its algo and math type are used below.
565
+ if (returned_algo_count < 1) return CUDNN_STATUS_NOT_SUPPORTED;
566
+ if (perf_result->status != CUDNN_STATUS_SUCCESS) return perf_result->status;
538
567
 
539
568
  // TODO: thread-safe
540
- algo_cache_map[key] = {perf_result->algo, perf_result->memory};
569
+ algo_cache_map[key] = {perf_result->algo, perf_result->memory, perf_result->mathType};
541
570
  return status;
542
571
  }
543
572
 
@@ -552,7 +581,15 @@ cumo_cuda_cudnn_GetBatchNormMode(size_t ndim, int* axis) {
552
581
  }
553
582
  if ((ndim == 3 && axis[0] == 0 && axis[1] == 2 && axis[2] == 3) ||
554
583
  (ndim == 4 && axis[0] == 0 && axis[1] == 2 && axis[2] == 3 && axis[3] == 4)) { // (1, channels, (1, )1, 1)
555
- // TODO: Consider CUDNN_BATCHNORM_SPATIAL_PERSISTENT if we can afford to check for overflow, with or without blocking.
584
+ // CUDNN_BATCHNORM_SPATIAL_PERSISTENT is not worth taking. The overflow
585
+ // check it asks for is affordable -- cudnnQueryRuntimeError costs 0.1us
586
+ // either way -- but the mode is 1.3 to 1.8x faster only where the
587
+ // spatial extent is large, and 1.6x slower on 32x256x14x14 backward.
588
+ // Worse, on 32x64x56x56 it raises CUDNN_STATUS_RUNTIME_FP_OVERFLOW for x
589
+ // in [-2, 2] with gamma 1 and beta 0, answering bit for bit what SPATIAL
590
+ // does; honouring that means running SPATIAL again on top. The mode, and
591
+ // the batch normalization API it belongs to, are deprecated as of cuDNN
592
+ // 9.0.0 besides.
556
593
  return CUDNN_BATCHNORM_SPATIAL;
557
594
  }
558
595
  rb_raise(rb_eRuntimeError, "Invalid axis for BatchNorm using cuDNN. Expected 1, 3 or 4 dimensions.");
@@ -337,7 +337,10 @@ public:
337
337
  assert(pool_->GetFreeBytes() == kRoundSize * 8);
338
338
  }
339
339
 
340
- // TODO(sonots): Fix after implementing compaction
340
+ // RemoveFromFreeList leaves an emptied bin in the arena -- it erases the
341
+ // chunk and returns, where Malloc drops one through CompactIndex. The
342
+ // assertions below spell that out, so they describe the arena as it is
343
+ // rather than wait on a compaction this path is not getting.
341
344
  void TestRemoveFromFreeList() {
342
345
  Arena& arena = pool_->GetArena(stream_ptr_);
343
346
  ArenaIndexMap& arena_index_map = pool_->GetArenaIndexMap(stream_ptr_);
@@ -63,7 +63,7 @@ push_strings(VALUE strings, VALUE ary, int n)
63
63
  int i;
64
64
 
65
65
  for (i = 0; i < n; i++) {
66
- VALUE str = rb_str_to_str(RARRAY_AREF(ary, i));
66
+ VALUE str = rb_str_to_str(rb_ary_entry(ary, i));
67
67
  StringValueCStr(str);
68
68
  rb_ary_push(strings, str);
69
69
  }
@@ -5,6 +5,7 @@
5
5
 
6
6
  VALUE cumo_cuda_eRuntimeError;
7
7
  VALUE cumo_cuda_mRuntime;
8
+ uint64_t cumo_cuda_sync_epoch = 0;
8
9
  #define eRuntimeError cumo_cuda_eRuntimeError
9
10
  #define mRuntime cumo_cuda_mRuntime
10
11
 
@@ -42,7 +43,7 @@ cumo_cuda_runtime_error_flag_new(void)
42
43
  bool
43
44
  cumo_cuda_runtime_error_flag_get(int *flag)
44
45
  {
45
- check_status(cudaDeviceSynchronize());
46
+ cumo_cuda_runtime_device_synchronize();
46
47
  return (*flag != 0);
47
48
  }
48
49
 
@@ -189,9 +190,7 @@ rb_cudaSetDevice(VALUE self, VALUE device)
189
190
  static VALUE
190
191
  rb_cudaDeviceSynchronize(VALUE self)
191
192
  {
192
- cudaError_t status;
193
- status = cudaDeviceSynchronize();
194
- check_status(status);
193
+ cumo_cuda_runtime_device_synchronize();
195
194
  return Qnil;
196
195
  }
197
196
 
data/ext/cumo/cumo.c CHANGED
@@ -20,11 +20,27 @@ void Init_cumo_uint8();
20
20
  void Init_cumo_uint16();
21
21
  void Init_cumo_uint32();
22
22
  void Init_cumo_uint64();
23
+ void Init_cumo_hfloat();
23
24
  void Init_cumo_sfloat();
24
25
  void Init_cumo_scomplex();
25
26
  void Init_cumo_dfloat();
26
27
  void Init_cumo_dcomplex();
27
28
  void Init_cumo_robject();
29
+ void Init_cumo_dcomplex_upcast();
30
+ void Init_cumo_dfloat_upcast();
31
+ void Init_cumo_scomplex_upcast();
32
+ void Init_cumo_sfloat_upcast();
33
+ void Init_cumo_hfloat_upcast();
34
+ void Init_cumo_int64_upcast();
35
+ void Init_cumo_uint64_upcast();
36
+ void Init_cumo_int32_upcast();
37
+ void Init_cumo_uint32_upcast();
38
+ void Init_cumo_int16_upcast();
39
+ void Init_cumo_uint16_upcast();
40
+ void Init_cumo_int8_upcast();
41
+ void Init_cumo_uint8_upcast();
42
+ void Init_cumo_bit_upcast();
43
+ void Init_cumo_robject_upcast();
28
44
  void Init_cumo_na_math();
29
45
  void Init_cumo_na_rand();
30
46
  void Init_cumo_na_array();
@@ -150,6 +166,7 @@ Init_cumo()
150
166
  Init_cumo_dfloat();
151
167
  Init_cumo_scomplex();
152
168
  Init_cumo_sfloat();
169
+ Init_cumo_hfloat();
153
170
 
154
171
  Init_cumo_int64();
155
172
  Init_cumo_uint64();
@@ -163,6 +180,23 @@ Init_cumo()
163
180
  Init_cumo_bit();
164
181
  Init_cumo_robject();
165
182
 
183
+ // Every class exists now, so the UPCAST tables can name all of them.
184
+ Init_cumo_dcomplex_upcast();
185
+ Init_cumo_dfloat_upcast();
186
+ Init_cumo_scomplex_upcast();
187
+ Init_cumo_sfloat_upcast();
188
+ Init_cumo_hfloat_upcast();
189
+ Init_cumo_int64_upcast();
190
+ Init_cumo_uint64_upcast();
191
+ Init_cumo_int32_upcast();
192
+ Init_cumo_uint32_upcast();
193
+ Init_cumo_int16_upcast();
194
+ Init_cumo_uint16_upcast();
195
+ Init_cumo_int8_upcast();
196
+ Init_cumo_uint8_upcast();
197
+ Init_cumo_bit_upcast();
198
+ Init_cumo_robject_upcast();
199
+
166
200
  Init_cumo_na_math();
167
201
 
168
202
  Init_cumo_na_rand();
data/ext/cumo/depend.erb CHANGED
@@ -49,6 +49,13 @@ list_type_rb.each do |type_rb|
49
49
 
50
50
  src : <%= list_type_cu.join(" ") %> <%= list_type_c.join(" ") %>
51
51
 
52
+ # mkmf writes no rule that makes an object depend on a header, so editing one
53
+ # and rebuilding was a no-op that kept the previous cumo.so. Rebuilding all of
54
+ # them is a minute and a half, so the list does not have to be per-object.
55
+ CUMO_HDRS = <%= Dir.glob("#{__dir__}/**/*.{h,hpp}").sort.join(" ") %>
56
+
57
+ $(OBJS): $(CUMO_HDRS)
58
+
52
59
  build-ctest : <%= __dir__ %>/cuda/memory_pool_impl_test.exe
53
60
 
54
61
  run-ctest : <%= __dir__ %>/cuda/memory_pool_impl_test.exe
data/ext/cumo/extconf.rb CHANGED
@@ -64,6 +64,7 @@ narray/types/uint8
64
64
  narray/types/uint16
65
65
  narray/types/uint32
66
66
  narray/types/uint64
67
+ narray/types/hfloat
67
68
  narray/types/sfloat
68
69
  narray/types/dfloat
69
70
  narray/types/scomplex
@@ -78,6 +79,7 @@ narray/types/uint8_kernel
78
79
  narray/types/uint16_kernel
79
80
  narray/types/uint32_kernel
80
81
  narray/types/uint64_kernel
82
+ narray/types/hfloat_kernel
81
83
  narray/types/sfloat_kernel
82
84
  narray/types/dfloat_kernel
83
85
  narray/types/scomplex_kernel