// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include "core/common/common.h" #include "core/common/inlined_containers.h" #include "core/framework/allocator.h" #include "core/framework/ort_value.h" #include "core/platform/env.h" #include "lora/adapter_format_utils.h" #include #include #include #include #include namespace onnxruntime { namespace lora { /// /// Container to hold and access Lora Parameters /// class LoraAdapter { public: LoraAdapter() = default; explicit LoraAdapter(AllocatorPtr device_allocator) : device_allocator_(std::move(device_allocator)) {} ~LoraAdapter() = default; LoraAdapter(const LoraAdapter&) = delete; LoraAdapter& operator=(const LoraAdapter&) = delete; LoraAdapter(LoraAdapter&&) = default; LoraAdapter& operator=(LoraAdapter&&) = default; /// /// Represents a named lora parameter (tensor) /// class Param { public: Param() = default; explicit Param(OrtValue ort_value_mapped) noexcept; Param(OrtValue ort_value_mapped, OrtValue ort_value_device) noexcept; const OrtValue& GetMapped() const noexcept { return ort_value_mapped_; } // For python interface OrtValue& GetMapped() noexcept { return ort_value_mapped_; } const OrtValue& GetDeviceOrMapped() const noexcept { if (ort_value_device_.IsAllocated()) { return ort_value_device_; } return ort_value_mapped_; } private: OrtValue ort_value_mapped_; OrtValue ort_value_device_; }; using param_const_iterator = std::unordered_map::const_iterator; using param_iterator = std::unordered_map::iterator; /// /// Obtain a range of the iterators /// /// std::pair GetParamIterators() const { return std::make_pair(params_values_.cbegin(), params_values_.cend()); } std::pair GetParamIterators() { return std::make_pair(params_values_.begin(), params_values_.end()); } /// /// Load parameters into memory from an adapter file and validates its format. /// /// file name that can be opened void Load(const std::filesystem::path& file_path); /// /// Load parameters from serialized bytes and validates its format. /// /// void Load(std::vector buffer); /// /// Memory maps adapter file into memory and validates its format. /// /// void MemoryMap(const std::filesystem::path& file_path); /// /// Returns number of parameters in the adapter. /// The number is expected to be even as lora params come in pairs. /// /// size of params_values_ container size_t GetParamNum() const { return params_values_.size(); } /// /// Gets lora format version /// /// int FormatVersion() const noexcept { return adapter_->format_version(); } /// /// Gets adapter version /// /// int AdapterVersion() const noexcept { return adapter_->adapter_version(); } /// /// Gets model version for which the adapter was created /// /// int ModelVersion() const noexcept { return adapter_->model_version(); } /// /// Outputs Lora Parameters on CPU, their names and values /// into the supplied output iterators. /// /// /// /// output iterator that accepts const char* /// output iterator that accepts const OrtValue* template void OutputAdapterParameters(NamesOutputIter names_out, TensorOutputIter tensor_out) const { for (const auto& [name, param] : params_values_) { *names_out = name.c_str(); ++names_out; *tensor_out = ¶m.GetDeviceOrMapped(); ++tensor_out; } } private: // Build a fresh parameter map from the supplied adapter pointer (which must alias // memory the caller is keeping alive). Throws on validation failure. Does not // touch this->params_values_ / buffer_ / adapter_; callers are responsible for // committing the result only after they have also committed the new backing // storage. Keeping this function free of side effects on *this is what gives // Load()/MemoryMap() the strong exception guarantee. std::unordered_map BuildParamsValues(const adapters::Adapter* adapter) const; struct BufferHolder { explicit BufferHolder(std::vector buffer) noexcept : buffer_(std::move(buffer)) {} BufferHolder(BufferHolder&&) noexcept = default; BufferHolder& operator=(BufferHolder&&) noexcept = default; std::vector buffer_; }; struct MemMapHolder { MemMapHolder(Env::MappedMemoryPtr mapped_memory, size_t file_size) noexcept : mapped_memory_(std::move(mapped_memory)), file_size_(file_size) {} MemMapHolder(MemMapHolder&&) noexcept = default; MemMapHolder& operator=(MemMapHolder&&) noexcept = default; Env::MappedMemoryPtr mapped_memory_; size_t file_size_; }; std::variant buffer_; AllocatorPtr device_allocator_; const adapters::Adapter* adapter_{nullptr}; std::unordered_map params_values_; }; } // namespace lora } // namespace onnxruntime