mrkn commented on code in PR #14381:
URL: https://github.com/apache/arrow/pull/14381#discussion_r995341876


##########
ruby/red-arrow/lib/arrow/raw-tensor-converter.rb:
##########
@@ -41,12 +41,22 @@ def convert
       case @raw_tensor
       when Buffer
         @data = @raw_tensor
+      when String
+        @data = Arrow::Buffer.new(@raw_tensor)

Review Comment:
   Is restriction by `BINARY` encoding unnecessary?



##########
ruby/red-arrow/lib/arrow/raw-tensor-converter.rb:
##########
@@ -41,12 +41,22 @@ def convert
       case @raw_tensor
       when Buffer
         @data = @raw_tensor
+      when String
+        @data = Arrow::Buffer.new(@raw_tensor)
       else
         @shape ||= guess_shape
         build_buffer
+        unless @strides.nil?
+          message = "strides: must not be specified: #{@strides.inspect}"

Review Comment:
   How about `strides: is only accepted with a Buffer or String raw_tensor`?



##########
ruby/red-arrow/test/test-tensor.rb:
##########
@@ -110,6 +110,131 @@ def setup
                        dimension_names: tensor.dimension_names,
                      })
       end
+
+      test("Array, strides:") do
+        message = "strides: must not be specified: #{@strides.inspect}"
+        assert_raise(ArgumentError.new(message)) do
+          Arrow::Tensor.new(@raw_tensor, strides: @strides)
+        end
+      end
+
+      test("Arrow::Buffer, data_type:, shape:") do
+        data_type = :uint8
+        data = Arrow::Buffer.new(@raw_tensor.flatten.pack("C*").freeze)
+        tensor = Arrow::Tensor.new(data,
+                                   data_type: data_type,
+                                   shape: @shape)
+        assert_equal({
+                       value_data_type: Arrow::UInt8DataType.new,
+                       buffer: @raw_tensor.flatten.pack("C*"),
+                       shape: @shape,
+                       strides: @strides,
+                       dimension_names: ["", "", ""],
+                     },
+                     {
+                       value_data_type: tensor.value_data_type,
+                       buffer: tensor.buffer.data.to_s,
+                       shape: tensor.shape,
+                       strides: tensor.strides,
+                       dimension_names: tensor.dimension_names,
+                     })
+      end
+
+      test("String, data_type:, shape:") do
+        data_type = :uint8
+        data = @raw_tensor.flatten.pack("C*").freeze
+        tensor = Arrow::Tensor.new(data,
+                                   data_type: data_type,
+                                   shape: @shape)
+        assert_equal({
+                       value_data_type: Arrow::UInt8DataType.new,
+                       buffer: @raw_tensor.flatten.pack("C*"),
+                       shape: @shape,
+                       strides: @strides,
+                       dimension_names: ["", "", ""],
+                     },
+                     {
+                       value_data_type: tensor.value_data_type,
+                       buffer: tensor.buffer.data.to_s,
+                       shape: tensor.shape,
+                       strides: tensor.strides,
+                       dimension_names: tensor.dimension_names,
+                     })
+      end
+
+      test("String, data_type:") do
+        data_type = :uint8
+        data = @raw_tensor.flatten.pack("C*").freeze
+        message = "shape: is missing: #{data.inspect}"
+        assert_raise(ArgumentError.new(message)) do
+          Arrow::Tensor.new(data, data_type: data_type)
+        end
+      end
+
+      test("String, shape:") do
+        data = @raw_tensor.flatten.pack("C*").freeze
+        message = "data_type: is missing: #{data.inspect}"
+        assert_raise(ArgumentError.new(message)) do
+          Arrow::Tensor.new(data, shape: @shape)
+        end
+      end
+
+      test("Symbol, Arrow::Buffer, shape:") do
+        data_type = :uint8
+        data = Arrow::Buffer.new(@raw_tensor.flatten.pack("C*").freeze)
+        tensor = Arrow::Tensor.new(data_type,
+                                   data,
+                                   shape: @shape)
+        assert_equal({
+                       value_data_type: Arrow::UInt8DataType.new,
+                       buffer: @raw_tensor.flatten.pack("C*"),
+                       shape: @shape,
+                       strides: @strides,
+                       dimension_names: ["", "", ""],
+                     },
+                     {
+                       value_data_type: tensor.value_data_type,
+                       buffer: tensor.buffer.data.to_s,
+                       shape: tensor.shape,
+                       strides: tensor.strides,
+                       dimension_names: tensor.dimension_names,
+                     })
+      end
+
+      test("Symbol, String, shape:, strides: - !contiguous and column major") 
do
+        data_type = :uint8
+        raw_tensor = [
+          [1, 2, 3, 0], # 0 is padding
+          [4, 5, 6, 0], # 0 is padding
+        ]
+        data = raw_tensor.flatten.pack("C*").freeze
+        shape = [3, 2]
+        strides = [1, 4]

Review Comment:
   This looks good to me, but I think we can reuse `@raw_tensor` in this test 
case by the following condition.
   
   ```
   shape = [3, 2, 3]
   strides = [1, 4, 8]
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to