|
| 1 | +defmodule Ortex.TestConcat do |
| 2 | + use ExUnit.Case |
| 3 | + |
| 4 | + # Testing each type, since there's a bunch of boilerplate that we want to |
| 5 | + # check for errors on the Rust side |
| 6 | + %{ |
| 7 | + "s8" => {:s, 8}, |
| 8 | + "s16" => {:s, 16}, |
| 9 | + "s32" => {:s, 16}, |
| 10 | + "s64" => {:s, 16}, |
| 11 | + "u8" => {:s, 16}, |
| 12 | + "u16" => {:s, 16}, |
| 13 | + "u32" => {:s, 16}, |
| 14 | + "u64" => {:s, 16}, |
| 15 | + "f16" => {:s, 16}, |
| 16 | + "bf16" => {:s, 16}, |
| 17 | + "f32" => {:s, 16}, |
| 18 | + "f64" => {:s, 16} |
| 19 | + } |
| 20 | + |> Enum.each(fn {type_str, type_tuple} -> |
| 21 | + test "Concat 1d tensors #{type_str}" do |
| 22 | + t1 = |
| 23 | + Nx.tensor([1, 2, 3, 4], type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 24 | + |
| 25 | + t2 = |
| 26 | + Nx.tensor([1, 2, 3, 4], type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 27 | + |
| 28 | + concatted = Nx.concatenate([t1, t2]) |> Nx.backend_transfer() |
| 29 | + expected = Nx.tensor([1, 2, 3, 4, 1, 2, 3, 4], type: unquote(type_tuple)) |
| 30 | + assert concatted == expected |
| 31 | + end |
| 32 | + |
| 33 | + test "Concat 3d tensors #{type_str}" do |
| 34 | + o1 = Nx.iota({2, 3, 5}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 35 | + o2 = Nx.iota({1, 3, 5}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 36 | + concatted = Nx.concatenate([o1, o2]) |> Nx.backend_transfer() |
| 37 | + expected = Nx.concatenate([o1 |> Nx.backend_transfer(), o2 |> Nx.backend_transfer()]) |
| 38 | + assert concatted == expected |
| 39 | + end |
| 40 | + |
| 41 | + test "Concat 3 #{type_str} vectors" do |
| 42 | + t1 = |
| 43 | + Nx.tensor([1, 2, 3, 4], type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 44 | + |
| 45 | + t2 = |
| 46 | + Nx.tensor([1, 2, 3, 4], type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 47 | + |
| 48 | + t3 = |
| 49 | + Nx.tensor([1, 2, 3, 4], type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 50 | + |
| 51 | + concatted = Nx.concatenate([t1, t2, t3]) |> Nx.backend_transfer() |
| 52 | + expected = Nx.tensor([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4], type: unquote(type_tuple)) |
| 53 | + assert concatted == expected |
| 54 | + end |
| 55 | + |
| 56 | + test "Concat axis #{type_str} 1" do |
| 57 | + o1 = Nx.iota({3, 5}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 58 | + o2 = Nx.iota({3, 5}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 59 | + |
| 60 | + concatted = Nx.concatenate([o1, o2], axis: 1) |> Nx.backend_transfer() |
| 61 | + |
| 62 | + n1 = Nx.iota({3, 5}, type: unquote(type_tuple)) |
| 63 | + n2 = Nx.iota({3, 5}, type: unquote(type_tuple)) |
| 64 | + |
| 65 | + expected = Nx.concatenate([n1, n2], axis: 1) |
| 66 | + assert concatted == expected |
| 67 | + end |
| 68 | + |
| 69 | + test "Concat axis 1 of three 3-dimensional #{type_str} vector" do |
| 70 | + t1 = Nx.iota({3, 5, 7}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 71 | + t2 = Nx.iota({3, 5, 7}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 72 | + t3 = Nx.iota({3, 5, 7}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 73 | + |
| 74 | + concatted = Nx.concatenate([t1, t2, t3], axis: 1) |> Nx.backend_transfer() |
| 75 | + |
| 76 | + expected = |
| 77 | + Nx.concatenate( |
| 78 | + [ |
| 79 | + Nx.iota({3, 5, 7}, type: unquote(type_tuple)), |
| 80 | + Nx.iota({3, 5, 7}, type: unquote(type_tuple)), |
| 81 | + Nx.iota({3, 5, 7}, type: unquote(type_tuple)) |
| 82 | + ], |
| 83 | + axis: 1 |
| 84 | + ) |
| 85 | + |
| 86 | + assert concatted == expected |
| 87 | + end |
| 88 | + |
| 89 | + test "Concat axis 2 of three 3-dimensional #{type_str} vector" do |
| 90 | + t1 = Nx.iota({3, 5, 7}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 91 | + t2 = Nx.iota({3, 5, 7}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 92 | + t3 = Nx.iota({3, 5, 7}, type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 93 | + |
| 94 | + concatted = Nx.concatenate([t1, t2, t3], axis: 2) |> Nx.backend_transfer() |
| 95 | + |
| 96 | + expected = |
| 97 | + Nx.concatenate( |
| 98 | + [ |
| 99 | + Nx.iota({3, 5, 7}, type: unquote(type_tuple)), |
| 100 | + Nx.iota({3, 5, 7}, type: unquote(type_tuple)), |
| 101 | + Nx.iota({3, 5, 7}, type: unquote(type_tuple)) |
| 102 | + ], |
| 103 | + axis: 2 |
| 104 | + ) |
| 105 | + |
| 106 | + assert concatted == expected |
| 107 | + end |
| 108 | + |
| 109 | + test "Concat doesn't alter component #{type_str} vectors" do |
| 110 | + t1 = |
| 111 | + Nx.tensor([1, 2, 3, 4], type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 112 | + |
| 113 | + t2 = |
| 114 | + Nx.tensor([1, 2, 3, 4], type: unquote(type_tuple)) |> Nx.backend_transfer(Ortex.Backend) |
| 115 | + |
| 116 | + concatted = Nx.concatenate([t1, t2]) |> Nx.backend_transfer() |
| 117 | + second_concatted = Nx.concatenate([t1, t2]) |> Nx.backend_transfer() |
| 118 | + |
| 119 | + assert concatted == second_concatted |
| 120 | + end |
| 121 | + end) |
| 122 | + |
| 123 | + test "Concat fails to concat vectors of differing types" do |
| 124 | + assert_raise RuntimeError, |
| 125 | + "Ortex does not currently support concatenation of vectors with differing types.", |
| 126 | + fn -> |
| 127 | + t1 = Nx.tensor([1, 2, 3], type: {:s, 16}) |> Nx.backend_transfer(Ortex.Backend) |
| 128 | + t2 = Nx.tensor([1, 2, 3], type: {:s, 32}) |> Nx.backend_transfer(Ortex.Backend) |
| 129 | + _err = Nx.concatenate([t1, t2]) |
| 130 | + end |
| 131 | + end |
| 132 | +end |
0 commit comments