Commit 9130aee8 authored by Umang Yadav's avatar Umang Yadav
Browse files

add nested converts Pass

parent 4315a991
...@@ -50,6 +50,7 @@ add_library(migraphx ...@@ -50,6 +50,7 @@ add_library(migraphx
eliminate_contiguous.cpp eliminate_contiguous.cpp
eliminate_data_type.cpp eliminate_data_type.cpp
eliminate_identity.cpp eliminate_identity.cpp
eliminate_nested_converts.cpp
eliminate_pad.cpp eliminate_pad.cpp
env.cpp env.cpp
file_buffer.cpp file_buffer.cpp
......
...@@ -120,22 +120,6 @@ void eliminate_data_type::apply(module& m) const ...@@ -120,22 +120,6 @@ void eliminate_data_type::apply(module& m) const
if(contains(unsupported_ops, "all") or contains(unsupported_ops, ins->name())) if(contains(unsupported_ops, "all") or contains(unsupported_ops, ins->name()))
insert_convert_to_supported_type(m, ins, target_type, unsupported_types); insert_convert_to_supported_type(m, ins, target_type, unsupported_types);
} }
// remove nested converts
for(auto ins : iterator_for(m))
{
if(ins->name() == "convert")
{
auto convert_input = ins->inputs().front();
while(convert_input->name() == "convert")
{
convert_input = convert_input->inputs().front();
}
if(convert_input->get_shape() == ins->get_shape())
{
m.replace_instruction(ins, convert_input);
}
}
}
} }
} // namespace MIGRAPHX_INLINE_NS } // namespace MIGRAPHX_INLINE_NS
......
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <iterator>
#include <migraphx/eliminate_nested_converts.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/op/as_shape.hpp>
#include <migraphx/op/transpose.hpp>
#include <migraphx/op/concat.hpp>
#include <migraphx/op/slice.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/matcher.hpp>
#include <migraphx/permutation.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <unordered_set>
#include <migraphx/make_op.hpp>
#include <migraphx/tune_axis.hpp>
#include <map>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct find_nested_convert
{
auto matcher() const { return match::name("convert")(match::arg(0)(match::name("convert"))); }
void apply(module& m, const match::matcher_result& mr) const
{
auto ins = mr.result;
auto x = ins->inputs().front();
auto input = x->inputs().front();
while(input->name() == "convert")
{
input = input->inputs().front();
}
if(ins->get_shape() != input->get_shape())
return;
m.replace_instruction(ins, input);
}
};
void eliminate_nested_converts::apply(module& m) const
{
match::find_matches(m, find_nested_convert{});
dead_code_elimination{}.apply(m);
}
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MIGRAPHX_GUARD_RTGLIB_ELIMINATE_NESTED_CONVERTS_HPP
#define MIGRAPHX_GUARD_RTGLIB_ELIMINATE_NESTED_CONVERTS_HPP
#include <string>
#include <migraphx/instruction_ref.hpp>
#include <migraphx/config.hpp>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct module;
/**
* Eliminate nested converts
*/
struct MIGRAPHX_EXPORT eliminate_nested_converts
{
std::string name() const { return "eliminate_nested_converts"; }
void apply(module& m) const;
};
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <migraphx/simplify_reshapes.hpp> #include <migraphx/simplify_reshapes.hpp>
#include <migraphx/simplify_algebra.hpp> #include <migraphx/simplify_algebra.hpp>
#include <migraphx/eliminate_common_subexpression.hpp> #include <migraphx/eliminate_common_subexpression.hpp>
#include <migraphx/eliminate_nested_converts.hpp>
#include <migraphx/dead_code_elimination.hpp> #include <migraphx/dead_code_elimination.hpp>
#include <migraphx/propagate_constant.hpp> #include <migraphx/propagate_constant.hpp>
...@@ -42,6 +43,7 @@ void optimize_module::apply(module_pass_manager& mpm) const ...@@ -42,6 +43,7 @@ void optimize_module::apply(module_pass_manager& mpm) const
mpm.run_pass(simplify_reshapes{}); mpm.run_pass(simplify_reshapes{});
mpm.run_pass(simplify_algebra{}); mpm.run_pass(simplify_algebra{});
} }
mpm.run_pass(eliminate_nested_converts{});
mpm.run_pass(eliminate_common_subexpression{}); mpm.run_pass(eliminate_common_subexpression{});
mpm.run_pass(dead_code_elimination{}); mpm.run_pass(dead_code_elimination{});
mpm.run_pass(propagate_constant{}); mpm.run_pass(propagate_constant{});
......
...@@ -173,28 +173,6 @@ struct find_transpose ...@@ -173,28 +173,6 @@ struct find_transpose
} }
}; };
struct find_nested_convert
{
auto matcher() const { return match::name("convert")(match::arg(0)(match::name("convert"))); }
void apply(module& m, const match::matcher_result& mr) const
{
auto ins = mr.result;
auto x = ins->inputs().front();
auto input = x->inputs().front();
while(input->name() == "convert")
{
input = input->inputs().front();
}
if(ins->get_shape() != input->get_shape())
return;
m.replace_instruction(ins, input);
}
};
struct find_nested_slice struct find_nested_slice
{ {
auto matcher() const { return match::name("slice")(match::arg(0)(match::name("slice"))); } auto matcher() const { return match::name("slice")(match::arg(0)(match::name("slice"))); }
...@@ -841,7 +819,6 @@ void simplify_reshapes::apply(module& m) const ...@@ -841,7 +819,6 @@ void simplify_reshapes::apply(module& m) const
find_transpose{}, find_transpose{},
find_concat_transpose{}, find_concat_transpose{},
find_concat_multibroadcasts{}, find_concat_multibroadcasts{},
find_nested_convert{},
find_nested_slice{}, find_nested_slice{},
find_nested_concat{}, find_nested_concat{},
find_transpose_slice{}, find_transpose_slice{},
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "migraphx/eliminate_nested_converts.hpp"
#include <migraphx/auto_contiguous.hpp> #include <migraphx/auto_contiguous.hpp>
#include <migraphx/adjust_allocation.hpp> #include <migraphx/adjust_allocation.hpp>
#include <migraphx/dead_code_elimination.hpp> #include <migraphx/dead_code_elimination.hpp>
...@@ -72,6 +73,7 @@ std::vector<pass> target::get_passes(migraphx::context& gctx, const compile_opti ...@@ -72,6 +73,7 @@ std::vector<pass> target::get_passes(migraphx::context& gctx, const compile_opti
dead_code_elimination{}, dead_code_elimination{},
eliminate_data_type{unsupported_types, shape::type_t::float_type}, eliminate_data_type{unsupported_types, shape::type_t::float_type},
dead_code_elimination{}, dead_code_elimination{},
eliminate_nested_converts{},
simplify_reshapes{}, simplify_reshapes{},
eliminate_identity{}, eliminate_identity{},
eliminate_pad{}, eliminate_pad{},
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <migraphx/eliminate_concat.hpp> #include <migraphx/eliminate_concat.hpp>
#include <migraphx/eliminate_contiguous.hpp> #include <migraphx/eliminate_contiguous.hpp>
#include <migraphx/eliminate_data_type.hpp> #include <migraphx/eliminate_data_type.hpp>
#include <migraphx/eliminate_nested_converts.hpp>
#include <migraphx/eliminate_identity.hpp> #include <migraphx/eliminate_identity.hpp>
#include <migraphx/eliminate_pad.hpp> #include <migraphx/eliminate_pad.hpp>
#include <migraphx/fuse_pointwise.hpp> #include <migraphx/fuse_pointwise.hpp>
...@@ -124,6 +125,8 @@ std::vector<pass> target::get_passes(migraphx::context& gctx, const compile_opti ...@@ -124,6 +125,8 @@ std::vector<pass> target::get_passes(migraphx::context& gctx, const compile_opti
enable_pass(not mlir_enabled(), rewrite_quantization{}), enable_pass(not mlir_enabled(), rewrite_quantization{}),
dead_code_elimination{}, dead_code_elimination{},
eliminate_data_type{unsupported_types, shape::type_t::float_type}, eliminate_data_type{unsupported_types, shape::type_t::float_type},
dead_code_elimination{},
eliminate_nested_converts{},
simplify_reshapes{}, simplify_reshapes{},
eliminate_identity{}, eliminate_identity{},
eliminate_pad{}, eliminate_pad{},
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <migraphx/pass.hpp> #include <migraphx/pass.hpp>
#include <migraphx/auto_contiguous.hpp> #include <migraphx/auto_contiguous.hpp>
#include <migraphx/rewrite_rnn.hpp> #include <migraphx/rewrite_rnn.hpp>
#include <migraphx/eliminate_nested_converts.hpp>
#include <migraphx/eliminate_pad.hpp> #include <migraphx/eliminate_pad.hpp>
#include <migraphx/insert_pad.hpp> #include <migraphx/insert_pad.hpp>
#include <migraphx/dead_code_elimination.hpp> #include <migraphx/dead_code_elimination.hpp>
...@@ -49,6 +50,7 @@ std::vector<pass> target::get_passes(migraphx::context&, const compile_options&) ...@@ -49,6 +50,7 @@ std::vector<pass> target::get_passes(migraphx::context&, const compile_options&)
dead_code_elimination{}, dead_code_elimination{},
eliminate_data_type{{migraphx::shape::fp8e4m3fnuz_type}, shape::float_type, {"quant_dot"}}, eliminate_data_type{{migraphx::shape::fp8e4m3fnuz_type}, shape::float_type, {"quant_dot"}},
dead_code_elimination{}, dead_code_elimination{},
eliminate_nested_converts{},
insert_pad{}, insert_pad{},
dead_code_elimination{}, dead_code_elimination{},
rewrite_rnn{}, rewrite_rnn{},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment