Skip to content

Commit

Permalink
Create types.hpp (#737)
Browse files Browse the repository at this point in the history
* Create a `types.hpp` for type aliases like `TensorIndex` and `tensor_map_t`

This is a scaled back version of PR #720 without a `forward.hpp`. The forward header didn't improve build performance, and complicated IWYU checks. The real motivation behind this was having a convenient place for `tensor_map_t`.

fixes #715

Authors:
  - David Gardner (https://github.com/dagardner-nv)

Approvers:
  - Michael Demoret (https://github.com/mdemoret-nv)

URL: #737
  • Loading branch information
dagardner-nv authored Mar 6, 2023
1 parent 4338a91 commit e3c6f52
Show file tree
Hide file tree
Showing 26 changed files with 144 additions and 102 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ mlruns/*
# Ignore generated pyi files for pybind and cython modules
morpheus/_lib/**/*.pyi

# Explicitly ignore .vscode/settings.json and .vscode/launch.json. Shared settings should go in morpheus.code-workspace
# and user settings will go in .vscode/launch.json and .vscode/settings.json
.vscode/settings.json
.vscode/launch.json
# Explicitly ignore .vscode/. Shared settings should go in morpheus.code-workspace
# and user settings will go in .vscode/
.vscode/
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include "morpheus/messages/memory/tensor_memory.hpp"
#include "morpheus/types.hpp" // for TensorMap

#include <cstddef>
#include <string>
Expand Down Expand Up @@ -50,7 +51,7 @@ class InferenceMemory : public TensorMemory
* @param count
* @param tensors
*/
InferenceMemory(size_t count, tensor_map_t&& tensors);
InferenceMemory(size_t count, TensorMap&& tensors);

/**
* @brief Checks if a tensor named `name` exists in `tensors`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "morpheus/messages/memory/tensor_memory.hpp"
#include "morpheus/objects/tensor_object.hpp" // for TensorObject
#include "morpheus/types.hpp" // for TensorMap

#include <pybind11/pytypes.h>

Expand Down Expand Up @@ -53,7 +54,7 @@ class ResponseMemory : public TensorMemory
* @param count
* @param tensors
*/
ResponseMemory(size_t count, tensor_map_t&& tensors);
ResponseMemory(size_t count, TensorMap&& tensors);

/**
* @brief Checks if a tensor named `name` exists in `tensors`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#pragma once

#include "morpheus/messages/memory/response_memory.hpp"
#include "morpheus/messages/memory/tensor_memory.hpp"
#include "morpheus/objects/tensor_object.hpp"
#include "morpheus/types.hpp" // for TensorMap

#include <cudf/types.hpp>
#include <pybind11/pytypes.h>
Expand Down Expand Up @@ -57,7 +57,7 @@ class ResponseMemoryProbs : public ResponseMemory
* @param count
* @param tensors
*/
ResponseMemoryProbs(size_t count, tensor_map_t&& tensors);
ResponseMemoryProbs(size_t count, TensorMap&& tensors);

/**
* @brief Returns the tensor named 'probs', throws a `std::runtime_error` if it does not exist
Expand Down
15 changes: 6 additions & 9 deletions morpheus/_lib/include/morpheus/messages/memory/tensor_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

#pragma once

#include "morpheus/objects/tensor_object.hpp" // for TensorIndex, TensorObject
#include "morpheus/types.hpp" // for TensorMap, TensorIndex

#include <cstddef> // for size_t
#include <map>
#include <string>
#include <utility> // for pair
#include <vector>
Expand All @@ -43,8 +42,6 @@ namespace morpheus {
class TensorMemory
{
public:
using tensor_map_t = std::map<std::string, TensorObject>;

/**
* @brief Construct a new Tensor Memory object
*
Expand All @@ -58,11 +55,11 @@ class TensorMemory
* @param count
* @param tensors
*/
TensorMemory(size_t count, tensor_map_t&& tensors);
TensorMemory(size_t count, TensorMap&& tensors);
virtual ~TensorMemory() = default;

size_t count{0};
tensor_map_t tensors;
TensorMap tensors;

/**
* @brief Verify whether the specified tensor name is present in the tensor memory
Expand All @@ -78,10 +75,10 @@ class TensorMemory
*
* @param ranges
* @param num_selected_rows
* @return tensor_map_t
* @return TensorMap
*/
tensor_map_t copy_tensor_ranges(const std::vector<std::pair<TensorIndex, TensorIndex>>& ranges,
size_t num_selected_rows) const;
TensorMap copy_tensor_ranges(const std::vector<std::pair<TensorIndex, TensorIndex>>& ranges,
size_t num_selected_rows) const;
};

/** @} */ // end of group
Expand Down
1 change: 1 addition & 0 deletions morpheus/_lib/include/morpheus/messages/multi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "morpheus/messages/meta.hpp"
#include "morpheus/objects/table_info.hpp"
#include "morpheus/objects/tensor_object.hpp"
#include "morpheus/types.hpp" // for TensorIndex

#include <cudf/types.hpp>
#include <glog/logging.h> // for DCHECK_NOTNULL
Expand Down
18 changes: 9 additions & 9 deletions morpheus/_lib/include/morpheus/objects/rmm_tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "morpheus/objects/dtype.hpp" // for DType
#include "morpheus/objects/tensor_object.hpp"
#include "morpheus/types.hpp" // for RankType, ShapeType, TensorIndex

#include <rmm/device_buffer.hpp>

Expand Down Expand Up @@ -47,8 +48,8 @@ class RMMTensor : public ITensor
RMMTensor(std::shared_ptr<rmm::device_buffer> device_buffer,
size_t offset,
DType dtype,
std::vector<TensorIndex> shape,
std::vector<TensorIndex> stride = {});
ShapeType shape,
ShapeType stride = {});

~RMMTensor() override = default;

Expand All @@ -75,13 +76,12 @@ class RMMTensor : public ITensor
/**
* TODO(Documentation)
*/
std::shared_ptr<ITensor> reshape(const std::vector<TensorIndex>& dims) const override;
std::shared_ptr<ITensor> reshape(const ShapeType& dims) const override;

/**
* TODO(Documentation)
*/
std::shared_ptr<ITensor> slice(const std::vector<TensorIndex>& min_dims,
const std::vector<TensorIndex>& max_dims) const override;
std::shared_ptr<ITensor> slice(const ShapeType& min_dims, const ShapeType& max_dims) const override;

/**
* @brief Creates a depp copy of the specified rows specified as vector<pair<start, stop>> not inclusive
Expand Down Expand Up @@ -127,12 +127,12 @@ class RMMTensor : public ITensor
/**
* TODO(Documentation)
*/
void get_shape(std::vector<TensorIndex>& s) const;
void get_shape(ShapeType& s) const;

/**
* TODO(Documentation)
*/
void get_stride(std::vector<TensorIndex>& s) const;
void get_stride(ShapeType& s) const;

// Tensor reshape(std::vector<TensorIndex> shape)
// {
Expand Down Expand Up @@ -162,8 +162,8 @@ class RMMTensor : public ITensor
DType m_dtype;

// Shape info
std::vector<TensorIndex> m_shape;
std::vector<TensorIndex> m_stride;
ShapeType m_shape;
ShapeType m_stride;
};

#pragma GCC visibility pop
Expand Down
8 changes: 3 additions & 5 deletions morpheus/_lib/include/morpheus/objects/tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "morpheus/objects/dtype.hpp"
#include "morpheus/objects/tensor_object.hpp"
#include "morpheus/types.hpp" // for ShapeType, TensorIndex

#include <rmm/device_buffer.hpp>

Expand Down Expand Up @@ -81,11 +82,8 @@ class Tensor
/**
* TODO(Documentation)
*/
static TensorObject create(std::shared_ptr<rmm::device_buffer> buffer,
DType dtype,
std::vector<TensorIndex> shape,
std::vector<TensorIndex> strides,
size_t offset = 0);
static TensorObject create(
std::shared_ptr<rmm::device_buffer> buffer, DType dtype, ShapeType shape, ShapeType strides, size_t offset = 0);

private:
size_t m_offset;
Expand Down
13 changes: 5 additions & 8 deletions morpheus/_lib/include/morpheus/objects/tensor_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include "morpheus/objects/dtype.hpp"
#include "morpheus/types.hpp" // for RankType, ShapeType, TensorIndex
#include "morpheus/utilities/string_util.hpp"

#include <cuda_runtime.h> // for cudaMemcpyDeviceToHost & cudaMemcpy
Expand Down Expand Up @@ -50,9 +51,6 @@ namespace morpheus {
* @file
*/

using TensorIndex = long long; // NOLINT
using RankType = int; // NOLINT

namespace detail {

template <typename IterT>
Expand Down Expand Up @@ -139,10 +137,9 @@ struct ITensor;

struct ITensorOperations
{
virtual std::shared_ptr<ITensor> slice(const std::vector<TensorIndex>& min_dims,
const std::vector<TensorIndex>& max_dims) const = 0;
virtual std::shared_ptr<ITensor> slice(const ShapeType& min_dims, const ShapeType& max_dims) const = 0;

virtual std::shared_ptr<ITensor> reshape(const std::vector<TensorIndex>& dims) const = 0;
virtual std::shared_ptr<ITensor> reshape(const ShapeType& dims) const = 0;

virtual std::shared_ptr<ITensor> deep_copy() const = 0;

Expand Down Expand Up @@ -265,7 +262,7 @@ struct TensorObject final
return m_tensor->is_compact();
}

TensorObject slice(std::vector<TensorIndex> min_dims, std::vector<TensorIndex> max_dims) const
TensorObject slice(ShapeType min_dims, ShapeType max_dims) const
{
// Replace any -1 values
std::replace_if(
Expand All @@ -278,7 +275,7 @@ struct TensorObject final
return {m_md, m_tensor->slice(min_dims, max_dims)};
}

TensorObject reshape(const std::vector<TensorIndex>& dims) const
TensorObject reshape(const ShapeType& dims) const
{
return {m_md, m_tensor->reshape(dims)};
}
Expand Down
42 changes: 42 additions & 0 deletions morpheus/_lib/include/morpheus/types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* SPDX-FileCopyrightText: Copyright (c) 2022-2023, 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 <map>
#include <string>
#include <vector>

namespace morpheus {

struct TensorObject;

/**
* @addtogroup objects
* @{
* @file
*/
// NOLINTBEGIN(readability-identifier-naming)
using TensorIndex = long long;
using RankType = int;

using ShapeType = std::vector<TensorIndex>;
using TensorMap = std::map<std::string, TensorObject>;
// NOLINTEND(readability-identifier-naming)

/** @} */ // end of group
} // namespace morpheus
17 changes: 7 additions & 10 deletions morpheus/_lib/include/morpheus/utilities/tensor_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#pragma once

#include "morpheus/objects/tensor_object.hpp"
#include "morpheus/types.hpp" // for ShapeType, TensorIndex

#include <algorithm> // IWYU pragma: keep
#include <functional> // for multiplies
Expand All @@ -43,39 +43,37 @@ namespace morpheus {
*/
struct TensorUtils
{
using shape_type_t = std::vector<TensorIndex>;

/**
* @brief Write a formatted shape to a stream
*
* @param shape
* @param os
*/
static void write_shape_to_stream(const shape_type_t& shape, std::ostream& os);
static void write_shape_to_stream(const ShapeType& shape, std::ostream& os);

/**
* @brief Convenience method to get a string from write_shape_to_stream
*
* @param shape
* @return std::string
*/
static std::string shape_to_string(const shape_type_t& shape);
static std::string shape_to_string(const ShapeType& shape);

/**
* @brief Set stride to be contiguous with respect to row-major layouts
*
* @param shape
* @param stride
*/
static void set_contiguous_stride(const std::vector<TensorIndex>& shape, std::vector<TensorIndex>& stride);
static void set_contiguous_stride(const ShapeType& shape, ShapeType& stride);

/**
* @brief Determines if the tensor layout is both contiguous and ordered.
*
* @note A tensor whose values are laid out in the storage starting from the rightmost
* dimension onward (that is, moving along rows for a 2D tensor) is defined as contiguous.
*/
static bool has_contiguous_stride(const std::vector<TensorIndex>& shape, const shape_type_t& stride);
static bool has_contiguous_stride(const ShapeType& shape, const ShapeType& stride);

/**
* @brief Validate the shape and stride are compatible
Expand All @@ -85,15 +83,14 @@ struct TensorUtils
* @return true
* @return false
*/
static bool validate_shape_and_stride(const std::vector<TensorIndex>& shape,
const std::vector<TensorIndex>& stride);
static bool validate_shape_and_stride(const ShapeType& shape, const ShapeType& stride);

/**
* @brief Returns a stride expressed in terms of elements given a stride expressed either in terms of bytes or
* elements.
*
* @param stride
* @return shape_type_t
* @return ShapeType
*/
template <typename IndexT, typename SrcIndexT = IndexT>
static std::vector<IndexT> get_element_stride(const std::vector<SrcIndexT>& stride)
Expand Down
5 changes: 4 additions & 1 deletion morpheus/_lib/src/messages/memory/inference_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

#include "morpheus/messages/memory/inference_memory.hpp"

// for TensorObject
#include "morpheus/objects/tensor_object.hpp" // IWYU pragma: keep

#include <string>
#include <utility> // for move

namespace morpheus {
/****** Component public implementations *******************/
/****** InferenceMemory****************************************/
InferenceMemory::InferenceMemory(size_t count) : TensorMemory(count) {}
InferenceMemory::InferenceMemory(size_t count, tensor_map_t&& tensors) : TensorMemory(count, std::move(tensors)) {}
InferenceMemory::InferenceMemory(size_t count, TensorMap&& tensors) : TensorMemory(count, std::move(tensors)) {}

bool InferenceMemory::has_input(const std::string& name) const
{
Expand Down
2 changes: 1 addition & 1 deletion morpheus/_lib/src/messages/memory/inference_memory_fil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "morpheus/messages/memory/inference_memory_fil.hpp"

#include "morpheus/messages/memory/inference_memory.hpp"
#include "morpheus/messages/memory/tensor_memory.hpp"
#include "morpheus/types.hpp" // for TensorMap
#include "morpheus/utilities/cupy_util.hpp"

#include <cudf/types.hpp>
Expand Down
Loading

0 comments on commit e3c6f52

Please sign in to comment.