MCPcopy Create free account
hub / github.com/ROCm/AMDMIGraphX / apply

Method apply

src/simplify_dyn_ops.cpp:93–159  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91 }
92
93 void apply(module& m, const match::matcher_result& mr) const
94 {
95 auto ins = mr.result;
96 auto inputs = ins->inputs();
97 auto resize_op = any_cast<op::resize>(ins->get_operator());
98
99 auto in_lens = inputs.at(0)->get_shape().lens();
100 std::vector<size_t> sizes_vec(inputs.at(0)->get_shape().ndim());
101 std::vector<float> scales_vec(inputs.at(0)->get_shape().ndim());
102 // populate both scales and sizes for the benefit of the algorithm.
103 inputs.at(1)->eval().visit([&](auto input) {
104 using type = typename decltype(input)::value_type;
105 if constexpr(std::is_integral<type>{})
106 {
107 // read output sizes and use them to compute scales
108 sizes_vec.assign(input.begin(), input.end());
109 std::transform(
110 input.begin(),
111 input.end(),
112 in_lens.begin(),
113 scales_vec.begin(),
114 [](auto sz, size_t in_len) { return static_cast<float>(sz) / in_len; });
115 }
116 else
117 {
118 // read scales and use them to compute output sizes
119 scales_vec.assign(input.begin(), input.end());
120 std::transform(
121 input.begin(),
122 input.end(),
123 in_lens.begin(),
124 sizes_vec.begin(),
125 [](auto sz, size_t in_len) { return static_cast<size_t>(sz * in_len); });
126 }
127 });
128
129 auto in_s = inputs.at(0)->get_shape();
130 shape out_s{in_s.type(), sizes_vec};
131
132 std::vector<int> ind(out_s.elements());
133
134 // map out_idx to in_idx
135 auto nearest_op = op::resize::get_nearest_op(resize_op.nearest_mode);
136 auto idx_op = op::resize::get_original_idx_op(resize_op.coordinate_transformation_mode);
137
138 shape_for_each(out_s, [&](const auto& out_idx_v, size_t out_idx) {
139 std::vector<size_t> in_idx(out_idx_v.size());
140 for(auto ii = 0; ii < in_lens.size(); ++ii)
141 {
142 auto idx_val = idx_op(in_lens[ii], sizes_vec[ii], out_idx_v[ii], scales_vec[ii]);
143 in_idx[ii] = nearest_op(in_lens[ii], idx_val);
144 }
145
146 ind[out_idx] = static_cast<int64_t>(in_s.index(in_idx));
147 });
148
149 // reshape input to one-dimension
150 std::vector<int64_t> rsp_lens = {static_cast<int64_t>(in_s.elements())};

Callers

nothing calls this directly

Calls 15

shape_for_eachFunction · 0.85
lensMethod · 0.80
atMethod · 0.80
ndimMethod · 0.80
insert_instructionMethod · 0.80
replace_instructionMethod · 0.80
make_opFunction · 0.70
transformFunction · 0.50
literalClass · 0.50
inputsMethod · 0.45
get_shapeMethod · 0.45
visitMethod · 0.45

Tested by

no test coverage detected