Warning fixes (casts) and fix Windows build for aligned_alloc

PiperOrigin-RevId: 689734618
This commit is contained in:
Jan Wassenberg 2024-10-25 04:13:38 -07:00 committed by Copybara-Service
parent 52af531820
commit 19cfe14c76
4 changed files with 29 additions and 18 deletions

View File

@ -275,7 +275,8 @@ BlobError BlobReader::ReadAll(hwy::ThreadPool& pool) {
[pfile, &requests, &err](uint64_t i, size_t /*thread*/) {
if (!pfile->Read(requests[i].offset, requests[i].size,
requests[i].data)) {
fprintf(stderr, "Failed to read blob %zu\n", i);
fprintf(stderr, "Failed to read blob %zu\n",
static_cast<size_t>(i));
err.test_and_set();
}
});

View File

@ -14,6 +14,7 @@ cc_library(
deps = [
"//compression:io",
"@highway//:hwy",
"@highway//:profiler",
],
)

View File

@ -14,8 +14,8 @@
// limitations under the License.
#include "paligemma/image.h"
#include "compression/io.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
@ -28,7 +28,10 @@
#include <utility>
#include <vector>
#include "compression/io.h"
#include "hwy/aligned_allocator.h" // hwy::Span
#include "hwy/base.h"
#include "hwy/profiler.h"
namespace gcpp {
namespace {
@ -95,12 +98,12 @@ bool Image::ReadPPM(const std::string& filename) {
std::cerr << filename << " does not exist\n";
return false;
}
auto content = ReadFileToString(path);
const std::string content = ReadFileToString(path);
return ReadPPM(hwy::Span<const char>(content.data(), content.size()));
}
bool Image::ReadPPM(const hwy::Span<const char>& buf) {
auto pos = CheckP6Format(buf.cbegin(), buf.cend());
const char* pos = CheckP6Format(buf.cbegin(), buf.cend());
if (!pos) {
std::cerr << "We only support binary PPM (P6)\n";
return false;
@ -134,8 +137,8 @@ bool Image::ReadPPM(const hwy::Span<const char>& buf) {
return false;
}
++pos;
auto data_size = width * height * 3;
if (buf.cend() - pos < data_size) {
const size_t data_size = width * height * 3;
if (buf.cend() - pos < static_cast<ptrdiff_t>(data_size)) {
std::cerr << "Insufficient data remaining\n";
return false;
}
@ -190,23 +193,24 @@ bool Image::WriteBinary(const std::string& filename) const {
// We want the N-th patch (of 256) of size kPatchSize x kPatchSize x 3.
// Patches are numbered in usual "pixel-order".
void Image::GetPatch(size_t patch_num, float* patch) const {
PROFILER_FUNC;
constexpr size_t kDataSize = kImageSize * kImageSize * 3;
HWY_ASSERT(size() == kDataSize);
constexpr size_t kPatchDataSize = kPatchSize * kPatchSize * 3;
int i_offs = patch_num / kNumPatches;
int j_offs = patch_num % kNumPatches;
size_t i_offs = patch_num / kNumPatches;
size_t j_offs = patch_num % kNumPatches;
HWY_ASSERT(0 <= i_offs && i_offs < kNumPatches);
HWY_ASSERT(0 <= j_offs && j_offs < kNumPatches);
i_offs *= kPatchSize;
j_offs *= kPatchSize;
// This can be made faster, but let's first see whether it matters.
const float* image_data = data();
for (int i = 0; i < kPatchSize; ++i) {
for (int j = 0; j < kPatchSize; ++j) {
for (int k = 0; k < 3; ++k) {
const int patch_index = (i * kPatchSize + j) * 3 + k;
for (size_t i = 0; i < kPatchSize; ++i) {
for (size_t j = 0; j < kPatchSize; ++j) {
for (size_t k = 0; k < 3; ++k) {
const size_t patch_index = (i * kPatchSize + j) * 3 + k;
HWY_ASSERT(patch_index < kPatchDataSize);
const int image_index =
const size_t image_index =
((i + i_offs) * kImageSize + (j + j_offs)) * 3 + k;
HWY_ASSERT(image_index < kDataSize);
patch[patch_index] = image_data[image_index];
@ -214,4 +218,5 @@ void Image::GetPatch(size_t patch_num, float* patch) const {
}
}
}
} // namespace gcpp

View File

@ -19,7 +19,7 @@
#include <stddef.h>
#include <stdint.h>
#include <cstdlib> // std::aligned_alloc
#include <cstdlib> // std::aligned_alloc / _aligned_malloc
// IWYU pragma: begin_exports
#include "util/threading.h"
@ -140,15 +140,19 @@ class Allocator {
}
// AlignedFreeUniquePtr has a deleter that can call an arbitrary `free`, but
// with an extra opaque pointer, which we discard via this adapter.
// with an extra opaque pointer, which we discard via `call_free`.
#if defined(__ANDROID_API__) && __ANDROID_API__ < 28
const auto call_free = [](void* ptr, void*) { std::free(ptr); };
#if !defined(__ANDROID_API__) || __ANDROID_API__ >= 28
T* p = static_cast<T*>(std::aligned_alloc(Alignment(), bytes));
#else
void* mem = nullptr;
int err = posix_memalign(&mem, Alignment(), bytes);
HWY_ASSERT(err == 0);
T* p = static_cast<T*>(mem);
#elif HWY_OS_WIN
const auto call_free = [](void* ptr, void*) { _aligned_free(ptr); };
T* p = static_cast<T*>(_aligned_malloc(bytes, Alignment()));
#else
const auto call_free = [](void* ptr, void*) { std::free(ptr); };
T* p = static_cast<T*>(std::aligned_alloc(Alignment(), bytes));
#endif
return hwy::AlignedFreeUniquePtr<T[]>(
p, hwy::AlignedFreer(call_free, nullptr));