Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hexagon] Introduce new DeviceAPI #9355

Merged
merged 9 commits into from
Oct 26, 2021
28 changes: 16 additions & 12 deletions cmake/modules/Hexagon.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ if(BUILD_FOR_HEXAGON)
include_directories(SYSTEM ${HEXAGON_SDK_INCLUDES} ${HEXAGON_QURT_INCLUDES})
endif()

if(USE_HEXAGON_LAUNCHER STREQUAL "ON")
set(USE_HEXAGON_DEVICE "${PICK_SIM}")
else()
if(USE_HEXAGON_DEVICE STREQUAL "OFF")
list(APPEND COMPILER_SRCS src/target/opt/build_hexagon_off.cc)
return()
elseif(NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}" AND
NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
set(ERROR_MSG
"USE_HEXAGON_DEVICE must be one of [${PICK_NONE}|${PICK_SIM}|${PICK_HW}]")
message(SEND_ERROR "${ERROR_MSG}")
return()
# Don't run these checks when compiling Hexagon device code,
# e.g. when compiling the TVM runtime for Hexagon.
if (NOT BUILD_FOR_HEXAGON)
if(USE_HEXAGON_LAUNCHER STREQUAL "ON")
set(USE_HEXAGON_DEVICE "${PICK_SIM}")
else()
if(USE_HEXAGON_DEVICE STREQUAL "OFF")
list(APPEND COMPILER_SRCS src/target/opt/build_hexagon_off.cc)
return()
elseif(NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}" AND
NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
set(ERROR_MSG
"USE_HEXAGON_DEVICE must be one of [${PICK_NONE}|${PICK_SIM}|${PICK_HW}]")
message(SEND_ERROR "${ERROR_MSG}")
return()
endif()
endif()
endif()

Expand Down
122 changes: 122 additions & 0 deletions src/runtime/hexagon/hexagon/hexagon_buffer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#include "hexagon_buffer.h"

#include <tvm/runtime/module.h>

#include <string>
#include <utility>

#include "hexagon_common.h"

namespace tvm {
namespace runtime {
namespace hexagon {

static size_t GetDataAlignment(const DLDataType dtype) {
size_t align = (dtype.bits / 8) * dtype.lanes;
if (align < kAllocAlignment) return kAllocAlignment;
return align;
}

HexagonBuffer::HexagonBuffer(int ndim, const int64_t* shape, DLDataType dtype,
Optional<String> scope) {
ICHECK_LE(ndim, 1) << "Hexagon currently only supports flat allocations "
<< "and arrays of flat allocations.";

size_t alignment = GetDataAlignment(dtype);
// TODO(csullivan): Extend to support arrays of allocations.
// Move assignment from r-value constructed flat allocation.
*this = HexagonBuffer(shape[0] * (dtype.bits / 8) * dtype.lanes, alignment, scope);
}

HexagonBuffer::HexagonBuffer(size_t nbytes, size_t alignment, Optional<String> scope) {
void* ptr = nullptr;
int ret = posix_memalign(&ptr, alignment, nbytes);
if (ret != 0) {
throw std::bad_alloc();
}
allocations_.push_back(ptr);
SetStorageScope(scope);
}

HexagonBuffer::HexagonBuffer(void* data, Optional<String> scope) : managed_{false} {
SetStorageScope(scope);
allocations_.push_back(data);
}

HexagonBuffer::~HexagonBuffer() {
if (managed_) {
for (auto& ptr : allocations_) {
free(ptr);
}
}
}

HexagonBuffer::HexagonBuffer(HexagonBuffer&& other)
: allocations_(other.allocations_),
managed_(other.managed_),
storage_scope_(other.storage_scope_) {
other.allocations_.clear();
other.managed_ = false;
other.storage_scope_ = StorageScope::kDDR;
}

HexagonBuffer& HexagonBuffer::operator=(HexagonBuffer&& other) {
std::swap(allocations_, other.allocations_);
std::swap(managed_, other.managed_);
std::swap(storage_scope_, other.storage_scope_);
return *this;
}

void* HexagonBuffer::GetPointer() {
if (!allocations_.size()) {
return nullptr;
}
return (allocations_.size() > 1) ? allocations_.data() : allocations_[0];
}

HexagonBuffer::StorageScope HexagonBuffer::GetStorageScope() const { return storage_scope_; }

void HexagonBuffer::SetStorageScope(Optional<String> scope) {
if (!scope.defined()) {
storage_scope_ = StorageScope::kDDR;
} else {
if (scope.value() == "global") {
storage_scope_ = StorageScope::kDDR;
} else if (scope.value() == "global.vtcm") {
storage_scope_ = StorageScope::kVTCM;
} else {
CHECK(false) << "Encountered unknown HexagonBuffer storage scope: "
<< std::string(scope.value());
}
}
}

HexagonBuffer* IsHexagonBuffer(DLTensor* tensor) {
if (TVMDeviceExtType(tensor->device.device_type) == kDLHexagon) {
return static_cast<HexagonBuffer*>(tensor->data);
}
return nullptr;
}

} // namespace hexagon
} // namespace runtime
} // namespace tvm
135 changes: 135 additions & 0 deletions src/runtime/hexagon/hexagon/hexagon_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#ifndef TVM_RUNTIME_HEXAGON_HEXAGON_HEXAGON_BUFFER_H_
#define TVM_RUNTIME_HEXAGON_HEXAGON_HEXAGON_BUFFER_H_

#include <tvm/runtime/c_runtime_api.h>
#include <tvm/runtime/device_api.h>
#include <tvm/runtime/logging.h>
#include <tvm/runtime/ndarray.h>
#include <tvm/runtime/packed_func.h>

#include <vector>

namespace tvm {
namespace runtime {
namespace hexagon {

class HexagonBuffer {
public:
/* \brief Allocate memory within hexagon accessible memory
* scopes.
*
* \param ndim The number of dimensions of physical storage
* to allocate.
*
* \param shape The shape of the ndarray for which to allocate
* physical storage.
*
* \param dtype The data type of the physical storage.
*
* \param scope Optional storage scope indicating the memory
* space in which to allocate. Defaults to global system
* memory (DDR).
*/
HexagonBuffer(int ndim, const int64_t* shape, DLDataType dtype, Optional<String> scope);

/* \brief Allocate memory within hexagon accessible memory
* scopes.
*
* \param nbytes The number of bytes of flat physical storage
* to allocate.
*
* \param alignment The byte alignment to be used when allocating.
*
* \param scope Optional storage scope indicating the memory
* space in which to allocate. Defaults to global system
* memory (DDR).
*/
HexagonBuffer(size_t nbytes, size_t alignment, Optional<String> scope);

/* \brief Construct a hexagon buffer from externally allocated storage.
*
* \param data The externally allocated storage.
*
* \param scope Optional storage scope indicating the memory
* space in the external allocation belongs. Assumes global system
* memory if not provided.
*/
explicit HexagonBuffer(void* data, Optional<String> scope = Optional<String>());

//! \brief Destruction deallocates the underlying allocations.
~HexagonBuffer();

//! \brief Prevent copy construction of HexagonBuffers.
HexagonBuffer(const HexagonBuffer&) = delete;

//! \brief Prevent copy assignment with HexagonBuffers.
HexagonBuffer& operator=(const HexagonBuffer&) = delete;

//! \brief Allow move construction.
HexagonBuffer(HexagonBuffer&&);

//! \brief Allow move assignment.
HexagonBuffer& operator=(HexagonBuffer&&);

//! \brief Return pointer to allocation or allocations.
void* GetPointer();

//! \brief Memory scopes managed by a Hexagon Buffer.
enum class StorageScope {
//! \brief System DDR corresponding to global storage.
kDDR,
/*! \brief Vector tightly coupled memory corresponding to
* global.vtcm storage.
*/
kVTCM,
};

//! \brief Return storage scope of underlying allocation.
StorageScope GetStorageScope() const;

private:
//! \brief Assign a storage scope to the buffer.
void SetStorageScope(Optional<String> scope);
/*! \brief Array of allocations required by the buffer.
*
* For a 1d (flat) storage, a single contiguous allocation will
* result. For 2d storage, (count, nbytes) = shape, which will
* result in `count` discrete allocations.
*/
std::vector<void*> allocations_;
/*! \brief Whether the allocation(s) present are managed
* and should be deallocated upon destruction.
*/
bool managed_{true};
/*! \brief The underlying storage type in which the allocation
* resides.
*/
StorageScope storage_scope_;
};

HexagonBuffer* IsHexagonBuffer(DLTensor* tensor);

} // namespace hexagon
} // namespace runtime
} // namespace tvm

#endif // TVM_RUNTIME_HEXAGON_HEXAGON_HEXAGON_BUFFER_H_
Loading