Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

7.1 KB
Raw Blame
#pragma once

#include <metal_stdlib>

#include "../../../generated/gemm.h"
#include "../../../generated/matmul.h"
#include "../../common/quant_unpack.h"

using namespace metal;

namespace uzu {
namespace gemm {
namespace operands {

template <GemmAPrologueKind PROLOGUE, typename Element, ushort ACTIVATION_GROUP_SIZE, bool CODES_GROUPED_BY_NIBBLE>
struct LeftOperand {
  UZU_CONST bool QUANTIZED = PROLOGUE == GemmAPrologueKind::Int8Symmetric;
  UZU_CONST bool PACKED_HALF = PROLOGUE == GemmAPrologueKind::PackedHalf;
  UZU_CONST ushort BITS = QUANTIZED ? 8 : 0;
  UZU_CONST ushort GROUP_SIZE = QUANTIZED ? ACTIVATION_GROUP_SIZE : 0;
  UZU_CONST bool GROUPED_BY_NIBBLE = CODES_GROUPED_BY_NIBBLE;
  static_assert(
      QUANTIZED == (ACTIVATION_GROUP_SIZE != 0),
      "activation group size must be present exactly for int8 activations"
  );

  using CodeElement = int8_t;
  using ScaleElement = float;
  using DenseElement = Element;
  using ElementType = metal::conditional_t<QUANTIZED, CodeElement, DenseElement>;
  template <ushort BLOCK_K>
  static constexpr ushort outer_block_k() {
    if constexpr (QUANTIZED) {
      static_assert(ACTIVATION_GROUP_SIZE % MXU_SIMDGROUP_BLOCK_K == 0, "activation groups must contain MMA chunks");
      return ACTIVATION_GROUP_SIZE;
    } else {
      return BLOCK_K;
    }
  }
};

template <GemmBPrologueKind PROLOGUE, ushort BITS_, ushort GROUP_SIZE_, typename Element>
struct RightOperand {
  UZU_CONST bool QUANTIZED = PROLOGUE != GemmBPrologueKind::FullPrecision;
  UZU_CONST ushort BITS = QUANTIZED ? BITS_ : 0;
  UZU_CONST ushort GROUP_SIZE = QUANTIZED ? GROUP_SIZE_ : 0;
  UZU_CONST GemmBPrologueKind SCHEME = PROLOGUE;
  UZU_CONST bool NEEDS_CORRECTION = PROLOGUE == GemmBPrologueKind::ScaleBiasDequant;

  UZU_CONST ushort CODE_ORIGIN =
      (!QUANTIZED || ((BITS_ == 2 || BITS_ == 4) && PROLOGUE != GemmBPrologueKind::ScaleSymmetricDequant))
          ? 0
          : ushort(1u << (BITS - 1));
  UZU_CONST ushort MMA_BLOCK_K = BITS_ == 2 ? 2 * MXU_SIMDGROUP_BLOCK_K : MXU_SIMDGROUP_BLOCK_K;

  static_assert(
      !QUANTIZED || BITS_ == 2 || BITS_ == 4 || BITS_ == 8,
      "quantized integer weights must use 2, 4 or 8 bits"
  );
  static_assert(!QUANTIZED || PROLOGUE != GemmBPrologueKind::FullPrecision, "quantized weights need a scheme");

  using CodeElement = int8_t;
  using ScaleElement = Element;
  using DenseElement = Element;
  using ElementType = DenseElement;
  template <ushort BLOCK_K>
  static constexpr ushort outer_block_k() {
    if constexpr (QUANTIZED) {
      static_assert(GROUP_SIZE_ % MMA_BLOCK_K == 0, "weight groups must contain complete MMA chunks");
      static_assert(BLOCK_K % GROUP_SIZE_ == 0, "tile block K must contain complete weight groups");
      return GROUP_SIZE_;
    } else {
      return BLOCK_K;
    }
  }
};

template <typename Left>
struct LeftStorage {
  const device typename Left::DenseElement* values;
  const device int8_t* codes;
  const device float* scales;
  const device int32_t* group_sums;
  const device uint* packed_half;

  METAL_FUNC const device int32_t* correction_sums() const thread {
    static_assert(Left::QUANTIZED, "correction_sums is only valid for int8 activations");
    return group_sums;
  }
};

template <typename Right>
struct RightStorage {
  const device typename Right::DenseElement* dense;
  const device uint8_t* codes;
  const device typename Right::ScaleElement* scales;
  const device typename Right::ScaleElement* biases;
  const device uint8_t* zero_points;
  bool signed_codes;
  bool hoist_row_metadata;
  GemmBLayout gemm_b_layout;

  METAL_FUNC uint sign_mask() const thread {
    return signed_codes ? (Right::BITS == 4 ? W4_SIGN_MASK : W8_SIGN_MASK) : 0u;
  }

  template <typename Index>
  METAL_FUNC const device uint8_t* row_codes(
      const Index row,
      const constant uzu::matmul::GemmParams* params
  ) const thread {
    const Index row_stride = Index(params->K) / (8 / Right::BITS);
    return codes + row * row_stride;
  }

  METAL_FUNC const device typename Right::ScaleElement* bias() const thread {
    static_assert(Right::SCHEME == GemmBPrologueKind::ScaleBiasDequant, "bias is only valid for ScaleBiasDequant");
    return biases;
  }

  METAL_FUNC const device uint8_t* zp() const thread {
    static_assert(
        Right::SCHEME == GemmBPrologueKind::ScaleZeroPointDequant,
        "zp is only valid for ScaleZeroPointDequant"
    );
    return zero_points;
  }

  template <typename Index>
  METAL_FUNC Index
  scale_index(const Index row, const Index group, const constant uzu::matmul::GemmParams* params) const thread {
    return row * Index(params->scale_output_stride) + group * Index(params->scale_group_stride);
  }

  METAL_FUNC typename Right::ScaleElement scale(
      const uint row,
      const uint group,
      const constant uzu::matmul::GemmParams* params
  ) const thread {
    return scales[scale_index(row, group, params)];
  }

  METAL_FUNC uint
  zero_point_index(const uint row, const uint group, const constant uzu::matmul::GemmParams* params) const thread {
    return row * params->zero_point_output_stride + group * params->zero_point_group_stride;
  }

  METAL_FUNC uint
  zero_point(const uint row, const uint group, const constant uzu::matmul::GemmParams* params) const thread {
    const uint index = zero_point_index(row, group, params);
    return decode_zero_point<Right::BITS>(zp() + index / ZERO_POINTS_PER_BYTE, index % ZERO_POINTS_PER_BYTE);
  }
};

template <typename Left, typename Element>
METAL_FUNC LeftStorage<Left> pack_left(
    const device Element* a,
    const device int8_t* codes,
    const device float* scales,
    const device int32_t* group_sums,
    const device uint* packed_half
) {
  if constexpr (Left::PACKED_HALF) {
    return {nullptr, nullptr, nullptr, nullptr, packed_half};
  } else if constexpr (Left::QUANTIZED) {
    return {nullptr, codes, scales, group_sums, nullptr};
  } else {
    return {a, nullptr, nullptr, nullptr, nullptr};
  }
}

template <typename Right, typename Element>
METAL_FUNC RightStorage<Right> pack_right(
    const device Element* dense,
    const device Element* scales,
    const device Element* biases,
    const device uint8_t* zero_points,
    const bool signed_codes,
    const bool hoist_row_metadata,
    const GemmBLayout gemm_b_layout
) {
  if constexpr (!Right::QUANTIZED) {
    return {dense, nullptr, nullptr, nullptr, nullptr, false, false, GemmBLayout::RowMajor};
  } else {
    return {
        nullptr,
        reinterpret_cast<const device uint8_t*>(dense),
        scales,
        Right::SCHEME == GemmBPrologueKind::ScaleBiasDequant ? biases : nullptr,
        Right::SCHEME == GemmBPrologueKind::ScaleZeroPointDequant ? zero_points : nullptr,
        signed_codes,
        hoist_row_metadata,
        gemm_b_layout
    };
  }
}

template <GemmAPrologueKind PROLOGUE, typename Element, ushort ACTIVATION_GROUP_SIZE, bool CODES_GROUPED_BY_NIBBLE>
using LeftOperandFor = LeftOperand<PROLOGUE, Element, ACTIVATION_GROUP_SIZE, CODES_GROUPED_BY_NIBBLE>;

template <GemmBPrologueKind PROLOGUE, ushort BITS, ushort GROUP_SIZE, typename Element>
using RightOperandFor = RightOperand<PROLOGUE, BITS, GROUP_SIZE, Element>;

} // namespace operands
} // namespace gemm
} // namespace uzu