akaashrp commented on code in PR #20167:
URL: https://github.com/apache/tvm/pull/20167#discussion_r3859639705
##########
web/emcc/wasm_runtime.cc:
##########
@@ -126,38 +128,93 @@ TVM_FFI_STATIC_INIT_BLOCK() {
});
}
+size_t GetCheckedTensorElementCount(const Tensor& tensor) {
+ size_t size = 1;
+ for (int i = 0; i < tensor->ndim; ++i) {
+ TVM_FFI_ICHECK_GE(tensor->shape[i], 0);
+ const uint64_t dim_u64 = static_cast<uint64_t>(tensor->shape[i]);
+ TVM_FFI_ICHECK_LE(dim_u64, std::numeric_limits<size_t>::max());
+ const size_t dim = static_cast<size_t>(dim_u64);
+ if (size != 0) {
+ TVM_FFI_ICHECK_LE(dim, std::numeric_limits<size_t>::max() / size);
+ }
+ size *= dim;
+ }
+ return size;
+}
+
+void CheckF32CPUTensor(const Tensor& tensor) {
+ TVM_FFI_ICHECK_EQ(tensor->device.device_type, kDLCPU);
+ TVM_FFI_ICHECK(tensor.IsContiguous());
+ TVM_FFI_ICHECK_EQ(tensor->dtype.code, kDLFloat);
+ TVM_FFI_ICHECK_EQ(tensor->dtype.bits, 32);
+ TVM_FFI_ICHECK_EQ(tensor->dtype.lanes, 1);
+}
+
void ArrayDecodeStorage(Tensor cpu_arr, TVMFFIByteArray* bytes, const
std::string& format,
const std::string& dtype) {
TVM_FFI_ICHECK_NE(bytes, nullptr);
const char* byte_data = bytes->data;
const size_t byte_size = bytes->size;
if (format == "f32-to-bf16" && dtype == "float32") {
- const uint16_t* bf16 = reinterpret_cast<const uint16_t*>(byte_data);
- uint32_t* data = static_cast<uint32_t*>(cpu_arr->data);
- TVM_FFI_ICHECK(cpu_arr.IsContiguous());
- size_t size = 1;
- for (int i = 0; i < cpu_arr->ndim; ++i) {
- size *= cpu_arr->shape[i];
+ CheckF32CPUTensor(cpu_arr);
+ const size_t size = GetCheckedTensorElementCount(cpu_arr);
+ TVM_FFI_ICHECK_LE(size, std::numeric_limits<size_t>::max() / 4);
+ TVM_FFI_ICHECK_EQ(byte_size, size * 2);
+ if (size == 0) {
+ return;
}
- TVM_FFI_ICHECK_EQ(size, byte_size / 2);
+ TVM_FFI_ICHECK_NE(cpu_arr->data, nullptr);
+ TVM_FFI_ICHECK_NE(byte_data, nullptr);
+ const uint8_t* bf16 = reinterpret_cast<const uint8_t*>(byte_data);
+ uint8_t* data = static_cast<uint8_t*>(cpu_arr->data) +
cpu_arr->byte_offset;
for (size_t i = 0; i < size; ++i) {
- data[i] = static_cast<uint32_t>(bf16[i]) << 16;
+ data[4 * i] = 0;
+ data[4 * i + 1] = 0;
+ data[4 * i + 2] = bf16[2 * i];
+ data[4 * i + 3] = bf16[2 * i + 1];
}
} else {
cpu_arr.CopyFromBytes(byte_data, byte_size);
}
}
+void ArrayDecodeBF16ToF32Inplace(Tensor cpu_arr, int64_t encoded_nbytes) {
+ CheckF32CPUTensor(cpu_arr);
+ TVM_FFI_ICHECK_GE(encoded_nbytes, 0);
+
+ const size_t size = GetCheckedTensorElementCount(cpu_arr);
+ TVM_FFI_ICHECK_LE(size, std::numeric_limits<size_t>::max() / 4);
+ TVM_FFI_ICHECK_EQ(static_cast<uint64_t>(encoded_nbytes),
static_cast<uint64_t>(size) * 2);
+ if (size == 0) {
+ return;
+ }
+ TVM_FFI_ICHECK_NE(cpu_arr->data, nullptr);
+
+ uint8_t* data = static_cast<uint8_t*>(cpu_arr->data) + cpu_arr->byte_offset;
+ for (size_t i = size; i != 0; --i) {
Review Comment:
Done. This now loads the BF16 bits with `memcpy` and writes each float as
one 32-bit value.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]