Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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 @@ -440,5 +441,3 @@ rocm_export_targets(
Threads
${MIGRAPHX_CONFIG_DEPENDS}
)


7 changes: 7 additions & 0 deletions src/api/include/migraphx/migraphx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,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 = 50)
{
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 @@ -392,6 +392,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
58 changes: 58 additions & 0 deletions src/compile_modes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 <cstdlib>
#include <algorithm>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

compile_modes convert_to_compile_mode(uint8_t mode)
{
// If mode is not in range 0-100, return BALANCED
if(mode > 100)
return compile_modes::BALANCED;
Comment thread
pnikolic-amd marked this conversation as resolved.
Outdated

// Define the enum values as integers for comparison
constexpr uint8_t eager_val = static_cast<uint8_t>(compile_modes::EAGER);
constexpr uint8_t balanced_val = static_cast<uint8_t>(compile_modes::BALANCED);
constexpr uint8_t max_val = static_cast<uint8_t>(compile_modes::MAX);
Comment thread
pnikolic-amd marked this conversation as resolved.
Outdated

// Calculate distances to each enum value
uint8_t dist_to_eager = std::abs(mode - eager_val);
uint8_t dist_to_balanced = std::abs(mode - balanced_val);
uint8_t dist_to_max = std::abs(mode - max_val);
// Find the minimum distance
uint8_t min_dist = std::min({dist_to_eager, dist_to_balanced, dist_to_max});

// Return the enum value with minimum distance
if(min_dist == dist_to_eager)
return compile_modes::EAGER;
if(min_dist == dist_to_balanced)
return compile_modes::BALANCED;
return compile_modes::MAX;
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
12 changes: 12 additions & 0 deletions src/driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,18 @@ 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 (0-100). 0 = fast compile, 100 = best optimizations"),
ap.write_action([](auto&, auto& x, const auto& params) {
if(params.empty())
throw std::runtime_error("Flag with no value.");
int val = std::stoi(params.back());
if(val < 0 or val > 100)
throw std::runtime_error("Compile mode must be between 0 and 100, got: " +
params.back());
x = static_cast<int8_t>(val);
}));
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
45 changes: 45 additions & 0 deletions src/include/migraphx/compile_modes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

enum class compile_modes
{
EAGER = 0,
BALANCED = 50,
MAX = 100
Comment thread
pnikolic-amd marked this conversation as resolved.
};

MIGRAPHX_EXPORT compile_modes convert_to_compile_mode(uint8_t mode);
Comment thread
pnikolic-amd marked this conversation as resolved.
Outdated

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif // MIGRAPHX_GUARD_MIGRAPHX_COMPILE_MODES_HPP
7 changes: 7 additions & 0 deletions src/include/migraphx/compile_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ struct compile_options
bool fast_math = true;
bool exhaustive_tune = false;

/**
* Value between 0 and 100 to define compilation mode.
* 0 - fast compile mode without optmizations, low performance
* 100 - best compile mode with max optimizations, best performance
*/
int8_t compile_mode = 50;
Comment thread
pnikolic-amd marked this conversation as resolved.
Outdated

tracer trace{};
};

Expand Down
7 changes: 5 additions & 2 deletions src/py/migraphx_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,17 +518,20 @@ MIGRAPHX_PYBIND11_MODULE(migraphx, m)
const migraphx::target& t,
bool offload_copy,
bool fast_math,
bool exhaustive_tune) {
bool exhaustive_tune,
int8_t 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") = 50)
Comment thread
pnikolic-amd marked this conversation as resolved.
Outdated
.def("get_main_module", [](const migraphx::program& p) { return p.get_main_module(); })
.def(
"create_module",
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 @@ -286,7 +286,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 @@ -306,7 +306,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{}))
if(skip_benchmark or enabled(MIGRAPHX_SKIP_BENCHMARKING{}))
{
ctx->get_problem_cache().insert(preop.name(), problem, solutions.front());
results.resize(1);
Expand Down Expand Up @@ -482,7 +482,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 @@ -500,7 +501,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 All @@ -523,7 +524,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
1 change: 1 addition & 0 deletions src/targets/gpu/include/migraphx/gpu/compile_ops.hpp
Original file line number Diff line number Diff line change
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