|
| 1 | +defmodule Ortex.TestSlice do |
| 2 | + use ExUnit.Case |
| 3 | + |
| 4 | + {tensor1d, _} = Nx.Random.uniform(Nx.Random.key(42), 0, 256, shape: {10}) |
| 5 | + {tensor2d, _} = Nx.Random.uniform(Nx.Random.key(42), 0, 256, shape: {10, 10}) |
| 6 | + |
| 7 | + @tensor1d tensor1d |
| 8 | + @tensor2d tensor2d |
| 9 | + |
| 10 | + defp tensor_binary(tensor, dtype) do |
| 11 | + tensor |> Nx.as_type(dtype) |
| 12 | + end |
| 13 | + |
| 14 | + defp tensor_ortex(tensor, dtype) do |
| 15 | + tensor |
| 16 | + |> Nx.as_type(dtype) |
| 17 | + |> Nx.backend_transfer(Ortex.Backend) |
| 18 | + end |
| 19 | + |
| 20 | + test "1d slice f32" do |
| 21 | + bin = tensor_binary(@tensor1d, :f32) |> Nx.slice([0], [4]) |
| 22 | + |
| 23 | + ort = tensor_ortex(@tensor1d, :f32) |> Nx.slice([0], [4]) |> Nx.backend_transfer() |
| 24 | + |
| 25 | + assert bin == ort |
| 26 | + end |
| 27 | + |
| 28 | + test "2d slice f32" do |
| 29 | + bin = tensor_binary(@tensor2d, :f32) |> Nx.slice([0, 2], [4, 6]) |
| 30 | + |
| 31 | + ort = |
| 32 | + tensor_ortex(@tensor2d, :f32) |
| 33 | + |> Nx.slice([0, 2], [4, 6]) |
| 34 | + |> Nx.backend_transfer() |
| 35 | + |
| 36 | + assert bin == ort |
| 37 | + end |
| 38 | + |
| 39 | + test "1d slice u8" do |
| 40 | + bin = tensor_binary(@tensor1d, :u8) |> Nx.slice([0], [4]) |
| 41 | + |
| 42 | + ort = tensor_ortex(@tensor1d, :u8) |> Nx.slice([0], [4]) |> Nx.backend_transfer() |
| 43 | + |
| 44 | + assert bin == ort |
| 45 | + end |
| 46 | + |
| 47 | + test "2d slice u8" do |
| 48 | + bin = tensor_binary(@tensor2d, :u8) |> Nx.slice([0, 2], [4, 6]) |
| 49 | + |
| 50 | + ort = |
| 51 | + tensor_ortex(@tensor2d, :u8) |
| 52 | + |> Nx.slice([0, 2], [4, 6]) |
| 53 | + |> Nx.backend_transfer() |
| 54 | + |
| 55 | + assert bin == ort |
| 56 | + end |
| 57 | + |
| 58 | + test "1d slice f32 strided" do |
| 59 | + bin = tensor_binary(@tensor1d, :f32) |> Nx.slice([0], [4], strides: [2]) |
| 60 | + |
| 61 | + ort = |
| 62 | + tensor_ortex(@tensor1d, :f32) |> Nx.slice([0], [4], strides: [2]) |> Nx.backend_transfer() |
| 63 | + |
| 64 | + assert bin == ort |
| 65 | + end |
| 66 | + |
| 67 | + test "2d slice f32 strided" do |
| 68 | + bin = tensor_binary(@tensor2d, :f32) |> Nx.slice([0, 2], [4, 6], strides: [2, 1]) |
| 69 | + |
| 70 | + ort = |
| 71 | + tensor_ortex(@tensor2d, :f32) |
| 72 | + |> Nx.slice([0, 2], [4, 6], strides: [2, 1]) |
| 73 | + |> Nx.backend_transfer() |
| 74 | + |
| 75 | + assert bin == ort |
| 76 | + end |
| 77 | + |
| 78 | + test "1d slice u8 strided" do |
| 79 | + bin = tensor_binary(@tensor1d, :u8) |> Nx.slice([0], [4], strides: [2]) |
| 80 | + |
| 81 | + ort = |
| 82 | + tensor_ortex(@tensor1d, :u8) |> Nx.slice([0], [4], strides: [2]) |> Nx.backend_transfer() |
| 83 | + |
| 84 | + assert bin == ort |
| 85 | + end |
| 86 | + |
| 87 | + test "2d slice u8 strided" do |
| 88 | + bin = tensor_binary(@tensor2d, :u8) |> Nx.slice([0, 2], [4, 6], strides: [2, 1]) |
| 89 | + |
| 90 | + ort = |
| 91 | + tensor_ortex(@tensor2d, :u8) |
| 92 | + |> Nx.slice([0, 2], [4, 6], strides: [2, 1]) |
| 93 | + |> Nx.backend_transfer() |
| 94 | + |
| 95 | + assert bin == ort |
| 96 | + end |
| 97 | +end |
0 commit comments