Skip to content

include/tenzr/modules/optim/optimizer.h

Namespaces

Name
tenzr
tenzr::optim

Classes

Name
class tenzr::optim::Optimizer
Binds a parameter set to a gradient transformation and applies update steps.

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_OPTIM_OPTIMIZER_H_
#define TENZR_MODULES_OPTIM_OPTIMIZER_H_

#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

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

namespace tenzr::optim {

  class Optimizer {
   public:
    Optimizer(std::unordered_map<std::string, Tensor> params, std::shared_ptr<GradientTransformation> transform);
    explicit Optimizer(nn::Module& module, std::shared_ptr<GradientTransformation> transform);

    void Step(const std::unordered_map<std::string, Tensor>& grads);

    uint64_t StepCount() const { return state_.step; }

    const OptState& State() const { return state_; }

    OptState& State() { return state_; }

    const std::unordered_map<std::string, Tensor>& Params() const { return params_; }

   private:
    std::unordered_map<std::string, Tensor> params_;
    std::shared_ptr<GradientTransformation> transform_;
    OptState state_;
  };

}  // namespace tenzr::optim

#endif  // TENZR_MODULES_OPTIM_OPTIMIZER_H_