Skip to content

New mmt4d builtin with general code and some assembly kernels (i8mm, dotprod) - #10339

Merged
bjacob merged 3 commits into
iree-org:mainfrom
bjacob:mmt4d-kernels-dotprod-i8mm
Sep 15, 2022
Merged

bjacob merged 3 commits into
iree-org:mainfrom
bjacob:mmt4d-kernels-dotprod-i8mm

Conversation

@bjacob

@bjacob bjacob commented Sep 8, 2022 •

Copy link
Copy Markdown
Collaborator

OK, I wanted to slice this down further for review (I only split #10366), but that's proving a bit of work and I wonder if the context from having this all in one shot might actually be helpful to understand the design.

Anyway, the diff breaks down into these areas:

  • iree/modules/vmvx/module.c: The main change here is that iree_vmvx_mmt4d is un-typed i.e. the types become a runtime enum, de-duplicating the code between the two mmt4d functions there. This required some changes to the MAP_BUFFER_* macros, adding _UNTYPED flavors. The general idea is that when one only needs a type to get its sizeof, one may as well be taking the size and not the type.

  • iree/builtins/ukernel/ BUILD/CMake changes - there is a general refactoring there, independent of the mmt4d-specific impl changes. I've tried to suggest a structure that future builtin kernels might want to follow, but the existing elementwise stuff didn't fit well, it's special in multiple ways: it implements its own shims, and it only has generic not actual architecture-specific code yet.

  • iree/builtins/ukernel/{mmt4d.h, mmt4d_types.h} - Some general, cheap declarations are moved to a new header mmt4d_types.h which is what the internal implementation files want to #include, while mmt4d.h becomes the user-facing API header. This split avoids circular dependencies.

  • iree/builtins/ukernel/mmt4d.c - the general implementation of mmt4d. Start reading from the entry point and you'll see the idea. Pasting it here for convenience:

IREE_UKERNEL_EXPORT iree_ukernel_mmt4d_status_t
iree_ukernel_mmt4d(const iree_ukernel_mmt4d_params_t* params) {
  IREE_UKERNEL_MMT4D_RETURN_IF_ERROR(iree_ukernel_mmt4d_validate(params));
  iree_ukernel_mmt4d_tile_func_t tile_func;
  IREE_UKERNEL_MMT4D_RETURN_IF_ERROR(
      iree_ukernel_mmt4d_select_tile_func(params, &tile_func));
  iree_ukernel_mmt4d_using_tile_func(params, tile_func);
  return iree_ukernel_mmt4d_status_ok;
}

The the idea is: first we select a tile function pointer, which might be some ARM assembly or some generic slow code; then we call the universal implementation iree_ukernel_mmt4d_using_tile_func with it.

  • iree/builtins/ukernel/mmt4d_tile_generic.c - the generic tile implementation.

  • iree/builtins/ukernel/arm_64/ - the ARM64 tile implementations.


Design thoughts:

Compared to ruy, the indirect call into the optimized kernel occurs at finer granularity: only the innermost loop is implemented by the asm kernel, all loops above that are generic code. This means that compared to ruy, we are changing the trade-off a little more towards optimizing for code size, possibly away from aggressive performance optimization. It's partly due to my understanding that this new trade-off is a better match for what we're trying to build here, and partly having learned from ruy's experience going maybe a little too deep in its direction. Can elaborate on request but too much to say for a github PR description (@silvasean and I just had an hour-long rehash of these topics so it's refreshed in my memory).

@bjacob
bjacob force-pushed the mmt4d-kernels-dotprod-i8mm branch 3 times, most recently from 963f8e7 to 1896715 Compare September 12, 2022 03:23
bjacob added a commit that referenced this pull request Sep 13, 2022
@bjacob
bjacob force-pushed the mmt4d-kernels-dotprod-i8mm branch from 1896715 to 8f7b9e1 Compare September 13, 2022 03:08
@bjacob bjacob changed the title Initial builtin mmt4d kernels for dotprod and i8mm Flesh out mmt4d builtin with general code and some assembly kernels (i8mm, dotprod) Sep 13, 2022
@bjacob
bjacob marked this pull request as ready for review September 13, 2022 03:24
@bjacob
bjacob requested a review from benvanik as a code owner September 13, 2022 03:24
@bjacob
bjacob requested a review from silvasean September 13, 2022 03:35
@bjacob bjacob changed the title Flesh out mmt4d builtin with general code and some assembly kernels (i8mm, dotprod) New mmt4d builtin with general code and some assembly kernels (i8mm, dotprod) Sep 13, 2022
@bjacob
bjacob force-pushed the mmt4d-kernels-dotprod-i8mm branch from 8f7b9e1 to 3172e3f Compare September 13, 2022 13:52
@bjacob

bjacob commented Sep 13, 2022

Copy link
Copy Markdown
Collaborator Author

Next PR in the queue is #10389, fleshing out mmt4d_test.cc .

#else
(void)params;
#endif
return 0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: return NULL

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(resolved on discord - keeping 0 because NULL requires including some standard header and this is a place where even stdint.h is off-limits)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"even stdint.h is off-limits" - makes me so happy :)


#include "iree/builtins/ukernel/mmt4d_tile_generic.h"

// In order to be helpful as a reference for future architecture-specific

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is cool! often in reference implementations they're implemented so differently from the optimized ones that it's hard to correlate the two - this should be much easier for people to take and translate than needing to look at a different arch-specific implementation and translate that

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, this kind of meta issue is somewhat important here, the hope being that over time this will get ported by to other archs by other people.

@bjacob
bjacob enabled auto-merge (squash) September 15, 2022 16:43
@bjacob
bjacob merged commit a96dd9e into iree-org:main Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants