Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for unpadded shapes in Matmul1D w/ gather_in0 #16627

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

avoraTT
Copy link
Contributor

@avoraTT avoraTT commented Jan 10, 2025

Ticket

Problem description

In the current use case of Matmul1D with gather_in0 in the Llama models, the activations and weights need to be padded. This results in significant overhead.

What's changed

To support unpadded shapes, the implementation leverages the implicit padding information that can be stored in the sharding spec. For example, a tensor might be of shape [1, 1, 32, 2048] and we want to shard it over 24 cores. Since K = 2048 doesn't divide with 24 cores, we can pad the shape to K_pad = 2304, while keeping the actual tensor shape the same. Then, the shapes in the sharding spec can be derived using K_pad, instead of K.

Since the matmul block sizes are derived using the sharding spec, the kernels implicitly process the padding shape, where some pages at the end of the inner dim (for activations) contain garbage data. Note, if the activations are being implicitly padded, to make sure the the output is correct when implicitly padding the inner dim, the inner dim for the weights must be explicitly padded with 0's. This is to ensure that when accumulating partials, the garbage/padded part of the activations get multiplied with 0's before summation.

Example for matmul on 24 cores
Original shapes

  • activations: [1, 1, 32, 2048]
  • weights: [1, 1, 2048, 3584]

Actual tensors passed into the op

  • activations: [1, 1, 32, 2048], with shard shape using padding: [32, 2304 / 24]
  • weights: [1, 1, 2304, 3584], with shard shape using padding: [2304, 3840 / 24]
  • output: (shape internally derived by matmul) [1, 1, 32, 3584], with shard shape using padding: [32, 3840 / 24]

Other changes include moving validation code in matmul to support different K's for the activation vs weights when gather_in0 = True.

Checklist

  • Post commit CI passes
  • Blackhole Post commit (if applicable)
  • Model regression CI testing passes (if applicable)
  • Device performance regression CI testing passes (if applicable)
  • (For models and ops writers) Full new models tests passes
  • New/Existing tests provide coverage for changes

@avoraTT avoraTT added the metal tt-metal issue label Jan 10, 2025
@avoraTT avoraTT self-assigned this Jan 10, 2025
@avoraTT avoraTT marked this pull request as ready for review January 10, 2025 21:38
@@ -32,7 +32,7 @@ void DramPrefetcher::validate(const std::vector<Tensor>& input_tensors) const {

// Check that all tensors' k is divisible by number of cores in global CB receiver
TT_FATAL(
tensor.get_legacy_shape()[1] % num_receiver_cores == 0,
tensor.get_legacy_shape()[0] % num_receiver_cores == 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

why is it changed from 1 to 0 (from K to M)?

@@ -1357,11 +1357,6 @@ void Matmul::validate(
TT_FATAL(
(input_tensor_a.get_layout() == Layout::TILE && input_tensor_b.get_layout() == Layout::TILE),
"Inputs to matmul must be tilized");
TT_FATAL(
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this is a redundant check and there's other places that check for a_shape[-1] == b_shape[-2] ?

@@ -447,7 +456,7 @@ def test_multi_core_matmul_1d_wh(
"num_iters",
[1, 3],
)
@pytest.mark.parametrize("device_params", [{"dispatch_core_axis": ttnn.DispatchCoreAxis.COL}], indirect=True)
# @pytest.mark.parametrize("device_params", [{"dispatch_core_axis": ttnn.DispatchCoreAxis.COL}], indirect=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

commented out?

@@ -1414,6 +1409,27 @@ void Matmul::validate(

MatmulProgramConfig chosen_program_config = get_program_config(input_tensor_a, input_tensor_b, this);

bool k_n_pad = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

is this flag essentially program_config.gather_in0? might be confusing for people without context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metal tt-metal issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants