Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ add_library(migraphx
color.cpp
common.cpp
common_dims.cpp
compile_modes.cpp
compile_src.cpp
convert_to_json.cpp
cpp_generator.cpp
Expand Down Expand Up @@ -450,5 +451,3 @@ rocm_export_targets(
Threads
${MIGRAPHX_CONFIG_DEPENDS}
)


8 changes: 8 additions & 0 deletions src/api/include/migraphx/migraphx.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ typedef enum

} migraphx_status;

typedef enum
{
migraphx_compile_mode_eager = 0,
migraphx_compile_mode_balanced = 50,
migraphx_compile_mode_max = 100,

} migraphx_compile_mode;

#define MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES(x, t) migraphx_shape_##x,
/// An enum to represent the different data type inputs
typedef enum
Expand Down
7 changes: 7 additions & 0 deletions src/api/include/migraphx/migraphx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,13 @@ struct compile_options : MIGRAPHX_HANDLE_BASE(compile_options)
{
call(&migraphx_compile_options_set_exhaustive_tune_flag, this->get_handle_ptr(), value);
}

/// Set compilation mode (0-100). 0 = fast compile, low performance.
/// 100 = best compile with max optimizations, best performance.
void set_compile_mode(int8_t value = migraphx_compile_mode_balanced)
{
call(&migraphx_compile_options_set_compile_mode, this->get_handle_ptr(), value);
}
};

/// A program represents the all computation graphs to be compiled and executed
Expand Down
3 changes: 3 additions & 0 deletions src/api/migraphx.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ def compile_options(h):
h.method('set_exhaustive_tune_flag',
api.params(value='bool'),
invoke='migraphx::set_exhaustive_tune_flag($@)')
h.method('set_compile_mode',
api.params(value='int8_t'),
invoke='migraphx::set_compile_mode($@)')


api.add_function('migraphx_parse_onnx',
Expand Down
84 changes: 84 additions & 0 deletions src/compile_modes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2026 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 <migraphx/compile_modes.hpp>
#include <migraphx/errors.hpp>
#include <migraphx/functional.hpp>
#include <migraphx/logger.hpp>
#include <migraphx/stringutils.hpp>
#include <cstdlib>
#include <algorithm>
#include <array>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

compile_modes convert_to_compile_mode(const uint8_t mode)
{
auto clamped = static_cast<uint8_t>(std::clamp<int>(mode, 0, 100));

Check warning on line 38 in src/compile_modes.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

style: Static cast is redundant. [migraphx-RedundantCast]
if(clamped != mode)
log::warn() << "Compile mode value " << static_cast<int>(mode)
<< " out of range [0, 100], clamping to " << static_cast<int>(clamped);

static const std::array<compile_modes, 3> modes = {
compile_modes::EAGER, compile_modes::BALANCED, compile_modes::MAX};

auto it = std::find_if(modes.begin(), modes.end(), [&](compile_modes m) {
return static_cast<uint8_t>(m) == clamped;
});
if(it != modes.end())
return *it;

log::warn() << "Compile mode value " << static_cast<int>(clamped)
<< " does not match a known mode, using closest match";
return *std::min_element(modes.begin(), modes.end(), by(std::less<>{}, [&](compile_modes m) {
return std::abs(static_cast<int>(clamped) - static_cast<int>(m));
}));
}

compile_modes convert_to_compile_mode(const std::string& mode)
{
auto lower = to_lower(mode);
if(lower == "eager")
return compile_modes::EAGER;
if(lower == "balanced")
return compile_modes::BALANCED;
if(lower == "max")
return compile_modes::MAX;
try
{
int val = std::stoi(mode);
if(val < 0 or val > 100)
log::warn() << "Compile mode value " << val << " out of range [0, 100], clamping to "
<< std::clamp(val, 0, 100);
return convert_to_compile_mode(static_cast<uint8_t>(std::clamp(val, 0, 100)));
}
catch(const std::invalid_argument&)
{
MIGRAPHX_THROW("Invalid compile mode: " + mode +
". Expected eager, balanced, max, or an integer 0-100");
}
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
8 changes: 8 additions & 0 deletions src/driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,14 @@ struct compiler
{"--exhaustive-tune"},
ap.help("Exhastively search for best tuning parameters for kernels"),
ap.set_value(true));
ap(co.compile_mode,
{"--compile-mode"},
ap.help("Set compilation mode: eager, balanced, max, or an integer 0-100"),
ap.write_action([](auto&, auto& x, const auto& params) {
if(params.empty())
throw std::runtime_error("Flag with no value.");
x = convert_to_compile_mode(params.back());
}));
ap(to_fp16, {"--fp16"}, ap.help("Quantize for fp16"), ap.set_value(true));
ap(to_bf16, {"--bf16"}, ap.help("Quantize for bf16"), ap.set_value(true));
ap(to_int8, {"--int8"}, ap.help("Quantize for int8"), ap.set_value(true));
Expand Down
47 changes: 47 additions & 0 deletions src/include/migraphx/compile_modes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2026 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_MIGRAPHX_COMPILE_MODES_HPP
#define MIGRAPHX_GUARD_MIGRAPHX_COMPILE_MODES_HPP

#include <migraphx/config.hpp>
#include <cstdint>
#include <string>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

enum class compile_modes
{
EAGER = 0,

Check warning on line 36 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for enum constant 'EAGER' [readability-identifier-naming,-warnings-as-errors]

Check warning on line 36 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for enum constant 'EAGER' [readability-identifier-naming,-warnings-as-errors]

Check warning on line 36 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for enum constant 'EAGER' [readability-identifier-naming,-warnings-as-errors]
BALANCED = 50,

Check warning on line 37 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for enum constant 'BALANCED' [readability-identifier-naming,-warnings-as-errors]

Check warning on line 37 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for enum constant 'BALANCED' [readability-identifier-naming,-warnings-as-errors]

Check warning on line 37 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for enum constant 'BALANCED' [readability-identifier-naming,-warnings-as-errors]
MAX = 100

Check warning on line 38 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for enum constant 'MAX' [readability-identifier-naming,-warnings-as-errors]

Check warning on line 38 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for enum constant 'MAX' [readability-identifier-naming,-warnings-as-errors]
Comment thread
pnikolic-amd marked this conversation as resolved.
};

MIGRAPHX_EXPORT compile_modes convert_to_compile_mode(const uint8_t mode);

Check warning on line 41 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

parameter 'mode' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls,-warnings-as-errors]

Check warning on line 41 in src/include/migraphx/compile_modes.hpp

View workflow job for this annotation

GitHub Actions / tidy

parameter 'mode' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls,-warnings-as-errors]
MIGRAPHX_EXPORT compile_modes convert_to_compile_mode(const std::string& mode);

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif // MIGRAPHX_GUARD_MIGRAPHX_COMPILE_MODES_HPP
5 changes: 4 additions & 1 deletion src/include/migraphx/compile_options.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2026 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
Expand All @@ -25,6 +25,7 @@
#define MIGRAPHX_GUARD_RTGLIB_COMPILE_OPTIONS_HPP

#include <migraphx/config.hpp>
#include <migraphx/compile_modes.hpp>
#include <migraphx/tracer.hpp>

namespace migraphx {
Expand All @@ -41,6 +42,8 @@ struct compile_options
bool fast_math = true;
bool exhaustive_tune = false;

compile_modes compile_mode = compile_modes::BALANCED;

tracer trace{};
};

Expand Down
3 changes: 2 additions & 1 deletion src/include/migraphx/output_iterator.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2026 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
Expand All @@ -26,6 +26,7 @@

#include <migraphx/config.hpp>
#include <migraphx/copy_assignable_function.hpp>
#include <cassert>
#include <iterator>

namespace migraphx {
Expand Down
13 changes: 11 additions & 2 deletions src/py/migraphx_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <migraphx/op/builder/insert.hpp>
#include <migraphx/float8.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/compile_modes.hpp>
#include <migraphx/version.h>
#include <migraphx/iterator_for.hpp>
#ifdef HAVE_GPU
Expand Down Expand Up @@ -553,17 +554,20 @@ MIGRAPHX_PYBIND11_MODULE(migraphx, m)
const migraphx::target& t,
bool offload_copy,
bool fast_math,
bool exhaustive_tune) {
bool exhaustive_tune,
migraphx::compile_modes compile_mode) {
migraphx::compile_options options;
options.offload_copy = offload_copy;
options.fast_math = fast_math;
options.exhaustive_tune = exhaustive_tune;
options.compile_mode = compile_mode;
p.compile(t, options);
},
py::arg("t"),
py::arg("offload_copy") = true,
py::arg("fast_math") = true,
py::arg("exhaustive_tune") = false)
py::arg("exhaustive_tune") = false,
py::arg("compile_mode") = migraphx::compile_modes::BALANCED)
.def("get_main_module", [](const migraphx::program& p) { return p.get_main_module(); })
.def(
"create_module",
Expand Down Expand Up @@ -634,6 +638,11 @@ MIGRAPHX_PYBIND11_MODULE(migraphx, m)
.value("reverse", migraphx::op::rnn_direction::reverse)
.value("bidirectional", migraphx::op::rnn_direction::bidirectional);

py::enum_<migraphx::compile_modes>(m, "compile_modes")
.value("eager", migraphx::compile_modes::EAGER)
.value("balanced", migraphx::compile_modes::BALANCED)
.value("max", migraphx::compile_modes::MAX);

py::class_<py_macro>(m, "macro")
.def(py::init([](const std::string& name, py::kwargs kwargs) {
migraphx::value v = migraphx::value::object{};
Expand Down
1 change: 1 addition & 0 deletions src/targets/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ add_library(migraphx_gpu
allocation_model.cpp
code_object_op.cpp
compile_ops.cpp
pipeline_factory.cpp
compile_gen.cpp
compile_hip.cpp
compile_hip_code_object.cpp
Expand Down
12 changes: 7 additions & 5 deletions src/targets/gpu/compile_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ struct compile_plan
}

template <class Vector>
void add_compiles(Vector& compiles)
void add_compiles(Vector& compiles, bool skip_benchmark)
{
if(config.has_value())
{
Expand All @@ -337,7 +337,7 @@ struct compile_plan
if(solutions.empty())
MIGRAPHX_THROW("No solutions provided for " + preop.name() + " with " +
problem_string() + "\n\n" + print_modules());
if(enabled(MIGRAPHX_SKIP_BENCHMARKING{}) or solutions.size() == 1)
if(skip_benchmark or enabled(MIGRAPHX_SKIP_BENCHMARKING{}) or solutions.size() == 1)
{
ctx->get_problem_cache().insert(preop.name(), problem, solutions.front());
results.resize(1);
Expand Down Expand Up @@ -514,7 +514,8 @@ static void par_compile(std::size_t n, F f)
struct compile_manager
{
std::vector<compile_plan> cps;
bool exhaustive = false;
bool exhaustive = false;
bool skip_benchmark = false;

template <class... Ts>
void add_plan(Ts&&... xs)
Expand All @@ -532,7 +533,7 @@ struct compile_manager
std::vector<std::function<void()>> compiles;
for(auto& cp : cps)
{
cp.add_compiles(compiles);
cp.add_compiles(compiles, skip_benchmark);
}
par_compile(compiles.size(), [&](auto i) { compiles[i](); });

Expand Down Expand Up @@ -576,7 +577,8 @@ struct compile_manager
void compile_ops::apply(module& m) const
{
compile_manager cm;
cm.exhaustive = exhaustive_tune;
cm.exhaustive = exhaustive_tune;
cm.skip_benchmark = skip_benchmark;
// Find all precompile ops
for(auto ins : iterator_for(m))
{
Expand Down
3 changes: 2 additions & 1 deletion src/targets/gpu/include/migraphx/gpu/compile_ops.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2026 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
Expand Down Expand Up @@ -40,6 +40,7 @@ struct MIGRAPHX_GPU_EXPORT compile_ops
{
context* ctx = nullptr;
bool exhaustive_tune = false;
bool skip_benchmark = false;
std::string name() const { return "gpu::compile_ops"; }
void apply(module& m) const;
};
Expand Down
Loading
Loading