micro_session.cc 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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.
 */

/*!
 * \file micro_session.cc
 */

#include <dmlc/thread_local.h>
#include <tvm/runtime/registry.h>
26
#include <memory>
27 28 29 30 31 32 33 34 35 36 37
#include <stack>
#include <tuple>
#include <vector>
#include "micro_session.h"
#include "low_level_device.h"
#include "target_data_layout_encoder.h"

namespace tvm {
namespace runtime {

struct TVMMicroSessionThreadLocalEntry {
38
  std::stack<ObjectPtr<MicroSession>> session_stack;
39 40 41 42
};

typedef dmlc::ThreadLocalStore<TVMMicroSessionThreadLocalEntry> TVMMicroSessionThreadLocalStore;

43
ObjectPtr<MicroSession>& MicroSession::Current() {
44 45 46 47 48
  TVMMicroSessionThreadLocalEntry *entry = TVMMicroSessionThreadLocalStore::Get();
  CHECK_GT(entry->session_stack.size(), 0) << "No current session";
  return entry->session_stack.top();
}

49
void MicroSession::EnterWithScope(ObjectPtr<MicroSession> session) {
50 51 52 53 54 55 56 57 58 59
  TVMMicroSessionThreadLocalEntry *entry = TVMMicroSessionThreadLocalStore::Get();
  entry->session_stack.push(session);
}

void MicroSession::ExitWithScope() {
  TVMMicroSessionThreadLocalEntry *entry = TVMMicroSessionThreadLocalStore::Get();
  CHECK(!entry->session_stack.empty());
  entry->session_stack.pop();
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
MicroSession::MicroSession(
    const std::string& comms_method,
    const std::string& binary_path,
    const std::string& toolchain_prefix,
    uint64_t text_start,
    size_t text_size,
    uint64_t rodata_start,
    size_t rodata_size,
    uint64_t data_start,
    size_t data_size,
    uint64_t bss_start,
    size_t bss_size,
    uint64_t args_start,
    size_t args_size,
    uint64_t heap_start,
    size_t heap_size,
    uint64_t workspace_start,
    size_t workspace_size,
    uint64_t stack_start,
    size_t stack_size,
    size_t word_size,
    bool thumb_mode,
    const std::string& server_addr,
    int port)
    : toolchain_prefix_(toolchain_prefix)
    , word_size_(word_size)
    , thumb_mode_(thumb_mode) {
  CHECK(word_size_ == 4 || word_size_ == 8) << "unsupported word size " << word_size_;
  if (comms_method == "host") {
    // TODO(weberlo): move checks to python
    CHECK(
        text_start == 0 &&
        rodata_start == 0 &&
        data_start == 0 &&
        bss_start == 0 &&
        args_start == 0 &&
        heap_start == 0 &&
        workspace_start == 0 &&
        stack_start == 0) << "unable to specify section addresses for host device";
    size_t memory_size =
      text_size + rodata_size + data_size + bss_size +
      args_size + heap_size + workspace_size + stack_size;
    void* base_addr;
    low_level_device_ = HostLowLevelDeviceCreate(memory_size, &base_addr);
    CHECK_EQ(reinterpret_cast<std::uintptr_t>(base_addr) % word_size_, 0)
      << "base address not aligned to " << word_size_ << " bytes";
    DevPtr curr_addr = DevPtr(reinterpret_cast<std::uintptr_t>(base_addr));

    section_allocators_[0] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = curr_addr,
      .size = text_size,
    }, word_size_);
    curr_addr += text_size;
    section_allocators_[1] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = curr_addr,
      .size = rodata_size,
    }, word_size_);
    curr_addr += rodata_size;
    section_allocators_[2] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = curr_addr,
      .size = data_size,
    }, word_size_);
    curr_addr += data_size;
    section_allocators_[3] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = curr_addr,
      .size = bss_size,
    }, word_size_);
    curr_addr += bss_size;
    section_allocators_[4] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = curr_addr,
      .size = args_size,
    }, word_size_);
    curr_addr += args_size;
    section_allocators_[5] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = curr_addr,
      .size = heap_size,
    }, word_size_);
    curr_addr += heap_size;
    section_allocators_[6] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = curr_addr,
      .size = workspace_size,
    }, word_size_);
    curr_addr += workspace_size;
    section_allocators_[7] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = curr_addr,
      .size = stack_size,
    }, word_size_);
    curr_addr += stack_size;
  } else if (comms_method == "openocd") {
    low_level_device_ = OpenOCDLowLevelDeviceCreate(server_addr, port);
    section_allocators_[0] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = DevPtr(text_start),
      .size = text_size,
    }, word_size_);
    section_allocators_[1] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = DevPtr(rodata_start),
      .size = rodata_size,
    }, word_size_);
    section_allocators_[2] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = DevPtr(data_start),
      .size = data_size,
    }, word_size_);
    section_allocators_[3] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = DevPtr(bss_start),
      .size = bss_size,
    }, word_size_);
    section_allocators_[4] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = DevPtr(args_start),
      .size = args_size,
    }, word_size_);
    section_allocators_[5] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = DevPtr(heap_start),
      .size = heap_size,
    }, word_size_);
    section_allocators_[6] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = DevPtr(workspace_start),
      .size = workspace_size,
    }, word_size_);
    section_allocators_[7] = std::make_shared<MicroSectionAllocator>(DevMemRegion {
      .start = DevPtr(stack_start),
      .size = stack_size,
    }, word_size_);
  } else {
    LOG(FATAL) << "unsupported micro low-level device";
  }

  runtime_symbol_map_ = LoadBinary(binary_path, false).symbol_map;

  // Patch pointers to define the bounds of the workspace section and the word
  // size (for allocation alignment).
  std::shared_ptr<MicroSectionAllocator> ws_allocator = GetAllocator(SectionKind::kWorkspace);
  TargetVal ws_start = ws_allocator->start_addr().value();
  TargetVal ws_end = ws_allocator->max_addr().value();
  TargetVal target_word_size { .val64 = word_size_ };
  if (word_size_ == 4) {
    DevSymbolWrite(runtime_symbol_map_, "utvm_workspace_start", ws_start.val32);
    DevSymbolWrite(runtime_symbol_map_, "utvm_workspace_end", ws_end.val32);
    DevSymbolWrite(runtime_symbol_map_, "utvm_word_size", target_word_size.val32);
  } else if (word_size_ == 8) {
    DevSymbolWrite(runtime_symbol_map_, "utvm_workspace_start", ws_start.val64);
    DevSymbolWrite(runtime_symbol_map_, "utvm_workspace_end", ws_end.val64);
    DevSymbolWrite(runtime_symbol_map_, "utvm_word_size", target_word_size.val64);
202 203 204 205 206 207 208 209 210 211
  }
}

MicroSession::~MicroSession() {
  for (size_t i = 0; i < static_cast<size_t>(SectionKind::kNumKinds); i++) {
    section_allocators_[i] = nullptr;
  }
  low_level_device_ = nullptr;
}

212 213 214
double MicroSession::PushToExecQueue(DevPtr func_ptr, const TVMArgs& args) {
  if (thumb_mode_) {
    func_ptr += 1;
215
  }
216

217 218
  // Create an allocator stream for the memory region after the most recent
  // allocation in the args section.
219 220
  DevPtr args_addr = GetAllocator(SectionKind::kArgs)->curr_end_addr();
  TargetDataLayoutEncoder encoder(args_addr, word_size_);
221 222

  std::tuple<DevPtr, DevPtr> arg_field_addrs = EncoderAppend(&encoder, args);
223

224
  // Flush `stream` to device memory.
225
  DevPtr stream_dev_addr =
226
      GetAllocator(SectionKind::kArgs)->Allocate(encoder.buf_size());
227
  low_level_device()->Write(stream_dev_addr,
228 229 230
                            reinterpret_cast<void*>(encoder.data()),
                            encoder.buf_size());

231 232 233 234 235 236 237
  TargetVal arg_values_dev_addr = std::get<0>(arg_field_addrs).value();
  TargetVal arg_type_codes_dev_addr = std::get<1>(arg_field_addrs).value();
  if (word_size_ == 4) {
    UTVMTask32 task = {
      .func = func_ptr.value().val32,
      .arg_values = arg_values_dev_addr.val32,
      .arg_type_codes = arg_type_codes_dev_addr.val32,
238
      .num_args = args.num_args,
239 240 241 242 243 244 245 246 247 248 249 250 251
    };
    // Write the task.
    DevSymbolWrite(runtime_symbol_map_, "utvm_task", task);
  } else if (word_size_ == 8) {
    UTVMTask64 task = {
      .func = func_ptr.value().val64,
      .arg_values = arg_values_dev_addr.val64,
      .arg_type_codes = arg_type_codes_dev_addr.val64,
      .num_args = args.num_args,
    };
    // Write the task.
    DevSymbolWrite(runtime_symbol_map_, "utvm_task", task);
  }
252

253 254 255 256 257 258 259
  DevPtr utvm_init_addr = runtime_symbol_map_["UTVMInit"];
  DevPtr utvm_done_addr = runtime_symbol_map_["UTVMDone"];
  if (thumb_mode_) {
    utvm_init_addr += 1;
  }

  low_level_device()->Execute(utvm_init_addr, utvm_done_addr);
260 261
  // Check if there was an error during execution.  If so, log it.
  CheckDeviceError();
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  uint32_t task_time = DevSymbolRead<uint32_t>(runtime_symbol_map_, "utvm_task_time");
  GetAllocator(SectionKind::kArgs)->Free(stream_dev_addr);
  return static_cast<double>(task_time);
}

BinaryInfo MicroSession::LoadBinary(const std::string& binary_path, bool patch_dylib_pointers) {
  DevMemRegion text_section;
  DevMemRegion rodata_section;
  DevMemRegion data_section;
  DevMemRegion bss_section;

  text_section.size = GetSectionSize(
      binary_path, SectionKind::kText, toolchain_prefix_, word_size_);
  rodata_section.size = GetSectionSize(
      binary_path, SectionKind::kRodata, toolchain_prefix_, word_size_);
  data_section.size = GetSectionSize(
      binary_path, SectionKind::kData, toolchain_prefix_, word_size_);
  bss_section.size = GetSectionSize(
      binary_path, SectionKind::kBss, toolchain_prefix_, word_size_);

  text_section.start = AllocateInSection(SectionKind::kText, text_section.size);
  rodata_section.start = AllocateInSection(SectionKind::kRodata, rodata_section.size);
  data_section.start = AllocateInSection(SectionKind::kData, data_section.size);
  bss_section.start = AllocateInSection(SectionKind::kBss, bss_section.size);
  CHECK(text_section.start != nullptr && rodata_section.start != nullptr &&
        data_section.start != nullptr && bss_section.start != nullptr)
      << "not enough space to load module on device";

  std::string relocated_bin = RelocateBinarySections(
      binary_path,
      word_size_,
      text_section.start,
      rodata_section.start,
      data_section.start,
      bss_section.start,
      GetAllocator(SectionKind::kStack)->max_addr(),
      toolchain_prefix_);
  std::string text_contents = ReadSection(relocated_bin, SectionKind::kText, toolchain_prefix_);
  std::string rodata_contents = ReadSection(relocated_bin, SectionKind::kRodata, toolchain_prefix_);
  std::string data_contents = ReadSection(relocated_bin, SectionKind::kData, toolchain_prefix_);
  std::string bss_contents = ReadSection(relocated_bin, SectionKind::kBss, toolchain_prefix_);

  low_level_device_->Write(text_section.start, &text_contents[0], text_section.size);
  low_level_device_->Write(rodata_section.start, &rodata_contents[0], rodata_section.size);
  low_level_device_->Write(data_section.start, &data_contents[0], data_section.size);
  low_level_device_->Write(bss_section.start, &bss_contents[0], bss_section.size);
  SymbolMap symbol_map {relocated_bin, toolchain_prefix_};

  if (patch_dylib_pointers) {
    // Patch device lib pointers.
    PatchImplHole(symbol_map, "TVMBackendAllocWorkspace");
    PatchImplHole(symbol_map, "TVMBackendFreeWorkspace");
    PatchImplHole(symbol_map, "TVMAPISetLastError");
  }
316

317 318 319 320 321 322 323
  return BinaryInfo {
      .text_section = text_section,
      .rodata_section = rodata_section,
      .data_section = data_section,
      .bss_section = bss_section,
      .symbol_map = symbol_map,
  };
324 325 326 327 328 329 330 331 332 333 334 335
}

std::tuple<DevPtr, DevPtr> MicroSession::EncoderAppend(
    TargetDataLayoutEncoder* encoder, const TVMArgs& args) {
  const int* type_codes = args.type_codes;
  int num_args = args.num_args;

  auto tvm_vals_slot = encoder->Alloc<TVMValue>(num_args);
  auto type_codes_slot = encoder->Alloc<const int>(num_args);

  for (int i = 0; i < num_args; i++) {
    switch (type_codes[i]) {
336 337 338
      case kTVMNDArrayHandle:
      case kTVMDLTensorHandle: {
        DLTensor* base_arr_handle = args[i];
339 340 341 342 343 344 345
        // All uTVM arrays store a `MicroDevSpace` struct in their `data` field,
        // which wraps the actual data and stores a reference to the session, in
        // order to prevent premature session destruction.
        void* old_data = base_arr_handle->data;
        // Mutate the array to unwrap the `data` field.
        base_arr_handle->data = reinterpret_cast<MicroDevSpace*>(old_data)->data;
        // Now, encode the unwrapped version.
346 347 348 349 350 351
        void* arr_ptr = nullptr;
        if (word_size_ == 4) {
          arr_ptr = EncoderAppend<TVMArray32>(encoder, *base_arr_handle).cast_to<void*>();
        } else if (word_size_ == 8) {
          arr_ptr = EncoderAppend<TVMArray64>(encoder, *base_arr_handle).cast_to<void*>();
        }
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        // And restore the original wrapped version.
        base_arr_handle->data = old_data;

        TVMValue val;
        val.v_handle = arr_ptr;
        tvm_vals_slot.WriteValue(val);
        break;
      }
      // TODO(weberlo): Implement `double` and `int64` case.
      case kDLFloat:
      case kDLInt:
      case kDLUInt:
      default:
        LOG(FATAL) << "unsupported type code for writing args: " << type_codes[i];
        break;
    }
  }
  type_codes_slot.WriteArray(type_codes, num_args);
  return std::make_tuple(tvm_vals_slot.start_addr(), type_codes_slot.start_addr());
}

373
template <typename T>
374
DevPtr MicroSession::EncoderAppend(TargetDataLayoutEncoder* encoder, const DLTensor& arr) {
375
  auto tvm_arr_slot = encoder->Alloc<T>();
376 377 378 379 380 381
  auto shape_slot = encoder->Alloc<int64_t>(arr.ndim);

  // `shape` and `strides` are stored on the host, so we need to write them to
  // the device first. The `data` field is already allocated on the device and
  // is a device pointer, so we don't need to write it.
  shape_slot.WriteArray(arr.shape, arr.ndim);
382 383
  DevPtr shape_dev_addr = shape_slot.start_addr();
  DevPtr strides_dev_addr = DevPtr(nullptr);
384 385 386
  if (arr.strides != nullptr) {
    auto stride_slot = encoder->Alloc<int64_t>(arr.ndim);
    stride_slot.WriteArray(arr.strides, arr.ndim);
387
    strides_dev_addr = stride_slot.start_addr();
388 389
  }

390 391 392 393 394 395 396 397
  T dev_arr(
      TargetVal { .val64 = reinterpret_cast<uint64_t>(arr.data) },
      arr.ctx,
      arr.ndim,
      arr.dtype,
      shape_dev_addr.value(),
      strides_dev_addr.value(),
      TargetVal { .val64 = arr.byte_offset });
398
  CHECK(dev_arr.ctx.device_type == static_cast<DLDeviceType>(kDLMicroDev))
399
    << "attempt to write DLTensor with non-micro device type";
400 401
  // Update the device type to CPU, because from the microcontroller's
  // perspective, it is.
402 403 404 405 406 407
  dev_arr.ctx.device_type = DLDeviceType::kDLCPU;
  tvm_arr_slot.WriteValue(dev_arr);
  return tvm_arr_slot.start_addr();
}

void MicroSession::CheckDeviceError() {
408
  int32_t return_code = DevSymbolRead<int32_t>(runtime_symbol_map_, "utvm_return_code");
409 410 411

  if (return_code) {
    std::uintptr_t last_error =
412
        DevSymbolRead<std::uintptr_t>(runtime_symbol_map_, "utvm_last_error");
413 414
    std::string last_error_str;
    if (last_error) {
415 416
      DevPtr last_err_addr = DevPtr(last_error);
      last_error_str = ReadString(last_err_addr);
417 418 419 420 421 422 423 424 425
    }
    LOG(FATAL) << "error during micro function execution:\n"
               << "  return code: " << std::dec << return_code << "\n"
               << "  dev str addr: 0x" << std::hex << last_error << "\n"
               << "  dev str data: " << last_error_str << std::endl;
  }
}

void MicroSession::PatchImplHole(const SymbolMap& symbol_map, const std::string& func_name) {
426 427 428 429
  DevPtr runtime_impl_addr = runtime_symbol_map_[func_name];
  if (thumb_mode_) {
    runtime_impl_addr += 1;
  }
430
  std::ostringstream func_name_underscore;
431
  func_name_underscore << func_name << "_";
432 433 434 435 436
  if (word_size_ == 4) {
    DevSymbolWrite(symbol_map, func_name_underscore.str(), runtime_impl_addr.value().val32);
  } else if (word_size_ == 8) {
    DevSymbolWrite(symbol_map, func_name_underscore.str(), runtime_impl_addr.value().val64);
  }
437 438
}

439
std::string MicroSession::ReadString(DevPtr str_addr) {
440
  std::ostringstream result;
441 442 443 444
  const size_t buf_size = 256;
  std::vector<char> buf(buf_size, 0);
  size_t i = buf_size;
  while (i == buf_size) {
445
    low_level_device()->Read(str_addr, buf.data(), buf_size);
446 447 448 449 450 451
    i = 0;
    while (i < buf_size) {
      if (buf[i] == 0) break;
      result << buf[i];
      i++;
    }
452
    str_addr = str_addr + i;
453 454 455 456
  }
  return result.str();
}

457
DevPtr MicroSession::AllocateInSection(SectionKind type, size_t size) {
458 459 460
  return GetAllocator(type)->Allocate(size);
}

461 462
void MicroSession::FreeInSection(SectionKind type, DevPtr addr) {
  return GetAllocator(type)->Free(addr);
463 464 465 466
}

template <typename T>
T MicroSession::DevSymbolRead(const SymbolMap& symbol_map, const std::string& symbol) {
467
  DevPtr sym_addr = symbol_map[symbol];
468
  T result;
469
  low_level_device()->Read(sym_addr, &result, sizeof(T));
470 471 472 473 474 475 476
  return result;
}

template <typename T>
void MicroSession::DevSymbolWrite(const SymbolMap& symbol_map,
                                  const std::string& symbol,
                                  const T& value) {
477 478
  DevPtr sym_addr = symbol_map[symbol];
  low_level_device()->Write(sym_addr, &value, sizeof(T));
479 480 481 482
}

PackedFunc MicroSession::GetFunction(
    const std::string& name,
483
    const ObjectPtr<Object>& sptr_to_self) {
484
  if (name == "enter") {
485 486
    return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
      MicroSession::EnterWithScope(GetObjectPtr<MicroSession>(this));
487 488 489 490 491 492 493 494 495 496 497 498 499
    });
  } else if (name == "exit") {
    return PackedFunc([sptr_to_self](TVMArgs args, TVMRetValue* rv) {
      MicroSession::ExitWithScope();
    });
  } else {
    return PackedFunc();
  }
}

// create micro session and low-level device from Python frontend
TVM_REGISTER_GLOBAL("micro._CreateSession")
.set_body([](TVMArgs args, TVMRetValue* rv) {
500
    const std::string& comms_method = args[0];
501 502
    const std::string& binary_path = args[1];
    const std::string& toolchain_prefix = args[2];
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    uint64_t text_start = args[3];
    size_t text_size = args[4];
    uint64_t rodata_start = args[5];
    size_t rodata_size = args[6];
    uint64_t data_start = args[7];
    size_t data_size = args[8];
    uint64_t bss_start = args[9];
    size_t bss_size = args[10];
    uint64_t args_start = args[11];
    size_t args_size = args[12];
    uint64_t heap_start = args[13];
    size_t heap_size = args[14];
    uint64_t workspace_start = args[15];
    size_t workspace_size = args[16];
    uint64_t stack_start = args[17];
    size_t stack_size = args[18];
    size_t word_size = args[19];
    bool thumb_mode = args[20];
    const std::string& server_addr = args[21];
    int port = args[22];
    ObjectPtr<MicroSession> session = make_object<MicroSession>(
        comms_method,
        binary_path,
        toolchain_prefix,
        text_start,
        text_size,
        rodata_start,
        rodata_size,
        data_start,
        data_size,
        bss_start,
        bss_size,
        args_start,
        args_size,
        heap_start,
        heap_size,
        workspace_start,
        workspace_size,
        stack_start,
        stack_size,
        word_size,
        thumb_mode,
        server_addr,
        port);
547 548 549 550 551
    *rv = Module(session);
    });

}  // namespace runtime
}  // namespace tvm