onnxruntime/winml/lib/Api/impl/Tensor.h
Paul McDaniel 5350abe19d
LearningModelSession is cleaned up to use the adapter, and parts of b… (#2382)
this is a big PR.    we are going to move it up to layer_dev , which is still a L3 so we are still safe to do work there agile.

we are going to move this into the L3 so that ryan can start doing intergration testing.   

we will pause for a full code review and integration test result prior to going into the L2.

>>>> raw comments from previous commits >>> 

* LearningModelSession is cleaned up to use the adapter, and parts of binding are.
* moved everything in the winmladapter
made it all nano-com using, WRL to construct objects in the ORT side.
base interfaces for everythign for winml to call
cleaned up a bunch of winml to use the base interfaces.
* more pieces
* GetData across the abi.
* renamed some namepsace
cleaned up OrtValue
cleaned up Tensor
cleaned up custom ops.
everything *but* learnignmodel should be clean
* make sure it's building.   winml.dll is still a monolith.
2019-11-14 17:44:07 -08:00

102 lines
No EOL
3.8 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#include "TensorBuffer.h"
#include "MLValueHelpers.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 Windows::AI::MachineLearning {
template <typename T>
class Tensor {
private:
using TensorBuffer = TensorBuffer<T>;
using TensorBufferPtr = typename TensorBuffer::TensorBufferPtr;
TensorBufferPtr m_buffer;
std::vector<int64_t> m_shape;
winrt::com_ptr<_winmla::IWinMLAdapter> adapter_;
public:
Tensor() = delete;
Tensor(
_winmla::IWinMLAdapter* adapter,
std::vector<int64_t> const& shape,
winrt::Windows::Storage::Streams::IBuffer buffer) : m_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)) {
adapter_.copy_from(adapter);
}
Tensor(
_winmla::IWinMLAdapter* adapter,
std::vector<int64_t> const& shape) : m_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>())))) {
adapter_.copy_from(adapter);
}
Tensor(
_winmla::IWinMLAdapter* adapter,
std::vector<int64_t> const&& shape) : m_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>())))) {
adapter_.copy_from(adapter);
}
auto size() const {
return m_buffer->Size();
}
auto buffer() {
return m_buffer->Buffer();
}
_winmla::IOrtValue* GetValue() {
// Get the data type
auto type = adapter_->GetTensorType(TensorKindFrom<T>::Type);
// create the ml value
winrt::com_ptr<_winmla::IOrtValue> value;
WINML_THROW_IF_FAILED(adapter_->CreateCPUMLValue(&m_shape, type, buffer().second, value.put()));
return value.detach();
}
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 m_shape;
}
};
} // namespace Windows::AI::MachineLearning