mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-21 18:59:21 +00:00
tint: add missing F16 conversion expression support
This CL add missing type conversions for f16, especially for SPIRV backend which require special handling. A transform, VectorizeMatrixConversions, are also added for SPIRV to replace a matrix conversion to a matrix construction with converted column vectors. Unittests for the transform and SPIRV writer, and end-to-end tests for all conversion rules are added. Bug: tint:1473, tint:1502, chromium:1356215 Change-Id: Iaff125e5dd295d35c4ab74757eb56b642802a51a Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/100483 Reviewed-by: David Neto <dneto@google.com> Commit-Queue: Zhaoming Jiang <zhaoming.jiang@intel.com> Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
committed by
Dawn LUCI CQ
parent
0df4e4aea3
commit
426b47e481
136
src/tint/transform/vectorize_matrix_conversions.cc
Normal file
136
src/tint/transform/vectorize_matrix_conversions.cc
Normal file
@@ -0,0 +1,136 @@
|
||||
// Copyright 2022 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "src/tint/transform/vectorize_matrix_conversions.h"
|
||||
|
||||
#include <tuple>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#include "src/tint/program_builder.h"
|
||||
#include "src/tint/sem/abstract_numeric.h"
|
||||
#include "src/tint/sem/call.h"
|
||||
#include "src/tint/sem/expression.h"
|
||||
#include "src/tint/sem/type_conversion.h"
|
||||
#include "src/tint/utils/hash.h"
|
||||
#include "src/tint/utils/map.h"
|
||||
|
||||
TINT_INSTANTIATE_TYPEINFO(tint::transform::VectorizeMatrixConversions);
|
||||
|
||||
namespace tint::transform {
|
||||
|
||||
VectorizeMatrixConversions::VectorizeMatrixConversions() = default;
|
||||
|
||||
VectorizeMatrixConversions::~VectorizeMatrixConversions() = default;
|
||||
|
||||
bool VectorizeMatrixConversions::ShouldRun(const Program* program, const DataMap&) const {
|
||||
for (auto* node : program->ASTNodes().Objects()) {
|
||||
if (auto* sem = program->Sem().Get<sem::Expression>(node)) {
|
||||
if (auto* call = sem->UnwrapMaterialize()->As<sem::Call>()) {
|
||||
if (call->Target()->Is<sem::TypeConversion>() && call->Type()->Is<sem::Matrix>()) {
|
||||
auto& args = call->Arguments();
|
||||
if (args.Length() == 1 && args[0]->Type()->UnwrapRef()->is_float_matrix()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void VectorizeMatrixConversions::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
|
||||
using HelperFunctionKey =
|
||||
utils::UnorderedKeyWrapper<std::tuple<const sem::Matrix*, const sem::Matrix*>>;
|
||||
|
||||
std::unordered_map<HelperFunctionKey, Symbol> matrix_convs;
|
||||
|
||||
ctx.ReplaceAll([&](const ast::CallExpression* expr) -> const ast::CallExpression* {
|
||||
auto* call = ctx.src->Sem().Get(expr)->UnwrapMaterialize()->As<sem::Call>();
|
||||
auto* ty_conv = call->Target()->As<sem::TypeConversion>();
|
||||
if (!ty_conv) {
|
||||
return nullptr;
|
||||
}
|
||||
auto* dst_type = call->Type()->As<sem::Matrix>();
|
||||
if (!dst_type) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto& args = call->Arguments();
|
||||
if (args.Length() != 1) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto& src = args[0];
|
||||
|
||||
auto* src_type = args[0]->Type()->UnwrapRef()->As<sem::Matrix>();
|
||||
if (!src_type) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// The source and destination type of a matrix conversion must have a same shape.
|
||||
if (!(src_type->rows() == dst_type->rows() && src_type->columns() == dst_type->columns())) {
|
||||
TINT_ICE(Transform, ctx.dst->Diagnostics())
|
||||
<< "source and destination matrix has different shape in matrix conversion";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto build_vectorized_conversion_expression = [&](auto&& src_expression_builder) {
|
||||
utils::Vector<const ast::Expression*, 4> columns;
|
||||
for (uint32_t c = 0; c < dst_type->columns(); c++) {
|
||||
auto* src_matrix_expr = src_expression_builder();
|
||||
auto* src_column_expr =
|
||||
ctx.dst->IndexAccessor(src_matrix_expr, ctx.dst->Expr(tint::AInt(c)));
|
||||
columns.Push(ctx.dst->Construct(CreateASTTypeFor(ctx, dst_type->ColumnType()),
|
||||
src_column_expr));
|
||||
}
|
||||
return ctx.dst->Construct(CreateASTTypeFor(ctx, dst_type), columns);
|
||||
};
|
||||
|
||||
// Replace the matrix conversion to column vector conversions and a matrix construction.
|
||||
if (!src->HasSideEffects()) {
|
||||
// Simply use the argument's declaration if it has no side effects.
|
||||
return build_vectorized_conversion_expression([&]() { //
|
||||
return ctx.Clone(src->Declaration());
|
||||
});
|
||||
} else {
|
||||
// If has side effects, use a helper function.
|
||||
auto fn =
|
||||
utils::GetOrCreate(matrix_convs, HelperFunctionKey{{src_type, dst_type}}, [&] {
|
||||
auto name =
|
||||
ctx.dst->Symbols().New("convert_mat" + std::to_string(src_type->columns()) +
|
||||
"x" + std::to_string(src_type->rows()) + "_" +
|
||||
ctx.dst->FriendlyName(src_type->type()) + "_" +
|
||||
ctx.dst->FriendlyName(dst_type->type()));
|
||||
ctx.dst->Func(
|
||||
name,
|
||||
utils::Vector{
|
||||
ctx.dst->Param("value", CreateASTTypeFor(ctx, src_type)),
|
||||
},
|
||||
CreateASTTypeFor(ctx, dst_type),
|
||||
utils::Vector{
|
||||
ctx.dst->Return(build_vectorized_conversion_expression([&]() { //
|
||||
return ctx.dst->Expr("value");
|
||||
})),
|
||||
});
|
||||
return name;
|
||||
});
|
||||
return ctx.dst->Call(fn, ctx.Clone(args[0]->Declaration()));
|
||||
}
|
||||
});
|
||||
|
||||
ctx.Clone();
|
||||
}
|
||||
|
||||
} // namespace tint::transform
|
||||
48
src/tint/transform/vectorize_matrix_conversions.h
Normal file
48
src/tint/transform/vectorize_matrix_conversions.h
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright 2022 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_TINT_TRANSFORM_VECTORIZE_MATRIX_CONVERSIONS_H_
|
||||
#define SRC_TINT_TRANSFORM_VECTORIZE_MATRIX_CONVERSIONS_H_
|
||||
|
||||
#include "src/tint/transform/transform.h"
|
||||
|
||||
namespace tint::transform {
|
||||
|
||||
/// A transform that converts matrix conversions (between f32 and f16 matrices) to the vector form.
|
||||
class VectorizeMatrixConversions final : public Castable<VectorizeMatrixConversions, Transform> {
|
||||
public:
|
||||
/// Constructor
|
||||
VectorizeMatrixConversions();
|
||||
|
||||
/// Destructor
|
||||
~VectorizeMatrixConversions() override;
|
||||
|
||||
/// @param program the program to inspect
|
||||
/// @param data optional extra transform-specific input data
|
||||
/// @returns true if this transform should be run for the given program
|
||||
bool ShouldRun(const Program* program, const DataMap& data = {}) const override;
|
||||
|
||||
protected:
|
||||
/// Runs the transform using the CloneContext built for transforming a
|
||||
/// program. Run() is responsible for calling Clone() on the CloneContext.
|
||||
/// @param ctx the CloneContext primed with the input program and
|
||||
/// ProgramBuilder
|
||||
/// @param inputs optional extra transform-specific input data
|
||||
/// @param outputs optional extra transform-specific output data
|
||||
void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs) const override;
|
||||
};
|
||||
|
||||
} // namespace tint::transform
|
||||
|
||||
#endif // SRC_TINT_TRANSFORM_VECTORIZE_MATRIX_CONVERSIONS_H_
|
||||
411
src/tint/transform/vectorize_matrix_conversions_test.cc
Normal file
411
src/tint/transform/vectorize_matrix_conversions_test.cc
Normal file
@@ -0,0 +1,411 @@
|
||||
// Copyright 2022 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "src/tint/transform/vectorize_matrix_conversions.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "src/tint/transform/test_helper.h"
|
||||
#include "src/tint/utils/string.h"
|
||||
|
||||
namespace tint::transform {
|
||||
namespace {
|
||||
|
||||
using VectorizeMatrixConversionsTest = TransformTestWithParam<std::pair<uint32_t, uint32_t>>;
|
||||
|
||||
TEST_F(VectorizeMatrixConversionsTest, ShouldRunEmptyModule) {
|
||||
auto* src = R"()";
|
||||
|
||||
EXPECT_FALSE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transforms the matRxC<f32> to matRxC<f16> conversion as
|
||||
// expected.
|
||||
//
|
||||
// Example input:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m = mat3x2<f32>(vec2<f32>(0.0, 1.0), vec2<f32>(2.0, 3.0), vec2<f32>(4.0, 5.0));
|
||||
// let n : mat3x2<f16> = mat3x2<f16>(m);
|
||||
// }
|
||||
//
|
||||
// Example output:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m = mat3x2<f32>(vec2<f32>(0.0, 1.0), vec2<f32>(2.0, 3.0), vec2<f32>(4.0, 5.0));
|
||||
// let n : mat3x2<f16> = mat3x2<f16>(vec2<f16>(m[0]), vec2<f16>(m[1]), vec2<f16>(m[2]));
|
||||
// }
|
||||
TEST_P(VectorizeMatrixConversionsTest, Conversion_F32ToF16) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string src_mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f32>";
|
||||
std::string src_vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string dst_mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f16>";
|
||||
std::string dst_vec_type = "vec" + std::to_string(rows) + "<f16>";
|
||||
std::string vector_values;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
vector_values += ", ";
|
||||
}
|
||||
vector_values += src_vec_type + "(";
|
||||
for (uint32_t r = 0; r < rows; r++) {
|
||||
if (r > 0) {
|
||||
vector_values += ", ";
|
||||
}
|
||||
auto value = std::to_string(c * rows + r) + ".0";
|
||||
vector_values += value;
|
||||
}
|
||||
vector_values += ")";
|
||||
}
|
||||
|
||||
std::string vectorized_args = "";
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
vectorized_args += ", ";
|
||||
}
|
||||
vectorized_args += dst_vec_type + "(m[" + std::to_string(c) + "])";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
enable f16;
|
||||
|
||||
@fragment
|
||||
fn main() {
|
||||
let m = ${src_mat_type}(${values});
|
||||
let n : ${dst_mat_type} = ${dst_mat_type}(${args});
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${src_mat_type}", src_mat_type);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${dst_mat_type}", dst_mat_type);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${values}", vector_values);
|
||||
auto src = utils::ReplaceAll(tmpl, "${args}", "m");
|
||||
auto expect = utils::ReplaceAll(tmpl, "${args}", vectorized_args);
|
||||
|
||||
EXPECT_TRUE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transforms the matRxC<f32> to matRxC<f16> conversion as
|
||||
// expected.
|
||||
//
|
||||
// Example input:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m = mat3x2<f16>(vec2<f16>(0.0, 1.0), vec2<f16>(2.0, 3.0), vec2<f16>(4.0, 5.0));
|
||||
// let n : mat3x2<f32> = mat3x2<f32>(m);
|
||||
// }
|
||||
//
|
||||
// Example output:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m = mat3x2<f16>(vec2<f16>(0.0, 1.0), vec2<f16>(2.0, 3.0), vec2<f16>(4.0, 5.0));
|
||||
// let n : mat3x2<f32> = mat3x2<f32>(vec2<f32>(m[0]), vec2<f32>(m[1]), vec2<f32>(m[2]));
|
||||
// }
|
||||
TEST_P(VectorizeMatrixConversionsTest, Conversion_F16ToF32) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string src_mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f16>";
|
||||
std::string src_vec_type = "vec" + std::to_string(rows) + "<f16>";
|
||||
std::string dst_mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f32>";
|
||||
std::string dst_vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string vector_values;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
vector_values += ", ";
|
||||
}
|
||||
vector_values += src_vec_type + "(";
|
||||
for (uint32_t r = 0; r < rows; r++) {
|
||||
if (r > 0) {
|
||||
vector_values += ", ";
|
||||
}
|
||||
auto value = std::to_string(c * rows + r) + ".0";
|
||||
vector_values += value;
|
||||
}
|
||||
vector_values += ")";
|
||||
}
|
||||
|
||||
std::string vectorized_args = "";
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
vectorized_args += ", ";
|
||||
}
|
||||
vectorized_args += dst_vec_type + "(m[" + std::to_string(c) + "])";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
enable f16;
|
||||
|
||||
@fragment
|
||||
fn main() {
|
||||
let m = ${src_mat_type}(${values});
|
||||
let n : ${dst_mat_type} = ${dst_mat_type}(${args});
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${src_mat_type}", src_mat_type);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${dst_mat_type}", dst_mat_type);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${values}", vector_values);
|
||||
auto src = utils::ReplaceAll(tmpl, "${args}", "m");
|
||||
auto expect = utils::ReplaceAll(tmpl, "${args}", vectorized_args);
|
||||
|
||||
EXPECT_TRUE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transform generates help functions for conversions of which
|
||||
// input expression has side effect.
|
||||
//
|
||||
// Example input:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// var<private> i : i32 = 0;
|
||||
//
|
||||
// fn mat_f32() -> mat2x2<f32> {
|
||||
// i = (i + 1);
|
||||
// return mat2x2<f32>(vec2<f32>(f32(i), f32(i)), vec2<f32>(f32(i), f32(i)));
|
||||
// }
|
||||
//
|
||||
// fn mat_f16() -> mat2x2<f16> {
|
||||
// i = (i + 1);
|
||||
// return mat2x2<f16>(vec2<f16>(f16(i), f16(i)), vec2<f16>(f16(i), f16(i)));
|
||||
// }
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m32 : mat2x2<f32> = mat2x2<f32>(mat_f16());
|
||||
// let m16 : mat2x2<f16> = mat2x2<f16>(mat_f32());
|
||||
// }
|
||||
//
|
||||
// Example output:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// var<private> i : i32 = 0;
|
||||
//
|
||||
// fn mat_f32() -> mat2x2<f32> {
|
||||
// i = (i + 1);
|
||||
// return mat2x2<f32>(vec2<f32>(f32(i), f32(i)), vec2<f32>(f32(i), f32(i)));
|
||||
// }
|
||||
//
|
||||
// fn mat_f16() -> mat2x2<f16> {
|
||||
// i = (i + 1);
|
||||
// return mat2x2<f16>(vec2<f16>(f16(i), f16(i)), vec2<f16>(f16(i), f16(i)));
|
||||
// }
|
||||
//
|
||||
// fn convert_mat2x2_f16_f32(value : mat2x2<f16>) -> mat2x2<f32> {
|
||||
// return mat2x2<f32>(vec2<f32>(value[0]), vec2<f32>(value[1]));
|
||||
// }
|
||||
//
|
||||
// fn convert_mat2x2_f32_f16(value : mat2x2<f32>) -> mat2x2<f16> {
|
||||
// return mat2x2<f16>(vec2<f16>(value[0]), vec2<f16>(value[1]));
|
||||
// }
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m32 : mat2x2<f32> = convert_mat2x2_f16_f32(mat_f16());
|
||||
// let m16 : mat2x2<f16> = convert_mat2x2_f32_f16(mat_f32());
|
||||
// }
|
||||
TEST_P(VectorizeMatrixConversionsTest, Conversion_WithSideEffect) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string mat_shape = "mat" + std::to_string(cols) + "x" + std::to_string(rows);
|
||||
std::string f32_mat_type = mat_shape + "<f32>";
|
||||
std::string f32_vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string f16_mat_type = mat_shape + "<f16>";
|
||||
std::string f16_vec_type = "vec" + std::to_string(rows) + "<f16>";
|
||||
std::string f32_vector_values;
|
||||
std::string f16_vector_values;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
f32_vector_values += ", ";
|
||||
f16_vector_values += ", ";
|
||||
}
|
||||
f32_vector_values += f32_vec_type + "(";
|
||||
f16_vector_values += f16_vec_type + "(";
|
||||
for (uint32_t r = 0; r < rows; r++) {
|
||||
if (r > 0) {
|
||||
f32_vector_values += ", ";
|
||||
f16_vector_values += ", ";
|
||||
}
|
||||
f32_vector_values += "f32(i)";
|
||||
f16_vector_values += "f16(i)";
|
||||
}
|
||||
f32_vector_values += ")";
|
||||
f16_vector_values += ")";
|
||||
}
|
||||
|
||||
std::string f32_vectorized_args = "";
|
||||
std::string f16_vectorized_args = "";
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
f32_vectorized_args += ", ";
|
||||
f16_vectorized_args += ", ";
|
||||
}
|
||||
f32_vectorized_args += f32_vec_type + "(value[" + std::to_string(c) + "])";
|
||||
f16_vectorized_args += f16_vec_type + "(value[" + std::to_string(c) + "])";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
enable f16;
|
||||
|
||||
var<private> i : i32 = 0;
|
||||
|
||||
fn mat_f32() -> ${f32_mat_type} {
|
||||
i = (i + 1);
|
||||
return ${f32_mat_type}(${f32_values});
|
||||
}
|
||||
|
||||
fn mat_f16() -> ${f16_mat_type} {
|
||||
i = (i + 1);
|
||||
return ${f16_mat_type}(${f16_values});
|
||||
}
|
||||
${helper_function}
|
||||
@fragment
|
||||
fn main() {
|
||||
let m32 : ${f32_mat_type} = ${f32_matrix_conversion};
|
||||
let m16 : ${f16_mat_type} = ${f16_matrix_conversion};
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${f32_values}", f32_vector_values);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${f16_values}", f16_vector_values);
|
||||
auto src = utils::ReplaceAll(tmpl, "${f32_matrix_conversion}", "${f32_mat_type}(mat_f16())");
|
||||
src = utils::ReplaceAll(src, "${f16_matrix_conversion}", "${f16_mat_type}(mat_f32())");
|
||||
src = utils::ReplaceAll(src, "${helper_function}", "");
|
||||
src = utils::ReplaceAll(src, "${f32_mat_type}", f32_mat_type);
|
||||
src = utils::ReplaceAll(src, "${f16_mat_type}", f16_mat_type);
|
||||
|
||||
auto helper_function = std::string(R"(
|
||||
fn convert_${mat_shape}_f16_f32(value : ${f16_mat_type}) -> ${f32_mat_type} {
|
||||
return ${f32_mat_type}(${f32_vectorized_args});
|
||||
}
|
||||
|
||||
fn convert_${mat_shape}_f32_f16(value : ${f32_mat_type}) -> ${f16_mat_type} {
|
||||
return ${f16_mat_type}(${f16_vectorized_args});
|
||||
}
|
||||
)");
|
||||
auto expect = utils::ReplaceAll(tmpl, "${helper_function}", helper_function);
|
||||
expect = utils::ReplaceAll(expect, "${f32_mat_type}", f32_mat_type);
|
||||
expect = utils::ReplaceAll(expect, "${f16_mat_type}", f16_mat_type);
|
||||
expect = utils::ReplaceAll(expect, "${f32_matrix_conversion}",
|
||||
"convert_${mat_shape}_f16_f32(mat_f16())");
|
||||
expect = utils::ReplaceAll(expect, "${f16_matrix_conversion}",
|
||||
"convert_${mat_shape}_f32_f16(mat_f32())");
|
||||
expect = utils::ReplaceAll(expect, "${mat_shape}", mat_shape);
|
||||
expect = utils::ReplaceAll(expect, "${f32_vectorized_args}", f32_vectorized_args);
|
||||
expect = utils::ReplaceAll(expect, "${f16_vectorized_args}", f16_vectorized_args);
|
||||
|
||||
EXPECT_TRUE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transform will not run for matrix constructor.
|
||||
TEST_P(VectorizeMatrixConversionsTest, NonConversion_ConstructorFromVectors) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f32>";
|
||||
std::string vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string columns;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
columns += ", ";
|
||||
}
|
||||
columns += vec_type + "()";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
@fragment
|
||||
fn main() {
|
||||
let m = ${matrix}(${columns});
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${matrix}", mat_type);
|
||||
auto src = utils::ReplaceAll(tmpl, "${columns}", columns);
|
||||
auto expect = src;
|
||||
|
||||
EXPECT_FALSE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transform will not run for identity matrix constructor,
|
||||
// which also take a single matrix as input.
|
||||
TEST_P(VectorizeMatrixConversionsTest, NonConversion_IdentityConstructor) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f32>";
|
||||
std::string vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string columns;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
columns += ", ";
|
||||
}
|
||||
columns += vec_type + "()";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
@fragment
|
||||
fn main() {
|
||||
let m = ${matrix}(${columns});
|
||||
let n : ${matrix} = ${matrix}(m);
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${matrix}", mat_type);
|
||||
auto src = utils::ReplaceAll(tmpl, "${columns}", columns);
|
||||
auto expect = src;
|
||||
|
||||
EXPECT_FALSE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(VectorizeMatrixConversionsTest,
|
||||
VectorizeMatrixConversionsTest,
|
||||
testing::Values(std::make_pair(2, 2),
|
||||
std::make_pair(2, 3),
|
||||
std::make_pair(2, 4),
|
||||
std::make_pair(3, 2),
|
||||
std::make_pair(3, 3),
|
||||
std::make_pair(3, 4),
|
||||
std::make_pair(4, 2),
|
||||
std::make_pair(4, 3),
|
||||
std::make_pair(4, 4)));
|
||||
|
||||
} // namespace
|
||||
} // namespace tint::transform
|
||||
Reference in New Issue
Block a user