// Copyright 2024 Google LLC // SPDX-License-Identifier: Apache-2.0 // // 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 // // https://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. #ifndef THIRD_PARTY_GEMMA_CPP_GEMMA_COMMON_H_ #define THIRD_PARTY_GEMMA_CPP_GEMMA_COMMON_H_ #include // sqrtf #include #include #include #include "compression/compress.h" #include "gemma/configs.h" // IWYU pragma: export #include "hwy/aligned_allocator.h" #include "hwy/base.h" // ConvertScalarTo namespace gcpp { using ByteStorageT = hwy::AlignedFreeUniquePtr; template ByteStorageT AllocateSizeof() { return hwy::AllocateAligned(sizeof(T)); } constexpr size_t kPrefillBatchSize = 16; constexpr size_t kDecodeBatchSize = 1; constexpr size_t kBatchedQueryBatchSize = 16; constexpr size_t kMinAdjustedPrefillBatchSize = HWY_MAX((size_t)1, kPrefillBatchSize / kBatchedQueryBatchSize); // Model variants: see configs.h for details. enum class Model { GEMMA_2B, GEMMA_7B, GEMMA_9B, GEMMA_27B, GRIFFIN_2B, GEMMA_TINY, }; // Instruction-tuned models require extra 'turn structure' tokens in prompts. enum class ModelTraining { GEMMA_IT, GEMMA_PT }; // Tensor types for loading weights. enum class Type { kF32, kBF16, kSFP }; // TODO(janwas): merge with parser/ToString. struct ModelInfo { Model model; ModelTraining training; Type weight; }; // Returns the return value of FuncT>().operator()(args), where // Config* is selected via `model`. Typically called by CallForModelAndWeight, // but can also be called directly when FuncT does not actually use TWeight. // // Note that a T prefix indicates a concrete type template argument, whereas a // T suffix indicates the argument is itself a template. // // `FuncT` must be a functor because function templates cannot be passed as a // template template argument, and we prefer to avoid the overhead of // std::function. template class FuncT, typename... TArgs> decltype(auto) CallForModel(Model model, TArgs&&... args) { switch (model) { case Model::GEMMA_TINY: return FuncT>()(std::forward(args)...); case Model::GEMMA_2B: return FuncT>()(std::forward(args)...); case Model::GEMMA_7B: return FuncT>()(std::forward(args)...); case Model::GEMMA_9B: return FuncT>()(std::forward(args)...); case Model::GEMMA_27B: return FuncT>()(std::forward(args)...); case Model::GRIFFIN_2B: return FuncT>()(std::forward(args)...); default: HWY_ABORT("Model type %d unknown.", static_cast(model)); } } // Returns the return value of FuncT().operator()(args), // where `TConfig` is selected based on `model` and `weight`. // This makes it easy to extend `Model` or `Type` without updating callers. // // Usage example: LoadWeights is type-erased so that it can be called from other // .cc files. It uses this function to call the appropriate instantiation of a // template functor LoadCompressedWeightsT. template