Skip to content

Enable data tiling + ukernels for linalg.vecmat and linalg.matvec #15053

Closed

Description

@dcaballe

Based on the discussion we had in the #mai-tai channel with @bjacob, we should enable data tiling + ukernels for linalg.vecmat and linalg.matvec ops. We can use the ContractionOpInterface to handle all these operations together in data tiling/ukernel path, if that's the preferred approach. In one way or another, these ops should be mapped to the mmt4d one and ukernels should be working again.

Activity

  1. added
    codegen
    codegen/llvm
    on Sep 27, 2023
  2. dcaballe commented on Oct 4, 2023

    @dcaballe
    ContributorAuthor

    Hey @bjacob, any chance you could help with this one? It's one of the top performance issues we have right now

  3. self-assigned this
    on Oct 4, 2023
  4. bjacob commented on Oct 4, 2023

    @bjacob
    Collaborator

    Putting in my queue :-)

  5. mariecwhite commented on Oct 5, 2023

    @mariecwhite
    Contributor

    This should improve latency on 1 token inputs. At the moment, data-tiling+ukernels are slower than the default flag:

    image

  6. bjacob commented on Oct 5, 2023

    @bjacob
    Collaborator

    Thanks for the context, that helps prioritizing.

    My own context: I just spent the past week getting set up at Nod. That's basically over now, but I also have to balance a number of short-term priorities. On the other hand, this particular Issue makes a lot of sense to frontload because the switch to ContractionInterface will ease other things that are in my queue anyways.

    All in all, I expect to come to this within ~ 1 week. On the other hand, this is a good mentoring opportunity into data-tiling. The change here should be simple:

  7. NatashaKnk commented on Oct 11, 2023

    @NatashaKnk
    Contributor

    Working on it now!
    I'm new to data tiling so I might be missing something -- do both matmul and matvec/vecmat create a mmt4d for tiling? If that's the case, does that mean I need to expand the vector to 1xK or Kx1, perform a matmul, and then collapse the result?
    If that is what happens, is there a reason not to do that in SetEncoding, and treat it like a regular matmul?

  8. bjacob commented on Oct 11, 2023

    @bjacob
    Collaborator

    Working on it now! I'm new to data tiling so I might be missing something -- do both matmul and matvec/vecmat create a mmt4d for tiling?

    Yes, that's the idea. vecmat and matvec are just convenience / syntactic sugar for some degenerate cases of matmul where some parallel dimension has size 1, allowing to use 1D ("vector") shapes instead of having to expand them to 2D shapes with a unit dim (as in 1xK or Kx1) as required by matmul.

    While this is a useful convenience to have at high levels, at lower levels (such as with mmt4d) we would rather have all usage patterns converge on the same shared mmt4d.

    If that's the case, does that mean I need to expand the vector to 1xK or Kx1, perform a matmul, and then collapse the result?

    Yes: whatever we do here will have to amount to exactly that.

    If that is what happens, is there a reason not to do that in SetEncoding, and treat it like a regular matmul?

    We could do that, indeed; feel free to take that route if you feel that's easier.

    When I heard of ContractionInterface via @dcaballe , I thought that offered a more elegant, general, and ultimately simpler alternative. Rather than keep the existing separate logic for matmul and batch_matmul and add more patterns to rewrite vecmat and matvec into that, the observation is that we never needed to make this logic so dependent on named ops in the first place; all what matters is that these ops implement ContractionInterface. So the idea is to just lift the existing matmul / batch_matmul to ContractionInterface and then let that naturally also cover any other op that is also a ContractionInterface, such as vecmat and matvec.

    But when we get there, we will run into the shape dimensionality discrepancy that you point out and we will have to bridge it with expanding with unit dims, and then collapsing. So it will amount to just what you proposed. Just arguably more elegant and future-proof w.r.t. possible future additional named ops.

  9. NatashaKnk commented on Oct 11, 2023

    @NatashaKnk
    Contributor

    I see, thanks for the clarification! I agree that this is a more elegant path and it's not a large amount of work to switch it over, I just figured that I should probably make sure I'm reasoning about it the right way.
    Should be out for review today/tomorrow, thanks!

  10. dcaballe commented on Oct 12, 2023

    @dcaballe
    ContributorAuthor

    I don't know the data tiling pipeline enough but I would say that to keep things consistent, let's use vecmat/matvec all the way until they have to be converted to an mmt4d, if that makes sense. This will basically ensure that we get vecmat/matvec/matmul consistently in the pipeline at any point and non only before the set encoding.

    Also, feel free to extend the ContractInterface with more methods or whatever you may need!

    Hopefully that helps!

  11. 2 remaining items

  12. bjacob commented on Oct 12, 2023

    @bjacob
    Collaborator

    @dcaballe :

    I don't know the data tiling pipeline enough but I would say that to keep things consistent, let's use vecmat/matvec all the way until they have to be converted to an mmt4d, if that makes sense. This will basically ensure that we get vecmat/matvec/matmul consistently in the pipeline at any point and non only before the set encoding.

    No worry, each named op stays what it is through SetEncoding. All what SetEncoding does is setting encoding on tensors. That we base the SetEncoding logic on ContractionInterface only affects how the tensor encodings are determined. The only op rewrite at that point is inserting new SetEncoding/UnsetEncoding ops. Existing matvec, vecmat, matmul ops are not getting rewritten at this point. It's only much later, at MaterializeEncoding, that they get rewritten.

    @NatashaKnk :

    I think the only problem I see with keeping is as a matvec/vecmat is that we would need to create a special encoding for it here.

    I agree that would be ugly. But we shouldn't need to do any of that. At the very least, vecmat and matvec should be able to use the existing MATMUL_* encodings, since they are special cases of matmuls. I still think we can proceed as in my above outline #15053 (comment)

    @pzread :

    What would be the problem if we use EncodingUser::MATMUL_* for all different kinds of matmul? I added BATCH_MATMUL_* for batch_matmul but now I'm thinking it might not be necessary.

    We could do that. The main implication of that would be that we agree that batch matmuls always use the same tile shaps as matmuls. Keeping separate encodings left the door open for different layouts for batch matmul. For the time being, though, batch mmt4d are lowered to for loops around regular mmt4d, so we are not using that degree of freedom, and in fact, we are building on an assumption that it is not being used (if batch matmul really did use a different tile shape than matmul, then lowering batch mmt4d to mmt4d would result in using mmt4d with an unexpected tile shape that we likely wouldn't have optimized code for, causing a fallback to slow generic code).

    So, all things considered, +1 to dropping BATCH_MATMUL_* and just having MATMUL_*. However note that:

    1. This could be done in a separate PR.
    2. The number of encoding enums is going to greatly shrink as I'm going to take the data types out of the enum. So for example where we currently have MATMUL_F32F32F32, MATMUL_I8I8I32, etc, soon we will only have MATMUL. So the motivation to limit the growth in the number of enums is already being taken care of.
    3. The separate enums for BATCH_ actually play a role: they mean that after they have been chosen at SetEncoding, we don't have to do any work again to tell if we have a normal or batch matmul, we can just look at the enum. See the MaterializeEncoding part of my above outline Enable data tiling + ukernels for linalg.vecmat and linalg.matvec #15053 (comment)
  13. dcaballe commented on Oct 12, 2023

    @dcaballe
    ContributorAuthor

    I think how the discussion is going. Cool! Perhaps we could rename the encoding to CONTRACTION_*?
    Just an hypothesis right now but something I would take into account in the design is that we may have to introduce different variants of mmt4d for vecmat, matvec, etc., if at some point we decide to code generate them as we do with the regular matmul today, so it would be great if we moved towards facilitating that future scenario.

  14. bjacob commented on Oct 12, 2023

    @bjacob
    Collaborator

    Curious to hear why you envision future variants of mmt4d! Maybe material for a call? For now, the MATMUL_* encoding name is only referring to the general fact, on a purely mathematical level , that matvec and vecmat are special cases of matmuls; that general math description of the op is relevant because it is what the tile-size selection (in MaterializeEncoding) needs to know. It doesn't by itself preclude introducing variants of mmt4d, we could still branch into different materialized ops in MaterializeEncoding.

    If that's OK, I'd suggest keeping this discussion a future/separate discussion, no need to resolve that as part of this PR.

  15. dcaballe commented on Oct 12, 2023

    @dcaballe
    ContributorAuthor

    If that's OK, I'd suggest keeping this discussion a future/separate discussion, no need to resolve that as part of this PR.

    +1. This shouldn't block the progress to fix this issue.

    Maybe material for a call?

    Difficult this week but we can talk next week!

  16. NatashaKnk commented on Oct 16, 2023

    @NatashaKnk
    Contributor

    I got this mostly-working but the one thing I'm struggling with is type conversion in MaterializeEncoding, and I would appreciate input on the matter.

    If SetEncoding keeps the vectors as-is and does not retain the way they are to be expanded at all, I'm not sure I understand how the typeConverter in MaterializeEncoding is supposed to know how to handle it. As it is at the moment, it takes a RankedTensorType and infers what the output of the packOp on it would be, but I'm not sure how to have it handle vectors.

    For example, an LHS of shape 4xf32 with a MATMUL encoding is clearly a vecmat LHS, so it can just be packed as 1x4xf32. However, what about a RESULT of the same shape? It could be either a 1x4 or a 4x1, and it doesn't seem like the typeConverter can arrive at a conclusion here without some change to the way things function. I'm not familiar enough with the design desicions that are at play here, so I would really appreciate any kind of insight on the topic :)

  17. bjacob commented on Oct 17, 2023

    @bjacob
    Collaborator

    Good points @NatashaKnk ! I hadn't thought of that.

    One way to solve that would be to introduce separate VECMAT and MATVEC encodings. Now that #15182 is merged, there are far fewer encoding enums than there used to be, so I don't see a problem anymore with having separate enums for separate linalg named ops.

  18. NatashaKnk commented on Oct 24, 2023

    @NatashaKnk
    Contributor

    An update -- it seems that the approach of encoding the vectors separately requires a large amount of ugly code, as the expansion has to happen in multiple places. It also results in some folders not working as expected. All in all, the approach to expand the vectors just seems cleaner and simpler. This difference is illustrated in 15257 vs 15273. I know @dcaballe had some reservations about this approach, so please let me know if you think the alternative (or any other approach) is better.

    Sorry this is taking a while to resolve :)

  19. hanhanW commented on Oct 24, 2023

    @hanhanW
    Contributor

    I generally +1 on reshape + matmul approach. If we want to generalize data tiling on contraction interface ops, we will likely insert reshape + transpose ops around contraction ops, and set encoding on matmul/batch_matmul ops.

    The next question would be how we transform the graph, and how we handle the fusion. We probably want to discuss it in a wider group. It is fine for matvec because there are no transpose ops involved. The reshape ops become metadata op at flow level, and it does not impact the fusion we have today.

  20. dcaballe commented on Oct 24, 2023

    @dcaballe
    ContributorAuthor

    If that's the easiest approach for DT that sounds good to me! We can always iterate as we see fit

  21. hanhanW commented on Oct 24, 2023

    @hanhanW
    Contributor

    Yes, it is. The strategy in my mind is keeping as less named ops as possible. I'm glad to see that we can limit it to matmul and batch_matmul cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegencodegen/llvm

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions