Skip to content

include/tenzr/modules/nn/sequential.h

Namespaces

Name
tenzr
tenzr::nn

Classes

Name
class tenzr::nn::Sequential
Container module that applies its layers in order.

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_SEQUENTIAL_H_
#define TENZR_MODULES_NN_SEQUENTIAL_H_

#include <initializer_list>
#include <memory>
#include <string>
#include <vector>

#include "tenzr/frontend/tensor.h"
#include "tenzr/modules/nn/module.h"

namespace tenzr::nn {

  class Sequential : public Module {
   public:
    Sequential() = default;
    Sequential(std::initializer_list<std::shared_ptr<Module>> modules);

    void Add(std::shared_ptr<Module> module);
    void Add(std::string name, std::shared_ptr<Module> module);

    Tensor Forward(const Tensor& input) override;

    size_t Size() const { return layers_.size(); }
    bool Empty() const { return layers_.empty(); }

    std::shared_ptr<Module> operator[](size_t index) {
      TENZR_CHECK(index < layers_.size(), "Index out of range in Sequential");
      return layers_[index];
    }

    const std::shared_ptr<Module>& operator[](size_t index) const {
      TENZR_CHECK(index < layers_.size(), "Index out of range in Sequential");
      return layers_[index];
    }

    std::string Name() const override { return "Sequential"; }

   private:
    std::vector<std::shared_ptr<Module>> layers_;
  };

}  // namespace tenzr::nn

#endif  // TENZR_MODULES_NN_SEQUENTIAL_H_