Repository navigation
nir/divergence: treat cmat_muladd_agx as divergent #43
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
Open
joshuaswarren
wants to merge
2
commits into
omacom:integration/aurora
Choose a base branch
from
joshuaswarren:w7k/cmat-muladd-divergent
base: integration/aurora
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| #include "agx_compile.h" | ||
| extern "C" { | ||
| #include "agx_nir.h" | ||
| } | ||
| #include "compiler/glsl_types.h" | ||
| #include "nir.h" | ||
| #include "nir_builder.h" | ||
| #include <gtest/gtest.h> | ||
|
|
||
| static const glsl_type * | ||
| cmat_type(enum glsl_base_type element, enum glsl_cmat_use use) | ||
| { | ||
| struct glsl_cmat_description desc = { | ||
| .rows = 16, | ||
| .cols = 16, | ||
| .element_type = (uint16_t)element, | ||
| .scope = SCOPE_SUBGROUP, | ||
| .use = (uint8_t)use, | ||
| }; | ||
| return glsl_cmat_type(&desc); | ||
| } | ||
|
|
||
| static nir_deref_instr * | ||
| cmat_var(nir_builder *b, enum glsl_base_type element, enum glsl_cmat_use use, | ||
| const char *name) | ||
| { | ||
| return nir_build_deref_var( | ||
| b, nir_local_variable_create(b->impl, cmat_type(element, use), name)); | ||
| } | ||
|
|
||
| /* A 16x16x16 multiply-add of half inputs with the given accumulator type, as | ||
| * the flash attention shaders of llama.cpp write it for the PV product. | ||
| */ | ||
| static unsigned | ||
| lowered_muladd_count(enum glsl_base_type accum) | ||
| { | ||
| nir_builder b = nir_builder_init_simple_shader( | ||
| MESA_SHADER_COMPUTE, &agx_nir_options, "cmat"); | ||
| b.shader->info.cs.has_cooperative_matrix = true; | ||
|
|
||
| nir_deref_instr *a = cmat_var(&b, GLSL_TYPE_FLOAT16, GLSL_CMAT_USE_A, "a"); | ||
| nir_deref_instr *bb = cmat_var(&b, GLSL_TYPE_FLOAT16, GLSL_CMAT_USE_B, "b"); | ||
| nir_deref_instr *c = cmat_var(&b, accum, GLSL_CMAT_USE_ACCUMULATOR, "c"); | ||
| nir_deref_instr *d = cmat_var(&b, accum, GLSL_CMAT_USE_ACCUMULATOR, "d"); | ||
|
|
||
| nir_cmat_construct(&b, &a->def, nir_imm_float16(&b, 1.0f)); | ||
| nir_cmat_construct(&b, &bb->def, nir_imm_float16(&b, 1.0f)); | ||
| nir_cmat_construct(&b, &c->def, | ||
| accum == GLSL_TYPE_FLOAT16 ? nir_imm_float16(&b, 0.0f) | ||
| : nir_imm_float(&b, 0.0f)); | ||
| nir_cmat_muladd(&b, &d->def, &a->def, &bb->def, &c->def); | ||
|
|
||
| agx_nir_lower_cmat(b.shader); | ||
| nir_validate_shader(b.shader, "after agx_nir_lower_cmat"); | ||
| nir_divergence_analysis(b.shader); | ||
|
|
||
| unsigned left = 0, agx = 0; | ||
| bool f16_store = false; | ||
| nir_foreach_function_impl(impl, b.shader) { | ||
| nir_foreach_block(block, impl) { | ||
| nir_foreach_instr(instr, block) { | ||
| if (instr->type == nir_instr_type_intrinsic) { | ||
| nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); | ||
| left += intr->intrinsic == nir_intrinsic_cmat_muladd; | ||
| /* The hardware accumulates in 32 bits whatever the matrix type, | ||
| * and every lane holds its own part of the result. | ||
| */ | ||
| agx += intr->intrinsic == nir_intrinsic_cmat_muladd_agx && | ||
| intr->src[2].ssa->bit_size == 32 && intr->def.divergent; | ||
| } else if (instr->type == nir_instr_type_alu) { | ||
| nir_alu_instr *alu = nir_instr_as_alu(instr); | ||
| if (alu->op == nir_op_f2f16) | ||
| f16_store = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ralloc_free(b.shader); | ||
| if (left || !agx) | ||
| return 0; | ||
| if (accum == GLSL_TYPE_FLOAT16 && !f16_store) | ||
| return 0; | ||
| return agx; | ||
| } | ||
|
|
||
| class CmatLower : public ::testing::Test { | ||
| protected: | ||
| CmatLower() { glsl_type_singleton_init_or_ref(); } | ||
| ~CmatLower() { glsl_type_singleton_decref(); } | ||
| }; | ||
|
|
||
| TEST_F(CmatLower, HalfInputsFloatAccumulator) | ||
| { | ||
| EXPECT_EQ(lowered_muladd_count(GLSL_TYPE_FLOAT), 1u); | ||
| } | ||
|
|
||
| TEST_F(CmatLower, HalfInputsHalfAccumulator) | ||
| { | ||
| EXPECT_EQ(lowered_muladd_count(GLSL_TYPE_FLOAT16), 1u); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
glsl_type_singleton_init_or_ref()at line 41 has no matchingglsl_type_singleton_decref(). Please add one afterralloc_free(b.shader), as the other asahi tests do.— Marcelo's agent