| 250 | |
| 251 | template <typename Opr, typename T, typename Proxy> |
| 252 | float BenchmarkerBase<Opr, T, Proxy>::exect(const TensorValueArray& testcase_in) { |
| 253 | auto opr = this->opr(); |
| 254 | opr->param() = m_param; |
| 255 | TensorLayoutArray layouts; |
| 256 | TensorNDArray tensors_cur_host; |
| 257 | for (auto& inp : testcase_in) { |
| 258 | layouts.push_back(inp.layout); |
| 259 | tensors_cur_host.emplace_back(inp); |
| 260 | } |
| 261 | auto user_layouts = layouts; |
| 262 | m_proxy->deduce_layout(opr, layouts); |
| 263 | for (size_t i = 0; i < layouts.size(); ++i) |
| 264 | if (user_layouts[i].ndim > 0) { |
| 265 | auto run = [&]() { |
| 266 | ASSERT_TRUE(layouts[i].eq_shape(user_layouts[i])) |
| 267 | << "User provided shape is " |
| 268 | << user_layouts[i].TensorShape::to_string() |
| 269 | << "\nExpected shape is " |
| 270 | << layouts[i].TensorShape::to_string(); |
| 271 | }; |
| 272 | run(); |
| 273 | } |
| 274 | auto allocate = [&layouts](Handle* handle) { |
| 275 | TensorNDArray tensors(layouts.size()); |
| 276 | auto trans_func = [handle](const TensorLayout& layout) { |
| 277 | auto span = layout.span(); |
| 278 | TensorND res; |
| 279 | res.reset_ptr( |
| 280 | static_cast<uint8_t*>(megdnn_malloc(handle, span.dist_byte())) - |
| 281 | span.low_byte); |
| 282 | res.layout = layout; |
| 283 | return res; |
| 284 | }; |
| 285 | std::transform(layouts.begin(), layouts.end(), tensors.begin(), trans_func); |
| 286 | return tensors; |
| 287 | }; |
| 288 | auto tensors_cur = allocate(m_handle); |
| 289 | //! init |
| 290 | for (size_t i = 0; i < tensors_cur_host.size(); ++i) { |
| 291 | TensorND& tensor = tensors_cur_host[i]; |
| 292 | auto size = tensor.layout.span().high_byte; |
| 293 | if (tensor.layout.ndim == 0) |
| 294 | continue; |
| 295 | megdnn_memcpy_H2D(m_handle, tensors_cur[i].raw_ptr(), tensor.raw_ptr(), size); |
| 296 | } |
| 297 | if (m_before_exec_callback) { |
| 298 | m_before_exec_callback(opr, tensors_cur); |
| 299 | } |
| 300 | //! init weights |
| 301 | m_proxy->init(opr, tensors_cur); |
| 302 | //! run |
| 303 | //! warm up |
| 304 | m_proxy->exec(opr, tensors_cur); |
| 305 | megcoreSynchronize(m_handle->megcore_computing_handle()); |
| 306 | |
| 307 | if (m_adaptive_secs) { |
| 308 | //! find m_times for adaptive benchmarking |
| 309 | m_times = 0; |
no test coverage detected