-
Notifications
You must be signed in to change notification settings - Fork 608
Refactor preprocess to use EagerModelBase #6530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 0 additions & 85 deletions
85
examples/models/llama3_2_vision/preprocess/export_preprocess_lib.py
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
# pyre-unsafe | ||
|
||
from dataclasses import dataclass | ||
from typing import Dict, List, Optional, Tuple | ||
|
||
import torch | ||
|
||
from executorch.extension.llm.custom_ops import op_tile_crop_aot # noqa | ||
from torch.export import Dim | ||
from torchtune.models.clip.inference._transform import _CLIPImageTransform | ||
|
||
from ...model_base import EagerModelBase | ||
|
||
|
||
@dataclass | ||
class PreprocessConfig: | ||
image_mean: Optional[List[float]] = None | ||
image_std: Optional[List[float]] = None | ||
resample: str = "bilinear" | ||
max_num_tiles: int = 4 | ||
tile_size: int = 224 | ||
antialias: bool = False | ||
|
||
|
||
class CLIPImageTransformModel(EagerModelBase): | ||
def __init__( | ||
self, | ||
config: PreprocessConfig, | ||
): | ||
super().__init__() | ||
|
||
# Eager model. | ||
self.model = _CLIPImageTransform( | ||
image_mean=config.image_mean, | ||
image_std=config.image_std, | ||
resample=config.resample, | ||
max_num_tiles=config.max_num_tiles, | ||
tile_size=config.tile_size, | ||
antialias=config.antialias, | ||
) | ||
|
||
# Replace non-exportable ops with custom ops. | ||
self.model.tile_crop = torch.ops.preprocess.tile_crop.default | ||
|
||
def get_eager_model(self) -> torch.nn.Module: | ||
return self.model | ||
|
||
def get_example_inputs(self) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
image = torch.ones(3, 800, 600) | ||
target_size = torch.tensor([448, 336]) | ||
canvas_size = torch.tensor([448, 448]) | ||
return (image, target_size, canvas_size) | ||
|
||
def get_dynamic_shapes(self) -> Dict[str, Dict[int, Dim]]: | ||
img_h = Dim("img_h", min=1, max=4000) | ||
img_w = Dim("img_w", min=1, max=4000) | ||
|
||
dynamic_shapes = { | ||
"image": {1: img_h, 2: img_w}, | ||
"target_size": None, | ||
"canvas_size": None, | ||
} | ||
return dynamic_shapes |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why commented out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#6553
Aoti still requires pytorch/pytorch#137063, Richard said will try to land eow.