include/tenzr/modules/nn/embedding.h
Namespaces
Classes
Source code
// Copyright 2026 The Tenzr Project Authors
//
// 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 TENZR_MODULES_NN_EMBEDDING_H_
#define TENZR_MODULES_NN_EMBEDDING_H_
#include <cstdint>
#include <optional>
#include <string>
#include "tenzr/frontend/dtype.h"
#include "tenzr/frontend/tensor.h"
#include "tenzr/modules/nn/module.h"
namespace tenzr::nn {
class Embedding : public Module {
public:
// Dynamic DType constructor (explicit dtype, optional seed)
Embedding(int64_t num_embeddings, int64_t embedding_dim, DType dtype, std::optional<uint64_t> seed = std::nullopt);
// Compile-time templated constructor using C++20 concepts (defaults to float via concepts::FloatingPoint<T>)
template <typename T = float>
requires concepts::FloatingPoint<T>
Embedding(int64_t num_embeddings, int64_t embedding_dim, std::optional<uint64_t> seed = std::nullopt)
: Embedding(num_embeddings, embedding_dim, DtypeOf<T>(), seed) {}
// Tag dispatch constructor with explicit type: Embedding(std::type_identity<double>{}, 10, 16)
template <typename T>
requires concepts::FloatingPoint<T>
explicit Embedding(std::type_identity<T>, int64_t num_embeddings, int64_t embedding_dim, std::optional<uint64_t> seed = std::nullopt)
: Embedding(num_embeddings, embedding_dim, DtypeOf<T>(), seed) {}
// Compile-time factory method: Embedding::create<double>(10, 16)
template <typename T = float>
requires concepts::FloatingPoint<T>
static Embedding Create(int64_t num_embeddings, int64_t embedding_dim, std::optional<uint64_t> seed = std::nullopt) {
return Embedding(num_embeddings, embedding_dim, DtypeOf<T>(), seed);
}
Tensor Forward(const Tensor& indices) override;
Tensor& Weight() { return weight_; }
const Tensor& Weight() const { return weight_; }
int64_t NumEmbeddings() const { return num_embeddings_; }
int64_t EmbeddingDim() const { return embedding_dim_; }
std::string Name() const override { return "Embedding"; }
private:
int64_t num_embeddings_;
int64_t embedding_dim_;
Tensor weight_;
};
} // namespace tenzr::nn
#endif // TENZR_MODULES_NN_EMBEDDING_H_