blob: 4d26795d89e56c970c1ef0b6472d5aa062843b92 [file] [log] [blame]
Michael Butlerb98aa6d2020-02-22 22:37:59 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "CommonUtils.h"
18
Michael Butler4b276a72020-08-06 23:22:35 -070019#include "HandleError.h"
20
Michael Butlerb98aa6d2020-02-22 22:37:59 -080021#include <android-base/logging.h>
Slava Shklyaev49817a02020-10-27 18:44:01 +000022#include <android-base/unique_fd.h>
Michael Butlerab2f4822021-02-08 00:05:07 -080023#include <android/hardware_buffer.h>
24#include <hidl/HidlSupport.h>
Michael Butlerb98aa6d2020-02-22 22:37:59 -080025#include <nnapi/Result.h>
26#include <nnapi/SharedMemory.h>
27#include <nnapi/TypeUtils.h>
28#include <nnapi/Types.h>
29#include <nnapi/Validation.h>
Michael Butlerab2f4822021-02-08 00:05:07 -080030#include <vndk/hardware_buffer.h>
Michael Butlerb98aa6d2020-02-22 22:37:59 -080031
32#include <algorithm>
33#include <any>
Michael Butler4b276a72020-08-06 23:22:35 -070034#include <functional>
Michael Butlerb98aa6d2020-02-22 22:37:59 -080035#include <optional>
36#include <variant>
37#include <vector>
38
39namespace android::hardware::neuralnetworks::utils {
40namespace {
41
42bool hasNoPointerData(const nn::Operand& operand);
43bool hasNoPointerData(const nn::Model::Subgraph& subgraph);
44bool hasNoPointerData(const nn::Request::Argument& argument);
45
46template <typename Type>
47bool hasNoPointerData(const std::vector<Type>& objects) {
48 return std::all_of(objects.begin(), objects.end(),
49 [](const auto& object) { return hasNoPointerData(object); });
50}
51
52bool hasNoPointerData(const nn::DataLocation& location) {
53 return std::visit([](auto ptr) { return ptr == nullptr; }, location.pointer);
54}
55
56bool hasNoPointerData(const nn::Operand& operand) {
57 return hasNoPointerData(operand.location);
58}
59
60bool hasNoPointerData(const nn::Model::Subgraph& subgraph) {
61 return hasNoPointerData(subgraph.operands);
62}
63
64bool hasNoPointerData(const nn::Request::Argument& argument) {
65 return hasNoPointerData(argument.location);
66}
67
68void copyPointersToSharedMemory(nn::Operand* operand, nn::ConstantMemoryBuilder* memoryBuilder) {
69 CHECK(operand != nullptr);
70 CHECK(memoryBuilder != nullptr);
71
72 if (operand->lifetime != nn::Operand::LifeTime::POINTER) {
73 return;
74 }
75
76 const void* data = std::visit([](auto ptr) { return static_cast<const void*>(ptr); },
77 operand->location.pointer);
78 CHECK(data != nullptr);
79 operand->lifetime = nn::Operand::LifeTime::CONSTANT_REFERENCE;
80 operand->location = memoryBuilder->append(data, operand->location.length);
81}
82
83void copyPointersToSharedMemory(nn::Model::Subgraph* subgraph,
84 nn::ConstantMemoryBuilder* memoryBuilder) {
85 CHECK(subgraph != nullptr);
86 std::for_each(subgraph->operands.begin(), subgraph->operands.end(),
87 [memoryBuilder](auto& operand) {
88 copyPointersToSharedMemory(&operand, memoryBuilder);
89 });
90}
91
Michael Butlerbed23d92021-03-25 15:27:38 -070092nn::GeneralResult<hidl_handle> createNativeHandleFrom(base::unique_fd fd,
93 const std::vector<int32_t>& ints) {
94 constexpr size_t kIntMax = std::numeric_limits<int>::max();
95 CHECK_LE(ints.size(), kIntMax);
96 native_handle_t* nativeHandle = native_handle_create(1, static_cast<int>(ints.size()));
97 if (nativeHandle == nullptr) {
98 return NN_ERROR() << "Failed to create native_handle";
99 }
100
101 nativeHandle->data[0] = fd.release();
102 std::copy(ints.begin(), ints.end(), nativeHandle->data + 1);
103
104 hidl_handle handle;
105 handle.setTo(nativeHandle, /*shouldOwn=*/true);
106 return handle;
107}
108
109nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Ashmem& memory) {
110 auto fd = NN_TRY(nn::dupFd(memory.fd));
111 auto handle = NN_TRY(createNativeHandleFrom(std::move(fd), {}));
112 return hidl_memory("ashmem", std::move(handle), memory.size);
113}
114
115nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Fd& memory) {
116 auto fd = NN_TRY(nn::dupFd(memory.fd));
117
118 const auto [lowOffsetBits, highOffsetBits] = nn::getIntsFromOffset(memory.offset);
119 const std::vector<int> ints = {memory.prot, lowOffsetBits, highOffsetBits};
120
121 auto handle = NN_TRY(createNativeHandleFrom(std::move(fd), ints));
122 return hidl_memory("mmap_fd", std::move(handle), memory.size);
123}
124
125nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::HardwareBuffer& memory) {
126 const auto* ahwb = memory.handle.get();
127 AHardwareBuffer_Desc bufferDesc;
128 AHardwareBuffer_describe(ahwb, &bufferDesc);
129
130 const bool isBlob = bufferDesc.format == AHARDWAREBUFFER_FORMAT_BLOB;
131 const size_t size = isBlob ? bufferDesc.width : 0;
132 const char* const name = isBlob ? "hardware_buffer_blob" : "hardware_buffer";
133
134 const native_handle_t* nativeHandle = AHardwareBuffer_getNativeHandle(ahwb);
135 const hidl_handle hidlHandle(nativeHandle);
136 hidl_handle copiedHandle(hidlHandle);
137
138 return hidl_memory(name, std::move(copiedHandle), size);
139}
140
141nn::GeneralResult<hidl_memory> createHidlMemoryFrom(const nn::Memory::Unknown& memory) {
142 return hidl_memory(memory.name, NN_TRY(hidlHandleFromSharedHandle(memory.handle)), memory.size);
143}
144
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800145} // anonymous namespace
146
147nn::Capabilities::OperandPerformanceTable makeQuantized8PerformanceConsistentWithP(
148 const nn::Capabilities::PerformanceInfo& float32Performance,
149 const nn::Capabilities::PerformanceInfo& quantized8Performance) {
150 // In Android P, most data types are treated as having the same performance as
151 // TENSOR_QUANT8_ASYMM. This collection must be in sorted order.
152 std::vector<nn::Capabilities::OperandPerformance> operandPerformances = {
153 {.type = nn::OperandType::FLOAT32, .info = float32Performance},
154 {.type = nn::OperandType::INT32, .info = quantized8Performance},
155 {.type = nn::OperandType::UINT32, .info = quantized8Performance},
156 {.type = nn::OperandType::TENSOR_FLOAT32, .info = float32Performance},
157 {.type = nn::OperandType::TENSOR_INT32, .info = quantized8Performance},
158 {.type = nn::OperandType::TENSOR_QUANT8_ASYMM, .info = quantized8Performance},
159 {.type = nn::OperandType::OEM, .info = quantized8Performance},
160 {.type = nn::OperandType::TENSOR_OEM_BYTE, .info = quantized8Performance},
161 };
162 return nn::Capabilities::OperandPerformanceTable::create(std::move(operandPerformances))
163 .value();
164}
165
166bool hasNoPointerData(const nn::Model& model) {
167 return hasNoPointerData(model.main) && hasNoPointerData(model.referenced);
168}
169
170bool hasNoPointerData(const nn::Request& request) {
171 return hasNoPointerData(request.inputs) && hasNoPointerData(request.outputs);
172}
173
Michael Butler4b276a72020-08-06 23:22:35 -0700174nn::GeneralResult<std::reference_wrapper<const nn::Model>> flushDataFromPointerToShared(
175 const nn::Model* model, std::optional<nn::Model>* maybeModelInSharedOut) {
176 CHECK(model != nullptr);
177 CHECK(maybeModelInSharedOut != nullptr);
178
179 if (hasNoPointerData(*model)) {
180 return *model;
181 }
182
183 // Make a copy of the model in order to make modifications. The modified model is returned to
184 // the caller through `maybeModelInSharedOut` if the function succeeds.
185 nn::Model modelInShared = *model;
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800186
187 nn::ConstantMemoryBuilder memoryBuilder(modelInShared.pools.size());
188 copyPointersToSharedMemory(&modelInShared.main, &memoryBuilder);
189 std::for_each(modelInShared.referenced.begin(), modelInShared.referenced.end(),
190 [&memoryBuilder](auto& subgraph) {
191 copyPointersToSharedMemory(&subgraph, &memoryBuilder);
192 });
193
194 if (!memoryBuilder.empty()) {
195 auto memory = NN_TRY(memoryBuilder.finish());
196 modelInShared.pools.push_back(std::move(memory));
197 }
198
Michael Butler4b276a72020-08-06 23:22:35 -0700199 *maybeModelInSharedOut = modelInShared;
200 return **maybeModelInSharedOut;
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800201}
202
Michael Butler4b276a72020-08-06 23:22:35 -0700203nn::GeneralResult<std::reference_wrapper<const nn::Request>> flushDataFromPointerToShared(
204 const nn::Request* request, std::optional<nn::Request>* maybeRequestInSharedOut) {
205 CHECK(request != nullptr);
206 CHECK(maybeRequestInSharedOut != nullptr);
207
208 if (hasNoPointerData(*request)) {
209 return *request;
210 }
211
212 // Make a copy of the request in order to make modifications. The modified request is returned
213 // to the caller through `maybeRequestInSharedOut` if the function succeeds.
214 nn::Request requestInShared = *request;
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800215
216 // Change input pointers to shared memory.
217 nn::ConstantMemoryBuilder inputBuilder(requestInShared.pools.size());
218 for (auto& input : requestInShared.inputs) {
219 const auto& location = input.location;
220 if (input.lifetime != nn::Request::Argument::LifeTime::POINTER) {
221 continue;
222 }
223
224 input.lifetime = nn::Request::Argument::LifeTime::POOL;
225 const void* data = std::visit([](auto ptr) { return static_cast<const void*>(ptr); },
226 location.pointer);
227 CHECK(data != nullptr);
228 input.location = inputBuilder.append(data, location.length);
229 }
230
231 // Allocate input memory.
232 if (!inputBuilder.empty()) {
233 auto memory = NN_TRY(inputBuilder.finish());
234 requestInShared.pools.push_back(std::move(memory));
235 }
236
237 // Change output pointers to shared memory.
238 nn::MutableMemoryBuilder outputBuilder(requestInShared.pools.size());
239 for (auto& output : requestInShared.outputs) {
240 const auto& location = output.location;
241 if (output.lifetime != nn::Request::Argument::LifeTime::POINTER) {
242 continue;
243 }
244
245 output.lifetime = nn::Request::Argument::LifeTime::POOL;
246 output.location = outputBuilder.append(location.length);
247 }
248
249 // Allocate output memory.
250 if (!outputBuilder.empty()) {
251 auto memory = NN_TRY(outputBuilder.finish());
252 requestInShared.pools.push_back(std::move(memory));
253 }
254
Michael Butler4b276a72020-08-06 23:22:35 -0700255 *maybeRequestInSharedOut = requestInShared;
256 return **maybeRequestInSharedOut;
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800257}
258
Michael Butler4b276a72020-08-06 23:22:35 -0700259nn::GeneralResult<void> unflushDataFromSharedToPointer(
260 const nn::Request& request, const std::optional<nn::Request>& maybeRequestInShared) {
261 if (!maybeRequestInShared.has_value() || maybeRequestInShared->pools.empty() ||
Michael Butlerfadeb8a2021-02-07 00:11:13 -0800262 !std::holds_alternative<nn::SharedMemory>(maybeRequestInShared->pools.back())) {
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800263 return {};
264 }
Michael Butler4b276a72020-08-06 23:22:35 -0700265 const auto& requestInShared = *maybeRequestInShared;
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800266
267 // Map the memory.
Michael Butlerfadeb8a2021-02-07 00:11:13 -0800268 const auto& outputMemory = std::get<nn::SharedMemory>(requestInShared.pools.back());
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800269 const auto [pointer, size, context] = NN_TRY(map(outputMemory));
270 const uint8_t* constantPointer =
271 std::visit([](const auto& o) { return static_cast<const uint8_t*>(o); }, pointer);
272
273 // Flush each output pointer.
274 CHECK_EQ(request.outputs.size(), requestInShared.outputs.size());
275 for (size_t i = 0; i < request.outputs.size(); ++i) {
276 const auto& location = request.outputs[i].location;
277 const auto& locationInShared = requestInShared.outputs[i].location;
278 if (!std::holds_alternative<void*>(location.pointer)) {
279 continue;
280 }
281
282 // Get output pointer and size.
283 void* data = std::get<void*>(location.pointer);
284 CHECK(data != nullptr);
285 const size_t length = location.length;
286
287 // Get output pool location.
288 CHECK(requestInShared.outputs[i].lifetime == nn::Request::Argument::LifeTime::POOL);
289 const size_t index = locationInShared.poolIndex;
290 const size_t offset = locationInShared.offset;
291 const size_t outputPoolIndex = requestInShared.pools.size() - 1;
292 CHECK(locationInShared.length == length);
293 CHECK(index == outputPoolIndex);
294
295 // Flush memory.
296 std::memcpy(data, constantPointer + offset, length);
297 }
298
299 return {};
300}
301
Michael Butler68b69262021-02-09 15:36:11 -0800302nn::GeneralResult<std::vector<uint32_t>> countNumberOfConsumers(
303 size_t numberOfOperands, const std::vector<nn::Operation>& operations) {
304 return makeGeneralFailure(nn::countNumberOfConsumers(numberOfOperands, operations));
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800305}
306
Michael Butlerab2f4822021-02-08 00:05:07 -0800307nn::GeneralResult<hidl_memory> createHidlMemoryFromSharedMemory(const nn::SharedMemory& memory) {
308 if (memory == nullptr) {
309 return NN_ERROR() << "Memory must be non-empty";
310 }
Michael Butlerbed23d92021-03-25 15:27:38 -0700311 return std::visit([](const auto& x) { return createHidlMemoryFrom(x); }, memory->handle);
Michael Butlerab2f4822021-02-08 00:05:07 -0800312}
313
314static uint32_t roundUpToMultiple(uint32_t value, uint32_t multiple) {
315 return (value + multiple - 1) / multiple * multiple;
316}
317
318nn::GeneralResult<nn::SharedMemory> createSharedMemoryFromHidlMemory(const hidl_memory& memory) {
Michael Butlerbed23d92021-03-25 15:27:38 -0700319 CHECK_LE(memory.size(), std::numeric_limits<size_t>::max());
320 if (!memory.valid()) {
321 return NN_ERROR() << "Unable to convert invalid hidl_memory";
322 }
323
324 if (memory.name() == "ashmem") {
325 if (memory.handle()->numFds != 1) {
326 return NN_ERROR() << "Unable to convert invalid ashmem memory object with "
327 << memory.handle()->numFds << " numFds, but expected 1";
328 }
329 if (memory.handle()->numInts != 0) {
330 return NN_ERROR() << "Unable to convert invalid ashmem memory object with "
331 << memory.handle()->numInts << " numInts, but expected 0";
332 }
333 auto handle = nn::Memory::Ashmem{
334 .fd = NN_TRY(nn::dupFd(memory.handle()->data[0])),
335 .size = static_cast<size_t>(memory.size()),
336 };
337 return std::make_shared<const nn::Memory>(nn::Memory{.handle = std::move(handle)});
338 }
339
340 if (memory.name() == "mmap_fd") {
341 if (memory.handle()->numFds != 1) {
342 return NN_ERROR() << "Unable to convert invalid mmap_fd memory object with "
343 << memory.handle()->numFds << " numFds, but expected 1";
344 }
345 if (memory.handle()->numInts != 3) {
346 return NN_ERROR() << "Unable to convert invalid mmap_fd memory object with "
347 << memory.handle()->numInts << " numInts, but expected 3";
348 }
349
350 const int fd = memory.handle()->data[0];
351 const int prot = memory.handle()->data[1];
352 const int lower = memory.handle()->data[2];
353 const int higher = memory.handle()->data[3];
354 const size_t offset = nn::getOffsetFromInts(lower, higher);
355
356 return nn::createSharedMemoryFromFd(static_cast<size_t>(memory.size()), prot, fd, offset);
357 }
Michael Butlerab2f4822021-02-08 00:05:07 -0800358
359 if (memory.name() != "hardware_buffer_blob") {
Michael Butlerbed23d92021-03-25 15:27:38 -0700360 auto handle = nn::Memory::Unknown{
Michael Butlerab2f4822021-02-08 00:05:07 -0800361 .handle = NN_TRY(sharedHandleFromNativeHandle(memory.handle())),
Michael Butlerbed23d92021-03-25 15:27:38 -0700362 .size = static_cast<size_t>(memory.size()),
Michael Butlerab2f4822021-02-08 00:05:07 -0800363 .name = memory.name(),
Michael Butlerbed23d92021-03-25 15:27:38 -0700364 };
365 return std::make_shared<const nn::Memory>(nn::Memory{.handle = std::move(handle)});
Michael Butlerab2f4822021-02-08 00:05:07 -0800366 }
367
368 const auto size = memory.size();
369 const auto format = AHARDWAREBUFFER_FORMAT_BLOB;
370 const auto usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
371 const uint32_t width = size;
372 const uint32_t height = 1; // height is always 1 for BLOB mode AHardwareBuffer.
373 const uint32_t layers = 1; // layers is always 1 for BLOB mode AHardwareBuffer.
374
375 // AHardwareBuffer_createFromHandle() might fail because an allocator
376 // expects a specific stride value. In that case, we try to guess it by
377 // aligning the width to small powers of 2.
378 // TODO(b/174120849): Avoid stride assumptions.
379 AHardwareBuffer* hardwareBuffer = nullptr;
380 status_t status = UNKNOWN_ERROR;
381 for (uint32_t alignment : {1, 4, 32, 64, 128, 2, 8, 16}) {
382 const uint32_t stride = roundUpToMultiple(width, alignment);
383 AHardwareBuffer_Desc desc{
384 .width = width,
385 .height = height,
386 .layers = layers,
387 .format = format,
388 .usage = usage,
389 .stride = stride,
390 };
391 status = AHardwareBuffer_createFromHandle(&desc, memory.handle(),
392 AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE,
393 &hardwareBuffer);
394 if (status == NO_ERROR) {
395 break;
396 }
397 }
398 if (status != NO_ERROR) {
399 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
400 << "Can't create AHardwareBuffer from handle. Error: " << status;
401 }
402
Michael Butlerbed23d92021-03-25 15:27:38 -0700403 return nn::createSharedMemoryFromAHWB(hardwareBuffer, /*takeOwnership=*/true);
Michael Butlerab2f4822021-02-08 00:05:07 -0800404}
405
406nn::GeneralResult<hidl_handle> hidlHandleFromSharedHandle(const nn::Handle& handle) {
Slava Shklyaev49817a02020-10-27 18:44:01 +0000407 std::vector<base::unique_fd> fds;
Michael Butlerab2f4822021-02-08 00:05:07 -0800408 fds.reserve(handle.fds.size());
409 for (const auto& fd : handle.fds) {
410 const int dupFd = dup(fd);
Slava Shklyaev49817a02020-10-27 18:44:01 +0000411 if (dupFd == -1) {
412 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to dup the fd";
413 }
414 fds.emplace_back(dupFd);
415 }
416
Michael Butlerab2f4822021-02-08 00:05:07 -0800417 constexpr size_t kIntMax = std::numeric_limits<int>::max();
418 CHECK_LE(handle.fds.size(), kIntMax);
419 CHECK_LE(handle.ints.size(), kIntMax);
420 native_handle_t* nativeHandle = native_handle_create(static_cast<int>(handle.fds.size()),
421 static_cast<int>(handle.ints.size()));
Slava Shklyaev49817a02020-10-27 18:44:01 +0000422 if (nativeHandle == nullptr) {
423 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to create native_handle";
424 }
425 for (size_t i = 0; i < fds.size(); ++i) {
426 nativeHandle->data[i] = fds[i].release();
427 }
Michael Butlerab2f4822021-02-08 00:05:07 -0800428 std::copy(handle.ints.begin(), handle.ints.end(), &nativeHandle->data[nativeHandle->numFds]);
Slava Shklyaev49817a02020-10-27 18:44:01 +0000429
430 hidl_handle hidlHandle;
431 hidlHandle.setTo(nativeHandle, /*shouldOwn=*/true);
432 return hidlHandle;
433}
434
Michael Butlerab2f4822021-02-08 00:05:07 -0800435nn::GeneralResult<nn::Handle> sharedHandleFromNativeHandle(const native_handle_t* handle) {
Slava Shklyaev49817a02020-10-27 18:44:01 +0000436 if (handle == nullptr) {
Michael Butlerab2f4822021-02-08 00:05:07 -0800437 return NN_ERROR() << "sharedHandleFromNativeHandle failed because handle is nullptr";
Slava Shklyaev49817a02020-10-27 18:44:01 +0000438 }
439
440 std::vector<base::unique_fd> fds;
441 fds.reserve(handle->numFds);
442 for (int i = 0; i < handle->numFds; ++i) {
Michael Butlerab2f4822021-02-08 00:05:07 -0800443 const int dupFd = dup(handle->data[i]);
Slava Shklyaev49817a02020-10-27 18:44:01 +0000444 if (dupFd == -1) {
445 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to dup the fd";
446 }
447 fds.emplace_back(dupFd);
448 }
449
450 std::vector<int> ints(&handle->data[handle->numFds],
451 &handle->data[handle->numFds + handle->numInts]);
452
Michael Butlerab2f4822021-02-08 00:05:07 -0800453 return nn::Handle{.fds = std::move(fds), .ints = std::move(ints)};
Slava Shklyaev49817a02020-10-27 18:44:01 +0000454}
455
456nn::GeneralResult<hidl_vec<hidl_handle>> convertSyncFences(
457 const std::vector<nn::SyncFence>& syncFences) {
458 hidl_vec<hidl_handle> handles(syncFences.size());
459 for (size_t i = 0; i < syncFences.size(); ++i) {
Michael Butlerab2f4822021-02-08 00:05:07 -0800460 const auto& handle = syncFences[i].getSharedHandle();
461 if (handle == nullptr) {
462 return NN_ERROR() << "convertSyncFences failed because sync fence is empty";
463 }
464 handles[i] = NN_TRY(hidlHandleFromSharedHandle(*handle));
Slava Shklyaev49817a02020-10-27 18:44:01 +0000465 }
466 return handles;
467}
468
Michael Butlerb98aa6d2020-02-22 22:37:59 -0800469} // namespace android::hardware::neuralnetworks::utils