Unverified Commit 2466dd6f authored by Shucai Xiao's avatar Shucai Xiao Committed by GitHub
Browse files

Refactor program to module (#684)



* code backup

* clang format

* change corresponding tool files

* clang format
Co-authored-by: default avatarmvermeulen <5479696+mvermeulen@users.noreply.github.com>
parent de10423f
......@@ -9,8 +9,9 @@ struct test_tanh : verify_program<test_tanh>
migraphx::program create_program() const
{
migraphx::program p;
auto x = p.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
p.add_instruction(migraphx::op::tanh{}, x);
auto* mm = p.get_main_module();
auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
mm->add_instruction(migraphx::op::tanh{}, x);
return p;
}
};
......@@ -9,11 +9,12 @@ struct test_trans_abs : verify_program<test_trans_abs>
migraphx::program create_program() const
{
migraphx::program p;
auto x = p.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto tx = p.add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, x);
auto absx = p.add_instruction(migraphx::op::abs{}, tx);
auto r = p.add_instruction(migraphx::op::add{}, absx, absx);
p.add_instruction(migraphx::op::contiguous{}, r);
auto* mm = p.get_main_module();
auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto tx = mm->add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, x);
auto absx = mm->add_instruction(migraphx::op::abs{}, tx);
auto r = mm->add_instruction(migraphx::op::add{}, absx, absx);
mm->add_instruction(migraphx::op::contiguous{}, r);
return p;
}
......
......@@ -9,9 +9,10 @@ struct test_trans_ret : verify_program<test_trans_ret>
migraphx::program create_program() const
{
migraphx::program p;
auto x = p.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto tx = p.add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, x);
p.add_return({tx});
auto* mm = p.get_main_module();
auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto tx = mm->add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, x);
mm->add_return({tx});
return p;
}
......
......@@ -9,11 +9,12 @@ struct test_trans_tanh : verify_program<test_trans_tanh>
migraphx::program create_program() const
{
migraphx::program p;
auto x = p.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto tx = p.add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, x);
auto tanhx = p.add_instruction(migraphx::op::tanh{}, tx);
auto r = p.add_instruction(migraphx::op::add{}, tanhx, tanhx);
p.add_instruction(migraphx::op::contiguous{}, r);
auto* mm = p.get_main_module();
auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto tx = mm->add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, x);
auto tanhx = mm->add_instruction(migraphx::op::tanh{}, tx);
auto r = mm->add_instruction(migraphx::op::add{}, tanhx, tanhx);
mm->add_instruction(migraphx::op::contiguous{}, r);
return p;
}
......
......@@ -9,11 +9,12 @@ struct test_trans_tanh1 : verify_program<test_trans_tanh1>
migraphx::program create_program() const
{
migraphx::program p;
auto x = p.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto tx = p.add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, x);
auto tanhx = p.add_instruction(migraphx::op::tanh{}, tx);
auto r = p.add_instruction(migraphx::op::add{}, tanhx, tanhx);
p.add_return({tx, r});
auto* mm = p.get_main_module();
auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto tx = mm->add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, x);
auto tanhx = mm->add_instruction(migraphx::op::tanh{}, tx);
auto r = mm->add_instruction(migraphx::op::add{}, tanhx, tanhx);
mm->add_return({tx, r});
return p;
}
......
......@@ -9,11 +9,12 @@ struct test_transpose : verify_program<test_transpose>
migraphx::program create_program() const
{
migraphx::program p;
auto* mm = p.get_main_module();
migraphx::shape s{migraphx::shape::float_type, {4, 3, 4, 4}};
auto x = p.add_parameter("x", s);
auto x = mm->add_parameter("x", s);
std::vector<int64_t> perm = {0, 2, 3, 1};
auto l = p.add_instruction(migraphx::op::transpose{perm}, x);
p.add_instruction(migraphx::op::contiguous{}, l);
auto l = mm->add_instruction(migraphx::op::transpose{perm}, x);
mm->add_instruction(migraphx::op::contiguous{}, l);
return p;
}
};
......@@ -9,12 +9,13 @@ struct test_triadd : verify_program<test_triadd>
migraphx::program create_program() const
{
migraphx::program p;
auto* mm = p.get_main_module();
migraphx::shape s{migraphx::shape::float_type, {3}};
auto x = p.add_parameter("x", s);
auto y = p.add_parameter("y", s);
auto z = p.add_parameter("z", s);
auto sum = p.add_instruction(migraphx::op::add{}, x, y);
p.add_instruction(migraphx::op::add{}, sum, z);
auto x = mm->add_parameter("x", s);
auto y = mm->add_parameter("y", s);
auto z = mm->add_parameter("z", s);
auto sum = mm->add_instruction(migraphx::op::add{}, x, y);
mm->add_instruction(migraphx::op::add{}, sum, z);
return p;
}
};
......@@ -9,14 +9,15 @@ struct test_triadd2 : verify_program<test_triadd2>
migraphx::program create_program() const
{
migraphx::program p;
auto* mm = p.get_main_module();
migraphx::shape s{migraphx::shape::float_type, {2, 3}};
migraphx::shape b{migraphx::shape::float_type, {3}};
auto x = p.add_parameter("x", s);
auto y = p.add_parameter("y", s);
auto z = p.add_parameter("z", b);
auto zb = p.add_instruction(migraphx::op::broadcast{1, s.lens()}, z);
auto sum = p.add_instruction(migraphx::op::add{}, x, y);
p.add_instruction(migraphx::op::add{}, sum, zb);
auto x = mm->add_parameter("x", s);
auto y = mm->add_parameter("y", s);
auto z = mm->add_parameter("z", b);
auto zb = mm->add_instruction(migraphx::op::broadcast{1, s.lens()}, z);
auto sum = mm->add_instruction(migraphx::op::add{}, x, y);
mm->add_instruction(migraphx::op::add{}, sum, zb);
return p;
}
};
......@@ -10,13 +10,14 @@ struct test_triadd_broadcast : verify_program<test_triadd_broadcast>
migraphx::program create_program() const
{
migraphx::program p;
auto* mm = p.get_main_module();
migraphx::shape s{migraphx::shape::float_type, {3}};
auto x = p.add_parameter("x", {migraphx::shape::float_type, {2, 2, 3}});
auto y = p.add_parameter("y", {migraphx::shape::float_type, {2, 2}});
auto z = p.add_parameter("z", {migraphx::shape::float_type, {2, 2, 3}});
auto by = p.add_instruction(migraphx::op::broadcast{0, x->get_shape().lens()}, y);
auto sum = p.add_instruction(migraphx::op::add{}, x, by);
p.add_instruction(migraphx::op::add{}, sum, z);
auto x = mm->add_parameter("x", {migraphx::shape::float_type, {2, 2, 3}});
auto y = mm->add_parameter("y", {migraphx::shape::float_type, {2, 2}});
auto z = mm->add_parameter("z", {migraphx::shape::float_type, {2, 2, 3}});
auto by = mm->add_instruction(migraphx::op::broadcast{0, x->get_shape().lens()}, y);
auto sum = mm->add_instruction(migraphx::op::add{}, x, by);
mm->add_instruction(migraphx::op::add{}, sum, z);
return p;
}
};
......@@ -9,12 +9,13 @@ struct test_triadd_relu : verify_program<test_triadd_relu>
migraphx::program create_program() const
{
migraphx::program p;
auto x = p.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto y = p.add_parameter("y", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto z = p.add_parameter("z", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto sum = p.add_instruction(migraphx::op::add{}, x, y);
auto triadd = p.add_instruction(migraphx::op::add{}, sum, z);
p.add_instruction(migraphx::op::relu{}, triadd);
auto* mm = p.get_main_module();
auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto y = mm->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto z = mm->add_parameter("z", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto sum = mm->add_instruction(migraphx::op::add{}, x, y);
auto triadd = mm->add_instruction(migraphx::op::add{}, sum, z);
mm->add_instruction(migraphx::op::relu{}, triadd);
return p;
}
};
......@@ -9,12 +9,13 @@ struct test_triadd_sigmoid : verify_program<test_triadd_sigmoid>
migraphx::program create_program() const
{
migraphx::program p;
auto x = p.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto y = p.add_parameter("y", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto z = p.add_parameter("z", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto sum = p.add_instruction(migraphx::op::add{}, x, y);
auto triadd = p.add_instruction(migraphx::op::add{}, sum, z);
p.add_instruction(migraphx::op::sigmoid{}, triadd);
auto* mm = p.get_main_module();
auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto y = mm->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto z = mm->add_parameter("z", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto sum = mm->add_instruction(migraphx::op::add{}, x, y);
auto triadd = mm->add_instruction(migraphx::op::add{}, sum, z);
mm->add_instruction(migraphx::op::sigmoid{}, triadd);
return p;
}
};
......@@ -9,12 +9,13 @@ struct test_triadd_tanh : verify_program<test_triadd_tanh>
migraphx::program create_program() const
{
migraphx::program p;
auto x = p.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto y = p.add_parameter("y", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto z = p.add_parameter("z", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto sum = p.add_instruction(migraphx::op::add{}, x, y);
auto triadd = p.add_instruction(migraphx::op::add{}, sum, z);
p.add_instruction(migraphx::op::tanh{}, triadd);
auto* mm = p.get_main_module();
auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto y = mm->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto z = mm->add_parameter("z", migraphx::shape{migraphx::shape::float_type, {4, 3, 3, 3}});
auto sum = mm->add_instruction(migraphx::op::add{}, x, y);
auto triadd = mm->add_instruction(migraphx::op::add{}, sum, z);
mm->add_instruction(migraphx::op::tanh{}, triadd);
return p;
}
};
......@@ -16,6 +16,7 @@ struct test_var_sl_gru_bidirct : verify_program<test_var_sl_gru_bidirct>
float clip = 0.0f;
migraphx::program p;
auto* mm = p.get_main_module();
migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
migraphx::shape w_shape{migraphx::shape::float_type,
{num_dirct, 3 * hidden_size, input_size}};
......@@ -25,27 +26,27 @@ struct test_var_sl_gru_bidirct : verify_program<test_var_sl_gru_bidirct>
migraphx::shape sl_shape{migraphx::shape::int32_type, {batch_size}};
migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
auto seq = p.add_parameter("seq", in_shape);
auto w = p.add_parameter("w", w_shape);
auto r = p.add_parameter("r", r_shape);
auto bias = p.add_parameter("bias", b_shape);
auto ih = p.add_parameter("ih", ih_shape);
auto seq = mm->add_parameter("seq", in_shape);
auto w = mm->add_parameter("w", w_shape);
auto r = mm->add_parameter("r", r_shape);
auto bias = mm->add_parameter("bias", b_shape);
auto ih = mm->add_parameter("ih", ih_shape);
std::vector<int> sl_data{2, 1, 3};
auto sql = p.add_literal(migraphx::literal{sl_shape, sl_data});
auto sql = mm->add_literal(migraphx::literal{sl_shape, sl_data});
auto hs =
p.add_instruction(migraphx::op::gru{hidden_size,
{migraphx::op::sigmoid{}, migraphx::op::tanh{}},
migraphx::op::rnn_direction::bidirectional,
clip},
seq,
w,
r,
bias,
sql,
ih);
auto lho = p.add_instruction(migraphx::op::rnn_last_hs_output{}, hs);
p.add_return({hs, lho});
mm->add_instruction(migraphx::op::gru{hidden_size,
{migraphx::op::sigmoid{}, migraphx::op::tanh{}},
migraphx::op::rnn_direction::bidirectional,
clip},
seq,
w,
r,
bias,
sql,
ih);
auto lho = mm->add_instruction(migraphx::op::rnn_last_hs_output{}, hs);
mm->add_return({hs, lho});
return p;
}
......
......@@ -16,6 +16,7 @@ struct test_var_sl_gru_forward : verify_program<test_var_sl_gru_forward>
float clip = 0.0f;
migraphx::program p;
auto* mm = p.get_main_module();
migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
migraphx::shape w_shape{migraphx::shape::float_type,
{num_dirct, 3 * hidden_size, input_size}};
......@@ -25,27 +26,27 @@ struct test_var_sl_gru_forward : verify_program<test_var_sl_gru_forward>
migraphx::shape sl_shape{migraphx::shape::int32_type, {batch_size}};
migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
auto seq = p.add_parameter("seq", in_shape);
auto w = p.add_parameter("w", w_shape);
auto r = p.add_parameter("r", r_shape);
auto bias = p.add_parameter("bias", b_shape);
auto ih = p.add_parameter("ih", ih_shape);
auto seq = mm->add_parameter("seq", in_shape);
auto w = mm->add_parameter("w", w_shape);
auto r = mm->add_parameter("r", r_shape);
auto bias = mm->add_parameter("bias", b_shape);
auto ih = mm->add_parameter("ih", ih_shape);
std::vector<int> sl_data{3, 2, 1};
auto sql = p.add_literal(migraphx::literal{sl_shape, sl_data});
auto sql = mm->add_literal(migraphx::literal{sl_shape, sl_data});
auto hs =
p.add_instruction(migraphx::op::gru{hidden_size,
{migraphx::op::sigmoid{}, migraphx::op::tanh{}},
migraphx::op::rnn_direction::forward,
clip},
seq,
w,
r,
bias,
sql,
ih);
auto lho = p.add_instruction(migraphx::op::rnn_last_hs_output{}, hs);
p.add_return({lho, hs});
mm->add_instruction(migraphx::op::gru{hidden_size,
{migraphx::op::sigmoid{}, migraphx::op::tanh{}},
migraphx::op::rnn_direction::forward,
clip},
seq,
w,
r,
bias,
sql,
ih);
auto lho = mm->add_instruction(migraphx::op::rnn_last_hs_output{}, hs);
mm->add_return({lho, hs});
return p;
}
......
......@@ -118,8 +118,8 @@ void quantize_fp16_with_op_names(program& prog, std::vector<std::string>& names)
struct quantize_int8_options
{
std::vector<program::parameter_map> calibration = {};
std::vector<std::string> op_names = {};
std::vector<parameter_map> calibration = {};
std::vector<std::string> op_names = {};
};
void add_op_name(quantize_int8_options& options, const char* name)
......@@ -127,7 +127,7 @@ void add_op_name(quantize_int8_options& options, const char* name)
options.op_names.push_back(name);
}
void add_calibration_data(quantize_int8_options& options, program::parameter_map& data)
void add_calibration_data(quantize_int8_options& options, parameter_map& data)
{
options.calibration.push_back(data);
}
......@@ -160,10 +160,7 @@ bool equal(const T& x, const T& y)
return x == y;
}
std::vector<argument> run(program& p, const program::parameter_map& params)
{
return p.eval(params);
}
std::vector<argument> run(program& p, const parameter_map& params) { return p.eval(params); }
std::vector<shape> get_output_shapes(program& p) { return p.get_output_shapes(); }
......
......@@ -13,6 +13,7 @@ namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct program;
using module = program;
#ifdef DOXYGEN
......@@ -23,7 +24,7 @@ struct pass
/// A unique name used to identify the pass
std::string name() const;
/// Run the pass on the program
void apply(program& p) const;
void apply(module& p) const;
};
#else
......@@ -31,7 +32,7 @@ struct pass
<%
interface('pass',
virtual('name', returns='std::string', const=True),
virtual('apply', returns='void', p='program &', const=True)
virtual('apply', returns='void', p='module &', const=True)
)
%>
......
......@@ -16,6 +16,7 @@ namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
struct program;
using module = program;
struct operation;
#ifdef DOXYGEN
......@@ -26,11 +27,11 @@ struct schedule_model
/// Get the number of concurrent instruction allowed
std::size_t concurrency() const;
/// Schedule a concurrent instruction
void sched(program& p, instruction_ref ins, std::size_t n) const;
void sched(module& p, instruction_ref ins, std::size_t n) const;
// Insert necessary waits before an instruction
void wait(program& p, instruction_ref ins, std::size_t wait_id) const;
void wait(module& p, instruction_ref ins, std::size_t wait_id) const;
// Insert necessary records after an instruction
void record(program& p, instruction_ref ins, std::size_t wait_id) const;
void record(module& p, instruction_ref ins, std::size_t wait_id) const;
/// Compute weights for an operation
std::size_t weight(const operation& op) const;
};
......@@ -40,9 +41,9 @@ struct schedule_model
<%
interface('schedule_model',
virtual('concurrency', returns='std::size_t', const=True),
virtual('sched', p='program&', ins='instruction_ref', n='std::size_t', const=True),
virtual('wait', p='program&', ins='instruction_ref', wait_id='std::size_t', const=True),
virtual('record', p='program&', ins='instruction_ref', wait_id='std::size_t', const=True),
virtual('sched', p='module&', ins='instruction_ref', n='std::size_t', const=True),
virtual('wait', p='module&', ins='instruction_ref', wait_id='std::size_t', const=True),
virtual('record', p='module&', ins='instruction_ref', wait_id='std::size_t', const=True),
virtual('weight', returns='std::size_t', op='const operation&', const=True)
)
%>
......
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