Spaces:
Running
Running
File size: 32,920 Bytes
5f923cd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 | // Copyright 2025 The ODML Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "runtime/components/sampler_factory.h"
#include <cstdlib>
#include <memory>
#include <optional>
#include <random>
#include <utility>
#include "absl/base/attributes.h" // from @com_google_absl
#include "absl/base/nullability.h" // from @com_google_absl
#include "absl/cleanup/cleanup.h" // from @com_google_absl
#include "absl/log/absl_check.h" // from @com_google_absl
#include "absl/log/absl_log.h" // from @com_google_absl
#include "absl/memory/memory.h" // from @com_google_absl
#include "absl/status/status.h" // from @com_google_absl
#include "absl/status/statusor.h" // from @com_google_absl
#include "absl/strings/str_cat.h" // from @com_google_absl
#include "litert/cc/internal/litert_handle.h" // from @litert
#include "litert/cc/internal/litert_shared_library.h" // from @litert
#include "litert/cc/litert_environment.h" // from @litert
#include "litert/cc/litert_environment_options.h" // from @litert
#include "litert/cc/litert_macros.h" // from @litert
#include "litert/cc/litert_tensor_buffer.h" // from @litert
#include "runtime/components/sampler.h"
#include "runtime/components/top_p_cpu_sampler.h"
#include "runtime/executor/executor_settings_base.h"
#include "runtime/proto/sampler_params.pb.h"
#include "runtime/util/status_macros.h" // IWYU pragma: keep
namespace litert::lm {
namespace {
// Common type definitions for sampler C APIs.
using LiteRtTopKSampler_Sampler = void;
using LiteRtTopKSampler_ActivationDataType = void;
using LiteRtTopKSampler_SamplerParameters = void;
// OpenCL Sampler C API function pointers.
extern "C" int (*LiteRtTopKOpenClSampler_Create_Static)(
LiteRtEnvironment env, int batch_size, int sequence_size, int vocab_size,
const LiteRtTopKSampler_ActivationDataType* activation_data_type,
const LiteRtTopKSampler_SamplerParameters* sampler_params,
LiteRtTopKSampler_Sampler** sampler_out, char** error_msg) = nullptr;
extern "C" void (*LiteRtTopKOpenClSampler_Destroy_Static)(
LiteRtTopKSampler_Sampler* sampler) = nullptr;
extern "C" int (*LiteRtTopKOpenClSampler_SampleToIdAndScoreBuffer_Static)(
LiteRtTopKSampler_Sampler* sampler, LiteRtTensorBuffer logits_tensor,
LiteRtTensorBuffer ids_tensor, const LiteRtTensorBuffer* scores_tensor,
char** error_msg) = nullptr;
extern "C" int (*LiteRtTopKOpenClSampler_UpdateConfig_Static)(
LiteRtTopKSampler_Sampler* sampler,
const LiteRtTopKSampler_SamplerParameters* sampler_params, int batch_size,
void* rand_gen_shared_ptr, char** error_msg) = nullptr;
// WebGPU Sampler C API function pointers.
extern "C" int (*LiteRtTopKWebGpuSampler_Create_Static)(
LiteRtEnvironment env, int batch_size, int sequence_size, int vocab_size,
const LiteRtTopKSampler_ActivationDataType* activation_data_type,
const LiteRtTopKSampler_SamplerParameters* sampler_params,
LiteRtTopKSampler_Sampler** sampler_out, char** error_msg) = nullptr;
extern "C" void (*LiteRtTopKWebGpuSampler_Destroy_Static)(
LiteRtTopKSampler_Sampler* sampler) = nullptr;
extern "C" int (*LiteRtTopKWebGpuSampler_SampleToIdAndScoreBuffer_Static)(
LiteRtTopKSampler_Sampler* sampler, LiteRtTensorBuffer logits_tensor,
LiteRtTensorBuffer ids_tensor, const LiteRtTensorBuffer* scores_tensor,
char** error_msg) = nullptr;
extern "C" int (*LiteRtTopKWebGpuSampler_UpdateConfig_Static)(
LiteRtTopKSampler_Sampler* sampler,
const LiteRtTopKSampler_SamplerParameters* sampler_params, int batch_size,
void* rand_gen_shared_ptr, char** error_msg) = nullptr;
extern "C" int (*LiteRtTopKWebGpuSampler_CanHandleInput_Static)(
LiteRtTopKSampler_Sampler* sampler) = nullptr;
extern "C" int (*LiteRtTopKWebGpuSampler_HandlesInput_Static)(
LiteRtTopKSampler_Sampler* sampler) = nullptr;
extern "C" int (
*LiteRtTopKWebGpuSampler_SetInputTensorsAndInferenceFunc_Static)(
LiteRtTopKSampler_Sampler* sampler,
LiteRtTensorBuffer absl_nullable ids_tensor,
LiteRtTensorBuffer absl_nullable prev_input_positions_tensor,
LiteRtTensorBuffer absl_nullable input_positions_tensor,
LiteRtTensorBuffer absl_nullable prev_mask_tensor,
LiteRtTensorBuffer absl_nullable mask_tensor,
int (*run_inference_func)(void* arg), void* arg,
char** error_msg) = nullptr;
// Metal Sampler C API function pointers.
extern "C" int (*LiteRtTopKMetalSampler_Create_Static)(
LiteRtEnvironment env, int batch_size, int sequence_size, int vocab_size,
const LiteRtTopKSampler_ActivationDataType* activation_data_type,
const LiteRtTopKSampler_SamplerParameters* sampler_params,
LiteRtTopKSampler_Sampler** sampler_out, char** error_msg) = nullptr;
extern "C" void (*LiteRtTopKMetalSampler_Destroy_Static)(
LiteRtTopKSampler_Sampler* sampler) = nullptr;
extern "C" int (*LiteRtTopKMetalSampler_SampleToIdAndScoreBuffer_Static)(
LiteRtTopKSampler_Sampler* sampler, LiteRtTensorBuffer logits_tensor,
LiteRtTensorBuffer ids_tensor, const LiteRtTensorBuffer* scores_tensor,
char** error_msg) = nullptr;
extern "C" int (*LiteRtTopKMetalSampler_UpdateConfig_Static)(
LiteRtTopKSampler_Sampler* sampler,
const LiteRtTopKSampler_SamplerParameters* sampler_params, int batch_size,
void* rand_gen_shared_ptr, char** error_msg) = nullptr;
extern "C" int (*LiteRtTopKMetalSampler_CanHandleInput_Static)(
LiteRtTopKSampler_Sampler* sampler) = nullptr;
extern "C" int (*LiteRtTopKMetalSampler_HandlesInput_Static)(
LiteRtTopKSampler_Sampler* sampler) = nullptr;
extern "C" int (*LiteRtTopKMetalSampler_SetInputTensorsAndInferenceFunc_Static)(
LiteRtTopKSampler_Sampler* sampler,
LiteRtTensorBuffer absl_nullable ids_tensor,
LiteRtTensorBuffer absl_nullable prev_input_positions_tensor,
LiteRtTensorBuffer absl_nullable input_positions_tensor,
LiteRtTensorBuffer absl_nullable prev_mask_tensor,
LiteRtTensorBuffer absl_nullable mask_tensor,
int (*run_inference_func)(void* arg), void* arg,
char** error_msg) = nullptr;
absl::Status CreateStatus(int error_code, const char* error_msg) {
absl::StatusCode code = static_cast<absl::StatusCode>(error_code);
return absl::Status(code, error_msg);
}
absl::Status CreateStatusAndFreeErrorMsg(int error_code, char* error_msg) {
absl::Cleanup cleanup = [error_msg] { free(error_msg); };
return error_code == 0 ? absl::OkStatus()
: CreateStatus(error_code, error_msg);
}
// A base wrapper of TopK Sampler C API functions.
class TopKCApiSampler : public Sampler {
public:
using LiteRtTopKSampler_Create = int (*)(
LiteRtEnvironment env, int batch_size, int sequence_size, int vocab_size,
const LiteRtTopKSampler_ActivationDataType* absl_nullable
activation_data_type,
const LiteRtTopKSampler_SamplerParameters* absl_nullable sampler_params,
LiteRtTopKSampler_Sampler** sampler_out, char** absl_nullable error_msg);
using LiteRtTopKSampler_Destroy =
void (*)(LiteRtTopKSampler_Sampler* sampler);
using LiteRtTopKSampler_SampleToIdAndScoreBuffer =
int (*)(LiteRtTopKSampler_Sampler* sampler,
LiteRtTensorBuffer logits_tensor, LiteRtTensorBuffer ids_tensor,
const LiteRtTensorBuffer* absl_nullable scores_tensor,
char** absl_nullable error_msg);
using LiteRtTopKSampler_UpdateConfig = int (*)(
LiteRtTopKSampler_Sampler* sampler,
const LiteRtTopKSampler_SamplerParameters* sampler_params, int batch_size,
void* absl_nullable rand_gen_shared_ptr, char** absl_nullable error_msg);
using LiteRtTopKSampler_CanHandleInput =
int (*)(LiteRtTopKSampler_Sampler* sampler);
using LiteRtTopKSampler_HandlesInput =
int (*)(LiteRtTopKSampler_Sampler* sampler);
using LiteRtTopKSampler_SetInputTensorsAndInferenceFunc =
int (*)(LiteRtTopKSampler_Sampler* sampler,
LiteRtTensorBuffer absl_nullable ids_tensor,
LiteRtTensorBuffer absl_nullable prev_input_positions_tensor,
LiteRtTensorBuffer absl_nullable input_positions_tensor,
LiteRtTensorBuffer absl_nullable prev_mask_tensor,
LiteRtTensorBuffer absl_nullable mask_tensor,
int (*run_inference_func)(void* arg), void* arg,
char** absl_nullable error_msg);
struct TopKSamplerCApi {
std::optional<SharedLibrary> lib;
LiteRtTopKSampler_Create create_func;
LiteRtTopKSampler_Destroy destroy_func;
LiteRtTopKSampler_SampleToIdAndScoreBuffer sample_func;
LiteRtTopKSampler_UpdateConfig update_config_func;
LiteRtTopKSampler_CanHandleInput can_handle_input_func;
LiteRtTopKSampler_HandlesInput handles_input_func;
LiteRtTopKSampler_SetInputTensorsAndInferenceFunc set_input_tensors_func;
TopKSamplerCApi(
std::optional<SharedLibrary> lib, LiteRtTopKSampler_Create create_func,
LiteRtTopKSampler_Destroy destroy_func,
LiteRtTopKSampler_SampleToIdAndScoreBuffer sample_func,
LiteRtTopKSampler_UpdateConfig update_config_func,
LiteRtTopKSampler_CanHandleInput can_handle_input_func = nullptr,
LiteRtTopKSampler_HandlesInput handles_input_func = nullptr,
LiteRtTopKSampler_SetInputTensorsAndInferenceFunc
set_input_tensors_func = nullptr)
: lib(std::move(lib)),
create_func(create_func),
destroy_func(destroy_func),
sample_func(sample_func),
update_config_func(update_config_func),
can_handle_input_func(can_handle_input_func),
handles_input_func(handles_input_func),
set_input_tensors_func(set_input_tensors_func) {}
};
~TopKCApiSampler() override { capi_->destroy_func(sampler_); }
absl::Status SampleToIdAndScoreBuffer(const TensorBuffer& logits_tensor,
TensorBuffer& ids_tensor,
TensorBuffer* scores_tensor) override {
char* error_msg = nullptr;
LiteRtTensorBuffer scores_tensor_capi = nullptr;
if (scores_tensor != nullptr) {
scores_tensor_capi = scores_tensor->Get();
}
int error_code = capi_->sample_func(
sampler_, logits_tensor.Get(), ids_tensor.Get(),
scores_tensor_capi ? &scores_tensor_capi : nullptr, &error_msg);
return CreateStatusAndFreeErrorMsg(error_code, error_msg);
}
absl::Status UpdateConfig(
const proto::SamplerParameters& sampler_params, int batch_size,
std::shared_ptr<std::default_random_engine> rand_gen) override {
char* error_msg = nullptr;
int error_code = capi_->update_config_func(
sampler_, &sampler_params, batch_size, &rand_gen, &error_msg);
return CreateStatusAndFreeErrorMsg(error_code, error_msg);
}
bool CanHandleInput() const override {
return capi_->can_handle_input_func
? static_cast<bool>(capi_->can_handle_input_func(sampler_))
: false;
}
bool HandlesInput() const override {
return capi_->handles_input_func
? static_cast<bool>(capi_->handles_input_func(sampler_))
: false;
}
absl::Status SetInputTensorsAndInferenceFunc(
const TensorBuffer* ids_tensor,
const TensorBuffer* prev_input_positions_tensor,
const TensorBuffer* input_positions_tensor,
const TensorBuffer* prev_mask_tensor, const TensorBuffer* mask_tensor,
int (*run_inference_func)(void* arg), void* arg) override {
if (!capi_->set_input_tensors_func) {
return absl::UnimplementedError("SetInputTensors is not implemented.");
}
char* error_msg = nullptr;
int error_code = capi_->set_input_tensors_func(
sampler_, ids_tensor ? ids_tensor->Get() : nullptr,
prev_input_positions_tensor ? prev_input_positions_tensor->Get()
: nullptr,
input_positions_tensor ? input_positions_tensor->Get() : nullptr,
prev_mask_tensor ? prev_mask_tensor->Get() : nullptr,
mask_tensor ? mask_tensor->Get() : nullptr, run_inference_func, arg,
&error_msg);
return CreateStatusAndFreeErrorMsg(error_code, error_msg);
}
protected:
TopKCApiSampler(std::unique_ptr<TopKSamplerCApi> capi,
LiteRtTopKSampler_Sampler* sampler)
: capi_(std::move(capi)), sampler_(sampler) {}
static absl::StatusOr<std::unique_ptr<TopKSamplerCApi>> GetSamplerCApi(
const char* lib_name, const char* create_func_name,
const char* destroy_func_name, const char* sample_func_name,
const char* update_config_func_name,
const char* can_handle_input_func_name = nullptr,
const char* handles_input_func_name = nullptr,
const char* set_input_tensors_func_name = nullptr) {
// Load Sampler C API library and get the symbols.
LITERT_ASSIGN_OR_RETURN(
auto lib, SharedLibrary::Load(lib_name, RtldFlags::Lazy().Local()));
LITERT_ASSIGN_OR_RETURN(
auto sampler_create_func,
lib.LookupSymbol<LiteRtTopKSampler_Create>(create_func_name));
RET_CHECK_NE(sampler_create_func, nullptr)
<< "Failed to load " << create_func_name;
LITERT_ASSIGN_OR_RETURN(
auto sampler_destroy_func,
lib.LookupSymbol<LiteRtTopKSampler_Destroy>(destroy_func_name));
RET_CHECK_NE(sampler_destroy_func, nullptr)
<< "Failed to load " << destroy_func_name;
LITERT_ASSIGN_OR_RETURN(
auto sampler_sample_func,
lib.LookupSymbol<LiteRtTopKSampler_SampleToIdAndScoreBuffer>(
sample_func_name));
LITERT_ASSIGN_OR_RETURN(auto sampler_update_config_func,
lib.LookupSymbol<LiteRtTopKSampler_UpdateConfig>(
update_config_func_name));
RET_CHECK_NE(sampler_sample_func, nullptr)
<< "Failed to load " << sample_func_name;
LiteRtTopKSampler_CanHandleInput sampler_can_handle_input_func = nullptr;
if (can_handle_input_func_name != nullptr) {
LITERT_ASSIGN_OR_RETURN(
sampler_can_handle_input_func,
lib.LookupSymbol<LiteRtTopKSampler_CanHandleInput>(
can_handle_input_func_name));
RET_CHECK_NE(sampler_can_handle_input_func, nullptr)
<< "Failed to load " << can_handle_input_func_name;
}
LiteRtTopKSampler_HandlesInput sampler_handles_input_func = nullptr;
if (handles_input_func_name != nullptr) {
LITERT_ASSIGN_OR_RETURN(sampler_handles_input_func,
lib.LookupSymbol<LiteRtTopKSampler_HandlesInput>(
handles_input_func_name));
RET_CHECK_NE(sampler_handles_input_func, nullptr)
<< "Failed to load " << handles_input_func_name;
}
LiteRtTopKSampler_SetInputTensorsAndInferenceFunc
sampler_set_input_tensors_func = nullptr;
if (set_input_tensors_func_name != nullptr) {
LITERT_ASSIGN_OR_RETURN(
sampler_set_input_tensors_func,
lib.LookupSymbol<LiteRtTopKSampler_SetInputTensorsAndInferenceFunc>(
set_input_tensors_func_name));
RET_CHECK_NE(sampler_set_input_tensors_func, nullptr)
<< "Failed to load " << set_input_tensors_func_name;
}
return std::make_unique<TopKSamplerCApi>(
std::move(lib), sampler_create_func, sampler_destroy_func,
sampler_sample_func, sampler_update_config_func,
sampler_can_handle_input_func, sampler_handles_input_func,
sampler_set_input_tensors_func);
}
std::unique_ptr<TopKSamplerCApi> capi_;
LiteRtTopKSampler_Sampler* const sampler_;
};
// A wrapper of TopKOpenClSampler C API functions.
class TopKOpenClCApiSampler : public TopKCApiSampler {
public:
static absl::StatusOr<std::unique_ptr<TopKOpenClCApiSampler>> Create(
LiteRtEnvironment env, int batch_size, int sequence_size, int vocab_size,
std::optional<ActivationDataType> activation_data_type,
proto::SamplerParameters sampler_params) {
std::unique_ptr<TopKSamplerCApi> capi;
auto capi_or = GetSamplerCApi(
"libLiteRtTopKOpenClSampler.so", "LiteRtTopKOpenClSampler_Create",
"LiteRtTopKOpenClSampler_Destroy",
"LiteRtTopKOpenClSampler_SampleToIdAndScoreBuffer",
"LiteRtTopKOpenClSampler_UpdateConfig");
if (capi_or.ok()) {
capi = std::move(capi_or.value());
ABSL_LOG(INFO) << "Dynamically loaded LiteRtTopKOpenClSampler C API.";
} else {
if (capi_or.status().code() != absl::StatusCode::kUnavailable) {
return capi_or.status();
}
ABSL_LOG(WARNING) << "OpenCL sampler not available, falling back to "
"statically linked C API: "
<< capi_or.status();
auto static_capi_or = GetStaticTopKOpenClSamplerCApi();
if (!static_capi_or.ok()) {
return capi_or.status();
}
capi = std::move(static_capi_or.value());
ABSL_LOG(INFO) << "Statically linked LiteRtTopKOpenClSampler C API.";
}
LiteRtTopKSampler_Sampler* sampler = nullptr;
char* error_msg = nullptr;
int error_code = capi->create_func(
env, batch_size, sequence_size, vocab_size,
activation_data_type.has_value() ? &activation_data_type.value()
: nullptr,
&sampler_params, &sampler, &error_msg);
RETURN_IF_ERROR(CreateStatusAndFreeErrorMsg(error_code, error_msg));
ABSL_CHECK(sampler);
return absl::WrapUnique(
new TopKOpenClCApiSampler(std::move(capi), sampler));
}
private:
TopKOpenClCApiSampler(std::unique_ptr<TopKSamplerCApi> capi,
LiteRtTopKSampler_Sampler* sampler)
: TopKCApiSampler(std::move(capi), sampler) {}
static absl::StatusOr<std::unique_ptr<TopKSamplerCApi>>
GetStaticTopKOpenClSamplerCApi() {
if (LiteRtTopKOpenClSampler_Create_Static == nullptr ||
LiteRtTopKOpenClSampler_Destroy_Static == nullptr ||
LiteRtTopKOpenClSampler_SampleToIdAndScoreBuffer_Static == nullptr ||
LiteRtTopKOpenClSampler_UpdateConfig_Static == nullptr) {
return absl::UnavailableError(
"Static LiteRtTopKOpenClSampler C API not available.");
}
return std::make_unique<TopKSamplerCApi>(
/*lib=*/std::nullopt, LiteRtTopKOpenClSampler_Create_Static,
LiteRtTopKOpenClSampler_Destroy_Static,
LiteRtTopKOpenClSampler_SampleToIdAndScoreBuffer_Static,
LiteRtTopKOpenClSampler_UpdateConfig_Static);
}
};
// A wrapper of TopKWebGpuSampler C API functions.
class TopKWebGpuCApiSampler : public TopKCApiSampler {
public:
static absl::StatusOr<std::unique_ptr<TopKWebGpuCApiSampler>> Create(
LiteRtEnvironment env, int batch_size, int sequence_size, int vocab_size,
std::optional<ActivationDataType> activation_data_type,
proto::SamplerParameters sampler_params) {
std::unique_ptr<TopKSamplerCApi> capi;
#if defined(_WIN32)
#define SO_EXT ".dll"
#elif defined(__APPLE__)
#define SO_EXT ".dylib"
#else
#define SO_EXT ".so"
#endif
auto capi_or = GetSamplerCApi(
"libLiteRtTopKWebGpuSampler" SO_EXT, "LiteRtTopKWebGpuSampler_Create",
"LiteRtTopKWebGpuSampler_Destroy",
"LiteRtTopKWebGpuSampler_SampleToIdAndScoreBuffer",
"LiteRtTopKWebGpuSampler_UpdateConfig",
"LiteRtTopKWebGpuSampler_CanHandleInput",
"LiteRtTopKWebGpuSampler_HandlesInput",
"LiteRtTopKWebGpuSampler_SetInputTensorsAndInferenceFunc");
if (capi_or.ok()) {
capi = std::move(capi_or.value());
ABSL_LOG(INFO) << "Dynamically loaded LiteRtTopKWebGpuSampler C API.";
} else {
if (capi_or.status().code() != absl::StatusCode::kUnavailable) {
return capi_or.status();
}
ABSL_LOG(WARNING) << "WebGPU sampler not available, falling back to "
"statically linked C API: "
<< capi_or.status();
auto static_capi_or = GetStaticTopKWebGpuSamplerCApi();
if (!static_capi_or.ok()) {
return capi_or.status();
}
capi = std::move(static_capi_or.value());
ABSL_LOG(INFO) << "Statically linked LiteRtTopKWebGpuSampler C API.";
}
LiteRtTopKSampler_Sampler* sampler = nullptr;
char* error_msg = nullptr;
int error_code = capi->create_func(
env, batch_size, sequence_size, vocab_size,
activation_data_type.has_value() ? &activation_data_type.value()
: nullptr,
&sampler_params, &sampler, &error_msg);
RETURN_IF_ERROR(CreateStatusAndFreeErrorMsg(error_code, error_msg));
ABSL_CHECK(sampler);
return absl::WrapUnique(
new TopKWebGpuCApiSampler(std::move(capi), sampler));
}
private:
TopKWebGpuCApiSampler(std::unique_ptr<TopKSamplerCApi> capi,
LiteRtTopKSampler_Sampler* sampler)
: TopKCApiSampler(std::move(capi), sampler) {}
static absl::StatusOr<std::unique_ptr<TopKSamplerCApi>>
GetStaticTopKWebGpuSamplerCApi() {
if (LiteRtTopKWebGpuSampler_Create_Static == nullptr ||
LiteRtTopKWebGpuSampler_Destroy_Static == nullptr ||
LiteRtTopKWebGpuSampler_SampleToIdAndScoreBuffer_Static == nullptr ||
LiteRtTopKWebGpuSampler_UpdateConfig_Static == nullptr ||
LiteRtTopKWebGpuSampler_CanHandleInput_Static == nullptr ||
LiteRtTopKWebGpuSampler_HandlesInput_Static == nullptr ||
LiteRtTopKWebGpuSampler_SetInputTensorsAndInferenceFunc_Static ==
nullptr) {
return absl::UnavailableError(
"Static LiteRtTopKWebGpuSampler C API not available.");
}
return std::make_unique<TopKSamplerCApi>(
/*lib=*/std::nullopt, LiteRtTopKWebGpuSampler_Create_Static,
LiteRtTopKWebGpuSampler_Destroy_Static,
LiteRtTopKWebGpuSampler_SampleToIdAndScoreBuffer_Static,
LiteRtTopKWebGpuSampler_UpdateConfig_Static,
LiteRtTopKWebGpuSampler_CanHandleInput_Static,
LiteRtTopKWebGpuSampler_HandlesInput_Static,
LiteRtTopKWebGpuSampler_SetInputTensorsAndInferenceFunc_Static);
}
};
// A wrapper of TopKMetalSampler C API functions.
class TopKMetalCApiSampler : public TopKCApiSampler {
public:
static absl::StatusOr<std::unique_ptr<TopKMetalCApiSampler>> Create(
LiteRtEnvironment env, int batch_size, int sequence_size, int vocab_size,
std::optional<ActivationDataType> activation_data_type,
proto::SamplerParameters sampler_params) {
std::unique_ptr<TopKSamplerCApi> capi;
// Metal is only supported on Apple platforms (macOS/iOS/tvOS/watchOS).
// The shared library validation will handle platform checks implicitly,
// but typically we expect .dylib on Apple.
auto capi_or = GetSamplerCApi(
"libLiteRtTopKMetalSampler.dylib", "LiteRtTopKMetalSampler_Create",
"LiteRtTopKMetalSampler_Destroy",
"LiteRtTopKMetalSampler_SampleToIdAndScoreBuffer",
"LiteRtTopKMetalSampler_UpdateConfig",
"LiteRtTopKMetalSampler_CanHandleInput",
"LiteRtTopKMetalSampler_HandlesInput",
"LiteRtTopKMetalSampler_SetInputTensorsAndInferenceFunc");
if (capi_or.ok()) {
capi = std::move(capi_or.value());
ABSL_LOG(INFO) << "Dynamically loaded LiteRtTopKMetalSampler C API.";
} else {
if (capi_or.status().code() != absl::StatusCode::kUnavailable) {
return capi_or.status();
}
ABSL_LOG(WARNING) << "Metal sampler not available, falling back to "
"statically linked C API: "
<< capi_or.status();
auto static_capi_or = GetStaticTopKMetalSamplerCApi();
if (!static_capi_or.ok()) {
return capi_or.status();
}
capi = std::move(static_capi_or.value());
ABSL_LOG(INFO) << "Statically linked LiteRtTopKMetalSampler C API.";
}
LiteRtTopKSampler_Sampler* sampler = nullptr;
char* error_msg = nullptr;
int error_code = capi->create_func(
env, batch_size, sequence_size, vocab_size,
activation_data_type.has_value() ? &activation_data_type.value()
: nullptr,
&sampler_params, &sampler, &error_msg);
RETURN_IF_ERROR(CreateStatusAndFreeErrorMsg(error_code, error_msg));
RET_CHECK(sampler);
return absl::WrapUnique(new TopKMetalCApiSampler(std::move(capi), sampler));
}
private:
TopKMetalCApiSampler(std::unique_ptr<TopKSamplerCApi> capi,
LiteRtTopKSampler_Sampler* sampler)
: TopKCApiSampler(std::move(capi), sampler) {}
static absl::StatusOr<std::unique_ptr<TopKSamplerCApi>>
GetStaticTopKMetalSamplerCApi() {
if (LiteRtTopKMetalSampler_Create_Static == nullptr ||
LiteRtTopKMetalSampler_Destroy_Static == nullptr ||
LiteRtTopKMetalSampler_SampleToIdAndScoreBuffer_Static == nullptr ||
LiteRtTopKMetalSampler_UpdateConfig_Static == nullptr ||
LiteRtTopKMetalSampler_CanHandleInput_Static == nullptr ||
LiteRtTopKMetalSampler_HandlesInput_Static == nullptr ||
LiteRtTopKMetalSampler_SetInputTensorsAndInferenceFunc_Static ==
nullptr) {
return absl::UnavailableError(
"Static LiteRtTopKMetalSampler C API not available.");
}
return std::make_unique<TopKSamplerCApi>(
/*lib=*/std::nullopt, LiteRtTopKMetalSampler_Create_Static,
LiteRtTopKMetalSampler_Destroy_Static,
LiteRtTopKMetalSampler_SampleToIdAndScoreBuffer_Static,
LiteRtTopKMetalSampler_UpdateConfig_Static,
LiteRtTopKMetalSampler_CanHandleInput_Static,
LiteRtTopKMetalSampler_HandlesInput_Static,
LiteRtTopKMetalSampler_SetInputTensorsAndInferenceFunc_Static);
}
};
absl::StatusOr<std::unique_ptr<Sampler>> CreateCpuSampler(
int batch_size, int sequence_size,
proto::SamplerParameters sampler_params) {
switch (sampler_params.type()) {
case proto::SamplerParameters::TYPE_UNSPECIFIED:
ABSL_LOG(INFO) << "Sampler type is unspecified. Assume the LLM Executor "
"handles the sampling logic.";
return nullptr;
case proto::SamplerParameters::TOP_P:
return TopPSampler::Create(sampler_params.k(), sampler_params.p(),
sampler_params.temperature(), batch_size,
sequence_size, sampler_params.seed());
default:
return absl::UnimplementedError(absl::StrCat(
"Sampler type: ", sampler_params.type(), " not implemented yet."));
}
}
absl::StatusOr<std::unique_ptr<Sampler>> CreateGpuSampler(
int batch_size, proto::SamplerParameters sampler_params,
LiteRtEnvironment env, int sequence_size, int vocab_size,
std::optional<ActivationDataType> activation_data_type) {
// Check environment options to determine the preferred backend.
auto cpp_env = litert::Environment::WrapCObject(env, litert::OwnHandle::kNo);
auto options_or = cpp_env.GetOptions();
bool use_metal = false;
bool use_webgpu = false;
if (options_or.HasValue()) {
for (const auto& option : options_or->GetOptions()) {
if (option.tag == litert::EnvironmentOptions::Tag::kMetalDevice) {
use_metal = true;
} else if (option.tag == litert::EnvironmentOptions::Tag::kWebGpuDevice) {
use_webgpu = true;
}
}
}
#ifdef __ANDROID__
if (use_webgpu) {
#if LITERT_HAS_WEBGPU_SUPPORT // NOLINT(misc-include-cleaner)
auto webgpu_sampler = TopKWebGpuCApiSampler::Create(
env, batch_size, sequence_size, vocab_size, activation_data_type,
sampler_params);
if (webgpu_sampler.ok() ||
webgpu_sampler.status().code() != absl::StatusCode::kUnavailable) {
return webgpu_sampler;
}
ABSL_LOG(INFO) << "WebGPU sampler explicitly requested but "
"failed/unavailable, falling back.";
#endif // LITERT_HAS_WEBGPU_SUPPORT
}
#if LITERT_HAS_OPENCL_SUPPORT // NOLINT(misc-include-cleaner)
auto opencl_sampler =
TopKOpenClCApiSampler::Create(env, batch_size, sequence_size, vocab_size,
activation_data_type, sampler_params);
if (opencl_sampler.ok() ||
opencl_sampler.status().code() != absl::StatusCode::kUnavailable) {
return opencl_sampler;
}
ABSL_LOG(INFO)
<< "OpenCL sampler not available, falling back to other sampler options.";
#endif // LITERT_HAS_OPENCL_SUPPORT
#if LITERT_HAS_WEBGPU_SUPPORT // NOLINT(misc-include-cleaner)
if (!use_webgpu) {
auto webgpu_sampler = TopKWebGpuCApiSampler::Create(
env, batch_size, sequence_size, vocab_size, activation_data_type,
sampler_params);
if (webgpu_sampler.ok() ||
webgpu_sampler.status().code() != absl::StatusCode::kUnavailable) {
return webgpu_sampler;
}
ABSL_LOG(INFO) << "WebGPU sampler not available, falling back to other "
"sampler options.";
}
#endif // LITERT_HAS_WEBGPU_SUPPORT
#else // !__ANDROID__
#if defined(__APPLE__)
if (use_metal || !use_webgpu) {
auto metal_sampler =
TopKMetalCApiSampler::Create(env, batch_size, sequence_size, vocab_size,
activation_data_type, sampler_params);
if (metal_sampler.ok() ||
metal_sampler.status().code() != absl::StatusCode::kUnavailable) {
return metal_sampler;
}
if (use_metal) {
ABSL_LOG(WARNING)
<< "Metal sampler explicitly requested but failed/unavailable.";
} else {
ABSL_LOG(INFO) << "Metal sampler not available, falling back to other "
"sampler options.";
}
}
#endif // __APPLE__
#if LITERT_HAS_WEBGPU_SUPPORT // NOLINT(misc-include-cleaner)
auto webgpu_sampler =
TopKWebGpuCApiSampler::Create(env, batch_size, sequence_size, vocab_size,
activation_data_type, sampler_params);
if (webgpu_sampler.ok() ||
webgpu_sampler.status().code() != absl::StatusCode::kUnavailable) {
return webgpu_sampler;
}
ABSL_LOG(INFO)
<< "WebGPU sampler not available, falling back to other sampler options.";
#endif // LITERT_HAS_WEBGPU_SUPPORT
#if LITERT_HAS_OPENCL_SUPPORT // NOLINT(misc-include-cleaner)
auto opencl_sampler =
TopKOpenClCApiSampler::Create(env, batch_size, sequence_size, vocab_size,
activation_data_type, sampler_params);
if (opencl_sampler.ok() ||
opencl_sampler.status().code() != absl::StatusCode::kUnavailable) {
return opencl_sampler;
}
ABSL_LOG(INFO)
<< "OpenCL sampler not available, falling back to other sampler options.";
#endif // LITERT_HAS_OPENCL_SUPPORT
#endif // !__ANDROID__
return absl::UnavailableError("GPU sampler not available.");
}
} // namespace
absl::StatusOr<std::unique_ptr<Sampler>> CreateSampler(
Backend backend, int batch_size, proto::SamplerParameters sampler_params,
LiteRtEnvironment env, std::optional<int> sequence_size,
std::optional<int> vocab_size,
std::optional<ActivationDataType> activation_data_type) {
int sequence_size_value = sequence_size.value_or(1);
switch (backend) {
case Backend::GPU: {
RET_CHECK(env != nullptr)
<< "LiteRT environment is needed for GPU sampling.";
RET_CHECK(vocab_size.has_value())
<< "Vocabulary size is needed for GPU sampling.";
auto sampler_or =
CreateGpuSampler(batch_size, sampler_params, env, sequence_size_value,
vocab_size.value(), activation_data_type);
if (sampler_or.ok() ||
sampler_or.status().code() != absl::StatusCode::kUnavailable) {
// For a normal failure or success, return the result.
return sampler_or;
}
// For a failure due to GPU sampler unavailable, fall back to CPU.
ABSL_LOG(WARNING)
<< "GPU sampler unavailable. Falling back to CPU sampling. To use "
"GPU sampling, please make sure libLiteRtTopKWebGpuSampler.so or "
"libLiteRtTopKOpenClSampler.so is available at LD_LIBRARY_PATH "
"on device. You can find the shared library under prebuilt/";
ABSL_FALLTHROUGH_INTENDED;
}
case Backend::CPU:
return CreateCpuSampler(batch_size, sequence_size_value, sampler_params);
default:
return absl::InvalidArgumentError(
absl::StrCat("Unsupported backend: ", backend));
}
}
} // namespace litert::lm
|