Program Listing for File tensor_memory.hpp#

Return to documentation for file (python/morpheus/morpheus/_lib/include/morpheus/messages/memory/tensor_memory.hpp)

/*
 * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * 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
 *
 * http://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.
 */

#pragma once

#include "morpheus/export.h"                   // for exporting symbols
#include "morpheus/objects/tensor_object.hpp"  // for TensorObject
#include "morpheus/types.hpp"                  // for TensorMap, TensorIndex
#include "morpheus/utilities/cupy_util.hpp"    // for CupyUtil

#include <pybind11/pytypes.h>  // for object

#include <memory>  // for shared_ptr
#include <string>
#include <vector>

namespace morpheus {
/****** Component public implementations *******************/
/****** TensorMemory****************************************/

class MORPHEUS_EXPORT TensorMemory
{
  public:
    TensorMemory(TensorIndex count);

    TensorMemory(TensorIndex count, TensorMap&& tensors);
    virtual ~TensorMemory() = default;

    TensorIndex count{0};

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

    TensorObject& get_tensor(const std::string& name);

    const TensorObject& get_tensor(const std::string& name) const;

    void set_tensor(const std::string& name, TensorObject&& tensor);

    const TensorMap& get_tensors() const;

    void set_tensors(TensorMap&& tensors);

    TensorMap copy_tensor_ranges(const std::vector<RangeType>& ranges, TensorIndex num_selected_rows) const;

  protected:
    void check_tensor_length(const TensorObject& tensor);

    void check_tensors_length(const TensorMap& tensors);

    void verify_tensor_exists(const std::string& name) const;

  private:
    TensorMap m_tensors;
};

/****** TensorMemoryInterfaceProxy *************************/
struct MORPHEUS_EXPORT TensorMemoryInterfaceProxy
{
    static std::shared_ptr<TensorMemory> init(TensorIndex count, pybind11::object& tensors);

    static TensorIndex get_count(TensorMemory& self);

    static std::vector<std::string> tensor_names_getter(TensorMemory& self);

    static bool has_tensor(TensorMemory& self, std::string name);

    static CupyUtil::py_tensor_map_t get_tensors(TensorMemory& self);

    static void set_tensors(TensorMemory& self, CupyUtil::py_tensor_map_t tensors);

    static pybind11::object get_tensor(TensorMemory& self, const std::string name);

    static pybind11::object get_tensor_property(TensorMemory& self, const std::string name);

    static void set_tensor(TensorMemory& self, const std::string name, const pybind11::object& cupy_tensor);
};  // end of group
}  // namespace morpheus