| 1 | // ======================================================================== // |
| 2 | // Copyright 2009-2019 Intel Corporation // |
| 3 | // // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); // |
| 5 | // you may not use this file except in compliance with the License. // |
| 6 | // You may obtain a copy of the License at // |
| 7 | // // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 // |
| 9 | // // |
| 10 | // Unless required by applicable law or agreed to in writing, software // |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, // |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // |
| 13 | // See the License for the specific language governing permissions and // |
| 14 | // limitations under the License. // |
| 15 | // ======================================================================== // |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include "platform.h" |
| 20 | #include <vector> |
| 21 | #include <map> |
| 22 | |
| 23 | namespace oidn { |
| 24 | |
| 25 | template<typename T> |
| 26 | using shared_vector = std::shared_ptr<std::vector<T>>; |
| 27 | |
| 28 | // Generic tensor |
| 29 | struct Tensor |
| 30 | { |
| 31 | float* data; |
| 32 | std::vector<int64_t> dims; |
| 33 | std::string format; |
| 34 | shared_vector<char> buffer; // optional, only for reference counting |
| 35 | |
| 36 | __forceinline Tensor() : data(nullptr) {} |
| 37 | |
| 38 | __forceinline Tensor(const std::vector<int64_t>& dims, const std::string& format) |
| 39 | : dims(dims), |
| 40 | format(format) |
| 41 | { |
| 42 | buffer = std::make_shared<std::vector<char>>(size() * sizeof(float)); |
| 43 | data = (float*)buffer->data(); |
| 44 | } |
| 45 | |
| 46 | __forceinline operator bool() const { return data != nullptr; } |
| 47 | |
| 48 | __forceinline int ndims() const { return (int)dims.size(); } |
| 49 | |
| 50 | // Returns the number of values |
| 51 | __forceinline size_t size() const |
| 52 | { |
| 53 | size_t size = 1; |
| 54 | for (int i = 0; i < ndims(); ++i) |
| 55 | size *= dims[i]; |
| 56 | return size; |
| 57 | } |
| 58 | |
| 59 | __forceinline float& operator [](size_t i) { return data[i]; } |
| 60 | __forceinline const float& operator [](size_t i) const { return data[i]; } |
| 61 | }; |
| 62 | |
| 63 | // Parses tensors from a buffer |
| 64 | std::map<std::string, Tensor> parseTensors(void* buffer); |
| 65 | |
| 66 | } // namespace oidn |
| 67 | |