Why do tensor and memref types have redundant shape information?

292 views
Skip to first unread message

Edward Connell

unread,
Sep 7, 2019, 6:10:19 PM9/7/19
to MLIR
I was wondering why both tensor and memref types define shape and element type? 
Is this carried as a convenience for lowering a memref to LLVM IR?


River Riddle

unread,
Sep 7, 2019, 6:15:02 PM9/7/19
to MLIR
Hi Edward,

I'm confused as to how these are redundant, could you elaborate further? These are two completely separate types, they semantics of both also have no direct relation to LLVM.

-- River 
Message has been deleted

Mehdi AMINI

unread,
Sep 7, 2019, 6:30:29 PM9/7/19
to Edward Connell, MLIR
The main difference between tensor and memref is that the former is an immutable value-based type, while the memref is not immutable.

Layout aside, if a memref didn't have an element type, that means you would just have a typeless pointer to a buffer with a given byte size. Any access would need to compute offsets in bytes, and any load/store would need to provide the actual element type (at this point we're just directly into the LLVM land and I don't know what difference would remain between "memref" and a LLVM typeless pointer).

Imagine for example how to express this without shape and element type:

func @test6(%arg0 : index, %arg1 : index, %src : memref<10x10x10xf32>, %dst : memref<10x10x10xf32>) {
  affine.for %i0 = 0 to 10 {
    affine.for %i1 = 0 to 10 {
      affine.for %i2 = 0 to 10 {
        %1 = affine.load %src[%i2 + %arg0, %i0 + %i1, %i1 + %arg0 + %arg1]
          : memref<10x10x10xf32>
        affine.store %1, %dst[%i2 + %arg0, %i0 + %i1, %i1 + %arg0 + %arg1]
          : memref<10x10x10xf32>
      }
    }
  }
  return
}


See also this discussion which may give more info about the direction around memref: https://groups.google.com/a/tensorflow.org/d/topic/mlir/zdc6Je15ozM/discussion

Hope this helps,

-- 
Mehdi



On Sat, Sep 7, 2019 at 3:19 PM Edward Connell <ewco...@gmail.com> wrote:
I was under the impression that a tensor was a logical type expressing memory shape, strides, and element type, and that a memref represents linear address space which is the backing store for a tensor or an array. So these are unrelated types?


On Saturday, September 7, 2019 at 3:10:19 PM UTC-7, Edward Connell wrote:
I was wondering why both tensor and memref types define shape and element type? 
Is this carried as a convenience for lowering a memref to LLVM IR?


--
You received this message because you are subscribed to the Google Groups "MLIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mlir+uns...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/mlir/c0d4f5c3-c52b-45fd-befe-8503285a3916%40tensorflow.org.

Edward Connell

unread,
Sep 7, 2019, 6:37:55 PM9/7/19
to MLIR
Hi Mehdi, thank you for the explanation and the note about mutability. So is a tensor transformed into memref at some point, or are these independent paths?


On Saturday, September 7, 2019 at 3:10:19 PM UTC-7, Edward Connell wrote:

Jacques Pienaar

unread,
Sep 7, 2019, 7:02:35 PM9/7/19
to Edward Connell, MLIR
Hi Edward,

Are you asking if a memref is always due to a tensor and a tensor always has a backing memref? If so, then no they are not as tightly coupled. A tensor could be materialized in memory as memref, but one could also create a memref without a tensor.

-- Jacques

--
You received this message because you are subscribed to the Google Groups "MLIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mlir+uns...@tensorflow.org.

Mehdi AMINI

unread,
Sep 7, 2019, 7:05:09 PM9/7/19
to Edward Connell, MLIR
On Sat, Sep 7, 2019 at 3:37 PM Edward Connell <ewco...@gmail.com> wrote:
Hi Mehdi, thank you for the explanation and the note about mutability. So is a tensor transformed into memref at some point, or are these independent paths?

I tend to see tensor as used in higher-level representations and memref being used for "late" optimization. There are multiple ways (conceptually) to go from tensor to memref, but MLIR Core does not provide anything today.
We're still exploring the various options at this point, and there might not be "one size fits all". One thing we're looking at is around our XLA-related work in the TensorFlow repository. This is very exploratory so we haven't communicated anything about it, but the development is fully open here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/

In particular we have two dialects for XLA that intend to include all the operators defined by XLA. The "early HLO" dialect operates on Tensor (and so operations are side-effect free in general):


The same set of operations exists in a "late HLO" dialect, where they operate on memref and so have side-effect (they take an output parameter instead of returning a new tensor value):


Going from "early HLO" to "late HLO" is not implemented at the moment, but the equivalent exists in XLA already: this is buffer assignment.

-- 
Mehdi

 


On Saturday, September 7, 2019 at 3:10:19 PM UTC-7, Edward Connell wrote:
I was wondering why both tensor and memref types define shape and element type? 
Is this carried as a convenience for lowering a memref to LLVM IR?


Edward Connell

unread,
Sep 8, 2019, 3:03:22 PM9/8/19
to MLIR
Thanks guys, after reading your responses and reviewing the links you provided I think I have a much better understanding of the two types and their relationship. I also now understand why River was confused by my question due to my misunderstanding.

I appreciate the time you all took to help clarify things.

Ed


On Saturday, September 7, 2019 at 3:10:19 PM UTC-7, Edward Connell wrote:
Reply all
Reply to author
Forward
0 new messages