Repository: ggerganov/llama.cpp
File: src/llama-model-loader.cpp (C++)

Code before:
                            uintptr_t ptr_data = ptr_dest_aligned;
                            size_t data_to_copy = read_size;

                            // Skip alignment padding at start of first chunk
                            if (bytes_read == 0) {
                                ptr_data += offset_from_alignment;
                                data_to_copy -= offset_from_alignment;
                            }

                            // Trim alignment padding at end of last chunk
                            if (aligned_offset + bytes_read + read_size > offset + n_size) {
                                data_to_copy -= (read_end - (offset + n_size));
                            }

                            // Async upload actual data to GPU
                            ggml_backend_tensor_set_async(upload_backend, cur,
                                                          reinterpret_cast<void *>(ptr_data), data_read, data_to_copy);
                            ggml_backend_event_record(events[buffer_idx], upload_backend);

                            data_read += data_to_copy;
                            bytes_read += read_size;

                            ++buffer_idx;
                            buffer_idx %= n_buffers;
                        }
                    } else {
                        size_t bytes_read = 0;

                        while (bytes_read < n_size) {
                            size_t read_iteration = std::min<size_t>(buffer_size, n_size - bytes_read);

                            ggml_backend_event_synchronize(events[buffer_idx]);
                            file->read_raw(host_ptrs[buffer_idx], read_iteration);
                            ggml_backend_tensor_set_async(upload_backend, cur, host_ptrs[buffer_idx], bytes_read, read_iteration);
                            ggml_backend_event_record(events[buffer_idx], upload_backend);

                            bytes_read += read_iteration;
                            ++buffer_idx;
                            buffer_idx %= n_buffers;
                        }
                    }
                } else {
                    read_buf.resize(n_size);
                    if (file->has_direct_io()) {
                        file->read_aligned_chunk(weight->offs, read_buf.data(), n_size, alignment);
                    } else {
                        file->seek(weight->offs, SEEK_SET);
                        file->read_raw(read_buf.data(), n_size);
                    }
                    ggml_backend_tensor_set(cur, read_buf.data(), 0, n_size);
                    if (check_tensors && !ggml_validate_row_data(cur->type, read_buf.data(), n_size)) {

Review comment (suggestion):
I think these two branches can become a single branch by setting `alignment = 1` when there is no direct io and always go through the first branch.

Code after:
                        }

                        // Trim alignment padding at end of last chunk
                        if (aligned_offset + bytes_read + read_size > offset + n_size) {
                            data_to_copy -= (read_end - (offset + n_size));
                        }

                        // Async upload actual data to GPU
                        ggml_backend_tensor_set_async(upload_backend, cur,
                                                      reinterpret_cast<void *>(ptr_data), data_read, data_to_copy);
                        ggml_backend_event_record(events[buffer_idx], upload_backend);

                        data_read += data_to_copy;
                        bytes_read += read_size;

                        ++buffer_idx;
                        buffer_idx %= n_buffers;
                    }
                } else {
                    read_buf.resize(n_size);
                    file->read_raw_at(read_buf.data(), n_size, weight->offs);
                    ggml_backend_tensor_set(cur, read_buf.data(), 0, n_size);
                    if (check_tensors && !ggml_validate_row_data(cur->type, read_buf.data(), n_size)) {
                        throw std::runtime_error(format("tensor '%s' has invalid data", ggml_get_name(cur)));
                    }
                }
            }
        }

        size_done += n_size;
    }

    // free temporary resources used for async uploads
    for (auto * event : events) {
        ggml_backend_event_synchronize(event);
        ggml_backend_event_free(event);
    }
    for (auto * buf : host_buffers) {
        ggml_backend_buffer_free(buf);
    }
    ggml_backend_free(upload_backend);

    // check validation results
    bool validation_failed = false;
    for (auto & future : validation_result) {
        auto result = future.get();
        if (!result.second) {
            LLAMA_LOG_ERROR("%s: tensor '%s' has invalid data\n", __func__, ggml_get_name(result.first));
            validation_failed = true;
        }
    }