|
8 | 8 |
|
9 | 9 | #include <executorch/backends/vulkan/runtime/api/Context.h>
|
10 | 10 |
|
| 11 | +#ifdef VULKAN_DEBUG |
| 12 | +#include <iomanip> |
| 13 | +#include <iostream> |
| 14 | +#endif // VULKAN_DEBUG |
| 15 | + |
11 | 16 | #ifndef VULKAN_DESCRIPTOR_POOL_SIZE
|
12 | 17 | #define VULKAN_DESCRIPTOR_POOL_SIZE 1024u
|
13 | 18 | #endif
|
@@ -261,5 +266,220 @@ Context* context() {
|
261 | 266 | return context.get();
|
262 | 267 | }
|
263 | 268 |
|
| 269 | +#ifdef VULKAN_DEBUG |
| 270 | + |
| 271 | +#ifdef VK_KHR_pipeline_executable_properties |
| 272 | + |
| 273 | +VkPipeline Context::get_shader_pipeline( |
| 274 | + const vkapi::ShaderInfo& shader, |
| 275 | + const vkapi::SpecVarList& spec_constants) { |
| 276 | + const uint32_t push_constants_size = 128u; |
| 277 | + |
| 278 | + VkDescriptorSetLayout shader_layout = |
| 279 | + shader_layout_cache().retrieve(shader.kernel_layout); |
| 280 | + VkPipelineLayout pipeline_layout = |
| 281 | + pipeline_layout_cache().retrieve(shader_layout, push_constants_size); |
| 282 | + |
| 283 | + vkapi::SpecVarList spec_constants_full_list = {4u, 4u, 1u}; |
| 284 | + spec_constants_full_list.append(spec_constants); |
| 285 | + |
| 286 | + VkPipeline pipeline = pipeline_cache().retrieve( |
| 287 | + {pipeline_layout, |
| 288 | + shader_cache().retrieve(shader), |
| 289 | + spec_constants_full_list}); |
| 290 | + |
| 291 | + return pipeline; |
| 292 | +} |
| 293 | + |
| 294 | +std::vector<VkPipelineExecutablePropertiesKHR> |
| 295 | +Context::get_pipeline_executable_props(const VkPipeline pipeline) { |
| 296 | + VkPipelineInfoKHR pipeline_info{ |
| 297 | + VK_STRUCTURE_TYPE_PIPELINE_INFO_KHR, |
| 298 | + nullptr, |
| 299 | + pipeline, |
| 300 | + }; |
| 301 | + |
| 302 | + uint32_t shader_props_count = 0u; |
| 303 | + vkGetPipelineExecutablePropertiesKHR( |
| 304 | + device(), &pipeline_info, &shader_props_count, nullptr); |
| 305 | + |
| 306 | + std::vector<VkPipelineExecutablePropertiesKHR> pipeline_props( |
| 307 | + shader_props_count); |
| 308 | + for (int i = 0; i < shader_props_count; i++) { |
| 309 | + pipeline_props.at(i).sType = |
| 310 | + VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_PROPERTIES_KHR; |
| 311 | + pipeline_props.at(i).pNext = nullptr; |
| 312 | + } |
| 313 | + vkGetPipelineExecutablePropertiesKHR( |
| 314 | + device(), &pipeline_info, &shader_props_count, pipeline_props.data()); |
| 315 | + |
| 316 | + return pipeline_props; |
| 317 | +} |
| 318 | + |
| 319 | +std::tuple< |
| 320 | + std::vector<VkPipelineExecutableInternalRepresentationKHR>, |
| 321 | + std::vector<std::vector<char>>> |
| 322 | +Context::get_shader_executable_irs( |
| 323 | + const VkPipeline pipeline, |
| 324 | + const uint32_t pipeline_exec_idx) { |
| 325 | + VkPipelineExecutableInfoKHR exec_info{ |
| 326 | + VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_INFO_KHR, |
| 327 | + nullptr, |
| 328 | + pipeline, |
| 329 | + pipeline_exec_idx, |
| 330 | + }; |
| 331 | + |
| 332 | + uint32_t ir_count; |
| 333 | + VK_CHECK(vkGetPipelineExecutableInternalRepresentationsKHR( |
| 334 | + device(), &exec_info, &ir_count, nullptr)); |
| 335 | + |
| 336 | + std::vector<VkPipelineExecutableInternalRepresentationKHR> irs(ir_count); |
| 337 | + for (int i = 0; i < ir_count; i++) { |
| 338 | + irs.at(i).sType = |
| 339 | + VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_INTERNAL_REPRESENTATION_KHR; |
| 340 | + irs.at(i).pNext = nullptr; |
| 341 | + irs.at(i).pData = nullptr; |
| 342 | + } |
| 343 | + VK_CHECK(vkGetPipelineExecutableInternalRepresentationsKHR( |
| 344 | + device(), &exec_info, &ir_count, irs.data())); |
| 345 | + |
| 346 | + std::vector<std::vector<char>> irs_data(ir_count); |
| 347 | + for (int i = 0; i < ir_count; i++) { |
| 348 | + irs_data.at(i).resize(irs.at(i).dataSize); |
| 349 | + irs.at(i).pData = irs_data.at(i).data(); |
| 350 | + } |
| 351 | + VK_CHECK(vkGetPipelineExecutableInternalRepresentationsKHR( |
| 352 | + device(), &exec_info, &ir_count, irs.data())); |
| 353 | + |
| 354 | + return std::make_tuple(irs, irs_data); |
| 355 | +} |
| 356 | + |
| 357 | +std::vector<VkPipelineExecutableStatisticKHR> |
| 358 | +Context::get_shader_executable_stats( |
| 359 | + const VkPipeline pipeline, |
| 360 | + const uint32_t pipeline_exec_idx) { |
| 361 | + VkPipelineExecutableInfoKHR exec_info{ |
| 362 | + VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_INFO_KHR, |
| 363 | + nullptr, |
| 364 | + pipeline, |
| 365 | + pipeline_exec_idx, |
| 366 | + }; |
| 367 | + |
| 368 | + uint32_t stats_count; |
| 369 | + VK_CHECK(vkGetPipelineExecutableStatisticsKHR( |
| 370 | + device(), &exec_info, &stats_count, NULL)); |
| 371 | + |
| 372 | + std::vector<VkPipelineExecutableStatisticKHR> shader_stats(stats_count); |
| 373 | + for (int i = 0; i < stats_count; i++) { |
| 374 | + shader_stats.at(i).sType = |
| 375 | + VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_STATISTIC_KHR; |
| 376 | + shader_stats.at(i).pNext = nullptr; |
| 377 | + } |
| 378 | + vkGetPipelineExecutableStatisticsKHR( |
| 379 | + device(), &exec_info, &stats_count, shader_stats.data()); |
| 380 | + |
| 381 | + return shader_stats; |
| 382 | +} |
| 383 | + |
| 384 | +std::ostream& operator<<( |
| 385 | + std::ostream& os, |
| 386 | + const VkPipelineExecutablePropertiesKHR& props) { |
| 387 | + os << std::left << std::setw(10) << "name: " << props.name << std::endl; |
| 388 | + os << std::left << std::setw(10) << "descr: " << props.description |
| 389 | + << std::endl; |
| 390 | + os << std::left << std::setw(10) << "subgroup: " << props.subgroupSize |
| 391 | + << std::endl; |
| 392 | + |
| 393 | + return os; |
| 394 | +} |
| 395 | + |
| 396 | +std::ostream& operator<<( |
| 397 | + std::ostream& os, |
| 398 | + const VkPipelineExecutableInternalRepresentationKHR& ir) { |
| 399 | + os << std::left << std::setw(10) << "descr: " << ir.description << std::endl; |
| 400 | + os << std::left << std::setw(10) << "isText: " << ir.isText << std::endl; |
| 401 | + os << std::left << std::setw(10) << "size: " << ir.dataSize << std::endl; |
| 402 | + if (ir.isText) { |
| 403 | + os << "text:" << std::endl; |
| 404 | + char* str = (char*)ir.pData; |
| 405 | + os << str << std::endl; |
| 406 | + } |
| 407 | + return os; |
| 408 | +} |
| 409 | + |
| 410 | +std::ostream& operator<<( |
| 411 | + std::ostream& os, |
| 412 | + VkPipelineExecutableStatisticKHR& stat) { |
| 413 | + os << stat.name << ": "; |
| 414 | + switch (stat.format) { |
| 415 | + case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_BOOL32_KHR: |
| 416 | + os << (stat.value.b32 ? "true" : "false") << std::endl; |
| 417 | + break; |
| 418 | + case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_INT64_KHR: |
| 419 | + os << stat.value.i64 << std::endl; |
| 420 | + break; |
| 421 | + case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR: |
| 422 | + os << stat.value.u64 << std::endl; |
| 423 | + break; |
| 424 | + case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_FLOAT64_KHR: |
| 425 | + os << stat.value.f64 << std::endl; |
| 426 | + break; |
| 427 | + default: |
| 428 | + break; |
| 429 | + } |
| 430 | + os << " " << stat.description << std::endl; |
| 431 | + return os; |
| 432 | +} |
| 433 | + |
| 434 | +std::ostream& operator<<( |
| 435 | + std::ostream& os, |
| 436 | + std::vector<VkPipelineExecutableStatisticKHR>& shader_stats) { |
| 437 | + for (int i = 0; i < shader_stats.size(); i++) { |
| 438 | + VkPipelineExecutableStatisticKHR& stat = shader_stats.at(i); |
| 439 | + os << stat; |
| 440 | + } |
| 441 | + return os; |
| 442 | +} |
| 443 | + |
| 444 | +void Context::print_shader_executable_properties( |
| 445 | + const vkapi::ShaderInfo& shader, |
| 446 | + const vkapi::SpecVarList& spec_constants) { |
| 447 | + VkPipeline pipeline = get_shader_pipeline(shader, spec_constants); |
| 448 | + |
| 449 | + std::vector<VkPipelineExecutablePropertiesKHR> pipeline_props_list = |
| 450 | + get_pipeline_executable_props(pipeline); |
| 451 | + |
| 452 | + VK_CHECK_COND(pipeline_props_list.size() == 1u); |
| 453 | + |
| 454 | + std::cout << pipeline_props_list.at(0) << std::endl; |
| 455 | + |
| 456 | + std::tuple< |
| 457 | + std::vector<VkPipelineExecutableInternalRepresentationKHR>, |
| 458 | + std::vector<std::vector<char>>> |
| 459 | + irs_and_irs_data = get_shader_executable_irs(pipeline, 0u); |
| 460 | + |
| 461 | + std::vector<VkPipelineExecutableInternalRepresentationKHR>& irs = |
| 462 | + std::get<0>(irs_and_irs_data); |
| 463 | + |
| 464 | + std::cout << "Found " << irs.size() << " IRs" << std::endl << std::endl; |
| 465 | + for (int i = 0; i < irs.size(); i++) { |
| 466 | + std::cout << "====== IR " << i << ": " << irs.at(i).name |
| 467 | + << " ======" << std::endl; |
| 468 | + std::cout << irs.at(i) << std::endl; |
| 469 | + } |
| 470 | + |
| 471 | + std::vector<VkPipelineExecutableStatisticKHR> shader_stats = |
| 472 | + get_shader_executable_stats(pipeline, 0u); |
| 473 | + std::cout << "Found " << shader_stats.size() << " Statistics" << std::endl; |
| 474 | + if (shader_stats.size() > 0) { |
| 475 | + std::cout << "====== Statistics: ======" << std::endl; |
| 476 | + std::cout << shader_stats << std::endl; |
| 477 | + } |
| 478 | +} |
| 479 | + |
| 480 | +#endif // VK_KHR_pipeline_executable_properties |
| 481 | + |
| 482 | +#endif // VULKAN_DEBUG |
| 483 | + |
264 | 484 | } // namespace api
|
265 | 485 | } // namespace vkcompute
|
0 commit comments