Unverified Commit 95431eb7 authored by Umang Yadav's avatar Umang Yadav Committed by GitHub
Browse files

Make `compile_options` an opaque object for ABI compatibility (#955)

Add forward compatibility support for compile options 
parent da26db34
#include <migraphx/operation.hpp>
#include <migraphx/migraphx.h> #include <migraphx/migraphx.h>
#include <migraphx/rank.hpp> #include <migraphx/rank.hpp>
#include <migraphx/shape.hpp> #include <migraphx/shape.hpp>
...@@ -74,13 +73,9 @@ migraphx_shape_datatype_t to_shape_type(shape::type_t t) ...@@ -74,13 +73,9 @@ migraphx_shape_datatype_t to_shape_type(shape::type_t t)
target get_target(const std::string& name) { return make_target(name); } target get_target(const std::string& name) { return make_target(name); }
migraphx::compile_options to_compile_options(const migraphx_compile_options& options) void set_offload_copy(compile_options& options, bool value) { options.offload_copy = value; }
{
migraphx::compile_options result{}; void set_fast_math(compile_options& options, bool value) { options.fast_math = value; }
result.offload_copy = options.offload_copy;
result.fast_math = options.fast_math;
return result;
}
void set_file_format(file_options& options, const char* format) { options.format = format; } void set_file_format(file_options& options, const char* format) { options.format = format; }
...@@ -331,6 +326,16 @@ struct migraphx_file_options ...@@ -331,6 +326,16 @@ struct migraphx_file_options
migraphx::file_options object; migraphx::file_options object;
}; };
extern "C" struct migraphx_compile_options;
struct migraphx_compile_options
{
template <class... Ts>
migraphx_compile_options(Ts&&... xs) : object(std::forward<Ts>(xs)...)
{
}
migraphx::compile_options object;
};
extern "C" struct migraphx_tf_options; extern "C" struct migraphx_tf_options;
struct migraphx_tf_options struct migraphx_tf_options
{ {
...@@ -689,17 +694,16 @@ extern "C" migraphx_status migraphx_program_get_main_module(migraphx_module_t* o ...@@ -689,17 +694,16 @@ extern "C" migraphx_status migraphx_program_get_main_module(migraphx_module_t* o
extern "C" migraphx_status migraphx_program_compile(migraphx_program_t program, extern "C" migraphx_status migraphx_program_compile(migraphx_program_t program,
migraphx_target_t target, migraphx_target_t target,
migraphx_compile_options* options) migraphx_compile_options_t options)
{ {
return migraphx::try_([&] { return migraphx::try_([&] {
if(program == nullptr) if(program == nullptr)
MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer"); MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
if(target == nullptr) if(target == nullptr)
MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter target: Null pointer"); MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter target: Null pointer");
(program->object) if(options == nullptr)
.compile((target->object), MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
(options == nullptr ? migraphx::compile_options{} (program->object).compile((target->object), (options->object));
: migraphx::to_compile_options(*options)));
}); });
} }
...@@ -853,6 +857,17 @@ migraphx_onnx_options_set_default_dim_value(migraphx_onnx_options_t onnx_options ...@@ -853,6 +857,17 @@ migraphx_onnx_options_set_default_dim_value(migraphx_onnx_options_t onnx_options
}); });
} }
extern "C" migraphx_status
migraphx_onnx_options_set_default_loop_iterations(migraphx_onnx_options_t onnx_options,
int64_t value)
{
return migraphx::try_([&] {
if(onnx_options == nullptr)
MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter onnx_options: Null pointer");
migraphx::set_default_loop_iterations((onnx_options->object), (value));
});
}
extern "C" migraphx_status migraphx_file_options_destroy(migraphx_file_options_t file_options) extern "C" migraphx_status migraphx_file_options_destroy(migraphx_file_options_t file_options)
{ {
return migraphx::try_([&] { destroy((file_options)); }); return migraphx::try_([&] { destroy((file_options)); });
...@@ -876,13 +891,39 @@ migraphx_file_options_set_file_format(migraphx_file_options_t file_options, cons ...@@ -876,13 +891,39 @@ migraphx_file_options_set_file_format(migraphx_file_options_t file_options, cons
} }
extern "C" migraphx_status extern "C" migraphx_status
migraphx_onnx_options_set_default_loop_iterations(migraphx_onnx_options_t onnx_options, migraphx_compile_options_destroy(migraphx_compile_options_t compile_options)
int64_t value) {
return migraphx::try_([&] { destroy((compile_options)); });
}
extern "C" migraphx_status
migraphx_compile_options_create(migraphx_compile_options_t* compile_options)
{ {
return migraphx::try_([&] { return migraphx::try_([&] {
if(onnx_options == nullptr) *compile_options =
MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter onnx_options: Null pointer"); object_cast<migraphx_compile_options_t>(allocate<migraphx::compile_options>());
migraphx::set_default_loop_iterations((onnx_options->object), (value)); });
}
extern "C" migraphx_status
migraphx_compile_options_set_offload_copy(migraphx_compile_options_t compile_options, bool value)
{
return migraphx::try_([&] {
if(compile_options == nullptr)
MIGRAPHX_THROW(migraphx_status_bad_param,
"Bad parameter compile_options: Null pointer");
migraphx::set_offload_copy((compile_options->object), (value));
});
}
extern "C" migraphx_status
migraphx_compile_options_set_fast_math(migraphx_compile_options_t compile_options, bool value)
{
return migraphx::try_([&] {
if(compile_options == nullptr)
MIGRAPHX_THROW(migraphx_status_bad_param,
"Bad parameter compile_options: Null pointer");
migraphx::set_fast_math((compile_options->object), (value));
}); });
} }
......
...@@ -41,19 +41,6 @@ typedef enum { ...@@ -41,19 +41,6 @@ typedef enum {
} migraphx_shape_datatype_t; } migraphx_shape_datatype_t;
#undef MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES #undef MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES
/// Options to be passed when compiling
typedef struct
{
/// For targets with offloaded memory(such as the gpu), this will insert
/// instructions during compilation to copy the input parameters to the
/// offloaded memory and to copy the final result from the offloaded
/// memory back to main memory.
bool offload_copy;
/// Optimize math functions to use faster approximate versions. There may
/// be slight accuracy degredation when enabled.
bool fast_math;
} migraphx_compile_options;
typedef struct migraphx_shape* migraphx_shape_t; typedef struct migraphx_shape* migraphx_shape_t;
typedef const struct migraphx_shape* const_migraphx_shape_t; typedef const struct migraphx_shape* const_migraphx_shape_t;
...@@ -90,6 +77,9 @@ typedef const struct migraphx_onnx_options* const_migraphx_onnx_options_t; ...@@ -90,6 +77,9 @@ typedef const struct migraphx_onnx_options* const_migraphx_onnx_options_t;
typedef struct migraphx_file_options* migraphx_file_options_t; typedef struct migraphx_file_options* migraphx_file_options_t;
typedef const struct migraphx_file_options* const_migraphx_file_options_t; typedef const struct migraphx_file_options* const_migraphx_file_options_t;
typedef struct migraphx_compile_options* migraphx_compile_options_t;
typedef const struct migraphx_compile_options* const_migraphx_compile_options_t;
typedef struct migraphx_tf_options* migraphx_tf_options_t; typedef struct migraphx_tf_options* migraphx_tf_options_t;
typedef const struct migraphx_tf_options* const_migraphx_tf_options_t; typedef const struct migraphx_tf_options* const_migraphx_tf_options_t;
...@@ -196,7 +186,7 @@ migraphx_status migraphx_program_get_main_module(migraphx_module_t* out, ...@@ -196,7 +186,7 @@ migraphx_status migraphx_program_get_main_module(migraphx_module_t* out,
migraphx_status migraphx_program_compile(migraphx_program_t program, migraphx_status migraphx_program_compile(migraphx_program_t program,
migraphx_target_t target, migraphx_target_t target,
migraphx_compile_options* options); migraphx_compile_options_t options);
migraphx_status migraphx_program_get_parameter_shapes(migraphx_program_parameter_shapes_t* out, migraphx_status migraphx_program_get_parameter_shapes(migraphx_program_parameter_shapes_t* out,
migraphx_program_t program); migraphx_program_t program);
...@@ -239,6 +229,10 @@ migraphx_status migraphx_onnx_options_set_input_parameter_shape( ...@@ -239,6 +229,10 @@ migraphx_status migraphx_onnx_options_set_input_parameter_shape(
migraphx_status migraphx_onnx_options_set_default_dim_value(migraphx_onnx_options_t onnx_options, migraphx_status migraphx_onnx_options_set_default_dim_value(migraphx_onnx_options_t onnx_options,
size_t value); size_t value);
migraphx_status
migraphx_onnx_options_set_default_loop_iterations(migraphx_onnx_options_t onnx_options,
int64_t value);
migraphx_status migraphx_file_options_destroy(migraphx_file_options_t file_options); migraphx_status migraphx_file_options_destroy(migraphx_file_options_t file_options);
migraphx_status migraphx_file_options_create(migraphx_file_options_t* file_options); migraphx_status migraphx_file_options_create(migraphx_file_options_t* file_options);
...@@ -246,9 +240,15 @@ migraphx_status migraphx_file_options_create(migraphx_file_options_t* file_optio ...@@ -246,9 +240,15 @@ migraphx_status migraphx_file_options_create(migraphx_file_options_t* file_optio
migraphx_status migraphx_file_options_set_file_format(migraphx_file_options_t file_options, migraphx_status migraphx_file_options_set_file_format(migraphx_file_options_t file_options,
const char* format); const char* format);
migraphx_status migraphx_compile_options_destroy(migraphx_compile_options_t compile_options);
migraphx_status migraphx_compile_options_create(migraphx_compile_options_t* compile_options);
migraphx_status migraphx_status
migraphx_onnx_options_set_default_loop_iterations(migraphx_onnx_options_t onnx_options, migraphx_compile_options_set_offload_copy(migraphx_compile_options_t compile_options, bool value);
int64_t value);
migraphx_status migraphx_compile_options_set_fast_math(migraphx_compile_options_t compile_options,
bool value);
migraphx_status migraphx_status
migraphx_parse_onnx(migraphx_program_t* out, const char* name, migraphx_onnx_options_t options); migraphx_parse_onnx(migraphx_program_t* out, const char* name, migraphx_onnx_options_t options);
......
...@@ -494,6 +494,29 @@ struct module ...@@ -494,6 +494,29 @@ struct module
void print() const { call(&migraphx_module_print, mm); } void print() const { call(&migraphx_module_print, mm); }
}; };
struct compile_options : MIGRAPHX_HANDLE_BASE(compile_options)
{
compile_options() { this->make_handle(&migraphx_compile_options_create); }
compile_options(migraphx_compile_options* p, own) { this->set_handle(p, own()); }
/// For targets with offloaded memory(such as the gpu), this will insert
/// instructions during compilation to copy the input parameters to the
/// offloaded memory and to copy the final result from the offloaded
/// memory back to main memory.
void set_offload_copy(bool value = true)
{
call(&migraphx_compile_options_set_offload_copy, this->get_handle_ptr(), value);
}
/// Optimize math functions to use faster approximate versions. There may
/// be slight accuracy degredation when enabled.
void set_fast_math(bool value = true)
{
call(&migraphx_compile_options_set_fast_math, this->get_handle_ptr(), value);
}
};
/// A program represents the all computation graphs to be compiled and executed /// A program represents the all computation graphs to be compiled and executed
struct program : MIGRAPHX_HANDLE_BASE(program) struct program : MIGRAPHX_HANDLE_BASE(program)
{ {
...@@ -504,16 +527,21 @@ struct program : MIGRAPHX_HANDLE_BASE(program) ...@@ -504,16 +527,21 @@ struct program : MIGRAPHX_HANDLE_BASE(program)
program(migraphx_program* p, borrow) { this->set_handle(p, borrow{}); } program(migraphx_program* p, borrow) { this->set_handle(p, borrow{}); }
/// Compile the program for a specific target to be ran on /// Compile the program for a specific target to be ran on
void compile(const target& ptarget, migraphx_compile_options poptions) const void compile(const target& ptarget, const compile_options& poptions) const
{ {
call( call(&migraphx_program_compile,
&migraphx_program_compile, this->get_handle_ptr(), ptarget.get_handle_ptr(), &poptions); this->get_handle_ptr(),
ptarget.get_handle_ptr(),
poptions.get_handle_ptr());
} }
/// Compile the program for a specific target to be ran on /// Compile the program for a specific target to be ran on
void compile(const target& ptarget) const void compile(const target& ptarget) const
{ {
call(&migraphx_program_compile, this->get_handle_ptr(), ptarget.get_handle_ptr(), nullptr); call(&migraphx_program_compile,
this->get_handle_ptr(),
ptarget.get_handle_ptr(),
migraphx::compile_options{}.get_handle_ptr());
} }
/// Return the shapes for the input parameters /// Return the shapes for the input parameters
......
...@@ -258,6 +258,17 @@ def file_options(h): ...@@ -258,6 +258,17 @@ def file_options(h):
invoke='migraphx::set_file_format($@)') invoke='migraphx::set_file_format($@)')
@auto_handle()
def compile_options(h):
h.constructor('create')
h.method('set_offload_copy',
api.params(value='bool'),
invoke='migraphx::set_offload_copy($@)')
h.method('set_fast_math',
api.params(value='bool'),
invoke='migraphx::set_fast_math($@)')
api.add_function('migraphx_parse_onnx', api.add_function('migraphx_parse_onnx',
api.params(name='const char*', api.params(name='const char*',
options='migraphx::onnx_options'), options='migraphx::onnx_options'),
......
...@@ -3,14 +3,14 @@ function(add_api_test TEST_NAME TEST_SRC TEST_DIR) ...@@ -3,14 +3,14 @@ function(add_api_test TEST_NAME TEST_SRC TEST_DIR)
set(NAME test_api_${TEST_NAME}) set(NAME test_api_${TEST_NAME})
add_executable(${NAME} EXCLUDE_FROM_ALL ${TEST_SRC}) add_executable(${NAME} EXCLUDE_FROM_ALL ${TEST_SRC})
rocm_clang_tidy_check(${NAME}) rocm_clang_tidy_check(${NAME})
target_link_libraries(${NAME} migraphx_c) target_link_libraries(${NAME} migraphx_c migraphx)
target_include_directories(${NAME} PUBLIC ../include) target_include_directories(${NAME} PUBLIC ../include)
add_test(NAME ${NAME} COMMAND $<TARGET_FILE:${NAME}> WORKING_DIRECTORY ${TEST_DIR}) add_test(NAME ${NAME} COMMAND $<TARGET_FILE:${NAME}> WORKING_DIRECTORY ${TEST_DIR})
add_dependencies(tests ${NAME}) add_dependencies(tests ${NAME})
add_dependencies(check ${NAME}) add_dependencies(check ${NAME})
endfunction() endfunction()
add_api_test(compile_options test_compile_options.cpp ${TEST_ONNX_DIR})
add_api_test(ref test_cpu.cpp ${TEST_ONNX_DIR}) add_api_test(ref test_cpu.cpp ${TEST_ONNX_DIR})
add_api_test(save_load test_save_load.cpp ${TEST_ONNX_DIR}) add_api_test(save_load test_save_load.cpp ${TEST_ONNX_DIR})
add_api_test(op test_op_construct.cpp ${TEST_ONNX_DIR}) add_api_test(op test_op_construct.cpp ${TEST_ONNX_DIR})
......
#include <migraphx/migraphx.h>
#include <migraphx/migraphx.hpp>
#include <migraphx/compile_options.hpp>
#include "test.hpp"
TEST_CASE(compile_options_api_test)
{
migraphx::api::compile_options options;
options.set_offload_copy(false);
options.set_fast_math(false);
const auto* s_options = reinterpret_cast<const migraphx::MIGRAPHX_INLINE_NS::compile_options*>(
options.get_handle_ptr());
CHECK(s_options->fast_math == false);
CHECK(s_options->offload_copy == false);
}
int main(int argc, const char* argv[]) { test::run(argc, argv); }
...@@ -7,8 +7,8 @@ TEST_CASE(load_and_run) ...@@ -7,8 +7,8 @@ TEST_CASE(load_and_run)
{ {
auto p = migraphx::parse_onnx("conv_relu_maxpool_test.onnx"); auto p = migraphx::parse_onnx("conv_relu_maxpool_test.onnx");
auto shapes_before = p.get_output_shapes(); auto shapes_before = p.get_output_shapes();
migraphx_compile_options options; migraphx::compile_options options;
options.offload_copy = true; options.set_offload_copy();
p.compile(migraphx::target("gpu"), options); p.compile(migraphx::target("gpu"), options);
auto shapes_after = p.get_output_shapes(); auto shapes_after = p.get_output_shapes();
CHECK(shapes_before.size() == 1); CHECK(shapes_before.size() == 1);
...@@ -30,8 +30,8 @@ TEST_CASE(if_pl_test) ...@@ -30,8 +30,8 @@ TEST_CASE(if_pl_test)
auto run_prog = [&](auto cond) { auto run_prog = [&](auto cond) {
auto p = migraphx::parse_onnx("if_pl_test.onnx"); auto p = migraphx::parse_onnx("if_pl_test.onnx");
auto shapes_before = p.get_output_shapes(); auto shapes_before = p.get_output_shapes();
migraphx_compile_options options; migraphx::compile_options options;
options.offload_copy = true; options.set_offload_copy();
p.compile(migraphx::target("gpu"), options); p.compile(migraphx::target("gpu"), options);
auto shapes_after = p.get_output_shapes(); auto shapes_after = p.get_output_shapes();
CHECK(shapes_before.size() == 1); CHECK(shapes_before.size() == 1);
...@@ -81,8 +81,8 @@ TEST_CASE(loop_test) ...@@ -81,8 +81,8 @@ TEST_CASE(loop_test)
parse_options.set_default_loop_iterations(max_iter_num); parse_options.set_default_loop_iterations(max_iter_num);
auto p = migraphx::parse_onnx("loop_default_test.onnx", parse_options); auto p = migraphx::parse_onnx("loop_default_test.onnx", parse_options);
auto shapes_before = p.get_output_shapes(); auto shapes_before = p.get_output_shapes();
migraphx_compile_options options; migraphx::compile_options options;
options.offload_copy = true; options.set_offload_copy();
p.compile(migraphx::target("gpu"), options); p.compile(migraphx::target("gpu"), options);
auto shapes_after = p.get_output_shapes(); auto shapes_after = p.get_output_shapes();
CHECK(shapes_before.size() == 2); CHECK(shapes_before.size() == 2);
......
#include <migraphx/operation.hpp>
#include <migraphx/migraphx.h> #include <migraphx/migraphx.h>
#include <migraphx/rank.hpp> #include <migraphx/rank.hpp>
#include <migraphx/shape.hpp> #include <migraphx/shape.hpp>
...@@ -74,13 +73,9 @@ migraphx_shape_datatype_t to_shape_type(shape::type_t t) ...@@ -74,13 +73,9 @@ migraphx_shape_datatype_t to_shape_type(shape::type_t t)
target get_target(const std::string& name) { return make_target(name); } target get_target(const std::string& name) { return make_target(name); }
migraphx::compile_options to_compile_options(const migraphx_compile_options& options) void set_offload_copy(compile_options& options, bool value) { options.offload_copy = value; }
{
migraphx::compile_options result{}; void set_fast_math(compile_options& options, bool value) { options.fast_math = value; }
result.offload_copy = options.offload_copy;
result.fast_math = options.fast_math;
return result;
}
void set_file_format(file_options& options, const char* format) { options.format = format; } void set_file_format(file_options& options, const char* format) { options.format = format; }
......
...@@ -41,19 +41,6 @@ typedef enum { ...@@ -41,19 +41,6 @@ typedef enum {
} migraphx_shape_datatype_t; } migraphx_shape_datatype_t;
#undef MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES #undef MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES
/// Options to be passed when compiling
typedef struct
{
/// For targets with offloaded memory(such as the gpu), this will insert
/// instructions during compilation to copy the input parameters to the
/// offloaded memory and to copy the final result from the offloaded
/// memory back to main memory.
bool offload_copy;
/// Optimize math functions to use faster approximate versions. There may
/// be slight accuracy degredation when enabled.
bool fast_math;
} migraphx_compile_options;
<% generate_c_header() %> <% generate_c_header() %>
#ifdef __cplusplus #ifdef __cplusplus
......
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