# Quickstart

> Encode two captions and a photo into one space, then pick the caption closest to the photo.

4.0.0-rc.1 · c58749fe

Encode two captions and a photo into one space, then pick the caption closest to the photo:

```cpp
#include <cmath>    // `std::sqrt`
#include <cstdio>   // `std::printf`
#include <fstream>  // `std::ifstream`
#include <iterator> // `std::istreambuf_iterator`
#include <vector>   // `std::vector`

#include <uform/uform.hpp>

namespace uf = unum::uform;

int main() {
    auto [executor, executor_status] = uf::executor_t::for_ram(); // the threads `on(executor)` names
    auto [texts_arena, texts_arena_status] = uf::arena_t::make(); // host pages for the calling thread
    auto [images_arena, images_arena_status] = uf::arena_t::make({.executor = executor.handle()}); // a call rewinds its arena
    auto [model, model_status] = uf::model_t::load_ram( // BFloat16 blocks, as the checkpoint stores them
        {.path = uf::bytes_view("model.safetensors"), .quantization = uform_scalar_bf16_k});
    if (model_status.failed()) return 1;

    std::string_view const captions[] = {"a cat on a mat", "a sunrise over the bay"};
    std::ifstream file("cat.png", std::ios::binary);
    std::vector<std::uint8_t> const photo {std::istreambuf_iterator<char>(file), {}};
    std::size_t const photo_offsets[] = {0, photo.size()};
    std::size_t offsets[3];
    char elements[64];
    auto [captions_tape, gather_status] = uf::gathered<char>(captions, offsets, elements); // one copy, end to end
    auto [texts, texts_status] = model.text_encode(captions_tape, uf::encode_outputs_t::embeddings_k, // the calling thread
                                                   uform_scalar_f32_k, texts_arena);
    auto [images, images_status] = model.on(executor).image_encode({photo, photo_offsets}, uf::encode_outputs_t::embeddings_k,
                                                                   uform_scalar_f32_k, images_arena); // blocks on the executor
    if (texts_status.failed() || images_status.failed()) return 1;

    std::span<float const> const image = images.embeddings.row<float>(0); // zero-copy from the arena; embeddings come unnormalized
    auto cosine = [&](std::span<float const> text) {
        float dot = 0, text_norm = 0, image_norm = 0;
        for (std::size_t dimension = 0; dimension != text.size(); ++dimension) {
            dot += text[dimension] * image[dimension];
            text_norm += text[dimension] * text[dimension];
            image_norm += image[dimension] * image[dimension];
        }
        return dot / std::sqrt(text_norm * image_norm);
    };
    std::size_t const best = cosine(texts.embeddings.row<float>(1)) > cosine(texts.embeddings.row<float>(0));
    std::printf("'%.*s' describes the photo best\n", int(captions[best].size()), captions[best].data());
    return 0;
}
```
