// 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_ACTIVATIONS_H_ #define THIRD_PARTY_GEMMA_CPP_GEMMA_ACTIVATIONS_H_ #include #include #include "gemma/common.h" // ByteStorageT namespace gcpp { template struct ForwardLayer { static constexpr size_t kSeqLen = TConfig::kSeqLen; static constexpr size_t kModelDim = TConfig::kModelDim; static constexpr size_t kQKVDim = TConfig::kQKVDim; static constexpr size_t kHeads = TConfig::kHeads; static constexpr size_t kFFHiddenDim = TConfig::kFFHiddenDim; std::array input; std::array pre_att_rms_out; std::array qkv; std::array att; std::array att_out; std::array att_post1; std::array attention_out; std::array bf_pre_ffw_rms_out; std::array ffw_hidden; std::array ffw_hidden_gated; }; template struct ForwardPass { ForwardPass() {} // prevents placement-new calling memset static constexpr size_t kSeqLen = TConfig::kSeqLen; static constexpr size_t kModelDim = TConfig::kModelDim; static constexpr size_t kVocabSize = TConfig::kVocabSize; static constexpr size_t kLayers = TConfig::kLayers; std::array, kLayers> layers; std::array final_layer_output; std::array final_norm_output; std::array logits; std::array probs; }; template struct AllocateForwardPass { ByteStorageT operator()() const { return AllocateSizeof>(); } }; // Owns activations and undoes the type erasure of AllocateAligned. template class ActivationsWrapper { using WrappedT = ForwardPass; public: ActivationsWrapper() : data_(AllocateSizeof()), activations_(*reinterpret_cast(data_.get())) {} const WrappedT& get() const { return activations_; } WrappedT& get() { return activations_; } private: ByteStorageT data_; WrappedT& activations_; }; } // namespace gcpp #endif // THIRD_PARTY_GEMMA_CPP_GEMMA_ACTIVATIONS_H_