// 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 "compression/compress.h" #include "gemma/configs.h" // IWYU pragma: export #include "hwy/base.h" // ConvertScalarTo namespace gcpp { // Model variants: see configs.h for details. When adding a new one, also // update GEMMA_FOREACH* and Call* below, and add instantiations/*.cc. enum class Model { GEMMA_2B, GEMMA_7B, GEMMA2_9B, GEMMA2_27B, GRIFFIN_2B, GEMMA_TINY, GEMMA2_2B, PALIGEMMA_224, }; // Instruction-tuned models require extra 'turn structure' tokens in prompts. enum class ModelTraining { GEMMA_IT, GEMMA_PT, PALIGEMMA }; // Tensor types for loading weights. When adding a new one, also // update GEMMA_FOREACH* and Call* below, and add instantiations/*.cc. enum class Type { kF32, kBF16, kSFP }; // TODO(janwas): merge with functions below. struct ModelInfo { Model model; ModelTraining training; Type weight; }; // Returns error string or nullptr if OK. // Thread-hostile. const char* ParseModelTypeAndTraining(const std::string& model_flag, Model& model, ModelTraining& training); const char* ParseType(const std::string& type_string, Type& type); // Inverse of ParseModelTypeAndTraining. const char* ModelString(Model model, ModelTraining training); const char* StringFromType(Type type); void Wrap(const ModelInfo& info, size_t pos, std::string& prompt); // 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::GEMMA2_9B: return FuncT>()(std::forward(args)...); case Model::GEMMA2_27B: return FuncT>()(std::forward(args)...); case Model::GRIFFIN_2B: return FuncT>()(std::forward(args)...); case Model::GEMMA2_2B: return FuncT>()(std::forward(args)...); case Model::PALIGEMMA_224: 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