---
title: vision
---

<div class="python-reference">
<a id="fastvideo.models.encoders.vision-functions"></a>
<a id="fastvideo.models.encoders.vision"></a>

<h2 id="fastvideo.models.encoders.vision.init_logger"><code>fastvideo.models.encoders.vision.init_logger</code></h2>
<pre class="doc-signature"><code>init_logger(name: str) -&gt; _FastvideoLogger</code></pre>
<p>The main purpose of this function is to ensure that loggers are
retrieved in such a way that we can be sure the root fastvideo logger has
already been configured.</p>
<h2 id="fastvideo.models.encoders.vision.VisionEncoderInfo"><code>fastvideo.models.encoders.vision.VisionEncoderInfo</code></h2>
<pre class="doc-signature"><code>VisionEncoderInfo(vision_config: _C)</code></pre>


<details><summary>Source</summary><pre><code>class VisionEncoderInfo(ABC, Generic&#91;_C&#93;):

    def __init__(self, vision_config: _C) -&gt; None:
        super().__init__()

        self.vision_config = vision_config

    @abstractmethod
    def get_num_image_tokens(
        self,
        *,
        image_width: int,
        image_height: int,
    ) -&gt; int:
        raise NotImplementedError

    @abstractmethod
    def get_max_image_tokens(self) -&gt; int:
        raise NotImplementedError

    @abstractmethod
    def get_image_size(self) -&gt; int:
        raise NotImplementedError

    @abstractmethod
    def get_patch_size(self) -&gt; int:
        raise NotImplementedError

    @abstractmethod
    def get_patch_grid_length(self) -&gt; int:
        raise NotImplementedError</code></pre></details>
<h3 id="fastvideo.models.encoders.vision.VisionEncoderInfo.get_num_image_tokens"><code>fastvideo.models.encoders.vision.VisionEncoderInfo.get_num_image_tokens</code></h3>
<pre class="doc-signature"><code>get_num_image_tokens(*, image_width: int, image_height: int) -&gt; int</code></pre>

<details><summary>Source</summary><pre><code>@abstractmethod
def get_num_image_tokens(
    self,
    *,
    image_width: int,
    image_height: int,
) -&gt; int:
    raise NotImplementedError</code></pre></details>
<h3 id="fastvideo.models.encoders.vision.VisionEncoderInfo.get_max_image_tokens"><code>fastvideo.models.encoders.vision.VisionEncoderInfo.get_max_image_tokens</code></h3>
<pre class="doc-signature"><code>get_max_image_tokens() -&gt; int</code></pre>

<details><summary>Source</summary><pre><code>@abstractmethod
def get_max_image_tokens(self) -&gt; int:
    raise NotImplementedError</code></pre></details>
<h3 id="fastvideo.models.encoders.vision.VisionEncoderInfo.get_image_size"><code>fastvideo.models.encoders.vision.VisionEncoderInfo.get_image_size</code></h3>
<pre class="doc-signature"><code>get_image_size() -&gt; int</code></pre>

<details><summary>Source</summary><pre><code>@abstractmethod
def get_image_size(self) -&gt; int:
    raise NotImplementedError</code></pre></details>
<h3 id="fastvideo.models.encoders.vision.VisionEncoderInfo.get_patch_size"><code>fastvideo.models.encoders.vision.VisionEncoderInfo.get_patch_size</code></h3>
<pre class="doc-signature"><code>get_patch_size() -&gt; int</code></pre>

<details><summary>Source</summary><pre><code>@abstractmethod
def get_patch_size(self) -&gt; int:
    raise NotImplementedError</code></pre></details>
<h3 id="fastvideo.models.encoders.vision.VisionEncoderInfo.get_patch_grid_length"><code>fastvideo.models.encoders.vision.VisionEncoderInfo.get_patch_grid_length</code></h3>
<pre class="doc-signature"><code>get_patch_grid_length() -&gt; int</code></pre>

<details><summary>Source</summary><pre><code>@abstractmethod
def get_patch_grid_length(self) -&gt; int:
    raise NotImplementedError</code></pre></details>
<h2 id="fastvideo.models.encoders.vision.resolve_visual_encoder_outputs"><code>fastvideo.models.encoders.vision.resolve_visual_encoder_outputs</code></h2>
<pre class="doc-signature"><code>resolve_visual_encoder_outputs(encoder_outputs: torch.Tensor | list&#91;torch.Tensor&#93;, feature_sample_layers: list&#91;int&#93; | None, post_layer_norm: torch.nn.LayerNorm | None, max_possible_layers: int) -&gt; torch.Tensor</code></pre>
<p>Given the outputs a visual encoder module that may correspond to the
output of the last layer, or a list of hidden states to be stacked,
handle post normalization and resolve it into a single output tensor.</p>
<h3>Parameters</h3>
<dl>
<dt><code>encoder_outputs: torch.Tensor | list[torch.Tensor]</code></dt><dd><p>Output of encoder's last layer or all hidden states.</p></dd>
<dt><code>feature_sample_layers: list[int] | None</code></dt><dd><p>Optional layer indices to grab from the encoder
outputs; if provided, encoder outputs must be a list.</p></dd>
<dt><code>post_layer_norm: torch.nn.LayerNorm | None</code></dt><dd><p>Post norm to apply to the output of the encoder.</p></dd>
<dt><code>max_possible_layers: int</code></dt><dd><p>Total layers in the fully loaded visual encoder.</p></dd>
</dl>
<details><summary>Source</summary><pre><code>def resolve_visual_encoder_outputs(
    encoder_outputs: torch.Tensor | list&#91;torch.Tensor&#93;,
    feature_sample_layers: list&#91;int&#93; | None,
    post_layer_norm: torch.nn.LayerNorm | None,
    max_possible_layers: int,
) -&gt; torch.Tensor:
    &quot;&quot;&quot;Given the outputs a visual encoder module that may correspond to the
    output of the last layer, or a list of hidden states to be stacked,
    handle post normalization and resolve it into a single output tensor.

    Args:
        encoder_outputs: Output of encoder&#x27;s last layer or all hidden states.
        feature_sample_layers: Optional layer indices to grab from the encoder
            outputs; if provided, encoder outputs must be a list.
        post_layer_norm: Post norm to apply to the output of the encoder.
        max_possible_layers: Total layers in the fully loaded visual encoder.

    &quot;&quot;&quot;
    if feature_sample_layers is None:
        if post_layer_norm is not None:
            return post_layer_norm(encoder_outputs)
        return encoder_outputs

    # Get the hidden states corresponding to the layer indices.
    # Negative values are relative to the full visual encoder,
    # so offset them depending on how many layers were loaded.
    # NOTE: this assumes that encoder_outputs is a list containing
    # the inputs to the visual encoder, followed by the hidden states
    # of each layer.
    num_loaded_layers = len(encoder_outputs) - 1
    offset = max_possible_layers - num_loaded_layers
    hs_pool = &#91;
        encoder_outputs&#91;layer_idx&#93; if layer_idx &gt;= 0 else encoder_outputs&#91;layer_idx + offset&#93;
        for layer_idx in feature_sample_layers
    &#93;

    # Apply post-norm on the final hidden state if we are using it
    uses_last_layer = feature_sample_layers&#91;-1&#93; in (len(hs_pool) - 1, -1)
    if post_layer_norm is not None and uses_last_layer:
        hs_pool&#91;-1&#93; = post_layer_norm(encoder_outputs)
    return torch.cat(hs_pool, dim=-1)</code></pre></details>
</div>
