tensor_utils.hpp 5.89 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2020 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15
//
// 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.
//*****************************************************************************
16 17 18 19 20 21 22 23 24

#pragma once

#include <iostream>
#include <stdexcept>

#include <ngraph/ngraph.hpp>

// Make a runtime tensor for a node output
25
std::shared_ptr<ngraph::runtime::Tensor> make_output_tensor(
26 27 28 29
    const std::shared_ptr<ngraph::runtime::Backend>& backend,
    const std::shared_ptr<ngraph::Node>& node,
    size_t output_pos)
{
30
    return backend->create_tensor(
31 32 33 34 35 36 37
        node->get_output_element_type(output_pos),
        node->get_output_shape(output_pos));
}

// Initialize a tensor from a random generator
template <typename T>
void randomize(std::function<T()> rand,
38
               const std::shared_ptr<ngraph::runtime::Tensor>& t)
39
{
40
    if (t->get_element_type().bitwidth() != 8 * sizeof(T))
41 42 43 44 45 46 47 48 49 50 51
    {
        throw std::invalid_argument(
            "Randomize generator size is not the same as tensor "
            "element size");
    }
    size_t element_count = t->get_element_count();
    std::vector<T> temp;
    for (size_t i = 0; i < element_count; ++i)
    {
        temp.push_back(rand());
    }
52
    t->write(&temp[0], element_count * sizeof(T));
53 54 55 56
}

// Get a scalar value from a tensor, optionally at an element offset
template <typename T>
57
T get_scalar(const std::shared_ptr<ngraph::runtime::Tensor>& t,
58 59 60
             size_t element_offset = 0)
{
    T result;
61
    t->read(&result + (element_offset * sizeof(T)), sizeof(T));
62 63 64 65 66
    return result;
}

// Set a scalar value in a tensor, optionally at an element offset
template <typename T>
67
void set_scalar(const std::shared_ptr<ngraph::runtime::Tensor>& t,
68 69 70
                T value,
                size_t element_offset = 0)
{
71
    t->write(&value + (element_offset * sizeof(T)), sizeof(T));
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

// Show a shape
std::ostream& operator<<(std::ostream& s, const ngraph::Shape& shape)
{
    s << "Shape{";
    for (size_t i = 0; i < shape.size(); ++i)
    {
        s << shape.at(i);
        if (i + 1 < shape.size())
        {
            s << ", ";
        }
    }
    s << "}";
    return s;
}

90 91
// A debug class that supports various ways to dump information about a
// tensor.
92 93 94
class TensorDumper
{
protected:
95 96
    TensorDumper(const std::string& name,
                 const std::shared_ptr<ngraph::runtime::Tensor>& tensor)
97 98 99 100 101 102 103 104
        : m_name(name)
        , m_tensor(tensor)
    {
    }

public:
    virtual ~TensorDumper() {}
    const std::string& get_name() const { return m_name; }
105
    std::shared_ptr<ngraph::runtime::Tensor> get_tensor() const
106 107 108 109 110 111 112
    {
        return m_tensor;
    }
    virtual std::ostream& dump(std::ostream& s) const = 0;

protected:
    std::string m_name;
113
    std::shared_ptr<ngraph::runtime::Tensor> m_tensor;
114 115 116 117 118 119 120 121 122 123 124 125
};

std::ostream& operator<<(std::ostream& s, const TensorDumper& td)
{
    return td.dump(s);
}

// Show the min and max values of a tensor
class MinMax : public TensorDumper
{
public:
    MinMax(const std::string& name,
126
           const std::shared_ptr<ngraph::runtime::Tensor>& tensor)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        : TensorDumper(name, tensor)
    {
        size_t n = m_tensor->get_element_count();
        for (size_t i = 0; i < n; ++i)
        {
            float s = get_scalar<float>(m_tensor, i);
            m_max = std::max(m_max, s);
            m_min = std::min(m_min, s);
        }
    }

    float get_min() const { return m_min; }
    float get_max() const { return m_max; }
    std::ostream& dump(std::ostream& s) const override
    {
        return s << "MinMax[" << get_name() << ":" << get_min() << ", "
                 << get_max() << "]";
    }

protected:
    float m_min{std::numeric_limits<float>::max()};
    float m_max{std::numeric_limits<float>::min()};
};

// Show the elements of a tensor
class DumpTensor : public TensorDumper
{
public:
    DumpTensor(const std::string& name,
156
               const std::shared_ptr<ngraph::runtime::Tensor>& tensor)
157 158 159 160 161 162
        : TensorDumper(name, tensor)
    {
    }

    std::ostream& dump(std::ostream& s) const override
    {
163
        std::shared_ptr<ngraph::runtime::Tensor> t{get_tensor()};
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        const ngraph::Shape& shape = t->get_shape();
        s << "Tensor<" << get_name() << ": ";
        for (size_t i = 0; i < shape.size(); ++i)
        {
            s << shape.at(i);
            if (i + 1 < shape.size())
            {
                s << ", ";
            }
        }
        size_t pos = 0;
        s << ">{";
        size_t rank = shape.size();
        if (rank == 0)
        {
            s << get_scalar<float>(t, pos++);
        }
        else if (rank <= 2)
        {
            s << "[";
            for (size_t i = 0; i < shape.at(0); ++i)
            {
                if (rank == 1)
                {
                    s << get_scalar<float>(t, pos++);
                }
                else if (rank == 2)
                {
                    s << "[";
                    for (size_t j = 0; j < shape.at(1); ++j)
                    {
                        s << get_scalar<float>(t, pos++);

                        if (j + 1 < shape.at(1))
                        {
                            s << ", ";
                        }
                    }
                    s << "]";
                }
                if (i + 1 < shape.at(0))
                {
                    s << ", ";
                }
            }
            s << "]";
        }
        s << "}";
        return s;
    }
};