You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_loader.h 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * \file sdk/load-and-run/src/json_loader.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied.
  11. */
  12. #pragma once
  13. #include <cctype>
  14. #include <fstream>
  15. #include <functional>
  16. #include <iostream>
  17. #include <map>
  18. #include <memory>
  19. #include "megbrain/common.h"
  20. #include "megdnn/thin/small_vector.h"
  21. namespace mgb {
  22. class JsonLoader {
  23. public:
  24. class Value {
  25. protected:
  26. enum struct Type : uint8_t { UNKNOWN, NUMBER, STRING, OBJECT, ARRAY };
  27. Type m_type;
  28. public:
  29. template <typename T>
  30. T* safe_cast();
  31. Value() { m_type = Type::UNKNOWN; }
  32. Value(Type type) : m_type(type) {}
  33. virtual ~Value() {}
  34. bool is_array() { return Type::ARRAY == m_type; }
  35. bool is_object() { return Type::OBJECT == m_type; }
  36. bool is_number() { return Type::NUMBER == m_type; }
  37. bool is_str() { return Type::STRING == m_type; }
  38. std::unique_ptr<Value>& operator[](const std::string& key);
  39. std::unique_ptr<Value>& operator[](const size_t index);
  40. std::map<std::string, std::unique_ptr<Value>>& objects();
  41. size_t len();
  42. megdnn::SmallVector<std::unique_ptr<Value>>& array();
  43. double number();
  44. std::string str();
  45. };
  46. void expect(char c);
  47. void skip_whitespace();
  48. std::unique_ptr<Value> parse_object();
  49. std::unique_ptr<Value> parse_array();
  50. std::unique_ptr<Value> parse_string();
  51. std::unique_ptr<Value> parse_number();
  52. std::unique_ptr<Value> parse_value();
  53. enum struct State : uint8_t {
  54. OK = 0,
  55. BAD_TYPE,
  56. BAD_DIGIT,
  57. BAD_ARRAY,
  58. MISS_COLON,
  59. MISS_BRACE,
  60. KEY_NOT_UNIQUE
  61. };
  62. JsonLoader() { m_state = State::OK; }
  63. std::unique_ptr<Value> load(const char* content, const size_t size);
  64. std::unique_ptr<Value> load(const char* path);
  65. class NumberValue final : public Value {
  66. friend std::unique_ptr<Value> JsonLoader::parse_number();
  67. double m_value;
  68. public:
  69. NumberValue() : Value(Type::NUMBER) {}
  70. double value() { return m_value; }
  71. };
  72. class StringValue final : public Value {
  73. std::string m_value;
  74. public:
  75. StringValue() : Value(Type::STRING) {}
  76. std::string value() { return m_value; }
  77. friend std::unique_ptr<Value> JsonLoader::parse_string();
  78. };
  79. class ArrayValue final : public Value {
  80. megdnn::SmallVector<std::unique_ptr<Value>> m_obj;
  81. public:
  82. ArrayValue() : Value(Type::ARRAY) {}
  83. ArrayValue(ArrayValue& arr) : Value(arr) {
  84. m_obj.clear();
  85. for (auto& item : arr.m_obj) {
  86. m_obj.emplace_back(item.get());
  87. item.release();
  88. }
  89. }
  90. ArrayValue(ArrayValue&& arr) : Value(arr) {
  91. m_obj.clear();
  92. for (auto& item : arr.m_obj) {
  93. m_obj.emplace_back(item.get());
  94. item.release();
  95. }
  96. }
  97. friend std::unique_ptr<Value> JsonLoader::parse_array();
  98. friend std::unique_ptr<JsonLoader::Value>& JsonLoader::Value::
  99. operator[](const size_t index);
  100. friend megdnn::SmallVector<std::unique_ptr<JsonLoader::Value>>&
  101. JsonLoader::Value::array();
  102. friend size_t JsonLoader::Value::len();
  103. };
  104. class ObjectValue final : public Value {
  105. std::map<std::string, std::unique_ptr<Value>> m_obj;
  106. public:
  107. ObjectValue() : Value(Type::OBJECT) {}
  108. ObjectValue(ObjectValue& arr) : Value(arr) {
  109. m_obj.clear();
  110. for (auto itra = arr.m_obj.begin(); itra != arr.m_obj.end();
  111. ++itra) {
  112. m_obj.emplace(
  113. std::make_pair(itra->first, std::move(itra->second)));
  114. }
  115. }
  116. ObjectValue(ObjectValue&& arr) : Value(arr) {
  117. m_obj.clear();
  118. for (auto itra = arr.m_obj.begin(); itra != arr.m_obj.end();
  119. ++itra) {
  120. m_obj.emplace(
  121. std::make_pair(itra->first, std::move(itra->second)));
  122. }
  123. }
  124. friend std::unique_ptr<Value> JsonLoader::parse_object();
  125. friend std::unique_ptr<JsonLoader::Value>& JsonLoader::Value::
  126. operator[](const std::string&);
  127. friend std::map<std::string, std::unique_ptr<JsonLoader::Value>>&
  128. JsonLoader::Value::objects();
  129. friend size_t JsonLoader::Value::len();
  130. };
  131. private:
  132. const char* m_buf;
  133. State m_state;
  134. };
  135. } // namespace mgb

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台