junrushao1994 commented on a change in pull request #7488:
URL: https://github.com/apache/tvm/pull/7488#discussion_r583992985
##########
File path: src/runtime/c_runtime_api.cc
##########
@@ -144,6 +144,48 @@ void* DeviceAPI::AllocWorkspace(TVMContext ctx, size_t
size, DLDataType type_hin
return AllocDataSpace(ctx, size, kTempAllocaAlignment, type_hint);
}
+static size_t GetDataAlignment(const DLDataType dtype) {
+ size_t align = (dtype.bits / 8) * dtype.lanes;
+ if (align < kAllocAlignment) return kAllocAlignment;
+ return align;
+}
+
+void* DeviceAPI::AllocDataSpace(TVMContext ctx, int ndim, const int64_t*
shape, DLDataType dtype,
+ Optional<String> mem_scope) {
+ if (!mem_scope.defined() || mem_scope.value() == "global") {
+ // by default, we can always redirect to the flat memory allocations
+ DLTensor temp;
+ temp.data = nullptr;
+ temp.ctx = ctx;
+ temp.ndim = ndim;
+ temp.dtype = dtype;
+ temp.shape = const_cast<int64_t*>(shape);
+ temp.strides = nullptr;
+ temp.byte_offset = 0;
+ size_t size = GetDataSize(temp);
+ size_t alignment = GetDataAlignment(temp.dtype);
+ return AllocDataSpace(ctx, size, alignment, dtype);
+ }
+ LOG(FATAL) << "Device does not support allocate data space with "
+ << "specified memory scope: " << mem_scope.value();
+ return nullptr;
+}
+
+void DeviceAPI::CopyDataFromTo(DLTensor* from, DLTensor* to, TVMStreamHandle
stream) {
+ // by default, we can always redirect to the flat memory copy operation.
+ size_t nbytes = GetDataSize(*from);
+ ICHECK_EQ(nbytes, GetDataSize(*to));
+ ICHECK(from->strides == nullptr && to->strides == nullptr);
Review comment:
Not sure...Do we want to CHECK or ICHECK here?
##########
File path: src/runtime/crt/common/crt_runtime_api.c
##########
@@ -87,16 +87,33 @@ int TVMDeviceAllocDataSpace(DLContext ctx, size_t nbytes,
size_t alignment, DLDa
if (alignment != 1) {
nbytes = (nbytes + alignment - 1) / alignment * alignment;
}
-
return TVMPlatformMemoryAllocate(nbytes, ctx, out_data);
}
+int TVMDeviceAllocDataSpaceWithScope(DLContext ctx, int ndim, const int64_t*
shape,
+ DLDataType dtype, const char* mem_scope,
void** out_data) {
+ size_t nbytes = 1;
+ for (int i = 0; i < ndim; ++i) {
+ nbytes *= shape[i];
+ }
+ nbytes *= (dtype.bits * dtype.lanes + 7) / 8;
+
+ int kAllocAlignment = 128;
Review comment:
nitpick: let's mark it as constexpr
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]