ruby-dnn 0.1.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 (127) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +5 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +42 -0
  8. data/Rakefile +10 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/lib/dnn.rb +14 -0
  12. data/lib/dnn/core/activations.rb +116 -0
  13. data/lib/dnn/core/error.rb +13 -0
  14. data/lib/dnn/core/initializers.rb +46 -0
  15. data/lib/dnn/core/layers.rb +366 -0
  16. data/lib/dnn/core/model.rb +158 -0
  17. data/lib/dnn/core/optimizers.rb +113 -0
  18. data/lib/dnn/core/util.rb +24 -0
  19. data/lib/dnn/core/version.rb +3 -0
  20. data/lib/dnn/ext/cifar10/Makefile +263 -0
  21. data/lib/dnn/ext/cifar10/cifar10_ext.c +52 -0
  22. data/lib/dnn/ext/cifar10/cifar10_ext.o +0 -0
  23. data/lib/dnn/ext/cifar10/cifar10_ext.so +0 -0
  24. data/lib/dnn/ext/cifar10/extconf.rb +3 -0
  25. data/lib/dnn/ext/cifar10/numo/compat.h +23 -0
  26. data/lib/dnn/ext/cifar10/numo/extconf.h +13 -0
  27. data/lib/dnn/ext/cifar10/numo/intern.h +117 -0
  28. data/lib/dnn/ext/cifar10/numo/narray.h +430 -0
  29. data/lib/dnn/ext/cifar10/numo/ndloop.h +94 -0
  30. data/lib/dnn/ext/cifar10/numo/template.h +149 -0
  31. data/lib/dnn/ext/cifar10/numo/types/bit.h +33 -0
  32. data/lib/dnn/ext/cifar10/numo/types/complex.h +409 -0
  33. data/lib/dnn/ext/cifar10/numo/types/complex_macro.h +377 -0
  34. data/lib/dnn/ext/cifar10/numo/types/dcomplex.h +44 -0
  35. data/lib/dnn/ext/cifar10/numo/types/dfloat.h +42 -0
  36. data/lib/dnn/ext/cifar10/numo/types/float_def.h +34 -0
  37. data/lib/dnn/ext/cifar10/numo/types/float_macro.h +186 -0
  38. data/lib/dnn/ext/cifar10/numo/types/int16.h +24 -0
  39. data/lib/dnn/ext/cifar10/numo/types/int32.h +24 -0
  40. data/lib/dnn/ext/cifar10/numo/types/int64.h +24 -0
  41. data/lib/dnn/ext/cifar10/numo/types/int8.h +24 -0
  42. data/lib/dnn/ext/cifar10/numo/types/int_macro.h +41 -0
  43. data/lib/dnn/ext/cifar10/numo/types/real_accum.h +486 -0
  44. data/lib/dnn/ext/cifar10/numo/types/robj_macro.h +75 -0
  45. data/lib/dnn/ext/cifar10/numo/types/robject.h +27 -0
  46. data/lib/dnn/ext/cifar10/numo/types/scomplex.h +44 -0
  47. data/lib/dnn/ext/cifar10/numo/types/sfloat.h +43 -0
  48. data/lib/dnn/ext/cifar10/numo/types/uint16.h +21 -0
  49. data/lib/dnn/ext/cifar10/numo/types/uint32.h +21 -0
  50. data/lib/dnn/ext/cifar10/numo/types/uint64.h +21 -0
  51. data/lib/dnn/ext/cifar10/numo/types/uint8.h +21 -0
  52. data/lib/dnn/ext/cifar10/numo/types/uint_macro.h +32 -0
  53. data/lib/dnn/ext/cifar10/numo/types/xint_macro.h +189 -0
  54. data/lib/dnn/ext/image_io/Makefile +263 -0
  55. data/lib/dnn/ext/image_io/extconf.rb +3 -0
  56. data/lib/dnn/ext/image_io/image_io_ext.c +89 -0
  57. data/lib/dnn/ext/image_io/image_io_ext.so +0 -0
  58. data/lib/dnn/ext/image_io/numo/compat.h +23 -0
  59. data/lib/dnn/ext/image_io/numo/extconf.h +13 -0
  60. data/lib/dnn/ext/image_io/numo/intern.h +117 -0
  61. data/lib/dnn/ext/image_io/numo/narray.h +430 -0
  62. data/lib/dnn/ext/image_io/numo/ndloop.h +94 -0
  63. data/lib/dnn/ext/image_io/numo/template.h +149 -0
  64. data/lib/dnn/ext/image_io/numo/types/bit.h +33 -0
  65. data/lib/dnn/ext/image_io/numo/types/complex.h +409 -0
  66. data/lib/dnn/ext/image_io/numo/types/complex_macro.h +377 -0
  67. data/lib/dnn/ext/image_io/numo/types/dcomplex.h +44 -0
  68. data/lib/dnn/ext/image_io/numo/types/dfloat.h +42 -0
  69. data/lib/dnn/ext/image_io/numo/types/float_def.h +34 -0
  70. data/lib/dnn/ext/image_io/numo/types/float_macro.h +186 -0
  71. data/lib/dnn/ext/image_io/numo/types/int16.h +24 -0
  72. data/lib/dnn/ext/image_io/numo/types/int32.h +24 -0
  73. data/lib/dnn/ext/image_io/numo/types/int64.h +24 -0
  74. data/lib/dnn/ext/image_io/numo/types/int8.h +24 -0
  75. data/lib/dnn/ext/image_io/numo/types/int_macro.h +41 -0
  76. data/lib/dnn/ext/image_io/numo/types/real_accum.h +486 -0
  77. data/lib/dnn/ext/image_io/numo/types/robj_macro.h +75 -0
  78. data/lib/dnn/ext/image_io/numo/types/robject.h +27 -0
  79. data/lib/dnn/ext/image_io/numo/types/scomplex.h +44 -0
  80. data/lib/dnn/ext/image_io/numo/types/sfloat.h +43 -0
  81. data/lib/dnn/ext/image_io/numo/types/uint16.h +21 -0
  82. data/lib/dnn/ext/image_io/numo/types/uint32.h +21 -0
  83. data/lib/dnn/ext/image_io/numo/types/uint64.h +21 -0
  84. data/lib/dnn/ext/image_io/numo/types/uint8.h +21 -0
  85. data/lib/dnn/ext/image_io/numo/types/uint_macro.h +32 -0
  86. data/lib/dnn/ext/image_io/numo/types/xint_macro.h +189 -0
  87. data/lib/dnn/ext/image_io/stb_image.h +7462 -0
  88. data/lib/dnn/ext/image_io/stb_image_write.h +1568 -0
  89. data/lib/dnn/ext/mnist/Makefile +263 -0
  90. data/lib/dnn/ext/mnist/extconf.rb +3 -0
  91. data/lib/dnn/ext/mnist/mnist_ext.c +49 -0
  92. data/lib/dnn/ext/mnist/mnist_ext.o +0 -0
  93. data/lib/dnn/ext/mnist/mnist_ext.so +0 -0
  94. data/lib/dnn/ext/mnist/numo/compat.h +23 -0
  95. data/lib/dnn/ext/mnist/numo/extconf.h +13 -0
  96. data/lib/dnn/ext/mnist/numo/intern.h +117 -0
  97. data/lib/dnn/ext/mnist/numo/narray.h +430 -0
  98. data/lib/dnn/ext/mnist/numo/ndloop.h +94 -0
  99. data/lib/dnn/ext/mnist/numo/template.h +149 -0
  100. data/lib/dnn/ext/mnist/numo/types/bit.h +33 -0
  101. data/lib/dnn/ext/mnist/numo/types/complex.h +409 -0
  102. data/lib/dnn/ext/mnist/numo/types/complex_macro.h +377 -0
  103. data/lib/dnn/ext/mnist/numo/types/dcomplex.h +44 -0
  104. data/lib/dnn/ext/mnist/numo/types/dfloat.h +42 -0
  105. data/lib/dnn/ext/mnist/numo/types/float_def.h +34 -0
  106. data/lib/dnn/ext/mnist/numo/types/float_macro.h +186 -0
  107. data/lib/dnn/ext/mnist/numo/types/int16.h +24 -0
  108. data/lib/dnn/ext/mnist/numo/types/int32.h +24 -0
  109. data/lib/dnn/ext/mnist/numo/types/int64.h +24 -0
  110. data/lib/dnn/ext/mnist/numo/types/int8.h +24 -0
  111. data/lib/dnn/ext/mnist/numo/types/int_macro.h +41 -0
  112. data/lib/dnn/ext/mnist/numo/types/real_accum.h +486 -0
  113. data/lib/dnn/ext/mnist/numo/types/robj_macro.h +75 -0
  114. data/lib/dnn/ext/mnist/numo/types/robject.h +27 -0
  115. data/lib/dnn/ext/mnist/numo/types/scomplex.h +44 -0
  116. data/lib/dnn/ext/mnist/numo/types/sfloat.h +43 -0
  117. data/lib/dnn/ext/mnist/numo/types/uint16.h +21 -0
  118. data/lib/dnn/ext/mnist/numo/types/uint32.h +21 -0
  119. data/lib/dnn/ext/mnist/numo/types/uint64.h +21 -0
  120. data/lib/dnn/ext/mnist/numo/types/uint8.h +21 -0
  121. data/lib/dnn/ext/mnist/numo/types/uint_macro.h +32 -0
  122. data/lib/dnn/ext/mnist/numo/types/xint_macro.h +189 -0
  123. data/lib/dnn/lib/cifar10.rb +26 -0
  124. data/lib/dnn/lib/image_io.rb +33 -0
  125. data/lib/dnn/lib/mnist.rb +61 -0
  126. data/ruby-dnn.gemspec +41 -0
  127. metadata +225 -0
@@ -0,0 +1,158 @@
1
+ module DNN
2
+ class Model
3
+ attr_reader :layers
4
+ attr_reader :optimizer
5
+ attr_reader :batch_size
6
+ attr_reader :training
7
+
8
+ def self.load(file_name)
9
+ Marshal.load(File.binread(file_name))
10
+ end
11
+
12
+ def initialize
13
+ @layers = []
14
+ @optimizer = nil
15
+ @batch_size = nil
16
+ end
17
+
18
+ def save(file_name)
19
+ File.binwrite(file_name, Marshal.dump(self))
20
+ end
21
+
22
+ def <<(layer)
23
+ unless layer.is_a?(Layers::Layer)
24
+ raise DNN_TypeError.new("layer is not an instance of the DNN::Layers::Layer class.")
25
+ end
26
+ @layers << layer
27
+ end
28
+
29
+ def compile(optimizer)
30
+ unless optimizer.is_a?(Optimizers::Optimizer)
31
+ raise DNN_TypeError.new("optimizer is not an instance of the DNN::Optimizers::Optimizer class.")
32
+ end
33
+ layers_check
34
+ @optimizer = optimizer
35
+ @layers.each do |layer|
36
+ layer.init(self)
37
+ end
38
+ layers_shape_check
39
+ end
40
+
41
+ def train(x, y, epochs,
42
+ batch_size: 1,
43
+ batch_proc: nil,
44
+ verbose: true,
45
+ &epoch_proc)
46
+ @batch_size = batch_size
47
+ num_train_data = x.shape[0]
48
+ (1..epochs).each do |epoch|
49
+ puts "【 epoch #{epoch}/#{epochs} 】" if verbose
50
+ (num_train_data.to_f / @batch_size).ceil.times do |index|
51
+ x_batch, y_batch = Util.get_minibatch(x, y, @batch_size)
52
+ loss = train_on_batch(x_batch, y_batch, @batch_size, &batch_proc)
53
+ if loss.nan?
54
+ puts "\nloss is nan" if verbose
55
+ return
56
+ end
57
+ num_trained_data = (index + 1) * batch_size
58
+ num_trained_data = num_trained_data > num_train_data ? num_train_data : num_trained_data
59
+ log = "\r"
60
+ 20.times do |i|
61
+ if i < num_trained_data * 20 / num_train_data
62
+ log << "■"
63
+ else
64
+ log << "・"
65
+ end
66
+ end
67
+ log << " #{num_trained_data}/#{num_train_data} loss: #{loss}"
68
+ print log if verbose
69
+ end
70
+ puts "" if verbose
71
+ epoch_proc.call(epoch) if epoch_proc
72
+ end
73
+ end
74
+
75
+ def train_on_batch(x, y, batch_size, &batch_proc)
76
+ @batch_size = batch_size
77
+ x, y = batch_proc.call(x, y) if batch_proc
78
+ forward(x, true)
79
+ backward(y)
80
+ @layers.each { |layer| layer.update if layer.respond_to?(:update) }
81
+ @layers[-1].loss(y)
82
+ end
83
+
84
+ def test(x, y, batch_size = nil, &batch_proc)
85
+ @batch_size = batch_size if batch_size
86
+ acc = accurate(x, y, @batch_size, &batch_proc)
87
+ puts "accurate: #{acc}"
88
+ acc
89
+ end
90
+
91
+ def accurate(x, y, batch_size = nil, &batch_proc)
92
+ @batch_size = batch_size if batch_size
93
+ correct = 0
94
+ (x.shape[0].to_f / @batch_size).ceil.times do |i|
95
+ x_batch = SFloat.zeros(@batch_size, *x.shape[1..-1])
96
+ y_batch = SFloat.zeros(@batch_size, *y.shape[1..-1])
97
+ @batch_size.times do |j|
98
+ k = i * @batch_size + j
99
+ break if k >= x.shape[0]
100
+ x_batch[j, false] = x[k, false]
101
+ y_batch[j, false] = y[k, false]
102
+ end
103
+ x_batch, y_batch = batch_proc.call(x_batch, y_batch) if batch_proc
104
+ out = forward(x_batch, false)
105
+ @batch_size.times do |j|
106
+ correct += 1 if out[j, true].max_index == y_batch[j, true].max_index
107
+ end
108
+ end
109
+ correct.to_f / x.shape[0]
110
+ end
111
+
112
+ def predict(x)
113
+ forward(x, false)
114
+ end
115
+
116
+ private
117
+
118
+ def forward(x, training)
119
+ @training = training
120
+ @layers.each do |layer|
121
+ x = layer.forward(x)
122
+ end
123
+ x
124
+ end
125
+
126
+ def backward(y)
127
+ dout = y
128
+ @layers[0..-1].reverse.each do |layer|
129
+ dout = layer.backward(dout)
130
+ end
131
+ end
132
+
133
+ def layers_check
134
+ unless @layers.first.is_a?(Layers::InputLayer)
135
+ raise DNN_Error.new("The first layer is not an InputLayer.")
136
+ end
137
+ unless @layers.last.is_a?(Layers::OutputLayer)
138
+ raise DNN_Error.new("The last layer is not an OutputLayer.")
139
+ end
140
+ end
141
+
142
+ def layers_shape_check
143
+ @layers.each.with_index do |layer, i|
144
+ if layer.is_a?(Layers::Dense)
145
+ prev_shape = layer.prev_layer.shape
146
+ if prev_shape.length != 1
147
+ raise DNN_SharpError.new("layer index(#{i}) Dense: The shape of the previous layer is #{prev_shape}. The shape of the previous layer must be 1 dimensional.")
148
+ end
149
+ elsif layer.is_a?(Layers::Conv2D) || layer.is_a?(Layers::MaxPool2D)
150
+ prev_shape = layer.prev_layer.shape
151
+ if prev_shape.length != 3
152
+ raise DNN_SharpError.new("layer index(#{i}) Conv2D: The shape of the previous layer is #{prev_shape}. The shape of the previous layer must be 3 dimensional.")
153
+ end
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,113 @@
1
+ module DNN
2
+ module Optimizers
3
+
4
+ #Super class of all optimizer classes.
5
+ class Optimizer
6
+ attr_accessor :learning_rate
7
+
8
+ def initialize(learning_rate)
9
+ @learning_rate = learning_rate
10
+ end
11
+
12
+ #Update layer has params.
13
+ def update(layer) end
14
+ end
15
+
16
+
17
+ class SGD < Optimizer
18
+ attr_accessor :momentum
19
+
20
+ def initialize(learning_rate = 0.01, momentum: 0)
21
+ super(learning_rate)
22
+ @momentum = momentum
23
+ @amounts = {}
24
+ end
25
+
26
+ def update(layer)
27
+ amount = if @amounts[layer]
28
+ @amounts[layer]
29
+ else
30
+ @amounts[layer] = {}
31
+ end
32
+ layer.params.each_key do |key|
33
+ amount[key] = layer.grads[key] * @learning_rate
34
+ if @momentum > 0
35
+ @amounts[layer][key] ||= 0
36
+ amount[key] += @momentum * @amounts[layer][key]
37
+ @amounts[layer] = amount
38
+ end
39
+ layer.params[key] -= amount[key]
40
+ end
41
+ end
42
+ end
43
+
44
+
45
+ class AdaGrad
46
+ def initialize(learning_rate = 0.01)
47
+ super(learning_rate)
48
+ @g = {}
49
+ end
50
+
51
+ def update(layer)
52
+ @g[layer] ||= {}
53
+ layer.params.each_key do |key|
54
+ @g[layer][key] ||= 0
55
+ @g[layer][key] += layer.grads[key]**2
56
+ layer.params[key] -= (@learning_rate / NMath.sqrt(@g[layer][key] + 1e-7)) * layer.grads[key]
57
+ end
58
+ end
59
+ end
60
+
61
+
62
+ class RMSProp < Optimizer
63
+ attr_accessor :muse
64
+
65
+ def initialize(learning_rate = 0.001, muse = 0.9)
66
+ super(learning_rate)
67
+ @muse = muse
68
+ @g = {}
69
+ end
70
+
71
+ def update(layer)
72
+ @g[layer] ||= {}
73
+ layer.params.each_key do |key|
74
+ @g[layer][key] ||= 0
75
+ @g[layer][key] = @muse * @g[layer][key] + (1 - @muse) * layer.grads[key]**2
76
+ layer.params[key] -= (@learning_rate / NMath.sqrt(@g[layer][key] + 1e-7)) * layer.grads[key]
77
+ end
78
+ end
79
+ end
80
+
81
+
82
+ class Adam < Optimizer
83
+ include Numo
84
+
85
+ attr_accessor :beta1
86
+ attr_accessor :beta2
87
+
88
+ def initialize(learning_rate = 0.001, beta1 = 0.9, beta2 = 0.999)
89
+ super(learning_rate)
90
+ @beta1 = beta1
91
+ @beta2 = beta2
92
+ @iter = 0
93
+ @m = {}
94
+ @v = {}
95
+ end
96
+
97
+ def update(layer)
98
+ @iter += 1
99
+ @m[layer] ||= {}
100
+ @v[layer] ||= {}
101
+ lr = @learning_rate * Math.sqrt(1 - @beta2**@iter) / (1 - @beta1**@iter)
102
+ layer.params.each_key do |key|
103
+ @m[layer][key] ||= 0
104
+ @v[layer][key] ||= 0
105
+ @m[layer][key] += (1 - @beta1) * (layer.grads[key] - @m[layer][key])
106
+ @v[layer][key] += (1 - @beta2) * (layer.grads[key]**2 - @v[layer][key])
107
+ layer.params[key] -= lr * @m[layer][key] / NMath.sqrt(@v[layer][key] + 1e-7)
108
+ end
109
+ end
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,24 @@
1
+ module DNN
2
+ module Util
3
+ #Create a mini batch for batch size.
4
+ def self.get_minibatch(x, y, batch_size)
5
+ indexes = (0...x.shape[0]).to_a.sample(batch_size)
6
+ [x[indexes, false], y[indexes, false]]
7
+ end
8
+
9
+ #Categorize labels into "num_classes" classes.
10
+ def self.to_categorical(y, num_classes, narray_type = nil)
11
+ narray_type ||= y.class
12
+ y2 = narray_type.zeros(y.shape[0], num_classes)
13
+ y.shape[0].times do |i|
14
+ y2[i, y[i]] = 1
15
+ end
16
+ y2
17
+ end
18
+
19
+ #Perform numerical differentiation on "forward" of "layer".
20
+ def self.numerical_grad(x, func)
21
+ (func.(x + 1e-7) - func.(x)) / 1e-7
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module DNN
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,263 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ # V=0 quiet, V=1 verbose. other values don't work.
5
+ V = 0
6
+ Q1 = $(V:1=)
7
+ Q = $(Q1:0=@)
8
+ ECHO1 = $(V:1=@ :)
9
+ ECHO = $(ECHO1:0=@ echo)
10
+ NULLCMD = :
11
+
12
+ #### Start of system configuration section. ####
13
+
14
+ srcdir = .
15
+ topdir = /home/ootoro/.rbenv/versions/2.5.1/include/ruby-2.5.0
16
+ hdrdir = $(topdir)
17
+ arch_hdrdir = /home/ootoro/.rbenv/versions/2.5.1/include/ruby-2.5.0/x86_64-linux
18
+ PATH_SEPARATOR = :
19
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
20
+ prefix = $(DESTDIR)/home/ootoro/.rbenv/versions/2.5.1
21
+ rubysitearchprefix = $(rubylibprefix)/$(sitearch)
22
+ rubyarchprefix = $(rubylibprefix)/$(arch)
23
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
24
+ exec_prefix = $(prefix)
25
+ vendorarchhdrdir = $(vendorhdrdir)/$(sitearch)
26
+ sitearchhdrdir = $(sitehdrdir)/$(sitearch)
27
+ rubyarchhdrdir = $(rubyhdrdir)/$(arch)
28
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
29
+ sitehdrdir = $(rubyhdrdir)/site_ruby
30
+ rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME)
31
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
32
+ vendorlibdir = $(vendordir)/$(ruby_version)
33
+ vendordir = $(rubylibprefix)/vendor_ruby
34
+ sitearchdir = $(sitelibdir)/$(sitearch)
35
+ sitelibdir = $(sitedir)/$(ruby_version)
36
+ sitedir = $(rubylibprefix)/site_ruby
37
+ rubyarchdir = $(rubylibdir)/$(arch)
38
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
39
+ sitearchincludedir = $(includedir)/$(sitearch)
40
+ archincludedir = $(includedir)/$(arch)
41
+ sitearchlibdir = $(libdir)/$(sitearch)
42
+ archlibdir = $(libdir)/$(arch)
43
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
44
+ mandir = $(datarootdir)/man
45
+ localedir = $(datarootdir)/locale
46
+ libdir = $(exec_prefix)/lib
47
+ psdir = $(docdir)
48
+ pdfdir = $(docdir)
49
+ dvidir = $(docdir)
50
+ htmldir = $(docdir)
51
+ infodir = $(datarootdir)/info
52
+ docdir = $(datarootdir)/doc/$(PACKAGE)
53
+ oldincludedir = $(DESTDIR)/usr/include
54
+ includedir = $(prefix)/include
55
+ localstatedir = $(prefix)/var
56
+ sharedstatedir = $(prefix)/com
57
+ sysconfdir = $(prefix)/etc
58
+ datadir = $(datarootdir)
59
+ datarootdir = $(prefix)/share
60
+ libexecdir = $(exec_prefix)/libexec
61
+ sbindir = $(exec_prefix)/sbin
62
+ bindir = $(exec_prefix)/bin
63
+ archdir = $(rubyarchdir)
64
+
65
+
66
+ CC = gcc
67
+ CXX = g++
68
+ LIBRUBY = $(LIBRUBY_A)
69
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
70
+ LIBRUBYARG_SHARED = -Wl,-rpath,$(libdir) -L$(libdir)
71
+ LIBRUBYARG_STATIC = -Wl,-rpath,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static
72
+ empty =
73
+ OUTFLAG = -o $(empty)
74
+ COUTFLAG = -o $(empty)
75
+ CSRCFLAG = $(empty)
76
+
77
+ RUBY_EXTCONF_H =
78
+ cflags = $(optflags) $(debugflags) $(warnflags)
79
+ cxxflags = $(optflags) $(debugflags) $(warnflags)
80
+ optflags = -O3
81
+ debugflags = -ggdb3
82
+ warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -Wno-maybe-uninitialized
83
+ CCDLFLAGS = -fPIC
84
+ CFLAGS = $(CCDLFLAGS) $(cflags) $(ARCH_FLAG)
85
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
86
+ DEFS =
87
+ CPPFLAGS = -I/home/ootoro/.rbenv/versions/2.5.1/include $(DEFS) $(cppflags)
88
+ CXXFLAGS = $(CCDLFLAGS) $(cxxflags) $(ARCH_FLAG)
89
+ ldflags = -L. -L/home/ootoro/.rbenv/versions/2.5.1/lib -fstack-protector -rdynamic -Wl,-export-dynamic
90
+ dldflags = -L/home/ootoro/.rbenv/versions/2.5.1/lib -Wl,--compress-debug-sections=zlib
91
+ ARCH_FLAG =
92
+ DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
93
+ LDSHARED = $(CC) -shared
94
+ LDSHAREDXX = $(CXX) -shared
95
+ AR = ar
96
+ EXEEXT =
97
+
98
+ RUBY_INSTALL_NAME = $(RUBY_BASE_NAME)
99
+ RUBY_SO_NAME = ruby
100
+ RUBYW_INSTALL_NAME =
101
+ RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
102
+ RUBYW_BASE_NAME = rubyw
103
+ RUBY_BASE_NAME = ruby
104
+
105
+ arch = x86_64-linux
106
+ sitearch = $(arch)
107
+ ruby_version = 2.5.0
108
+ ruby = $(bindir)/$(RUBY_BASE_NAME)
109
+ RUBY = $(ruby)
110
+ ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/backward.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h
111
+
112
+ RM = rm -f
113
+ RM_RF = $(RUBY) -run -e rm -- -rf
114
+ RMDIRS = rmdir --ignore-fail-on-non-empty -p
115
+ MAKEDIRS = /bin/mkdir -p
116
+ INSTALL = /usr/bin/install -c
117
+ INSTALL_PROG = $(INSTALL) -m 0755
118
+ INSTALL_DATA = $(INSTALL) -m 644
119
+ COPY = cp
120
+ TOUCH = exit >
121
+
122
+ #### End of system configuration section. ####
123
+
124
+ preload =
125
+ libpath = . $(libdir)
126
+ LIBPATH = -L. -L$(libdir) -Wl,-rpath,$(libdir)
127
+ DEFFILE =
128
+
129
+ CLEANFILES = mkmf.log
130
+ DISTCLEANFILES =
131
+ DISTCLEANDIRS =
132
+
133
+ extout =
134
+ extout_prefix =
135
+ target_prefix =
136
+ LOCAL_LIBS =
137
+ LIBS = -lpthread -ldl -lcrypt -lm -lc
138
+ ORIG_SRCS = cifar10_ext.c
139
+ SRCS = $(ORIG_SRCS)
140
+ OBJS = cifar10_ext.o
141
+ HDRS =
142
+ LOCAL_HDRS =
143
+ TARGET = cifar10_ext
144
+ TARGET_NAME = cifar10_ext
145
+ TARGET_ENTRY = Init_$(TARGET_NAME)
146
+ DLLIB = $(TARGET).so
147
+ EXTSTATIC =
148
+ STATIC_LIB =
149
+
150
+ TIMESTAMP_DIR = .
151
+ BINDIR = $(bindir)
152
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
153
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
154
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
155
+ HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
156
+ ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
157
+ TARGET_SO_DIR =
158
+ TARGET_SO = $(TARGET_SO_DIR)$(DLLIB)
159
+ CLEANLIBS = $(TARGET_SO)
160
+ CLEANOBJS = *.o *.bak
161
+
162
+ all: $(DLLIB)
163
+ static: $(STATIC_LIB)
164
+ .PHONY: all install static install-so install-rb
165
+ .PHONY: clean clean-so clean-static clean-rb
166
+
167
+ clean-static::
168
+ clean-rb-default::
169
+ clean-rb::
170
+ clean-so::
171
+ clean: clean-so clean-static clean-rb-default clean-rb
172
+ -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
173
+
174
+ distclean-rb-default::
175
+ distclean-rb::
176
+ distclean-so::
177
+ distclean-static::
178
+ distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
179
+ -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
180
+ -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
181
+ -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
182
+
183
+ realclean: distclean
184
+ install: install-so install-rb
185
+
186
+ install-so: $(DLLIB) $(TIMESTAMP_DIR)/.sitearchdir.time
187
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
188
+ clean-static::
189
+ -$(Q)$(RM) $(STATIC_LIB)
190
+ install-rb: pre-install-rb do-install-rb install-rb-default
191
+ install-rb-default: pre-install-rb-default do-install-rb-default
192
+ pre-install-rb: Makefile
193
+ pre-install-rb-default: Makefile
194
+ do-install-rb:
195
+ do-install-rb-default:
196
+ pre-install-rb-default:
197
+ @$(NULLCMD)
198
+ $(TIMESTAMP_DIR)/.sitearchdir.time:
199
+ $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
200
+ $(Q) $(TOUCH) $@
201
+
202
+ site-install: site-install-so site-install-rb
203
+ site-install-so: install-so
204
+ site-install-rb: install-rb
205
+
206
+ .SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S
207
+
208
+ .cc.o:
209
+ $(ECHO) compiling $(<)
210
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
211
+
212
+ .cc.S:
213
+ $(ECHO) translating $(<)
214
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
215
+
216
+ .mm.o:
217
+ $(ECHO) compiling $(<)
218
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
219
+
220
+ .mm.S:
221
+ $(ECHO) translating $(<)
222
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
223
+
224
+ .cxx.o:
225
+ $(ECHO) compiling $(<)
226
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
227
+
228
+ .cxx.S:
229
+ $(ECHO) translating $(<)
230
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
231
+
232
+ .cpp.o:
233
+ $(ECHO) compiling $(<)
234
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
235
+
236
+ .cpp.S:
237
+ $(ECHO) translating $(<)
238
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
239
+
240
+ .c.o:
241
+ $(ECHO) compiling $(<)
242
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
243
+
244
+ .c.S:
245
+ $(ECHO) translating $(<)
246
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
247
+
248
+ .m.o:
249
+ $(ECHO) compiling $(<)
250
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
251
+
252
+ .m.S:
253
+ $(ECHO) translating $(<)
254
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
255
+
256
+ $(TARGET_SO): $(OBJS) Makefile
257
+ $(ECHO) linking shared-object $(DLLIB)
258
+ -$(Q)$(RM) $(@)
259
+ $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
260
+
261
+
262
+
263
+ $(OBJS): $(HDRS) $(ruby_headers)