Is the purpose of DPS(DestinationStype) for to reduce the memref.copy when one-shot-bufferization?

Hi
is the purpose of DPS(DestinationStype) for to reduce the memref.copy when one-shot-bufferization?
thanks.

In case you missed it, have you read this doc yet Bufferization - MLIR ?

yes, I read, but still don’t get the key informaion

DestinationPassing style exists in itself without bufferization, many ops are “updating” part of a tensor, and so they take the tensor and the slice to update. Since tensors are immutable object, the op returns a new tensor.
Another example is linalg, with the misleading “outs” parameters which provides the initial values when the operation is doing a reduction, and also used to infer the resulting shape.

There is a relationship to bufferization though in that when this “immutable tensor” that serves as initial value to the operation isn’t used anywhere else, it becomes trivial to just reuse its memory and update it in place instead of creating a copy.

This is more visible for an operation which does not actually need an initial tensor, let’s say you want just to add two tensors with linalg:

  %r = linalg.generic {
    indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>,
                     affine_map<(d0, d1) -> (do, d1)>,
                     affine_map<(d0, d1)-> (d0, d1)>],
    iterator_types = ["parallel", "parallel"]}
    ins(%t1, %t2 : tensor<?x?xf32>, tensor<?x?xf32>)
    outs(%empty : tensor<?x?xf32>) {
      ^bb0(%arg0 : f32, %arg1 : f32, %arg2 : f32) :
        %add = arith.addf %arg0, %arg1 : f32
        linalg.yield %add : f32
    } -> tensor<?x?xf32>

Conceptually if I simplify it is:

  %r = %t1, %t2, %empty : linalg.addf tensor<?x?xf32>

There are three input In this form the outs isn’t strictly needed, but linalg still requires it. We will consider this op in destination passing style, and if the “%empty” tensor isn’t used anywhere else, it’ll provide an “anchor” for the bufferization algorithm to use for aliasing its buffer allocation with the result.

But I’m just repeating here what’s in the doc, and you should be more precise about what is unclear…

2 Likes

now, it is clearly, thanks very much

This paper also has an excellent explanation for DPS: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/11/dps-submitted.pdf

1 Like

got it, thanks