PageRenderTime 77ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

/tensorflow_serving/resources/resource_util.cc

https://gitlab.com/vectorci/serving
C++ | 337 lines | 275 code | 33 blank | 29 comment | 58 complexity | 07a71f5b174cf10f81733904fb863194 MD5 | raw file
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "tensorflow_serving/resources/resource_util.h"
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. #include "google/protobuf/wrappers.pb.h"
  17. #include "tensorflow/core/lib/core/errors.h"
  18. #include "tensorflow/core/platform/logging.h"
  19. #include "tensorflow/core/platform/types.h"
  20. namespace tensorflow {
  21. namespace serving {
  22. namespace {
  23. // Returns a copy of 'devices', stripped of any entries whose value is 0.
  24. std::map<string, uint32> StripDevicesWithZeroInstances(
  25. const std::map<string, uint32>& devices) {
  26. std::map<string, uint32> result;
  27. for (const auto& entry : devices) {
  28. if (entry.second > 0) {
  29. result.insert(entry);
  30. }
  31. }
  32. return result;
  33. }
  34. // Obtains the quantity associated with 'resource' in 'allocation'. If none is
  35. // found, returns 0.
  36. uint64 GetQuantityForResource(const Resource& resource,
  37. const ResourceAllocation& allocation) {
  38. for (const ResourceAllocation::Entry& entry :
  39. allocation.resource_quantities()) {
  40. if (entry.resource() == resource) {
  41. return entry.quantity();
  42. }
  43. }
  44. return 0;
  45. }
  46. // Returns a pointer to the entry associated with 'resource' in 'allocation'. If
  47. // none is found, returns nullptr.
  48. ResourceAllocation::Entry* FindMutableEntry(const Resource& resource,
  49. ResourceAllocation* allocation) {
  50. for (ResourceAllocation::Entry& entry :
  51. *allocation->mutable_resource_quantities()) {
  52. if (entry.resource() == resource) {
  53. return &entry;
  54. }
  55. }
  56. return nullptr;
  57. }
  58. // Returns a pointer to the entry associated with 'resource' in 'allocation'. If
  59. // none is found, inserts an entry with quantity 0 and returns a pointer to it.
  60. ResourceAllocation::Entry* FindOrInsertMutableEntry(
  61. const Resource& resource, ResourceAllocation* allocation) {
  62. ResourceAllocation::Entry* entry = FindMutableEntry(resource, allocation);
  63. if (entry == nullptr) {
  64. entry = allocation->add_resource_quantities();
  65. *entry->mutable_resource() = resource;
  66. entry->set_quantity(0);
  67. }
  68. return entry;
  69. }
  70. } // namespace
  71. ResourceUtil::ResourceUtil(const Options& options)
  72. : devices_(StripDevicesWithZeroInstances(options.devices)) {}
  73. Status ResourceUtil::VerifyValidity(
  74. const ResourceAllocation& allocation) const {
  75. // We use 'validated_entries' to look for duplicates.
  76. ResourceAllocation validated_entries;
  77. for (const auto& entry : allocation.resource_quantities()) {
  78. auto it = devices_.find(entry.resource().device());
  79. if (it == devices_.end()) {
  80. return errors::InvalidArgument(
  81. "Invalid resource allocation: Invalid device ",
  82. entry.resource().device(), " in resource allocation\n",
  83. allocation.DebugString());
  84. }
  85. const uint32 num_instances = it->second;
  86. if (entry.resource().has_device_instance() &&
  87. entry.resource().device_instance().value() >= num_instances) {
  88. return errors::InvalidArgument(
  89. "Invalid resource allocation: Invalid device instance ",
  90. entry.resource().device(), ":",
  91. entry.resource().device_instance().value(),
  92. " in resource allocation\n", allocation.DebugString());
  93. }
  94. if (FindMutableEntry(entry.resource(), &validated_entries) != nullptr) {
  95. return errors::InvalidArgument(
  96. "Invalid resource allocation: Repeated resource\n",
  97. entry.resource().DebugString(), "in allocation\n",
  98. allocation.DebugString());
  99. }
  100. *validated_entries.add_resource_quantities() = entry;
  101. }
  102. return Status::OK();
  103. }
  104. ResourceAllocation ResourceUtil::Normalize(
  105. const ResourceAllocation& allocation) const {
  106. const Status validity = VerifyValidity(allocation);
  107. DCHECK_EQ(Status::OK(), validity);
  108. if (!validity.ok()) {
  109. LOG(ERROR) << validity;
  110. return allocation;
  111. }
  112. ResourceAllocation normalized;
  113. for (const auto& entry : allocation.resource_quantities()) {
  114. if (entry.quantity() == 0) {
  115. continue;
  116. }
  117. ResourceAllocation::Entry* normalized_entry =
  118. normalized.add_resource_quantities();
  119. *normalized_entry = entry;
  120. if (entry.resource().has_device_instance()) {
  121. continue;
  122. }
  123. const uint32 num_instances =
  124. devices_.find(entry.resource().device())->second;
  125. if (num_instances == 1) {
  126. normalized_entry->mutable_resource()
  127. ->mutable_device_instance()
  128. ->set_value(0);
  129. }
  130. }
  131. return normalized;
  132. }
  133. bool ResourceUtil::IsNormalized(const ResourceAllocation& allocation) const {
  134. const Status validity = VerifyValidity(allocation);
  135. DCHECK_EQ(Status::OK(), validity);
  136. if (!validity.ok()) {
  137. LOG(ERROR) << validity;
  138. return false;
  139. }
  140. for (const auto& entry : allocation.resource_quantities()) {
  141. if (entry.quantity() == 0) {
  142. return false;
  143. }
  144. if (entry.resource().has_device_instance()) {
  145. continue;
  146. }
  147. // For singleton devices (ones that have one instance), the resource should
  148. // be bound to the single device in the normalized representation.
  149. const uint32 num_instances =
  150. devices_.find(entry.resource().device())->second;
  151. if (num_instances == 1) {
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. bool ResourceUtil::IsBound(const ResourceAllocation& allocation) const {
  158. DCHECK(IsNormalized(allocation));
  159. for (const auto& entry : allocation.resource_quantities()) {
  160. if (!entry.resource().has_device_instance()) {
  161. return false;
  162. }
  163. }
  164. return true;
  165. }
  166. void ResourceUtil::Add(const ResourceAllocation& to_add,
  167. ResourceAllocation* base) const {
  168. DCHECK(IsNormalized(to_add));
  169. DCHECK(IsNormalized(*base));
  170. for (const ResourceAllocation::Entry& to_add_entry :
  171. to_add.resource_quantities()) {
  172. ResourceAllocation::Entry* base_entry =
  173. FindOrInsertMutableEntry(to_add_entry.resource(), base);
  174. base_entry->set_quantity(base_entry->quantity() + to_add_entry.quantity());
  175. }
  176. DCHECK(IsNormalized(*base));
  177. }
  178. bool ResourceUtil::Subtract(const ResourceAllocation& to_subtract,
  179. ResourceAllocation* base) const {
  180. DCHECK(IsNormalized(to_subtract));
  181. DCHECK(IsNormalized(*base));
  182. // We buffer the mutations to 'base' so that if we bail out due to a negative
  183. // quantity we leave it untouched.
  184. std::vector<std::pair<ResourceAllocation::Entry*, uint64>> new_quantities;
  185. for (const ResourceAllocation::Entry& to_subtract_entry :
  186. to_subtract.resource_quantities()) {
  187. ResourceAllocation::Entry* base_entry =
  188. FindMutableEntry(to_subtract_entry.resource(), base);
  189. if (base_entry == nullptr ||
  190. base_entry->quantity() < to_subtract_entry.quantity()) {
  191. LOG(ERROR) << "Subtracting\n"
  192. << to_subtract.DebugString() << "from\n"
  193. << base->DebugString()
  194. << "would result in a negative quantity";
  195. return false;
  196. }
  197. const uint64 new_quantity =
  198. base_entry->quantity() - to_subtract_entry.quantity();
  199. new_quantities.push_back({base_entry, new_quantity});
  200. }
  201. for (const auto& new_quantity : new_quantities) {
  202. ResourceAllocation::Entry* base_entry = new_quantity.first;
  203. const uint64 quantity = new_quantity.second;
  204. base_entry->set_quantity(quantity);
  205. }
  206. *base = Normalize(*base);
  207. return true;
  208. }
  209. bool ResourceUtil::LessThanOrEqual(const ResourceAllocation& lhs,
  210. const ResourceAllocation& rhs) const {
  211. const Status validity = VerifyValidity(lhs);
  212. DCHECK_EQ(Status::OK(), validity);
  213. if (!validity.ok()) {
  214. LOG(ERROR) << validity;
  215. return false;
  216. }
  217. DCHECK(IsNormalized(lhs));
  218. DCHECK(IsNormalized(rhs));
  219. DCHECK(IsBound(rhs))
  220. << "LessThanOrEqual() requires the second argument to be bound";
  221. // Phase 1: Attempt to subtract the bound entries in 'lhs' from 'rhs'.
  222. ResourceAllocation subtracted_rhs = rhs;
  223. for (const ResourceAllocation::Entry& lhs_entry : lhs.resource_quantities()) {
  224. if (lhs_entry.resource().has_device_instance()) {
  225. ResourceAllocation to_subtract;
  226. *to_subtract.add_resource_quantities() = lhs_entry;
  227. if (!Subtract(to_subtract, &subtracted_rhs)) {
  228. return false;
  229. }
  230. }
  231. }
  232. // Phase 2: See if each unbound entry in 'lhs' can fit into a 'subtracted_rhs'
  233. // via some device instance.
  234. for (const ResourceAllocation::Entry& lhs_entry : lhs.resource_quantities()) {
  235. if (!lhs_entry.resource().has_device_instance()) {
  236. const uint32 num_instances =
  237. devices_.find(lhs_entry.resource().device())->second;
  238. Resource bound_resource = lhs_entry.resource();
  239. bool found_room = false;
  240. for (int instance = 0; instance < num_instances; ++instance) {
  241. bound_resource.mutable_device_instance()->set_value(instance);
  242. if (lhs_entry.quantity() <=
  243. GetQuantityForResource(bound_resource, subtracted_rhs)) {
  244. found_room = true;
  245. break;
  246. }
  247. }
  248. if (!found_room) {
  249. return false;
  250. }
  251. }
  252. }
  253. return true;
  254. }
  255. ResourceAllocation ResourceUtil::Overbind(
  256. const ResourceAllocation& allocation) const {
  257. const Status validity = VerifyValidity(allocation);
  258. DCHECK_EQ(Status::OK(), validity);
  259. if (!validity.ok()) {
  260. LOG(ERROR) << validity;
  261. return allocation;
  262. }
  263. DCHECK(IsNormalized(allocation));
  264. ResourceAllocation result;
  265. for (const ResourceAllocation::Entry& entry :
  266. allocation.resource_quantities()) {
  267. if (entry.resource().has_device_instance()) {
  268. ResourceAllocation::Entry* result_entry =
  269. FindOrInsertMutableEntry(entry.resource(), &result);
  270. result_entry->set_quantity(entry.quantity() + result_entry->quantity());
  271. continue;
  272. }
  273. const uint32 num_instances =
  274. devices_.find(entry.resource().device())->second;
  275. Resource bound_resource = entry.resource();
  276. for (uint32 instance = 0; instance < num_instances; ++instance) {
  277. bound_resource.mutable_device_instance()->set_value(instance);
  278. ResourceAllocation::Entry* result_entry =
  279. FindOrInsertMutableEntry(bound_resource, &result);
  280. result_entry->set_quantity(entry.quantity() + result_entry->quantity());
  281. }
  282. }
  283. DCHECK(IsNormalized(result));
  284. return result;
  285. }
  286. bool operator==(const Resource& a, const Resource& b) {
  287. if (a.device() != b.device()) {
  288. return false;
  289. }
  290. if (a.has_device_instance() != b.has_device_instance()) {
  291. return false;
  292. }
  293. if (a.has_device_instance()) {
  294. if (a.device_instance().value() != b.device_instance().value()) {
  295. return false;
  296. }
  297. }
  298. return a.kind() == b.kind();
  299. }
  300. } // namespace serving
  301. } // namespace tensorflow