onnxruntime/winml/lib/Api/impl/Tensor.h
Sheil Kumar 2717c178cc
Fork the WinML APIs into the Microsoft namespace (#3503)
* Migrate winml to Microsoft Namespace (packaging changes are pending)

* add ns_prefix toggle

* fix packaging

* Users/sheilk/add missing raw header (#3484)

* add dualapipartition

* wrong variable for repo root

Co-authored-by: Sheil Kumar <sheilk@microsoft.com>

* remove existence check to force failures

* extra paren

* dualapipartition needs to be referenced from the source

* add microsoft.ai.machinelearning.dll to the output dir

* rename the idl file so that assembly info is correctly added into the winmd

* fix namespaces

* update namespaces

* default to microsoft, and add namespace override as build argument

* update cmakesetings.json as well

* remove from cmakelists.txt

Co-authored-by: Sheil Kumar <sheilk@microsoft.com>
Co-authored-by: Changming Sun <chasun@microsoft.com>
2020-04-17 06:18:54 -07:00

93 lines
No EOL
3 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "TensorBuffer.h"
//
// the Tensor class is the actual object for CPU memory buffers.
// TensorBase contains one of these to represent the raw memory
// GetCpuResource() returns it
//
namespace _winml {
template <typename T>
class Tensor {
private:
using TensorBuffer = TensorBuffer<T>;
using TensorBufferPtr = typename TensorBuffer::TensorBufferPtr;
TensorBufferPtr m_buffer;
std::vector<int64_t> shape_;
public:
Tensor() = delete;
Tensor(
std::vector<int64_t> const& shape,
wss::IBuffer buffer) : shape_(shape),
m_buffer(
TensorBuffer::Create(
static_cast<uint32_t>(
std::accumulate(
std::begin(shape),
std::end(shape),
static_cast<int64_t>(1),
std::multiplies<int64_t>())),
buffer)) {
}
Tensor(
std::vector<int64_t> const& shape) : shape_(shape),
m_buffer(
TensorBuffer::Create(
static_cast<uint32_t>(
std::accumulate(
std::begin(shape),
std::end(shape),
static_cast<int64_t>(1),
std::multiplies<int64_t>())))) {
}
Tensor(
std::vector<int64_t> const&& shape) : shape_(std::move(shape)),
m_buffer(
TensorBuffer::Create(
static_cast<uint32_t>(
std::accumulate(
std::begin(shape),
std::end(shape),
static_cast<int64_t>(1),
std::multiplies<int64_t>())))) {
}
auto size() const {
return m_buffer->Size();
}
auto size_in_bytes() const {
return m_buffer->SizeInBytes();
}
auto buffer() {
return m_buffer->Buffer();
}
void set(uint32_t size, const T* pData) {
m_buffer->Set(size, pData);
}
void set(std::vector<T>&& other) {
m_buffer->Set(other);
}
const std::vector<int64_t>& shape() const {
return shape_;
}
auto get_tensor_buffer() {
return m_buffer;
}
};
} // namespace _winml