Commit f4e7d9d9 authored by Brian Pickrell's avatar Brian Pickrell
Browse files

comment cleanup, some test fixes, minor refactoring of parse_resize. ...

comment cleanup, some test fixes, minor refactoring of parse_resize.  onnx_test still fails; needs to be updated with new instructions.
parent 73ae15f5
...@@ -208,6 +208,8 @@ struct parse_resize : op_parser<parse_resize> ...@@ -208,6 +208,8 @@ struct parse_resize : op_parser<parse_resize>
// input data shape info. Convert static lens to dynamic to simplify referencing them later // input data shape info. Convert static lens to dynamic to simplify referencing them later
auto in_s = args[0]->get_shape().to_dynamic(); auto in_s = args[0]->get_shape().to_dynamic();
if(in_s.ndim() < 2)
MIGRAPHX_THROW("PARSE_" + opd.op_name + ": requires 2 or more dimensions input, where first dimension is batch #");
std::vector<migraphx::shape::dynamic_dimension> in_dims = in_s.dyn_dims(); std::vector<migraphx::shape::dynamic_dimension> in_dims = in_s.dyn_dims();
// output shape is explicitly specified // output shape is explicitly specified
...@@ -334,11 +336,11 @@ struct parse_resize : op_parser<parse_resize> ...@@ -334,11 +336,11 @@ struct parse_resize : op_parser<parse_resize>
// create a shape with the scaled lens and no batch dimension // create a shape with the scaled lens and no batch dimension
migraphx::shape static_out_shape(args[0]->get_shape().type(), fixed_out_lens); migraphx::shape static_out_shape(args[0]->get_shape().type(), fixed_out_lens);
size_t out_elements = std::accumulate(fixed_out_lens.begin(), // size_t out_elements = std::accumulate(fixed_out_lens.begin(),
fixed_out_lens.end(), // fixed_out_lens.end(),
std::size_t{1}, // std::size_t{1},
std::multiplies<>()); // std::multiplies<>());
std::vector<int> ind(out_elements); // std::vector<int> ind(out_elements);
// map out_idx to in_idx // map out_idx to in_idx
auto idx_op = get_original_idx_op(coord_trans_mode); auto idx_op = get_original_idx_op(coord_trans_mode);
...@@ -346,62 +348,31 @@ struct parse_resize : op_parser<parse_resize> ...@@ -346,62 +348,31 @@ struct parse_resize : op_parser<parse_resize>
// For each element of static_out_shape, find the matching location of input shape. // For each element of static_out_shape, find the matching location of input shape.
// The indexes we find will be an argument to the gather op. // The indexes we find will be an argument to the gather op.
shape_for_each(static_out_shape, [&](const auto& out_idx_v, size_t out_idx) { shape_for_each(static_out_shape, [&](const auto& out_idx_v, size_t ) {
std::vector<size_t> in_idx(out_idx_v.size()); std::vector<size_t> in_idx(out_idx_v.size());
// printf(" index ");
for(auto ii = 0; ii < fixed_dims.size(); ++ii) for(auto ii = 0; ii < fixed_dims.size(); ++ii)
{ {
// Convert this index by scaling. Inefficient since indexes are repeated // Convert this index by scaling.
auto idx_val = idx_op( auto idx_val = idx_op(
fixed_dims[ii], fixed_out_lens[ii], out_idx_v[ii], vec_scale[ii]); fixed_dims[ii], fixed_out_lens[ii], out_idx_v[ii], vec_scale[ii]);
// round the scaled value to an index // round the scaled value to an int index
in_idx[ii] = nearest_op(fixed_dims[ii], idx_val); in_idx[ii] = nearest_op(fixed_dims[ii], idx_val);
// printf(" %lu ", in_idx[ii]);
} }
// printf("\n");
// convert a 3-D index to a single index into a vector
// ind has a size equal to the output, each value is a 1D index into the
// input data
ind[out_idx] = static_cast<int64_t>(static_out_shape.index(in_idx));
// printf("Maps to %d \n", ind[out_idx]);
}); });
// Create a static shape that's just like the scaled out_lens except we set to 1 the
// 0'th dimension of output, later to be broadcasted to dynamic batch size
// out_lens[0] = 1;
// shape ind_s{shape::int32_type, out_lens};
// auto ins_ind = info.add_literal(literal(ind_s, ind));
// define a dynamic shape including the batch dimension
// Not using this now; the next block seems to work with gather
// std::vector<shape::dynamic_dimension> out_dyn_dims(in_dims.size());
// out_dyn_dims[0] = in_dims[0];
// std::transform(fixed_out_lens.begin(),
// fixed_out_lens.end(),
// out_dyn_dims.begin() + 1,
// [&](auto len) {
// return shape::dynamic_dimension{len, len};
// });
// shape dyn_out_shape{in_s.type(), out_dyn_dims};
instruction_ref gather_ins{args[0]}; instruction_ref gather_ins{args[0]};
// for each static dimension // for each static dimension
for(auto ii = 0; ii < fixed_dims.size(); ++ii) for(auto ii = 0; ii < fixed_dims.size(); ++ii)
{ {
std::vector<size_t> in_idx(fixed_out_lens[ii]); std::vector<size_t> in_idx(fixed_out_lens[ii]);
// for range of this dimension's size in output // for range of this dimension's size in output
for(auto len : range(fixed_out_lens[ii])) for(auto len : range(fixed_out_lens[ii]))
{ {
// Convert this index by scaling. // Convert this index by scaling.
auto idx_val = idx_op(fixed_dims[ii], fixed_out_lens[ii], len, vec_scale[ii+1]); auto idx_val = idx_op(fixed_dims[ii], fixed_out_lens[ii], len, vec_scale[ii+1]);
printf(" ii %d out_lens %lu len %lu vec_scale[ii+1] %f ---> idx_val %f\n", ii, fixed_out_lens[ii],
len, vec_scale[ii+1], idx_val);
// round the scaled value to an index // round the scaled value to an index
in_idx[len] = nearest_op(fixed_dims[ii], idx_val); in_idx[len] = nearest_op(fixed_dims[ii], idx_val);
printf(" in_idx %lu\n", in_idx[len]);
// Put the value into index vector // Put the value into index vector
} }
// Create a 1D shape literal // Create a 1D shape literal
...@@ -410,25 +381,8 @@ printf(" in_idx %lu\n", in_idx[len]); ...@@ -410,25 +381,8 @@ printf(" in_idx %lu\n", in_idx[len]);
// add a "gather" instruction // add a "gather" instruction
gather_ins = info.add_instruction(make_op("gather", {{"axis", 1 + ii}}), gather_ins, index_litA); gather_ins = info.add_instruction(make_op("gather", {{"axis", 1 + ii}}), gather_ins, index_litA);
printf("***\n");
if( ii == (fixed_dims.size() - 1))
return gather_ins;
} }
// If we get here, no gather instructions were added. return gather_ins;
MIGRAPHX_THROW("PARSE_RESIZE: inputs didn't have enough dimensions");
// define an index dynamic shape without the batch dimension
// shape index_shape{in_s.type(), fixed_out_lens};
// this has the same data as ins_ind, but 1 less dimension ;lacks the leading dimension of 1
// auto index_lit = info.add_literal(literal(index_shape, ind));
printf("fixed_out_lens: ");
for(size_t aa : fixed_out_lens) printf (" %lu ", aa);printf("\n");
// Axis 0 or 1? look into shape in gather's compute_shape' and results
// return info.add_instruction(make_op("gather", {{"axis", 0}}), args[0], index_lit);
} }
else else
{ {
......
...@@ -5659,7 +5659,7 @@ def resize_downsample_f_test(): ...@@ -5659,7 +5659,7 @@ def resize_downsample_f_test():
'Resize', 'Resize',
inputs=['X', '', 'scales'], inputs=['X', '', 'scales'],
outputs=['Y'], outputs=['Y'],
coordinate_transformation_mode='asymmetric', coordinate_transformation_mode='align_corners',
mode='nearest', mode='nearest',
nearest_mode='floor') nearest_mode='floor')
......
...@@ -1281,21 +1281,19 @@ TEST_CASE(resize_downsample_f_dyn_test) ...@@ -1281,21 +1281,19 @@ TEST_CASE(resize_downsample_f_dyn_test)
migraphx::parameter_map pp; migraphx::parameter_map pp;
pp["X"] = migraphx::argument(sx, dx.data()); pp["X"] = migraphx::argument(sx, dx.data());
p.debug_print();
auto result = p.eval(pp).back(); auto result = p.eval(pp).back();
std::vector<float> result_vector; std::vector<float> result_vector;
result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); }); result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); });
printf("result_vector has size %lu: \n", result_vector.size());
for(float aa : result_vector) printf (" %f ", aa);printf("\n");
// clang-format off // clang-format off
// TODO: gold value includes floating-point rounding errors std::vector<float> gold = {
std::vector<float> gold = {0.100000, 1.100000, 3.100000, 4.100000, 6.100000, 0.1f, 1.1f, 3.1f, 4.1f, 6.1f,
9.100000, 10.100000, 12.100000, 13.100000, 15.100000, 9.1f, 10.1f, 12.1f, 13.1f, 15.1f,
27.100000, 28.100000, 30.100000, 31.100000, 33.099998, 27.1f, 28.1f, 30.1f, 31.1f, 33.1f,
45.099998, 46.099998, 48.099998, 49.099998, 51.099998, 45.1f, 46.1f, 48.1f, 49.1f, 51.1f,
54.099998, 55.099998, 57.099998, 58.099998, 60.099998, 54.1f, 55.1f, 57.1f, 58.1f, 60.1f,
72.099998, 73.099998, 75.099998, 76.099998, 78.099998}; 72.1f, 73.1f, 75.1f, 76.1f, 78.1f};
// clang-format on // clang-format on
EXPECT(migraphx::verify::verify_range(result_vector, gold)); EXPECT(migraphx::verify::verify_range(result_vector, gold));
......
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