diff --git a/README.md b/README.md index 4e24edf..696e81c 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,24 @@ $ bundle exec ruby examples/mnist/mnist.rb $ ruby examples/mnist/mnist.rb ``` +## Development + +### Run tests + +``` +$ bundle exec ruby test/run_test.rb +``` + +### Run tests with Cumo + +On GPU machine, add `gem 'cumo'` on Gemfile and do `bundle install`. + +Run tests with `RED_CHAINER_GPU` environment variable whose value indicates GPU device ID such as: + +``` +$ bundle exec env RED_CHAINER_GPU=0 ruby test/run_test.rb +``` + ## License The MIT license. See [LICENSE.txt](./LICENSE.txt) for details. diff --git a/test/backend_test.rb b/test/backend_test.rb index 5d1ca57..ee60254 100644 --- a/test/backend_test.rb +++ b/test/backend_test.rb @@ -3,23 +3,13 @@ require 'chainer' class TestBackend < Test::Unit::TestCase - def test_get_array_module_for_numo() - assert Numo == Chainer.get_array_module(Numo::NArray[]) - assert Numo == Chainer.get_array_module(Chainer::Variable.new(Numo::NArray[])) + def test_get_array_module + assert xm == Chainer.get_array_module(xm::NArray[]) + assert xm == Chainer.get_array_module(Chainer::Variable.new(xm::NArray[])) end - def test_get_array_module_for_cumo() - assert_equal(Cumo, Chainer.get_array_module(Cumo::NArray[])) - assert_equal(Cumo, Chainer.get_array_module(Chainer::Variable.new(Cumo::NArray[]))) - end if Chainer::CUDA.available? - - def test_array_p_for_numo() - assert_equal(Numo, Chainer.get_array_module(Numo::NArray[])) - assert_equal(Numo, Chainer.get_array_module(Chainer::Variable.new(Numo::NArray[]))) + def test_array_p + assert_equal(xm, Chainer.get_array_module(xm::NArray[])) + assert_equal(xm, Chainer.get_array_module(Chainer::Variable.new(xm::NArray[]))) end - - def test_array_p_for_cumo() - assert_equal(Cumo, Chainer.get_array_module(Cumo::NArray[])) - assert_equal(Cumo, Chainer.get_array_module(Chainer::Variable.new(Cumo::NArray[]))) - end if Chainer::CUDA.available? end diff --git a/test/dataset/convert_test.rb b/test/dataset/convert_test.rb index 40ee46f..b827a53 100644 --- a/test/dataset/convert_test.rb +++ b/test/dataset/convert_test.rb @@ -24,7 +24,7 @@ def check_concat_arrays(arrays, device: nil) end def test_concat_arrays() - arrays = get_arrays_to_concat(Numo) + arrays = get_arrays_to_concat(xm) check_concat_arrays(arrays) end @@ -46,7 +46,7 @@ def check_concat_tuples(tuples, device: nil) end def test_concat_tuples() - tuples = get_tuple_arrays_to_concat(Numo) + tuples = get_tuple_arrays_to_concat(xm) check_concat_tuples(tuples) end end @@ -69,7 +69,7 @@ def check_concat_arrays_padding(xumo) end def test_concat_arrays_padding() - check_concat_arrays_padding(Numo) + check_concat_arrays_padding(xm) end def check_concat_tuples_padding(xumo) @@ -106,7 +106,7 @@ def check_concat_tuples_padding(xumo) end def test_concat_tuples_padding() - check_concat_tuples_padding(Numo) + check_concat_tuples_padding(xm) end end @@ -122,7 +122,7 @@ def check_device(array, device) if device && device >= 0 # T.B.I (GPU Check) else - assert_true array.is_a?(Numo::NArray) + assert_true array.is_a?(xm::NArray) end end @@ -132,7 +132,7 @@ def check_concat_arrays(arrays, device:, expected_type:) check_device(array, device) array.to_a.zip(arrays.to_a).each do |x, y| - assert_true Numo::NArray.cast(y).nearly_eq(Numo::NArray.cast(x)).all? + assert_true xm::NArray.cast(y).nearly_eq(xm::NArray.cast(x)).all? end end @@ -141,8 +141,8 @@ def test_concat_arrays(data) @padding = data[:padding] [-1, nil].each do |device| - check_concat_arrays(@@int_arrays, device: device, expected_type: Numo::Int64) - check_concat_arrays(@@float_arrays, device: device, expected_type: Numo::DFloat) + check_concat_arrays(@@int_arrays, device: device, expected_type: xm::Int64) + check_concat_arrays(@@float_arrays, device: device, expected_type: xm::DFloat) end end end diff --git a/test/device_test.rb b/test/device_test.rb index a93eb37..acc7bdd 100644 --- a/test/device_test.rb +++ b/test/device_test.rb @@ -18,6 +18,10 @@ def test_use end class TestGpuDevice < Test::Unit::TestCase + def setup + require_gpu + end + def test_xm assert Chainer::GpuDevice.new.xm == Cumo end @@ -35,16 +39,22 @@ def test_use begin Chainer::GpuDevice.new(0).use assert Cumo::CUDA::Runtime.cudaGetDevice == 0 + ensure + Cumo::CUDA::Runtime.cudaSetDevice(orig_device_id) + end + end - if Chainer::CUDA.available?(1) - Chainer::GpuDevice.new(1).use - assert Cumo::CUDA::Runtime.cudaGetDevice == 1 - end + def test_use_1 + require_gpu(1) + orig_device_id = Cumo::CUDA::Runtime.cudaGetDevice + begin + Chainer::GpuDevice.new(1).use + assert Cumo::CUDA::Runtime.cudaGetDevice == 1 ensure Cumo::CUDA::Runtime.cudaSetDevice(orig_device_id) end end - end if Chainer::CUDA.available? + end class TestGetDevice < Test::Unit::TestCase def test_device @@ -57,8 +67,9 @@ def test_negative_integer end def test_non_negative_integer + require_gpu assert Chainer.get_device(0) == Chainer::GpuDevice.new(0) - end if Chainer::CUDA.available? + end end def test_set_get_default_device diff --git a/test/functions/activation/leaky_relu_test.rb b/test/functions/activation/leaky_relu_test.rb index 5a3dc83..f0dad5f 100644 --- a/test/functions/activation/leaky_relu_test.rb +++ b/test/functions/activation/leaky_relu_test.rb @@ -4,10 +4,10 @@ class Chainer::Functions::Activation::LeakyReLUTest < Test::Unit::TestCase data = { - 'test1' => {shape: [3, 2], dtype: Numo::SFloat}, - 'test2' => {shape: [], dtype: Numo::SFloat}, - 'test3' => {shape: [3, 2], dtype: Numo::DFloat}, - 'test4' => {shape: [], dtype: Numo::DFloat}} + 'test1' => {shape: [3, 2], dtype: xm::SFloat}, + 'test2' => {shape: [], dtype: xm::SFloat}, + 'test3' => {shape: [3, 2], dtype: xm::DFloat}, + 'test4' => {shape: [], dtype: xm::DFloat}} def _setup(data) # Avoid unstability of numerical grad @@ -26,7 +26,7 @@ def _setup(data) @gy = @dtype.new(@shape).rand(2) - 1 @slope = Random.rand @check_forward_options = {} - @check_backward_options_dtype = Numo::DFloat + @check_backward_options_dtype = xm::DFloat end def check_forward(x_data) diff --git a/test/functions/activation/log_softmax_test.rb b/test/functions/activation/log_softmax_test.rb index 93110e5..e31f61b 100644 --- a/test/functions/activation/log_softmax_test.rb +++ b/test/functions/activation/log_softmax_test.rb @@ -6,15 +6,15 @@ class Chainer::Functions::Activation::LogSoftmaxTest < Test::Unit::TestCase data = { - # Not Support test1 case. See Numo::NArray issue #78. - #'test1' => {shape: nil, dtype: Numo::SFloat}, - 'test2' => {shape: [2, 3], dtype: Numo::SFloat}, - 'test3' => {shape: [2, 2, 3], dtype: Numo::SFloat}, - 'test4' => {shape: [2, 2, 2, 3], dtype: Numo::SFloat}, - 'test5' => {shape: nil, dtype: Numo::DFloat}, - 'test6' => {shape: [2, 3], dtype: Numo::DFloat}, - 'test7' => {shape: [2, 2, 3], dtype: Numo::DFloat}, - 'test8' => {shape: [2, 2, 2, 3], dtype: Numo::DFloat}} + # Not Support test1 case. See xm::NArray issue #78. + #'test1' => {shape: nil, dtype: xm::SFloat}, + 'test2' => {shape: [2, 3], dtype: xm::SFloat}, + 'test3' => {shape: [2, 2, 3], dtype: xm::SFloat}, + 'test4' => {shape: [2, 2, 2, 3], dtype: xm::SFloat}, + 'test5' => {shape: nil, dtype: xm::DFloat}, + 'test6' => {shape: [2, 3], dtype: xm::DFloat}, + 'test7' => {shape: [2, 2, 3], dtype: xm::DFloat}, + 'test8' => {shape: [2, 2, 2, 3], dtype: xm::DFloat}} def _setup(data) @shape = data[:shape] @@ -28,7 +28,7 @@ def _setup(data) end @gy = @dtype.new(@x.shape).rand(2) - 1 @check_forward_options = {} - @check_backward_options = {dtype: Numo::DFloat} + @check_backward_options = {dtype: xm::DFloat} end def check_forward(x_data, use_cudnn: "always") diff --git a/test/functions/activation/relu_test.rb b/test/functions/activation/relu_test.rb index 7f550d4..d1f8ea7 100644 --- a/test/functions/activation/relu_test.rb +++ b/test/functions/activation/relu_test.rb @@ -4,10 +4,10 @@ class Chainer::Functions::Activation::ReLUTest < Test::Unit::TestCase data = { - 'test1' => {shape: [3, 2], dtype: Numo::SFloat}, - 'test2' => {shape: [], dtype: Numo::SFloat}, - 'test3' => {shape: [3, 2], dtype: Numo::DFloat}, - 'test4' => {shape: [], dtype: Numo::DFloat}} + 'test1' => {shape: [3, 2], dtype: xm::SFloat}, + 'test2' => {shape: [], dtype: xm::SFloat}, + 'test3' => {shape: [3, 2], dtype: xm::DFloat}, + 'test4' => {shape: [], dtype: xm::DFloat}} def _setup(data) # Avoid unstability of numerical grad diff --git a/test/functions/activation/sigmoid_test.rb b/test/functions/activation/sigmoid_test.rb index 4e0babb..f6054ce 100644 --- a/test/functions/activation/sigmoid_test.rb +++ b/test/functions/activation/sigmoid_test.rb @@ -4,10 +4,10 @@ class Chainer::Functions::Activation::SigmoidTest < Test::Unit::TestCase data = { - 'test1' => {shape: [3, 2], dtype: Numo::SFloat}, - 'test2' => {shape: [], dtype: Numo::SFloat}, - 'test3' => {shape: [3, 2], dtype: Numo::DFloat}, - 'test4' => {shape: [], dtype: Numo::DFloat}} + 'test1' => {shape: [3, 2], dtype: xm::SFloat}, + 'test2' => {shape: [], dtype: xm::SFloat}, + 'test3' => {shape: [3, 2], dtype: xm::DFloat}, + 'test4' => {shape: [], dtype: xm::DFloat}} def _setup(data) @shape = data[:shape] diff --git a/test/functions/activation/tanh_test.rb b/test/functions/activation/tanh_test.rb index 99340d2..01452d2 100644 --- a/test/functions/activation/tanh_test.rb +++ b/test/functions/activation/tanh_test.rb @@ -4,10 +4,10 @@ class Chainer::Functions::Activation::TanhTest < Test::Unit::TestCase data = { - 'test1' => {shape: [3, 2], dtype: Numo::SFloat}, - 'test2' => {shape: [], dtype: Numo::SFloat}, - 'test3' => {shape: [3, 2], dtype: Numo::DFloat}, - 'test4' => {shape: [], dtype: Numo::DFloat}} + 'test1' => {shape: [3, 2], dtype: xm::SFloat}, + 'test2' => {shape: [], dtype: xm::SFloat}, + 'test3' => {shape: [3, 2], dtype: xm::DFloat}, + 'test4' => {shape: [], dtype: xm::DFloat}} def _setup(data) @shape = data[:shape] diff --git a/test/functions/array/broadcast_to_test.rb b/test/functions/array/broadcast_to_test.rb index 0214710..e3d10fa 100644 --- a/test/functions/array/broadcast_to_test.rb +++ b/test/functions/array/broadcast_to_test.rb @@ -18,7 +18,7 @@ class Chainer::Functions::Array::BroadcastToTest < Test::Unit::TestCase } ] - dtypes = [ Numo::SFloat, Numo::DFloat ] + dtypes = [ xm::SFloat, xm::DFloat ] data = shapes.map.with_index {|shape, i| dtypes.map do |dtype| @@ -40,8 +40,8 @@ def test_backward(data) in_data = data[:dtype].new(data[:in_shape]).rand grads = data[:dtype].new(data[:out_shape]).rand check_backward_options = {} - if data[:dtype] == Numo::SFloat - check_backward_options = { eps: 2 ** -5, atol: 1e-3, rtol: 1e-2 } + if data[:dtype] == xm::SFloat + check_backward_options = { eps: 2 ** -5, atol: 1e-3, rtol: 1e-2 } end func = Chainer::Functions::Array::BroadcastTo.new(data[:out_shape]) diff --git a/test/functions/array/reshape_test.rb b/test/functions/array/reshape_test.rb index 8fcf8bd..8f2c38a 100644 --- a/test/functions/array/reshape_test.rb +++ b/test/functions/array/reshape_test.rb @@ -3,9 +3,11 @@ require 'chainer/functions/array/reshape' class Chainer::Functions::Array::ReshapeTest < Test::Unit::TestCase + xm = Chainer.get_default_device.xm + in_shape = [4, 3, 2] out_shape = [2, 2, 6] - dtypes = [ Numo::SFloat, Numo::DFloat ] + dtypes = [ xm::SFloat, xm::DFloat ] data = dtypes.reduce({}) {|hash, dtype| hash[dtype.to_s] = {in_shape: in_shape, out_shape: out_shape, dtype: dtype} diff --git a/test/functions/connection/convolution_2d_test.rb b/test/functions/connection/convolution_2d_test.rb index 8050146..8b58ea6 100644 --- a/test/functions/connection/convolution_2d_test.rb +++ b/test/functions/connection/convolution_2d_test.rb @@ -3,28 +3,28 @@ class Chainer::Functions::Connection::Convolution2DTest < Test::Unit::TestCase data({ test1: { - case: { x: Numo::DFloat.new(1, 1, 4, 4).seq, w: Numo::DFloat.new(2, 1, 3, 3).seq, options: {} }, - expected: Numo::DFloat[[[[258.0, 294.0], [402.0, 438.0]], [[663.0, 780.0], [1131.0, 1248.0]]]] + case: { x: xm::DFloat.new(1, 1, 4, 4).seq, w: xm::DFloat.new(2, 1, 3, 3).seq, options: {} }, + expected: xm::DFloat[[[[258.0, 294.0], [402.0, 438.0]], [[663.0, 780.0], [1131.0, 1248.0]]]] }, test2: { - case: { x: Numo::DFloat.new(1, 2, 4, 4).seq, w: Numo::DFloat.new(2, 2, 3, 3).seq, options: {} }, - expected: Numo::DFloat[[[[2793.0, 2946.0], [3405.0, 3558.0]], [[7005.0, 7482.0], [8913.0, 9390.0]]]] + case: { x: xm::DFloat.new(1, 2, 4, 4).seq, w: xm::DFloat.new(2, 2, 3, 3).seq, options: {} }, + expected: xm::DFloat[[[[2793.0, 2946.0], [3405.0, 3558.0]], [[7005.0, 7482.0], [8913.0, 9390.0]]]] }, test3: { - case: { x: Numo::DFloat.new(2, 2, 4, 4).seq, w: Numo::DFloat.new(2, 2, 3, 3).seq, options: {} }, - expected: Numo::DFloat[[[[2793.0, 2946.0], [3405.0, 3558.0]], [[7005.0, 7482.0], [8913.0, 9390.0]]], [[[7689.0, 7842.0], [8301.0, 8454.0]], [[22269.0, 22746.0], [24177.0, 24654.0]]]] + case: { x: xm::DFloat.new(2, 2, 4, 4).seq, w: xm::DFloat.new(2, 2, 3, 3).seq, options: {} }, + expected: xm::DFloat[[[[2793.0, 2946.0], [3405.0, 3558.0]], [[7005.0, 7482.0], [8913.0, 9390.0]]], [[[7689.0, 7842.0], [8301.0, 8454.0]], [[22269.0, 22746.0], [24177.0, 24654.0]]]] }, test4: { - case: { x: Numo::DFloat.new(2, 2, 4, 4).seq, w: Numo::DFloat.new(2, 2, 3, 3).seq, options: { stride: 2 } }, - expected: Numo::DFloat[[[[2793.0]], [[7005.0]]], [[[7689.0]], [[22269.0]]]] + case: { x: xm::DFloat.new(2, 2, 4, 4).seq, w: xm::DFloat.new(2, 2, 3, 3).seq, options: { stride: 2 } }, + expected: xm::DFloat[[[[2793.0]], [[7005.0]]], [[[7689.0]], [[22269.0]]]] }, test5: { - case: { x: Numo::DFloat.new(2, 2, 4, 4).seq, w: Numo::DFloat.new(2, 2, 3, 3).seq, options: { b: Numo::DFloat[10, 33] } }, - expected: Numo::DFloat[[[[2803.0, 2956.0], [3415.0, 3568.0]], [[7038.0, 7515.0], [8946.0, 9423.0]]], [[[7699.0, 7852.0], [8311.0, 8464.0]], [[22302.0, 22779.0], [24210.0, 24687.0]]]] + case: { x: xm::DFloat.new(2, 2, 4, 4).seq, w: xm::DFloat.new(2, 2, 3, 3).seq, options: { b: xm::DFloat[10, 33] } }, + expected: xm::DFloat[[[[2803.0, 2956.0], [3415.0, 3568.0]], [[7038.0, 7515.0], [8946.0, 9423.0]]], [[[7699.0, 7852.0], [8311.0, 8464.0]], [[22302.0, 22779.0], [24210.0, 24687.0]]]] }, test6: { - case: { x: Numo::DFloat.new(2, 2, 4, 4).seq, w: Numo::DFloat.new(2, 2, 3, 3).seq, options: { b: Numo::DFloat[3, 5], pad: 1 } }, - expected: Numo::DFloat[[[[1199.0, 1799.0, 1919.0, 1267.0], [1884.0, 2796.0, 2949.0, 1926.0], [2316.0, 3408.0, 3561.0, 2310.0], [1427.0, 2075.0, 2159.0, 1383.0]], [[2713.0, 4177.0, 4513.0, 3069.0], [4586.0, 7010.0, 7487.0, 5060.0], [5882.0, 8918.0, 9395.0, 6308.0], [4093.0, 6181.0, 6481.0, 4337.0]]], [[[3887.0, 5639.0, 5759.0, 3699.0], [5340.0, 7692.0, 7845.0, 4998.0], [5772.0, 8304.0, 8457.0, 5382.0], [3347.0, 4763.0, 4847.0, 3047.0]], [[10009.0, 14929.0, 15265.0, 10109.0], [14954.0, 22274.0, 22751.0, 15044.0], [16250.0, 24182.0, 24659.0, 16292.0], [10621.0, 15781.0, 16081.0, 10609.0]]]] + case: { x: xm::DFloat.new(2, 2, 4, 4).seq, w: xm::DFloat.new(2, 2, 3, 3).seq, options: { b: xm::DFloat[3, 5], pad: 1 } }, + expected: xm::DFloat[[[[1199.0, 1799.0, 1919.0, 1267.0], [1884.0, 2796.0, 2949.0, 1926.0], [2316.0, 3408.0, 3561.0, 2310.0], [1427.0, 2075.0, 2159.0, 1383.0]], [[2713.0, 4177.0, 4513.0, 3069.0], [4586.0, 7010.0, 7487.0, 5060.0], [5882.0, 8918.0, 9395.0, 6308.0], [4093.0, 6181.0, 6481.0, 4337.0]]], [[[3887.0, 5639.0, 5759.0, 3699.0], [5340.0, 7692.0, 7845.0, 4998.0], [5772.0, 8304.0, 8457.0, 5382.0], [3347.0, 4763.0, 4847.0, 3047.0]], [[10009.0, 14929.0, 15265.0, 10109.0], [14954.0, 22274.0, 22751.0, 15044.0], [16250.0, 24182.0, 24659.0, 16292.0], [10621.0, 15781.0, 16081.0, 10609.0]]]] }, }) @@ -37,28 +37,28 @@ def test_convolution_2d(data) data({ test1: { case: { - x: Numo::DFloat.new(2, 1, 4, 3).seq, - w: Numo::DFloat.new(2, 1, 3, 3).seq, + x: xm::DFloat.new(2, 1, 4, 3).seq, + w: xm::DFloat.new(2, 1, 3, 3).seq, b: nil, - gy: Numo::DFloat.new(2, 2, 3, 2).seq + gy: xm::DFloat.new(2, 2, 3, 2).seq }, expected: { - gx: Numo::DFloat[[[[78.0, 171.0, 95.0], [178.0, 386.0, 212.0], [112.0, 239.0, 129.0], [246.0, 522.0, 280.0]]], [[[282.0, 579.0, 299.0], [586.0, 1202.0, 620.0], [316.0, 647.0, 333.0], [654.0, 1338.0, 688.0]]]], - gw: Numo::DFloat[[[[676.0, 1304.0, 624.0], [476.0, 916.0, 436.0], [572.0, 1096.0, 520.0]]], [[[988.0, 1928.0, 936.0], [716.0, 1396.0, 676.0], [884.0, 1720.0, 832.0]]]], + gx: xm::DFloat[[[[78.0, 171.0, 95.0], [178.0, 386.0, 212.0], [112.0, 239.0, 129.0], [246.0, 522.0, 280.0]]], [[[282.0, 579.0, 299.0], [586.0, 1202.0, 620.0], [316.0, 647.0, 333.0], [654.0, 1338.0, 688.0]]]], + gw: xm::DFloat[[[[676.0, 1304.0, 624.0], [476.0, 916.0, 436.0], [572.0, 1096.0, 520.0]]], [[[988.0, 1928.0, 936.0], [716.0, 1396.0, 676.0], [884.0, 1720.0, 832.0]]]], gb: nil } }, test2: { case: { - x: Numo::DFloat.new(2, 1, 4, 3).seq, - w: Numo::DFloat.new(2, 1, 3, 3).seq, - b: Numo::DFloat.new(2).seq, - gy: Numo::DFloat.new(2, 2, 3, 2).seq + x: xm::DFloat.new(2, 1, 4, 3).seq, + w: xm::DFloat.new(2, 1, 3, 3).seq, + b: xm::DFloat.new(2).seq, + gy: xm::DFloat.new(2, 2, 3, 2).seq }, expected: { - gx: Numo::DFloat[[[[78.0, 171.0, 95.0], [178.0, 386.0, 212.0], [112.0, 239.0, 129.0], [246.0, 522.0, 280.0]]], [[[282.0, 579.0, 299.0], [586.0, 1202.0, 620.0], [316.0, 647.0, 333.0], [654.0, 1338.0, 688.0]]]], - gw: Numo::DFloat[[[[676.0, 1304.0, 624.0], [476.0, 916.0, 436.0], [572.0, 1096.0, 520.0]]], [[[988.0, 1928.0, 936.0], [716.0, 1396.0, 676.0], [884.0, 1720.0, 832.0]]]], - gb: Numo::DFloat[102.0, 174.0] + gx: xm::DFloat[[[[78.0, 171.0, 95.0], [178.0, 386.0, 212.0], [112.0, 239.0, 129.0], [246.0, 522.0, 280.0]]], [[[282.0, 579.0, 299.0], [586.0, 1202.0, 620.0], [316.0, 647.0, 333.0], [654.0, 1338.0, 688.0]]]], + gw: xm::DFloat[[[[676.0, 1304.0, 624.0], [476.0, 916.0, 436.0], [572.0, 1096.0, 520.0]]], [[[988.0, 1928.0, 936.0], [716.0, 1396.0, 676.0], [884.0, 1720.0, 832.0]]]], + gb: xm::DFloat[102.0, 174.0] } } }) diff --git a/test/functions/loss/mean_squared_error_test.rb b/test/functions/loss/mean_squared_error_test.rb index c4e3279..d1cb9a7 100644 --- a/test/functions/loss/mean_squared_error_test.rb +++ b/test/functions/loss/mean_squared_error_test.rb @@ -5,8 +5,8 @@ class Chainer::Functions::Loss::MeanSquaredErrorTest < Test::Unit::TestCase def _setup() - @x0 = Numo::SFloat.new([4, 3]).rand(2) - 1 - @x1 = Numo::SFloat.new([4, 3]).rand(2) - 1 + @x0 = xm::SFloat.new([4, 3]).rand(2) - 1 + @x1 = xm::SFloat.new([4, 3]).rand(2) - 1 end def check_forward(x0_data, x1_data) @@ -14,7 +14,7 @@ def check_forward(x0_data, x1_data) x1 = Chainer::Variable.new(x1_data) loss = Chainer::Functions::Loss::MeanSquaredError.mean_squared_error(x0, x1) loss_value = loss.data - assert_equal(Numo::SFloat, loss_value.class) + assert_equal(xm::SFloat, loss_value.class) assert_equal([], loss_value.shape) loss_expect = 0.0 @x0.each_with_index{|x,*i| loss_expect += (@x0[*i] - @x1[*i]) ** 2} diff --git a/test/functions/loss/softmax_cross_entropy_test.rb b/test/functions/loss/softmax_cross_entropy_test.rb index 0325a5f..5558043 100644 --- a/test/functions/loss/softmax_cross_entropy_test.rb +++ b/test/functions/loss/softmax_cross_entropy_test.rb @@ -7,7 +7,7 @@ class TestSoftmaxCrossEntropy < Test::Unit::TestCase cache_score = [true, false] normalize = [true, false] ignore_index = [nil, false, [0], [0, 1], [0, 1, 0]] - dtype = [Numo::SFloat] + dtype = [xm::SFloat] weight_apply = [false, true] value1 = shape.product(cache_score, normalize, ignore_index, dtype, weight_apply).collect {|v| @@ -17,7 +17,7 @@ class TestSoftmaxCrossEntropy < Test::Unit::TestCase cache_score = [false] normalize = [true] ignore_index = [[0, 1]] - dtype = [Numo::SFloat, Numo::DFloat] + dtype = [xm::SFloat, xm::DFloat] weight_apply = [false, true] value2 = shape.product(cache_score, normalize, ignore_index, dtype, weight_apply).collect {|v| @@ -35,18 +35,18 @@ def _setup(data) if @shape.nil? @x = @dtype.cast([[-1000, 1]]) - @t = Numo::Int32[0] + @t = xm::Int32[0] else @x = @dtype.new(@shape).rand(2) - 1 out_shape = [@shape[0]] + @shape[2..-1] - @t = Numo::Int32.new(out_shape).rand(@shape[1]) + @t = xm::Int32.new(out_shape).rand(@shape[1]) if @ignore_index && @ignore_index.size <= @t.ndim @t[@ignore_index] = -1 end end @check_forward_options = {} - @check_backward_options_dtype = Numo::DFloat + @check_backward_options_dtype = xm::DFloat if @weight_apply @class_weight = @dtype.new([@x.shape[1]]).rand(10) @@ -113,13 +113,13 @@ def test_backward(data) class TestClassWeightAssertion < Test::Unit::TestCase def _setup() - @x = Numo::NArray[[0, 1], [2, 3]] - @t = Numo::NArray[0, 1] + @x = xm::NArray[[0, 1], [2, 3]] + @t = xm::NArray[0, 1] end def test_ndim_assertion() _setup() - wrong_ndim_class_weight = Numo::NArray.cast([[0, 0]]) + wrong_ndim_class_weight = xm::NArray.cast([[0, 0]]) assert_raise(ArgumentError) { Chainer::Functions::Loss::SoftmaxCrossEntropy.softmax_cross_entropy(@x, @t, class_weight: wrong_ndim_class_weight) } @@ -127,7 +127,7 @@ def test_ndim_assertion() def test_dtype_assertion() _setup() - wrong_dtype_class_weight = Numo::Int32.cast([0, 0]) + wrong_dtype_class_weight = xm::Int32.cast([0, 0]) assert_raise(ArgumentError) { Chainer::Functions::Loss::SoftmaxCrossEntropy.softmax_cross_entropy(@x, @t, class_weight: wrong_dtype_class_weight) } @@ -135,7 +135,7 @@ def test_dtype_assertion() def test_variable_assertion() _setup() - wrong_inst_class_weight = Chainer::Variable.new(Numo::NArray.cast([0, 0])) + wrong_inst_class_weight = Chainer::Variable.new(xm::NArray.cast([0, 0])) assert_raise(ArgumentError) { Chainer::Functions::Loss::SoftmaxCrossEntropy.softmax_cross_entropy(@x, @t, class_weight: wrong_inst_class_weight) } @@ -147,7 +147,7 @@ class TestElementwiseSoftmaxCrossEntropy < Test::Unit::TestCase cache_score = [true, false] normalize = [true, false] ignore_index = [nil, false, [0], [0, 1], [0, 1, 0]] - dtype = [Numo::SFloat] + dtype = [xm::SFloat] weight_apply = [false, true] value1 = shape.product(cache_score, normalize, ignore_index, dtype, weight_apply).collect {|v| @@ -157,7 +157,7 @@ class TestElementwiseSoftmaxCrossEntropy < Test::Unit::TestCase cache_score = [false] normalize = [true] ignore_index = [[0, 1]] - dtype = [Numo::SFloat, Numo::DFloat] + dtype = [xm::SFloat, xm::DFloat] weight_apply = [false, true] value2 = shape.product(cache_score, normalize, ignore_index, dtype, weight_apply).collect {|v| @@ -175,11 +175,11 @@ def _setup(data) if @shape.nil? @x = @dtype[[-1000, 1]] - @t = Numo::Int32.cast([0]) + @t = xm::Int32.cast([0]) else @x = @dtype.new(@shape).rand(2) - 1 out_shape = [@shape[0]] + (@shape[2..-1]) - @t = Numo::Int32.new(out_shape).rand(@shape[1]) + @t = xm::Int32.new(out_shape).rand(@shape[1]) if @ignore_index && @ignore_index.size <= @t.ndim @t[@ignore_index] = -1 end @@ -187,7 +187,7 @@ def _setup(data) @g = @dtype.new(@t.shape).rand(2) - 1 @check_forward_options = {} - @check_backward_options_dtype = Numo::DFloat + @check_backward_options_dtype = xm::DFloat if @weight_apply @class_weight = @dtype.new([@x.shape[1]]).rand(10) @@ -258,8 +258,8 @@ def _setup(data) @normalize = data[:normalize] @cache_score = data[:cache_score] - @x = Numo::SFloat.new([2, 3]).rand(2) - 1 - @t = Numo::Int32.zeros([2]) + @x = xm::SFloat.new([2, 3]).rand(2) - 1 + @t = xm::Int32.zeros([2]) end def check_invalid_reduce(x, t) @@ -277,7 +277,7 @@ def test_invalid_reduce(data) class TestNonDefaultIgnoreLabel < Test::Unit::TestCase reduce = ['mean', 'no'] - class_weight = [nil, Numo::SFloat.ones([3])] + class_weight = [nil, xm::SFloat.ones([3])] data = reduce.product(class_weight).collect {|v| {reduce: v[0], class_weight: v[1]}} @@ -288,14 +288,14 @@ def _setup(data) @class_weight = data[:class_weight] @ignore_label = -2 - @x = Numo::SFloat.new([2, 3]).rand(2) - 1 - @t = Numo::Int32.new([2]).fill(@ignore_label) + @x = xm::SFloat.new([2, 3]).rand(2) - 1 + @t = xm::Int32.new([2]).fill(@ignore_label) if @reduce == "mean" gy_shape = [] else gy_shape = [2] end - @gy = Numo::SFloat.new(gy_shape).rand(2) - 1 + @gy = xm::SFloat.new(gy_shape).rand(2) - 1 end def check_forward(xp) @@ -311,7 +311,7 @@ def check_forward(xp) if @reduce == "mean" expect = 0.0 else - expect = Numo::SFloat.zeros([2]) + expect = xm::SFloat.zeros([2]) end Chainer::Testing.assert_allclose(expect, loss.data) end @@ -319,7 +319,7 @@ def check_forward(xp) data(data) def test_forward(data) _setup(data) - check_forward(Numo::NArray) + check_forward(xm::NArray) end def check_backward(xp) @@ -338,6 +338,6 @@ def check_backward(xp) data(data) def test_backward(data) _setup(data) - check_backward(Numo::NArray) + check_backward(xm::NArray) end end diff --git a/test/functions/math/basic_math_test.rb b/test/functions/math/basic_math_test.rb index 1729ef6..996b661 100644 --- a/test/functions/math/basic_math_test.rb +++ b/test/functions/math/basic_math_test.rb @@ -3,7 +3,7 @@ class Chainer::Functions::Math::BasicMathTest < Test::Unit::TestCase test("Neg#forward") do - x = Chainer::Variable.new(Numo::DFloat[[-1, 0],[1, 2]]) - assert_equal(Numo::DFloat[[1,0],[-1,-2]], (-x).data) + x = Chainer::Variable.new(xm::DFloat[[-1, 0],[1, 2]]) + assert_equal(xm::DFloat[[1,0],[-1,-2]], (-x).data) end end diff --git a/test/functions/normalization/batch_normalization_test.rb b/test/functions/normalization/batch_normalization_test.rb index 18d64aa..78fc3a5 100644 --- a/test/functions/normalization/batch_normalization_test.rb +++ b/test/functions/normalization/batch_normalization_test.rb @@ -2,11 +2,11 @@ class Chainer::Functions::Normalization::BatchNormalizationFunctionTest < Test:: data({ test1: { case: { - x: Numo::DFloat.new(5, 3, 4, 2, 1).seq, - gamma: Numo::DFloat.new(3, 4).seq, - beta: Numo::DFloat.new(3, 4).seq + x: xm::DFloat.new(5, 3, 4, 2, 1).seq, + gamma: xm::DFloat.new(3, 4).seq, + beta: xm::DFloat.new(3, 4).seq }, - expected: Numo::DFloat[[[[[0.0], [0.0]], [[-0.35546910762786865], [-0.3275212049484253]], [[-0.7109382152557373], [-0.6550424098968506]], [[-1.0664072036743164], [-0.9825634956359863]]], [[[-1.4218764305114746], [-1.3100848197937012]], [[-1.7773456573486328], [-1.637606143951416]], [[-2.132814407348633], [-1.9651269912719727]], [[-2.488284111022949], [-2.2926483154296875]]], [[[-2.843752861022949], [-2.6201696395874023]], [[-3.199221611022949], [-2.947690963745117]], [[-3.5546913146972656], [-3.275212287902832]], [[-3.9101600646972656], [-3.602733612060547]]]], [[[[0.0], [0.0]], [[0.3152785301208496], [0.3432263135910034]], [[0.6305570602416992], [0.6864526271820068]], [[0.9458355903625488], [1.0296789407730103]]], [[[1.2611141204833984], [1.3729052543640137]], [[1.576392650604248], [1.7161316871643066]], [[1.8916711807250977], [2.0593578815460205]], [[2.2069497108459473], [2.4025840759277344]]], [[[2.522228240966797], [2.7458105087280273]], [[2.8375067710876465], [3.0890369415283203]], [[3.152785301208496], [3.4322633743286133]], [[3.4680638313293457], [3.775489330291748]]]], [[[[0.0], [0.0]], [[0.9860261082649231], [1.0139739513397217]], [[1.9720522165298462], [2.0279479026794434]], [[2.958078384399414], [3.041921615600586]]], [[[3.9441044330596924], [4.055895805358887]], [[4.930130481719971], [5.069869518280029]], [[5.916156768798828], [6.083843231201172]], [[6.902182579040527], [7.097817420959473]]], [[[7.888208866119385], [8.111791610717773]], [[8.874235153198242], [9.125764846801758]], [[9.860260963439941], [10.139739036560059]], [[10.84628677368164], [11.15371322631836]]]], [[[[0.0], [0.0]], [[1.6567736864089966], [1.6847214698791504]], [[3.313547372817993], [3.369442939758301]], [[4.970321178436279], [5.054164409637451]]], [[[6.627094745635986], [6.738885879516602]], [[8.283868789672852], [8.423606872558594]], [[9.940642356872559], [10.108328819274902]], [[11.597415924072266], [11.793050765991211]]], [[[13.254189491271973], [13.477771759033203]], [[14.91096305847168], [15.162492752075195]], [[16.567737579345703], [16.847213745117188]], [[18.224510192871094], [18.531936645507812]]]], [[[[0.0], [0.0]], [[2.327521324157715], [2.355469226837158]], [[4.65504264831543], [4.710938453674316]], [[6.982563495635986], [7.066407203674316]]], [[[9.31008529663086], [9.421876907348633]], [[11.637605667114258], [11.777345657348633]], [[13.965126991271973], [14.132814407348633]], [[16.292648315429688], [16.488285064697266]]], [[[18.62017059326172], [18.843753814697266]], [[20.947690963745117], [21.199222564697266]], [[23.275211334228516], [23.554691314697266]], [[25.602733612060547], [25.910160064697266]]]]] + expected: xm::DFloat[[[[[0.0], [0.0]], [[-0.35546910762786865], [-0.3275212049484253]], [[-0.7109382152557373], [-0.6550424098968506]], [[-1.0664072036743164], [-0.9825634956359863]]], [[[-1.4218764305114746], [-1.3100848197937012]], [[-1.7773456573486328], [-1.637606143951416]], [[-2.132814407348633], [-1.9651269912719727]], [[-2.488284111022949], [-2.2926483154296875]]], [[[-2.843752861022949], [-2.6201696395874023]], [[-3.199221611022949], [-2.947690963745117]], [[-3.5546913146972656], [-3.275212287902832]], [[-3.9101600646972656], [-3.602733612060547]]]], [[[[0.0], [0.0]], [[0.3152785301208496], [0.3432263135910034]], [[0.6305570602416992], [0.6864526271820068]], [[0.9458355903625488], [1.0296789407730103]]], [[[1.2611141204833984], [1.3729052543640137]], [[1.576392650604248], [1.7161316871643066]], [[1.8916711807250977], [2.0593578815460205]], [[2.2069497108459473], [2.4025840759277344]]], [[[2.522228240966797], [2.7458105087280273]], [[2.8375067710876465], [3.0890369415283203]], [[3.152785301208496], [3.4322633743286133]], [[3.4680638313293457], [3.775489330291748]]]], [[[[0.0], [0.0]], [[0.9860261082649231], [1.0139739513397217]], [[1.9720522165298462], [2.0279479026794434]], [[2.958078384399414], [3.041921615600586]]], [[[3.9441044330596924], [4.055895805358887]], [[4.930130481719971], [5.069869518280029]], [[5.916156768798828], [6.083843231201172]], [[6.902182579040527], [7.097817420959473]]], [[[7.888208866119385], [8.111791610717773]], [[8.874235153198242], [9.125764846801758]], [[9.860260963439941], [10.139739036560059]], [[10.84628677368164], [11.15371322631836]]]], [[[[0.0], [0.0]], [[1.6567736864089966], [1.6847214698791504]], [[3.313547372817993], [3.369442939758301]], [[4.970321178436279], [5.054164409637451]]], [[[6.627094745635986], [6.738885879516602]], [[8.283868789672852], [8.423606872558594]], [[9.940642356872559], [10.108328819274902]], [[11.597415924072266], [11.793050765991211]]], [[[13.254189491271973], [13.477771759033203]], [[14.91096305847168], [15.162492752075195]], [[16.567737579345703], [16.847213745117188]], [[18.224510192871094], [18.531936645507812]]]], [[[[0.0], [0.0]], [[2.327521324157715], [2.355469226837158]], [[4.65504264831543], [4.710938453674316]], [[6.982563495635986], [7.066407203674316]]], [[[9.31008529663086], [9.421876907348633]], [[11.637605667114258], [11.777345657348633]], [[13.965126991271973], [14.132814407348633]], [[16.292648315429688], [16.488285064697266]]], [[[18.62017059326172], [18.843753814697266]], [[20.947690963745117], [21.199222564697266]], [[23.275211334228516], [23.554691314697266]], [[25.602733612060547], [25.910160064697266]]]]] } }) @@ -23,15 +23,15 @@ def test_forward(data) data({ test1: { case: { - x: Numo::DFloat.new(5, 3, 4, 2, 1).seq, - gamma: Numo::DFloat.new(3, 4).seq, - beta: Numo::DFloat.new(3, 4).seq, - gy: Numo::DFloat.new(5, 3, 4, 2, 1).seq, + x: xm::DFloat.new(5, 3, 4, 2, 1).seq, + gamma: xm::DFloat.new(3, 4).seq, + beta: xm::DFloat.new(3, 4).seq, + gy: xm::DFloat.new(5, 3, 4, 2, 1).seq, }, expected: { - gx: Numo::DFloat[[[[[-0.0], [-0.0]], [[-0.1355469971895218], [-0.13275229930877686]], [[-0.2710941433906555], [-0.26550477743148804]], [[-0.4066409766674042], [-0.3982568681240082]]], [[[-0.5421879887580872], [-0.5310091376304626]], [[-0.6777349710464478], [-0.6637614369392395]], [[-0.8132824897766113], [-0.7965143322944641]], [[-0.9488295316696167], [-0.9292667508125305]]], [[[-1.0843744277954102], [-1.0620169639587402]], [[-1.2199227809906006], [-1.1947705745697021]], [[-1.3554707765579224], [-1.327523946762085]], [[-1.4910167455673218], [-1.4602751731872559]]]], [[[[-0.0], [-0.0]], [[-0.06847222149372101], [-0.06567737460136414]], [[-0.13694465160369873], [-0.13135485351085663]], [[-0.20541682839393616], [-0.19703197479248047]]], [[[-0.27388909459114075], [-0.26270928978919983]], [[-0.3423613905906677], [-0.3283866345882416]], [[-0.4108336567878723], [-0.3940645754337311]], [[-0.4793059229850769], [-0.45974200963974]]], [[[-0.5477773547172546], [-0.5254185795783997]], [[-0.6162505149841309], [-0.5910959243774414]], [[-0.6847227811813354], [-0.6567743420600891]], [[-0.75319504737854], [-0.7224505543708801]]]], [[[[-0.0], [0.0]], [[-0.0013973694294691086], [0.0013973694294691086]], [[-0.002794738858938217], [0.002794738858938217]], [[-0.004192108288407326], [0.004192108288407326]]], [[[-0.005589477717876434], [0.005589477717876434]], [[-0.00698684761300683], [0.00698684761300683]], [[-0.008384216576814651], [0.008384216576814651]], [[-0.00978158600628376], [0.00978158600628376]]], [[[-0.011179808527231216], [0.011179808527231216]], [[-0.012577285058796406], [0.012577285058796406]], [[-0.01397476065903902], [0.01397476065903902]], [[-0.01537223719060421], [0.01537223719060421]]]], [[[[0.0], [0.0]], [[0.06567732244729996], [0.06847206503152847]], [[0.13135464489459991], [0.13694454729557037]], [[0.19703197479248047], [0.20541618764400482]]], [[[0.26270928978919983], [0.2738882601261139]], [[0.3283866345882416], [0.34236031770706177]], [[0.39406394958496094], [0.4108336567878723]], [[0.4597412645816803], [0.4793059229850769]]], [[[0.5254185795783997], [0.5477765202522278]], [[0.5910959243774414], [0.616248607635498]], [[0.6567732691764832], [0.6847227811813354]], [[0.7224505543708801], [0.7531927227973938]]]], [[[[0.0], [0.0]], [[0.13275232911109924], [0.13554686307907104]], [[0.2655051052570343], [0.2710941433906555]], [[0.3982570171356201], [0.40664058923721313]]], [[[0.531009316444397], [0.5421874523162842]], [[0.6637616753578186], [0.6777343153953552]], [[0.7965152859687805], [0.8132811784744263]], [[0.9292678236961365], [0.9488280415534973]]], [[[1.0620169639587402], [1.0843749046325684]], [[1.1947691440582275], [1.2199218273162842]], [[1.3275254964828491], [1.3554686307907104]], [[1.4602733850479126], [1.4910155534744263]]]]], - ggama: Numo::DFloat[[322.0287170410156, 322.0287170410156, 322.0286865234375, 322.0287170410156], [322.0287170410156, 322.0287170410156, 322.0286865234375, 322.0286865234375], [322.02874755859375, 322.0287170410156, 322.0286865234375, 322.0287170410156]], - gbeta: Numo::DFloat[[485.0, 505.0, 525.0, 545.0], [565.0, 585.0, 605.0, 625.0], [645.0, 665.0, 685.0, 705.0]], + gx: xm::DFloat[[[[[-0.0], [-0.0]], [[-0.1355469971895218], [-0.13275229930877686]], [[-0.2710941433906555], [-0.26550477743148804]], [[-0.4066409766674042], [-0.3982568681240082]]], [[[-0.5421879887580872], [-0.5310091376304626]], [[-0.6777349710464478], [-0.6637614369392395]], [[-0.8132824897766113], [-0.7965143322944641]], [[-0.9488295316696167], [-0.9292667508125305]]], [[[-1.0843744277954102], [-1.0620169639587402]], [[-1.2199227809906006], [-1.1947705745697021]], [[-1.3554707765579224], [-1.327523946762085]], [[-1.4910167455673218], [-1.4602751731872559]]]], [[[[-0.0], [-0.0]], [[-0.06847222149372101], [-0.06567737460136414]], [[-0.13694465160369873], [-0.13135485351085663]], [[-0.20541682839393616], [-0.19703197479248047]]], [[[-0.27388909459114075], [-0.26270928978919983]], [[-0.3423613905906677], [-0.3283866345882416]], [[-0.4108336567878723], [-0.3940645754337311]], [[-0.4793059229850769], [-0.45974200963974]]], [[[-0.5477773547172546], [-0.5254185795783997]], [[-0.6162505149841309], [-0.5910959243774414]], [[-0.6847227811813354], [-0.6567743420600891]], [[-0.75319504737854], [-0.7224505543708801]]]], [[[[-0.0], [0.0]], [[-0.0013973694294691086], [0.0013973694294691086]], [[-0.002794738858938217], [0.002794738858938217]], [[-0.004192108288407326], [0.004192108288407326]]], [[[-0.005589477717876434], [0.005589477717876434]], [[-0.00698684761300683], [0.00698684761300683]], [[-0.008384216576814651], [0.008384216576814651]], [[-0.00978158600628376], [0.00978158600628376]]], [[[-0.011179808527231216], [0.011179808527231216]], [[-0.012577285058796406], [0.012577285058796406]], [[-0.01397476065903902], [0.01397476065903902]], [[-0.01537223719060421], [0.01537223719060421]]]], [[[[0.0], [0.0]], [[0.06567732244729996], [0.06847206503152847]], [[0.13135464489459991], [0.13694454729557037]], [[0.19703197479248047], [0.20541618764400482]]], [[[0.26270928978919983], [0.2738882601261139]], [[0.3283866345882416], [0.34236031770706177]], [[0.39406394958496094], [0.4108336567878723]], [[0.4597412645816803], [0.4793059229850769]]], [[[0.5254185795783997], [0.5477765202522278]], [[0.5910959243774414], [0.616248607635498]], [[0.6567732691764832], [0.6847227811813354]], [[0.7224505543708801], [0.7531927227973938]]]], [[[[0.0], [0.0]], [[0.13275232911109924], [0.13554686307907104]], [[0.2655051052570343], [0.2710941433906555]], [[0.3982570171356201], [0.40664058923721313]]], [[[0.531009316444397], [0.5421874523162842]], [[0.6637616753578186], [0.6777343153953552]], [[0.7965152859687805], [0.8132811784744263]], [[0.9292678236961365], [0.9488280415534973]]], [[[1.0620169639587402], [1.0843749046325684]], [[1.1947691440582275], [1.2199218273162842]], [[1.3275254964828491], [1.3554686307907104]], [[1.4602733850479126], [1.4910155534744263]]]]], + ggama: xm::DFloat[[322.0287170410156, 322.0287170410156, 322.0286865234375, 322.0287170410156], [322.0287170410156, 322.0287170410156, 322.0286865234375, 322.0286865234375], [322.02874755859375, 322.0287170410156, 322.0286865234375, 322.0287170410156]], + gbeta: xm::DFloat[[485.0, 505.0, 525.0, 545.0], [565.0, 585.0, 605.0, 625.0], [645.0, 665.0, 685.0, 705.0]], gmean: nil, gvar: nil } diff --git a/test/functions/pooling/average_pooling_2d_test.rb b/test/functions/pooling/average_pooling_2d_test.rb index b9d6148..a4cc9bd 100644 --- a/test/functions/pooling/average_pooling_2d_test.rb +++ b/test/functions/pooling/average_pooling_2d_test.rb @@ -2,11 +2,11 @@ class Chainer::Functions::Pooling::AveragePooling2DTest < Test::Unit::TestCase data( test1: { case: { - x: Numo::SFloat.new(1, 3, 4, 6).seq, + x: xm::SFloat.new(1, 3, 4, 6).seq, ksize: 2, options: {} }, - expected: Numo::SFloat[[[[ 3.5, 5.5, 7.5], + expected: xm::SFloat[[[[ 3.5, 5.5, 7.5], [15.5, 17.5, 19.5]], [[27.5, 29.5, 31.5], [39.5, 41.5, 43.5]], @@ -15,11 +15,11 @@ class Chainer::Functions::Pooling::AveragePooling2DTest < Test::Unit::TestCase }, test2: { case: { - x: Numo::SFloat.new(1, 3, 4, 4).seq, + x: xm::SFloat.new(1, 3, 4, 4).seq, ksize: 2, options: { stride: 2 } }, - expected: Numo::SFloat[[[[ 2.5, 4.5], + expected: xm::SFloat[[[[ 2.5, 4.5], [10.5, 12.5]], [[18.5, 20.5], [26.5, 28.5]], @@ -28,11 +28,11 @@ class Chainer::Functions::Pooling::AveragePooling2DTest < Test::Unit::TestCase }, test3: { case: { - x: Numo::SFloat.new(1, 3, 4, 4).seq, + x: xm::SFloat.new(1, 3, 4, 4).seq, ksize: 4, options: { stride: 2, pad: 1 } }, - expected: Numo::SFloat[[[[ 2.8125, 3.375 ], + expected: xm::SFloat[[[[ 2.8125, 3.375 ], [ 5.0625, 5.625 ]], [[11.8125, 12.375 ], [14.0625, 14.625 ]], @@ -49,14 +49,14 @@ def test_average_pooling_2d(data) data({ test1: { case: { - x: Numo::SFloat.new(2, 3, 2, 2).seq, - gy: [Numo::SFloat.new(2, 3, 1, 1).seq], + x: xm::SFloat.new(2, 3, 2, 2).seq, + gy: [xm::SFloat.new(2, 3, 1, 1).seq], ksize: 2, stride: 2, pad: 0, cover_all: false }, - expected: Numo::SFloat[[[[0.0 , 0.0 ], + expected: xm::SFloat[[[[0.0 , 0.0 ], [0.0 , 0.0 ]], [[0.25, 0.25], [0.25, 0.25]], @@ -71,14 +71,14 @@ def test_average_pooling_2d(data) }, test2: { case: { - x: Numo::SFloat.new(2, 2, 4, 4).seq, - gy: [Numo::SFloat.new(2, 3 ,1, 1).seq], + x: xm::SFloat.new(2, 2, 4, 4).seq, + gy: [xm::SFloat.new(2, 3 ,1, 1).seq], ksize: 6, stride: 8, pad: 1, cover_all: false }, - expected: Numo::SFloat[[[[0.0 , 0.0 , 0.0 , 0.0 ], + expected: xm::SFloat[[[[0.0 , 0.0 , 0.0 , 0.0 ], [0.0 , 0.0 , 0.0 , 0.0 ], [0.0 , 0.0 , 0.0 , 0.0 ], [0.0 , 0.0 , 0.0 , 0.0 ]], diff --git a/test/functions/pooling/max_pooling_2d_test.rb b/test/functions/pooling/max_pooling_2d_test.rb index a3e47eb..1d895a2 100644 --- a/test/functions/pooling/max_pooling_2d_test.rb +++ b/test/functions/pooling/max_pooling_2d_test.rb @@ -2,27 +2,27 @@ class Chainer::Functions::Pooling::MaxPooling2DTest < Test::Unit::TestCase data( test1: { case: { - x: Numo::DFloat.new(1, 3, 4, 6).seq, + x: xm::DFloat.new(1, 3, 4, 6).seq, ksize: 2, options: {} }, - expected: Numo::DFloat[[[[7.0, 9.0, 11.0], [19.0, 21.0, 23.0]], [[31.0, 33.0, 35.0], [43.0, 45.0, 47.0]], [[55.0, 57.0, 59.0], [67.0, 69.0, 71.0]]]] + expected: xm::DFloat[[[[7.0, 9.0, 11.0], [19.0, 21.0, 23.0]], [[31.0, 33.0, 35.0], [43.0, 45.0, 47.0]], [[55.0, 57.0, 59.0], [67.0, 69.0, 71.0]]]] }, test2: { case: { - x: Numo::DFloat.new(1, 3, 4, 4).seq, + x: xm::DFloat.new(1, 3, 4, 4).seq, ksize: 2, options: { stride: 2 } }, - expected: Numo::DFloat[[[[5.0, 7.0], [13.0, 15.0]], [[21.0, 23.0], [29.0, 31.0]], [[37.0, 39.0], [45.0, 47.0]]]] + expected: xm::DFloat[[[[5.0, 7.0], [13.0, 15.0]], [[21.0, 23.0], [29.0, 31.0]], [[37.0, 39.0], [45.0, 47.0]]]] }, test3: { case: { - x: Numo::DFloat.new(1, 3, 4, 4).seq, + x: xm::DFloat.new(1, 3, 4, 4).seq, ksize: 4, options: {} }, - expected: Numo::DFloat[[[[15.0]], [[31.0]], [[47.0]]]] + expected: xm::DFloat[[[[15.0]], [[31.0]], [[47.0]]]] }, ) def test_max_pooling_2d(data) @@ -34,36 +34,36 @@ def test_max_pooling_2d(data) data({ test1: { case: { - x: Numo::DFloat.new(2, 3, 6, 3).seq, - gy: [Numo::DFloat.new(2, 3, 4, 2).seq], + x: xm::DFloat.new(2, 3, 6, 3).seq, + gy: [xm::DFloat.new(2, 3, 4, 2).seq], ksize: 3, stride: 2, pad: 1, cover_all: true }, - expected: Numo::DFloat[[[[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 0.0], [0.0, 2.0, 3.0], [0.0, 0.0, 0.0], [0.0, 10.0, 12.0]], [[0.0, 0.0, 0.0], [0.0, 8.0, 9.0], [0.0, 0.0, 0.0], [0.0, 10.0, 11.0], [0.0, 0.0, 0.0], [0.0, 26.0, 28.0]], [[0.0, 0.0, 0.0], [0.0, 16.0, 17.0], [0.0, 0.0, 0.0], [0.0, 18.0, 19.0], [0.0, 0.0, 0.0], [0.0, 42.0, 44.0]]], [[[0.0, 0.0, 0.0], [0.0, 24.0, 25.0], [0.0, 0.0, 0.0], [0.0, 26.0, 27.0], [0.0, 0.0, 0.0], [0.0, 58.0, 60.0]], [[0.0, 0.0, 0.0], [0.0, 32.0, 33.0], [0.0, 0.0, 0.0], [0.0, 34.0, 35.0], [0.0, 0.0, 0.0], [0.0, 74.0, 76.0]], [[0.0, 0.0, 0.0], [0.0, 40.0, 41.0], [0.0, 0.0, 0.0], [0.0, 42.0, 43.0], [0.0, 0.0, 0.0], [0.0, 90.0, 92.0]]]] + expected: xm::DFloat[[[[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 0.0], [0.0, 2.0, 3.0], [0.0, 0.0, 0.0], [0.0, 10.0, 12.0]], [[0.0, 0.0, 0.0], [0.0, 8.0, 9.0], [0.0, 0.0, 0.0], [0.0, 10.0, 11.0], [0.0, 0.0, 0.0], [0.0, 26.0, 28.0]], [[0.0, 0.0, 0.0], [0.0, 16.0, 17.0], [0.0, 0.0, 0.0], [0.0, 18.0, 19.0], [0.0, 0.0, 0.0], [0.0, 42.0, 44.0]]], [[[0.0, 0.0, 0.0], [0.0, 24.0, 25.0], [0.0, 0.0, 0.0], [0.0, 26.0, 27.0], [0.0, 0.0, 0.0], [0.0, 58.0, 60.0]], [[0.0, 0.0, 0.0], [0.0, 32.0, 33.0], [0.0, 0.0, 0.0], [0.0, 34.0, 35.0], [0.0, 0.0, 0.0], [0.0, 74.0, 76.0]], [[0.0, 0.0, 0.0], [0.0, 40.0, 41.0], [0.0, 0.0, 0.0], [0.0, 42.0, 43.0], [0.0, 0.0, 0.0], [0.0, 90.0, 92.0]]]] }, test2: { case: { - x: Numo::DFloat.new(2, 3, 4, 3).seq, - gy: [Numo::DFloat.new(2, 3, 3, 2).seq], + x: xm::DFloat.new(2, 3, 4, 3).seq, + gy: [xm::DFloat.new(2, 3, 3, 2).seq], ksize: 3, stride: 2, pad: 1, cover_all: true }, - expected: Numo::DFloat[[[[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 0.0], [0.0, 6.0, 8.0]], [[0.0, 0.0, 0.0], [0.0, 6.0, 7.0], [0.0, 0.0, 0.0], [0.0, 18.0, 20.0]], [[0.0, 0.0, 0.0], [0.0, 12.0, 13.0], [0.0, 0.0, 0.0], [0.0, 30.0, 32.0]]], [[[0.0, 0.0, 0.0], [0.0, 18.0, 19.0], [0.0, 0.0, 0.0], [0.0, 42.0, 44.0]], [[0.0, 0.0, 0.0], [0.0, 24.0, 25.0], [0.0, 0.0, 0.0], [0.0, 54.0, 56.0]], [[0.0, 0.0, 0.0], [0.0, 30.0, 31.0], [0.0, 0.0, 0.0], [0.0, 66.0, 68.0]]]] + expected: xm::DFloat[[[[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 0.0], [0.0, 6.0, 8.0]], [[0.0, 0.0, 0.0], [0.0, 6.0, 7.0], [0.0, 0.0, 0.0], [0.0, 18.0, 20.0]], [[0.0, 0.0, 0.0], [0.0, 12.0, 13.0], [0.0, 0.0, 0.0], [0.0, 30.0, 32.0]]], [[[0.0, 0.0, 0.0], [0.0, 18.0, 19.0], [0.0, 0.0, 0.0], [0.0, 42.0, 44.0]], [[0.0, 0.0, 0.0], [0.0, 24.0, 25.0], [0.0, 0.0, 0.0], [0.0, 54.0, 56.0]], [[0.0, 0.0, 0.0], [0.0, 30.0, 31.0], [0.0, 0.0, 0.0], [0.0, 66.0, 68.0]]]] }, test3: { case: { - x: Numo::DFloat.new(1, 2, 2, 2).seq, - gy: [Numo::DFloat.new(1, 2, 4, 4).seq], + x: xm::DFloat.new(1, 2, 2, 2).seq, + gy: [xm::DFloat.new(1, 2, 4, 4).seq], ksize: 1, stride: 1, pad: 1, cover_all: true }, - expected: Numo::DFloat[[[[5.0, 6.0], [9.0, 10.0]], [[21.0, 22.0], [25.0, 26.0]]]] + expected: xm::DFloat[[[[5.0, 6.0], [9.0, 10.0]], [[21.0, 22.0], [25.0, 26.0]]]] }, }) def test_backward(data) diff --git a/test/gradient_check_test.rb b/test/gradient_check_test.rb index d5ddcc3..aa2ebc7 100644 --- a/test/gradient_check_test.rb +++ b/test/gradient_check_test.rb @@ -1,7 +1,7 @@ require 'chainer/gradient_check' def _uniform(*shape) - return Numo::SFloat.new(shape).rand(2) - 1 + return xm::SFloat.new(shape).rand(2) - 1 end def _dot(x, y) @@ -94,8 +94,8 @@ def test_numerical_grad() class NumericalGradientInvalidType < Test::Unit::TestCase def _setup() - @x = Numo::NArray.cast(0) - @y = Numo::NArray.cast(0) + @x = xm::NArray.cast(0) + @y = xm::NArray.cast(0) @f = lambda{} end @@ -126,8 +126,8 @@ def test_invalid_mixed() class NumericalGradientEpsTest < Test::Unit::TestCase def _setup() - @x = Numo::SFloat.cast(0.0) - @y = Numo::SFloat.cast(1.0) + @x = xm::SFloat.cast(0.0) + @y = xm::SFloat.cast(1.0) end def check_different_eps(x, y) @@ -168,16 +168,16 @@ def backward(inputs, grads) class TestCheckBackward < Test::Unit::TestCase data = { 'test1' => {dtype: nil}, - 'test2' => {dtype: Numo::SFloat}, - 'test3' => {dtype: Numo::DFloat}} + 'test2' => {dtype: xm::SFloat}, + 'test3' => {dtype: xm::DFloat}} data(data) def test_multiple_output(data) @dtype = data[:dtype] - x1 = Numo::DFloat[1] - x2 = Numo::DFloat[1] - g1 = Numo::DFloat[1] - g2 = Numo::DFloat[1] + x1 = xm::DFloat[1] + x2 = xm::DFloat[1] + g1 = xm::DFloat[1] + g2 = xm::DFloat[1] f = lambda do |x, y| s,t = Ident.new().(x, y) u = Ident.new().(t) @@ -187,9 +187,9 @@ def test_multiple_output(data) end def test_no_grads_for_not_float() - x1 = Numo::DFloat.cast([1]) - x2 = Numo::Int32.cast([0, 1]) - g1 = Numo::DFloat.cast([1]) + x1 = xm::DFloat.cast([1]) + x2 = xm::Int32.cast([0, 1]) + g1 = xm::DFloat.cast([1]) f = lambda do |x, y| s = Ident.new().(x) return [s] @@ -198,9 +198,9 @@ def test_no_grads_for_not_float() end def test_no_grads_option() - x1 = Numo::DFloat.cast([1]) - x2 = Numo::DFloat.cast([1]) - g1 = Numo::DFloat.cast([1]) + x1 = xm::DFloat.cast([1]) + x2 = xm::DFloat.cast([1]) + g1 = xm::DFloat.cast([1]) f = lambda do |x, y| s = Ident.new().(x) return [s] diff --git a/test/initializers/uniform_test.rb b/test/initializers/uniform_test.rb index cfcb90f..8499bb9 100644 --- a/test/initializers/uniform_test.rb +++ b/test/initializers/uniform_test.rb @@ -5,7 +5,7 @@ class Chainer::Initializers::UniformTest < Test::Unit::TestCase shapes = [[2, 3], [2, 3, 4]] - dtypes = [ Numo::SFloat, Numo::DFloat ] + dtypes = [ xm::SFloat, xm::DFloat ] data = shapes.map.with_index {|shape, i| dtypes.map do |dtype| diff --git a/test/links/connection/convolution_2d_test.rb b/test/links/connection/convolution_2d_test.rb index 8658b81..dd2f9d3 100644 --- a/test/links/connection/convolution_2d_test.rb +++ b/test/links/connection/convolution_2d_test.rb @@ -3,11 +3,11 @@ class Chainer::Links::Connection::Convolution2DTest < Test::Unit::TestCase data({ test1: { - case: { x: Numo::DFloat.new(1, 3, 10, 10).seq, in_channels: 3, out_channels: 7, ksize: 5, options: {} }, + case: { x: xm::DFloat.new(1, 3, 10, 10).seq, in_channels: 3, out_channels: 7, ksize: 5, options: {} }, expected: [1, 7, 6, 6] }, test2: { - case: { x: Numo::DFloat.new(3, 3, 6, 6).seq, in_channels: nil, out_channels: 4, ksize: 3, options: { stride: 3, pad: 3, initial_w: Numo::DFloat.new(4, 3, 3, 3).seq } }, + case: { x: xm::DFloat.new(3, 3, 6, 6).seq, in_channels: nil, out_channels: 4, ksize: 3, options: { stride: 3, pad: 3, initial_w: xm::DFloat.new(4, 3, 3, 3).seq } }, expected: [3, 4, 4, 4] }, }) diff --git a/test/optimizer_test.rb b/test/optimizer_test.rb index 20debc3..081a344 100644 --- a/test/optimizer_test.rb +++ b/test/optimizer_test.rb @@ -6,18 +6,18 @@ class Chainer::WeightDecayTest < Test::Unit::TestCase data({ test1: { case: { rate: 0.5 }, - expected: Numo::DFloat[4.5, 6.0 , 7.5] + expected: xm::DFloat[4.5, 6.0 , 7.5] }, test2: { case: { rate: 0.3 }, - expected: Numo::DFloat[4.3, 5.6, 6.9] + expected: xm::DFloat[4.3, 5.6, 6.9] } }) def test_weight_decay(data) - var = Chainer::Variable.new(Numo::DFloat[1, 2, 3]) - var.grad = Numo::DFloat[4, 5, 6] + var = Chainer::Variable.new(xm::DFloat[1, 2, 3]) + var.grad = xm::DFloat[4, 5, 6] Chainer::WeightDecay.new(data[:case][:rate]).(nil, var) assert_equal(data[:expected], var.grad) - assert_equal(Numo::DFloat[1, 2, 3], var.data) + assert_equal(xm::DFloat[1, 2, 3], var.data) end end diff --git a/test/optimizers/momentum_sgd_test.rb b/test/optimizers/momentum_sgd_test.rb index 19fb1ab..5e459b3 100644 --- a/test/optimizers/momentum_sgd_test.rb +++ b/test/optimizers/momentum_sgd_test.rb @@ -4,16 +4,16 @@ class Chainer::Optimizers::MomentumSGDTest < Test::Unit::TestCase data({ test1: { case: { lr: nil, momentum: nil }, - expected: Numo::DFloat[0.96, 1.95, 2.94] + expected: xm::DFloat[0.96, 1.95, 2.94] }, test2: { case: { lr: 0.05, momentum: 0.5 }, - expected: Numo::DFloat[0.8 , 1.75, 2.7] + expected: xm::DFloat[0.8 , 1.75, 2.7] } }) def test_momentum_sgd(data) - var = Chainer::Variable.new(Numo::DFloat[1, 2, 3]) - var.grad = Numo::DFloat[4, 5, 6] + var = Chainer::Variable.new(xm::DFloat[1, 2, 3]) + var.grad = xm::DFloat[4, 5, 6] sgd = Chainer::Optimizers::MomentumSGD.new(lr: data[:case][:lr], momentum: data[:case][:momentum]) opt = sgd.create_update_rule opt.instance_variable_set(:@state, {}) diff --git a/test/run_test.rb b/test/run_test.rb index 8632e4b..8b643e6 100644 --- a/test/run_test.rb +++ b/test/run_test.rb @@ -7,4 +7,15 @@ require 'test/unit' require 'chainer' +def require_gpu(id = nil) + omit(['GPU', id, 'is needed'].join(' ')) unless Chainer::CUDA.available?(id) +end + +def xm + Chainer.get_default_device.xm +end + +device = Chainer.get_device(Integer(ENV['RED_CHAINER_GPU'] || -1)) +Chainer.set_default_device(device) + exit Test::Unit::AutoRunner.run(true, test_dir) diff --git a/test/training/extensions/exponential_shift_test.rb b/test/training/extensions/exponential_shift_test.rb index a7e66a8..ca5da13 100644 --- a/test/training/extensions/exponential_shift_test.rb +++ b/test/training/extensions/exponential_shift_test.rb @@ -15,7 +15,7 @@ def test_exponential_shift(data) model = Chainer::Links::Model::Classifier.new(1) optimizer = Chainer::Optimizers::MomentumSGD.new optimizer.setup(model) - train_iter = Chainer::Iterators::SerialIterator.new(Numo::DFloat[1, 2, 3], 1) + train_iter = Chainer::Iterators::SerialIterator.new(xm::DFloat[1, 2, 3], 1) updater = Chainer::Training::StandardUpdater.new(train_iter, optimizer) trainer = Chainer::Training::Trainer.new(updater) extension = Chainer::Training::Extensions::ExponentialShift.new("lr", data[:case][:rate], **data[:case][:options]) diff --git a/test/utils/array_test.rb b/test/utils/array_test.rb index 9d02c62..377f38e 100644 --- a/test/utils/array_test.rb +++ b/test/utils/array_test.rb @@ -6,8 +6,8 @@ class Chainer::Utils::ArrayTest < Test::Unit::TestCase sub_test_case "force_array" do data = { 'test1' => {dtype: nil}, - 'test2' => {dtype: Numo::SFloat}, - 'test3' => {dtype: Numo::DFloat}} + 'test2' => {dtype: xm::SFloat}, + 'test3' => {dtype: xm::DFloat}} def _setup(data) @dtype = data[:dtype] @@ -16,10 +16,10 @@ def _setup(data) data(data) def test_scalar(data) _setup(data) - x = Chainer::Utils::Array.force_array(Numo::SFloat.cast(1), dtype=@dtype) - assert_true(x.is_a? Numo::NArray) + x = Chainer::Utils::Array.force_array(xm::SFloat.cast(1), dtype=@dtype) + assert_true(x.is_a? xm::NArray) if @dtype.nil? - assert_equal(Numo::SFloat, x.class) + assert_equal(xm::SFloat, x.class) else assert_equal(@dtype, x.class) end @@ -28,10 +28,10 @@ def test_scalar(data) data(data) def test_0dim_array(data) _setup(data) - x = Chainer::Utils::Array.force_array(Numo::SFloat.cast(1), dtype=@dtype) - assert_true(x.is_a? Numo::NArray) + x = Chainer::Utils::Array.force_array(xm::SFloat.cast(1), dtype=@dtype) + assert_true(x.is_a? xm::NArray) if @dtype.nil? - assert_equal(Numo::SFloat, x.class) + assert_equal(xm::SFloat, x.class) else assert_equal(@dtype, x.class) end @@ -40,10 +40,10 @@ def test_0dim_array(data) data(data) def test_array(data) _setup(data) - x = Chainer::Utils::Array.force_array(Numo::SFloat.cast([1]), dtype=@dtype) - assert_true(x.is_a? Numo::NArray) + x = Chainer::Utils::Array.force_array(xm::SFloat.cast([1]), dtype=@dtype) + assert_true(x.is_a? xm::NArray) if @dtype.nil? - assert_equal(Numo::SFloat, x.class) + assert_equal(xm::SFloat, x.class) else assert_equal(@dtype, x.class) end @@ -55,32 +55,32 @@ def test_array(data) axis0: { indices: 1, axis: 0, - expect: Numo::DFloat[[12, 13, 14, 15], + expect: xm::DFloat[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]] }, axis1: { indices: 1, axis: 1, - expect: Numo::DFloat[[ 4, 5, 6, 7], + expect: xm::DFloat[[ 4, 5, 6, 7], [16, 17, 18, 19]] }, axis2: { indices: 1, axis: 2, - expect: Numo::DFloat[[ 1, 5, 9], + expect: xm::DFloat[[ 1, 5, 9], [13, 17, 21]] }, axis1_array: { indices: [1], axis: 1, - expect: Numo::DFloat[[[ 4, 5, 6, 7]], + expect: xm::DFloat[[[ 4, 5, 6, 7]], [[16, 17, 18, 19]]] }, axis1_array12: { indices: [1, 2], axis: 1, - expect: Numo::DFloat[[[ 4, 5, 6, 7], + expect: xm::DFloat[[[ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[16, 17, 18, 19], [20, 21, 22, 23]]] @@ -88,7 +88,7 @@ def test_array(data) axis2_array12: { indices: [1, 2], axis: 2, - expect: Numo::DFloat[[[ 1, 2], + expect: xm::DFloat[[[ 1, 2], [ 5, 6], [ 9, 10]], [[13, 14], @@ -97,7 +97,7 @@ def test_array(data) } }) def test_take(data) - x = Numo::DFloat.new(2,3,4).seq + x = xm::DFloat.new(2,3,4).seq result = Chainer::Utils::Array.take(x, data[:indices], axis: data[:axis]) assert_equal data[:expect], result end @@ -107,26 +107,26 @@ def test_take(data) data({ test1: { # shape : [3, 4, 5, 6] => [3, 6, 4, 5] - a: Numo::DFloat.ones([3, 4, 5, 6]), + a: xm::DFloat.ones([3, 4, 5, 6]), axis: 3, start: 1, expected: [3, 6, 4, 5] }, test2: { # shape : [3, 4, 5, 6] => [5, 3, 4, 6] - a: Numo::DFloat.ones([3, 4, 5, 6]), + a: xm::DFloat.ones([3, 4, 5, 6]), axis: 2, start: 0, expected: [5, 3, 4, 6] }, test3: { - a: Numo::DFloat.ones([3, 4, 5, 6]), + a: xm::DFloat.ones([3, 4, 5, 6]), axis: 1, start: 4, expected: [3, 5, 6, 4] }, test4: { - a: Numo::DFloat.ones([2, 3, 2]), + a: xm::DFloat.ones([2, 3, 2]), axis: 1, start: 0, expected: [3, 2, 2] @@ -145,39 +145,39 @@ def test_rollaxis(data) # shape : [3] => [3] a: [1, 2, 3], shape: [3], - expected: Numo::SFloat[1, 2, 3] + expected: xm::SFloat[1, 2, 3] }, test2: { # shape : [3] => [2, 3] a: [1, 2, 3], shape: [2, 3], - expected: Numo::SFloat[[1, 2, 3], [1, 2, 3]] + expected: xm::SFloat[[1, 2, 3], [1, 2, 3]] }, test3: { # shape : [3] => [2, 2, 3] a: [1, 2, 3], shape: [2, 2, 3], - expected: Numo::SFloat[[[1, 2, 3], [1, 2, 3]], + expected: xm::SFloat[[[1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]] }, test4: { # shape : [2, 3] => [2, 3] a: [[0, 1, 2], [3, 4, 5]], shape: [2, 3], - expected: Numo::SFloat[[0, 1, 2], [3, 4, 5]] + expected: xm::SFloat[[0, 1, 2], [3, 4, 5]] }, test5: { # shape : [2, 3] => [2, 2, 3] a: [[0, 1, 2], [3, 4, 5]], shape: [2, 2, 3], - expected: Numo::SFloat[[[0, 1, 2], [3, 4, 5]], + expected: xm::SFloat[[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, 4, 5]]] }, test6: { # shape : [2, 3] => [2, 2, 2, 3] a: [[0, 1, 2], [3, 4, 5]], shape: [2, 2, 2, 3], - expected: Numo::SFloat[[[[0, 1, 2], [3, 4, 5]], + expected: xm::SFloat[[[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, 4, 5]]], [[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, 4, 5]]]] @@ -186,43 +186,43 @@ def test_rollaxis(data) # shape : [1, 3] => [1, 3] a: [[1, 2, 3]], shape: [1, 3], - expected: Numo::SFloat[[1, 2, 3]] + expected: xm::SFloat[[1, 2, 3]] }, test8: { # shape : [1, 3] => [2, 3] a: [[1, 2, 3]], shape: [2, 3], - expected: Numo::SFloat[[1, 2, 3], [1, 2, 3]] + expected: xm::SFloat[[1, 2, 3], [1, 2, 3]] }, test9: { # shape : [1, 3] => [2, 2, 3] a: [[1, 2, 3]], shape: [2, 2, 3], - expected: Numo::SFloat[[[1, 2, 3], [1, 2, 3]], + expected: xm::SFloat[[[1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]] }, test10: { # shape : [1, 3, 1] => [1, 3, 1] a: [[[1], [2], [3]]], shape: [1, 3, 1], - expected: Numo::SFloat[[[1], [2], [3]]] + expected: xm::SFloat[[[1], [2], [3]]] }, test11: { # shape : [1, 3, 1] => [1, 3, 2] a: [[1], [2], [3]], shape: [1, 3, 2], - expected: Numo::SFloat[[[1, 1], [2, 2], [3, 3]]] + expected: xm::SFloat[[[1, 1], [2, 2], [3, 3]]] }, test12: { # shape : [1, 3, 1] => [2, 3, 1] a: [[1], [2], [3]], shape: [2, 3, 1], - expected: Numo::SFloat[[[1], [2], [3]], [[1], [2], [3]]] + expected: xm::SFloat[[[1], [2], [3]], [[1], [2], [3]]] } }) def test_broadcast_to(data) - x = Numo::SFloat.cast(data[:a]) + x = xm::SFloat.cast(data[:a]) y = Chainer::Utils::Array.broadcast_to(x, data[:shape]) assert_equal(data[:shape], y.shape) assert_equal(data[:expected], y) diff --git a/test/utils/conv_test.rb b/test/utils/conv_test.rb index 91bc60b..503f448 100644 --- a/test/utils/conv_test.rb +++ b/test/utils/conv_test.rb @@ -32,45 +32,45 @@ def test_get_conv_outsize(data) data({ test1: { case: { - img: Numo::DFloat.new(1, 2, 4, 4).seq, kh: 2, kw: 2, sy: 1, sx: 1, ph: 0, pw: 0, options: {} + img: xm::DFloat.new(1, 2, 4, 4).seq, kh: 2, kw: 2, sy: 1, sx: 1, ph: 0, pw: 0, options: {} }, - expected: Numo::DFloat[[[[[[0.0, 1.0, 2.0], [4.0, 5.0, 6.0], [8.0, 9.0, 10.0]], [[1.0, 2.0, 3.0], [5.0, 6.0, 7.0], [9.0, 10.0, 11.0]]], [[[4.0, 5.0, 6.0], [8.0, 9.0, 10.0], [12.0, 13.0, 14.0]], [[5.0, 6.0, 7.0], [9.0, 10.0, 11.0], [13.0, 14.0, 15.0]]]], [[[[16.0, 17.0, 18.0], [20.0, 21.0, 22.0], [24.0, 25.0, 26.0]], [[17.0, 18.0, 19.0], [21.0, 22.0, 23.0], [25.0, 26.0, 27.0]]], [[[20.0, 21.0, 22.0], [24.0, 25.0, 26.0], [28.0, 29.0, 30.0]], [[21.0, 22.0, 23.0], [25.0, 26.0, 27.0], [29.0, 30.0, 31.0]]]]]] + expected: xm::DFloat[[[[[[0.0, 1.0, 2.0], [4.0, 5.0, 6.0], [8.0, 9.0, 10.0]], [[1.0, 2.0, 3.0], [5.0, 6.0, 7.0], [9.0, 10.0, 11.0]]], [[[4.0, 5.0, 6.0], [8.0, 9.0, 10.0], [12.0, 13.0, 14.0]], [[5.0, 6.0, 7.0], [9.0, 10.0, 11.0], [13.0, 14.0, 15.0]]]], [[[[16.0, 17.0, 18.0], [20.0, 21.0, 22.0], [24.0, 25.0, 26.0]], [[17.0, 18.0, 19.0], [21.0, 22.0, 23.0], [25.0, 26.0, 27.0]]], [[[20.0, 21.0, 22.0], [24.0, 25.0, 26.0], [28.0, 29.0, 30.0]], [[21.0, 22.0, 23.0], [25.0, 26.0, 27.0], [29.0, 30.0, 31.0]]]]]] }, test2: { case: { - img: Numo::DFloat.new(2, 2, 4, 4).seq, kh: 2, kw: 2, sy: 1, sx: 1, ph: 0, pw: 0, options: {} + img: xm::DFloat.new(2, 2, 4, 4).seq, kh: 2, kw: 2, sy: 1, sx: 1, ph: 0, pw: 0, options: {} }, - expected: Numo::DFloat[[[[[[0.0, 1.0, 2.0], [4.0, 5.0, 6.0], [8.0, 9.0, 10.0]], [[1.0, 2.0, 3.0], [5.0, 6.0, 7.0], [9.0, 10.0, 11.0]]], [[[4.0, 5.0, 6.0], [8.0, 9.0, 10.0], [12.0, 13.0, 14.0]], [[5.0, 6.0, 7.0], [9.0, 10.0, 11.0], [13.0, 14.0, 15.0]]]], [[[[16.0, 17.0, 18.0], [20.0, 21.0, 22.0], [24.0, 25.0, 26.0]], [[17.0, 18.0, 19.0], [21.0, 22.0, 23.0], [25.0, 26.0, 27.0]]], [[[20.0, 21.0, 22.0], [24.0, 25.0, 26.0], [28.0, 29.0, 30.0]], [[21.0, 22.0, 23.0], [25.0, 26.0, 27.0], [29.0, 30.0, 31.0]]]]], [[[[[32.0, 33.0, 34.0], [36.0, 37.0, 38.0], [40.0, 41.0, 42.0]], [[33.0, 34.0, 35.0], [37.0, 38.0, 39.0], [41.0, 42.0, 43.0]]], [[[36.0, 37.0, 38.0], [40.0, 41.0, 42.0], [44.0, 45.0, 46.0]], [[37.0, 38.0, 39.0], [41.0, 42.0, 43.0], [45.0, 46.0, 47.0]]]], [[[[48.0, 49.0, 50.0], [52.0, 53.0, 54.0], [56.0, 57.0, 58.0]], [[49.0, 50.0, 51.0], [53.0, 54.0, 55.0], [57.0, 58.0, 59.0]]], [[[52.0, 53.0, 54.0], [56.0, 57.0, 58.0], [60.0, 61.0, 62.0]], [[53.0, 54.0, 55.0], [57.0, 58.0, 59.0], [61.0, 62.0, 63.0]]]]]] + expected: xm::DFloat[[[[[[0.0, 1.0, 2.0], [4.0, 5.0, 6.0], [8.0, 9.0, 10.0]], [[1.0, 2.0, 3.0], [5.0, 6.0, 7.0], [9.0, 10.0, 11.0]]], [[[4.0, 5.0, 6.0], [8.0, 9.0, 10.0], [12.0, 13.0, 14.0]], [[5.0, 6.0, 7.0], [9.0, 10.0, 11.0], [13.0, 14.0, 15.0]]]], [[[[16.0, 17.0, 18.0], [20.0, 21.0, 22.0], [24.0, 25.0, 26.0]], [[17.0, 18.0, 19.0], [21.0, 22.0, 23.0], [25.0, 26.0, 27.0]]], [[[20.0, 21.0, 22.0], [24.0, 25.0, 26.0], [28.0, 29.0, 30.0]], [[21.0, 22.0, 23.0], [25.0, 26.0, 27.0], [29.0, 30.0, 31.0]]]]], [[[[[32.0, 33.0, 34.0], [36.0, 37.0, 38.0], [40.0, 41.0, 42.0]], [[33.0, 34.0, 35.0], [37.0, 38.0, 39.0], [41.0, 42.0, 43.0]]], [[[36.0, 37.0, 38.0], [40.0, 41.0, 42.0], [44.0, 45.0, 46.0]], [[37.0, 38.0, 39.0], [41.0, 42.0, 43.0], [45.0, 46.0, 47.0]]]], [[[[48.0, 49.0, 50.0], [52.0, 53.0, 54.0], [56.0, 57.0, 58.0]], [[49.0, 50.0, 51.0], [53.0, 54.0, 55.0], [57.0, 58.0, 59.0]]], [[[52.0, 53.0, 54.0], [56.0, 57.0, 58.0], [60.0, 61.0, 62.0]], [[53.0, 54.0, 55.0], [57.0, 58.0, 59.0], [61.0, 62.0, 63.0]]]]]] }, test3: { case: { - img: Numo::DFloat.new(1, 1, 4, 4).seq, kh: 2, kw: 2, sy: 2, sx: 2, ph: 0, pw: 0, options: {} + img: xm::DFloat.new(1, 1, 4, 4).seq, kh: 2, kw: 2, sy: 2, sx: 2, ph: 0, pw: 0, options: {} }, - expected: Numo::DFloat[[[[[[0.0, 2.0], [8.0, 10.0]], [[1.0, 3.0], [9.0, 11.0]]], [[[4.0, 6.0], [12.0, 14.0]], [[5.0, 7.0], [13.0, 15.0]]]]]] + expected: xm::DFloat[[[[[[0.0, 2.0], [8.0, 10.0]], [[1.0, 3.0], [9.0, 11.0]]], [[[4.0, 6.0], [12.0, 14.0]], [[5.0, 7.0], [13.0, 15.0]]]]]] }, test4: { case: { - img: Numo::DFloat.new(1, 1, 4, 4).seq, kh: 2, kw: 2, sy: 2, sx: 2, ph: 1, pw: 1, options: {} + img: xm::DFloat.new(1, 1, 4, 4).seq, kh: 2, kw: 2, sy: 2, sx: 2, ph: 1, pw: 1, options: {} }, - expected: Numo::DFloat[[[[[[0.0, 0.0, 0.0], [0.0, 5.0, 7.0], [0.0, 13.0, 15.0]], [[0.0, 0.0, 0.0], [4.0, 6.0, 0.0], [12.0, 14.0, 0.0]]], [[[0.0, 1.0, 3.0], [0.0, 9.0, 11.0], [0.0, 0.0, 0.0]], [[0.0, 2.0, 0.0], [8.0, 10.0, 0.0], [0.0, 0.0, 0.0]]]]]] + expected: xm::DFloat[[[[[[0.0, 0.0, 0.0], [0.0, 5.0, 7.0], [0.0, 13.0, 15.0]], [[0.0, 0.0, 0.0], [4.0, 6.0, 0.0], [12.0, 14.0, 0.0]]], [[[0.0, 1.0, 3.0], [0.0, 9.0, 11.0], [0.0, 0.0, 0.0]], [[0.0, 2.0, 0.0], [8.0, 10.0, 0.0], [0.0, 0.0, 0.0]]]]]] }, test5: { case: { - img: Numo::DFloat.new(1, 1, 4, 4).seq, kh: 2, kw: 2, sy: 2, sx: 2, ph: 1, pw: 1, options: { pval: 3 } + img: xm::DFloat.new(1, 1, 4, 4).seq, kh: 2, kw: 2, sy: 2, sx: 2, ph: 1, pw: 1, options: { pval: 3 } }, - expected: Numo::DFloat[[[[[[3.0, 3.0, 3.0], [3.0, 5.0, 7.0], [3.0, 13.0, 15.0]], [[3.0, 3.0, 3.0], [4.0, 6.0, 3.0], [12.0, 14.0, 3.0]]], [[[3.0, 1.0, 3.0], [3.0, 9.0, 11.0], [3.0, 3.0, 3.0]], [[0.0, 2.0, 3.0], [8.0, 10.0, 3.0], [3.0, 3.0, 3.0]]]]]] + expected: xm::DFloat[[[[[[3.0, 3.0, 3.0], [3.0, 5.0, 7.0], [3.0, 13.0, 15.0]], [[3.0, 3.0, 3.0], [4.0, 6.0, 3.0], [12.0, 14.0, 3.0]]], [[[3.0, 1.0, 3.0], [3.0, 9.0, 11.0], [3.0, 3.0, 3.0]], [[0.0, 2.0, 3.0], [8.0, 10.0, 3.0], [3.0, 3.0, 3.0]]]]]] }, test6: { case: { - img: Numo::DFloat.new(1, 1, 4, 4).seq, kh: 2, kw: 2, sy: 2, sx: 2, ph: 0, pw: 0, options: {} + img: xm::DFloat.new(1, 1, 4, 4).seq, kh: 2, kw: 2, sy: 2, sx: 2, ph: 0, pw: 0, options: {} }, - expected: Numo::DFloat[[[[[[0.0, 2.0], [8.0, 10.0]], [[1.0, 3.0], [9.0, 11.0]]], [[[4.0, 6.0], [12.0, 14.0]], [[5.0, 7.0], [13.0, 15.0]]]]]] + expected: xm::DFloat[[[[[[0.0, 2.0], [8.0, 10.0]], [[1.0, 3.0], [9.0, 11.0]]], [[[4.0, 6.0], [12.0, 14.0]], [[5.0, 7.0], [13.0, 15.0]]]]]] }, test7: { # cover_all case: { - img: Numo::DFloat.new(1, 1, 4, 6).seq, kh: 2, kw: 2, sy: 3, sx: 3, ph: 0, pw: 0, options: { cover_all: true } + img: xm::DFloat.new(1, 1, 4, 6).seq, kh: 2, kw: 2, sy: 3, sx: 3, ph: 0, pw: 0, options: { cover_all: true } }, - expected: Numo::DFloat[[[[[[0.0, 3.0, 0.0], [18.0, 21.0, 0.0]], [[1.0, 4.0, 0.0], [19.0, 22.0, 0.0]]], [[[6.0, 9.0, 0.0], [0.0, 0.0, 0.0]], [[7.0, 10.0, 0.0], [0.0, 0.0, 0.0]]]]]] + expected: xm::DFloat[[[[[[0.0, 3.0, 0.0], [18.0, 21.0, 0.0]], [[1.0, 4.0, 0.0], [19.0, 22.0, 0.0]]], [[[6.0, 9.0, 0.0], [0.0, 0.0, 0.0]], [[7.0, 10.0, 0.0], [0.0, 0.0, 0.0]]]]]] } }) def test_im2col(data) @@ -82,24 +82,24 @@ def test_im2col(data) data({ test1: { case: { - col: Numo::DFloat[[[[[[0.0, 2.0], [8.0, 10.0]], [[1.0, 3.0], [9.0, 11.0]]], [[[4.0, 6.0], [12.0, 14.0]], [[5.0, 7.0], [13.0, 15.0]]]]]], + col: xm::DFloat[[[[[[0.0, 2.0], [8.0, 10.0]], [[1.0, 3.0], [9.0, 11.0]]], [[[4.0, 6.0], [12.0, 14.0]], [[5.0, 7.0], [13.0, 15.0]]]]]], sy: 2, sx: 2, ph: 1, pw: 1, h: 2, w:2 }, - expected: Numo::DFloat[[[[5.0, 6.0], [9.0, 10.0]]]] + expected: xm::DFloat[[[[5.0, 6.0], [9.0, 10.0]]]] }, test2: { case: { - col: Numo::DFloat[[[[[[0.0, 3.0, 0.0], [18.0, 21.0, 0.0]], [[1.0, 4.0, 0.0], [19.0, 22.0, 0.0]]], [[[6.0, 9.0, 0.0], [0.0, 0.0, 0.0]], [[7.0, 10.0, 0.0], [0.0, 0.0, 0.0]]]]]], + col: xm::DFloat[[[[[[0.0, 3.0, 0.0], [18.0, 21.0, 0.0]], [[1.0, 4.0, 0.0], [19.0, 22.0, 0.0]]], [[[6.0, 9.0, 0.0], [0.0, 0.0, 0.0]], [[7.0, 10.0, 0.0], [0.0, 0.0, 0.0]]]]]], sy: 1, sx: 1, ph: 1, pw: 1, h: 2, w:2 }, - expected: Numo::DFloat[[[[56.0, 32.0], [0.0, 0.0]]]] + expected: xm::DFloat[[[[56.0, 32.0], [0.0, 0.0]]]] }, test3: { case: { - col: Numo::DFloat.new(2, 2, 2, 2, 3, 4).seq, + col: xm::DFloat.new(2, 2, 2, 2, 3, 4).seq, sy: 2, sx: 2, ph: 2, pw: 2, h: 3, w: 4 }, - expected: Numo::DFloat[[[[5.0, 17.0, 6.0, 18.0], [29.0, 41.0, 30.0, 42.0], [9.0, 21.0, 10.0, 22.0]], [[53.0, 65.0, 54.0, 66.0], [77.0, 89.0, 78.0, 90.0], [57.0, 69.0, 58.0, 70.0]]], [[[101.0, 113.0, 102.0, 114.0], [125.0, 137.0, 126.0, 138.0], [105.0, 117.0, 106.0, 118.0]], [[149.0, 161.0, 150.0, 162.0], [173.0, 185.0, 174.0, 186.0], [153.0, 165.0, 154.0, 166.0]]]], + expected: xm::DFloat[[[[5.0, 17.0, 6.0, 18.0], [29.0, 41.0, 30.0, 42.0], [9.0, 21.0, 10.0, 22.0]], [[53.0, 65.0, 54.0, 66.0], [77.0, 89.0, 78.0, 90.0], [57.0, 69.0, 58.0, 70.0]]], [[[101.0, 113.0, 102.0, 114.0], [125.0, 137.0, 126.0, 138.0], [105.0, 117.0, 106.0, 118.0]], [[149.0, 161.0, 150.0, 162.0], [173.0, 185.0, 174.0, 186.0], [153.0, 165.0, 154.0, 166.0]]]], } }) def test_col2im(data) diff --git a/test/utils/math_test.rb b/test/utils/math_test.rb index f792701..4803dfb 100644 --- a/test/utils/math_test.rb +++ b/test/utils/math_test.rb @@ -4,27 +4,27 @@ class Chainer::Utils::MathTest < Test::Unit::TestCase data({ test1: { case: { - a: Numo::DFloat.new(2, 5, 2).seq, - b: Numo::DFloat.new(5, 2).seq, + a: xm::DFloat.new(2, 5, 2).seq, + b: xm::DFloat.new(5, 2).seq, axes: 2 }, - expected: Numo::DFloat[285.0, 735.0] + expected: xm::DFloat[285.0, 735.0] }, test2: { case: { - a: Numo::DFloat.new(1, 3, 4).seq, - b: Numo::DFloat.new(5, 3).seq, + a: xm::DFloat.new(1, 3, 4).seq, + b: xm::DFloat.new(5, 3).seq, axes: [1, 1] }, - expected: Numo::DFloat[[[20.0, 56.0, 92.0, 128.0, 164.0], [23.0, 68.0, 113.0, 158.0, 203.0], [26.0, 80.0, 134.0, 188.0, 242.0], [29.0, 92.0, 155.0, 218.0, 281.0]]] + expected: xm::DFloat[[[20.0, 56.0, 92.0, 128.0, 164.0], [23.0, 68.0, 113.0, 158.0, 203.0], [26.0, 80.0, 134.0, 188.0, 242.0], [29.0, 92.0, 155.0, 218.0, 281.0]]] }, test3: { case: { - a: Numo::DFloat.new(1, 3, 4, 2).seq, - b: Numo::DFloat.new(1, 3, 1, 4).seq, + a: xm::DFloat.new(1, 3, 4, 2).seq, + b: xm::DFloat.new(1, 3, 1, 4).seq, axes: [[1, 2], [1, 3]] }, - expected: Numo::DFloat[[[[1012.0]], [[1078.0]]]] + expected: xm::DFloat[[[[1012.0]], [[1078.0]]]] } }) def test_tensordot(data) diff --git a/test/variable_test.rb b/test/variable_test.rb index 7219c70..739d522 100644 --- a/test/variable_test.rb +++ b/test/variable_test.rb @@ -21,22 +21,22 @@ def constant(xs, value) class Chainer::VariableTest < Test::Unit::TestCase data = { - 'test1' => {x_shape: [10], c_shape: [2, 5], label: '(2, 5), Numo::SFloat'}, - 'test2' => {x_shape: [], c_shape: [1], label: '(1), Numo::SFloat'}} + 'test1' => {x_shape: [10], c_shape: [2, 5], label: "(2, 5), #{xm}::SFloat"}, + 'test2' => {x_shape: [], c_shape: [1], label: "(1), #{xm}::SFloat"}} def _setup(data) @x_shape = data[:x_shape] @label = data[:label] @c_shape = data[:c_shape] - @x = Numo::SFloat.new(@x_shape).rand(2) - 1 - @a = Numo::SFloat.new(@x_shape).rand(9.9) + 0.1 + @x = xm::SFloat.new(@x_shape).rand(2) - 1 + @a = xm::SFloat.new(@x_shape).rand(9.9) + 0.1 if @x_shape.size != 0 - @size = Numo::NArray.cast(@x_shape).prod().to_i + @size = xm::NArray.cast(@x_shape).prod().to_i else @size = 1 end - @c = Numo::DFloat.new(@size).seq(0).reshape(*@c_shape).cast_to(Numo::SFloat) + @c = xm::DFloat.new(@size).seq(0).reshape(*@c_shape).cast_to(xm::SFloat) end def check_attributes(gpu) @@ -112,28 +112,28 @@ def test_backward(data) end def test_grad_type_check_pass() - a = Chainer::Variable.new(Numo::SFloat.new([3])) - a.grad = Numo::SFloat.new([3]) + a = Chainer::Variable.new(xm::SFloat.new([3])) + a.grad = xm::SFloat.new([3]) end def test_grad_type_check_type() - a = Chainer::Variable.new(Numo::SFloat.new([])) + a = Chainer::Variable.new(xm::SFloat.new([])) #assert_raise(TypeError) { ## No Error - a.grad = Numo::SFloat.new() + a.grad = xm::SFloat.new() #} end def test_grad_type_check_dtype() - a = Chainer::Variable.new(Numo::SFloat.new([3])) + a = Chainer::Variable.new(xm::SFloat.new([3])) assert_raise(TypeError) { - a.grad = Numo::DFloat.new([3]) + a.grad = xm::DFloat.new([3]) } end def test_grad_type_check_shape() - a = Chainer::Variable.new(Numo::SFloat.new([3])) + a = Chainer::Variable.new(xm::SFloat.new([3])) assert_raise(TypeError) { - a.grad = Numo::SFloat.new([2]) + a.grad = xm::SFloat.new([2]) } end end