|
7 | 7 | */ |
8 | 8 | #include <executorch/examples/models/muse-glimmer/runtime/cuda_dflash_executor.h> |
9 | 9 |
|
| 10 | +#include <executorch/examples/models/muse-glimmer/runtime/model_embeddings.h> |
| 11 | + |
10 | 12 | #include <cuda_runtime.h> |
11 | 13 | #include <executorch/backends/cuda/runtime/backend_options.h> |
12 | 14 | #include <executorch/extension/cuda/caller_stream.h> |
@@ -462,6 +464,77 @@ MuseGlimmerCudaDFlashExecutor::~MuseGlimmerCudaDFlashExecutor() { |
462 | 464 | } |
463 | 465 | } |
464 | 466 |
|
| 467 | +Result<std::shared_ptr<const MuseGlimmerPreparationSpec>> make_cuda_preparation( |
| 468 | + cb::CudaExecutor& target, |
| 469 | + const MuseGlimmerBackendConfig& config, |
| 470 | + std::int32_t max_context, |
| 471 | + std::shared_ptr<MuseGlimmerModelEmbeddings>* embeddings) { |
| 472 | + ET_ASSIGN_OR_RETURN(names, target.module().method_names()); |
| 473 | + const bool has_vision = names.count("vision_encoder") != 0; |
| 474 | + const auto hidden = |
| 475 | + static_cast<std::int32_t>(target.embeddings_hidden_size()); |
| 476 | + std::int64_t max_soft_tokens = 0; |
| 477 | + if (has_vision) { |
| 478 | + ET_CHECK_OR_RETURN_ERROR( |
| 479 | + hidden > 0, |
| 480 | + InvalidProgram, |
| 481 | + "Muse Glimmer CUDA: vision needs %s", |
| 482 | + cb::kEmbeddingsMethod); |
| 483 | + ET_ASSIGN_OR_RETURN( |
| 484 | + soft_tokens, |
| 485 | + muse_glimmer_abi::check_vision_abi( |
| 486 | + target.module(), |
| 487 | + hidden, |
| 488 | + aten::ScalarType::BFloat16, |
| 489 | + config.max_vision_patches, |
| 490 | + ::executorch::backends::cuda::kCudaBackendId)); |
| 491 | + max_soft_tokens = soft_tokens; |
| 492 | + } |
| 493 | + auto spec = std::make_shared<const MuseGlimmerPreparationSpec>( |
| 494 | + MuseGlimmerPreparationSpec{ |
| 495 | + aten::ScalarType::BFloat16, |
| 496 | + has_vision ? hidden : 0, |
| 497 | + max_context, |
| 498 | + static_cast<std::int32_t>(target.preferred_batch_tokens()), |
| 499 | + target.vocab_size(), |
| 500 | + has_vision, |
| 501 | + max_soft_tokens, |
| 502 | + config.image_limits, |
| 503 | + config.bos_id}); |
| 504 | + if (!has_vision) { |
| 505 | + ET_CHECK_OR_RETURN_ERROR( |
| 506 | + config.pos_embed_path.empty(), |
| 507 | + InvalidArgument, |
| 508 | + "Muse Glimmer CUDA: the program has no vision_encoder, but --pos-embed-path was given (%s)", |
| 509 | + config.pos_embed_path.c_str()); |
| 510 | + return spec; |
| 511 | + } |
| 512 | + auto path = config.pos_embed_path; |
| 513 | + if (path.empty()) { |
| 514 | + const auto slash = config.model_path.find_last_of('/'); |
| 515 | + path = (slash == std::string::npos ? "." |
| 516 | + : config.model_path.substr(0, slash)) + |
| 517 | + "/pos_embed.bin"; |
| 518 | + } |
| 519 | + auto model_embeddings = |
| 520 | + std::make_shared<MuseGlimmerModelEmbeddings>(spec, std::move(path)); |
| 521 | + Module* module = &target.module(); |
| 522 | + ET_CHECK_OK_OR_RETURN_ERROR(target.set_input_embedder( |
| 523 | + {[model_embeddings, module] { |
| 524 | + return model_embeddings->initialize(*module); |
| 525 | + }, |
| 526 | + [model_embeddings](const batching::PreparedInput& input) { |
| 527 | + return model_embeddings->accepts(input); |
| 528 | + }, |
| 529 | + [model_embeddings, module](const std::vector<batching::Input>& inputs) { |
| 530 | + return model_embeddings->materialize(*module, inputs); |
| 531 | + }})); |
| 532 | + if (embeddings) { |
| 533 | + *embeddings = std::move(model_embeddings); |
| 534 | + } |
| 535 | + return spec; |
| 536 | +} |
| 537 | + |
465 | 538 | Result<MuseGlimmerBackend> MuseGlimmerCudaDFlashExecutor::create( |
466 | 539 | const MuseGlimmerBackendConfig& config) { |
467 | 540 | ET_CHECK_OR_RETURN_ERROR( |
|
0 commit comments