Skip to content

include/tenzr/frontend/ufunc.h

Namespaces

Name
tenzr
tenzr::ufunc

Classes

Name
class tenzr::UFunc
class tenzr::UFuncRegistry
Process-wide registry of named ufuncs, including the standard built-ins.

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_FRONTEND_UFUNC_H_
#define TENZR_FRONTEND_UFUNC_H_

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

#include "tenzr/common/error.h"
#include "tenzr/frontend/dtype.h"
#include "tenzr/frontend/shape.h"
#include "tenzr/frontend/tensor.h"

namespace tenzr {

  enum class UFuncOp {
    kAdd,
    kSubtract,
    kMultiply,
    kDivide,
    kMaximum,
    kMinimum,
    kNegative,
    kAbsolute,
    kExponential,
    kLogarithm,
    kSquareRoot,
    kSin,
    kCos,
    kTanh,
    kSigmoid
  };

  class UFunc {
   public:
    using BinaryFn = std::function<Tensor(const Tensor&, const Tensor&)>;

    using UnaryFn = std::function<Tensor(const Tensor&)>;

    UFunc(std::string name, UFuncOp op, BinaryFn binary_impl);

    UFunc(std::string name, UFuncOp op, UnaryFn unary_impl);

    const std::string& Name() const { return name_; }

    UFuncOp Op() const { return op_; }

    bool IsBinary() const { return is_binary_; }

    Tensor operator()(const Tensor& a, const Tensor& b) const;

    Tensor operator()(const Tensor& a) const;

   private:
    std::string name_;
    UFuncOp op_;
    bool is_binary_{true};
    BinaryFn binary_impl_;
    UnaryFn unary_impl_;
  };

  class UFuncRegistry {
   public:
    static UFuncRegistry& Instance();

    void RegisterUfunc(std::shared_ptr<UFunc> ufunc);

    std::shared_ptr<UFunc> Find(const std::string& name) const;

    bool Has(const std::string& name) const;

    // Common standard ufuncs
    Tensor Add(const Tensor& a, const Tensor& b);
    Tensor Subtract(const Tensor& a, const Tensor& b);
    Tensor Multiply(const Tensor& a, const Tensor& b);
    Tensor Divide(const Tensor& a, const Tensor& b);

    Tensor Negative(const Tensor& a);
    Tensor Absolute(const Tensor& a);
    Tensor Exponential(const Tensor& a);
    Tensor Logarithm(const Tensor& a);
    Tensor SquareRoot(const Tensor& a);
    Tensor Sin(const Tensor& a);
    Tensor Cos(const Tensor& a);
    Tensor tanh(const Tensor& a);
    Tensor sigmoid(const Tensor& a);

   private:
    UFuncRegistry();
    void RegisterBuiltins();

    std::unordered_map<std::string, std::shared_ptr<UFunc>> ufuncs_;
  };

  namespace ufunc {
    inline Tensor Add(const Tensor& a, const Tensor& b) { return UFuncRegistry::Instance().Add(a, b); }

    inline Tensor Subtract(const Tensor& a, const Tensor& b) { return UFuncRegistry::Instance().Subtract(a, b); }

    inline Tensor Multiply(const Tensor& a, const Tensor& b) { return UFuncRegistry::Instance().Multiply(a, b); }

    inline Tensor Divide(const Tensor& a, const Tensor& b) { return UFuncRegistry::Instance().Divide(a, b); }
  }  // namespace ufunc

}  // namespace tenzr

#endif  // TENZR_FRONTEND_UFUNC_H_