[go: nahoru, domu]

Skip to content
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

[Nano] Fix the long warm-up time of jit model #7247

Open
wants to merge 3 commits into
base: main_backup
Choose a base branch
from

Conversation

ACupofAir
Copy link
Contributor

Description

  • Add warm up action when optimize jit model by trace or quantize function

1. Why the change?

2. User API changes

None

3. Summary of the change

  • Add warm up action before return jit model optimized by trace or quantize
  • Add warning when load jit model

4. How to test?

  • test code: change the PRECISION to test bf16_jit or fp32_jit
#%%
import torch
from torchvision.models import resnet18
from bigdl.nano.pytorch import InferenceOptimizer

#%%
USE_JIT = True
# USE_JIT = False
# PRECISION = "fp32"
PRECISION = "bf16"

model_ft = resnet18(pretrained=True)

if PRECISION == "fp32":
    jit_model = InferenceOptimizer.trace(
        model_ft,
        accelerator="jit",
        use_ipex=True,
        input_sample=torch.rand(1, 3, 224, 224),
        thread_num=1,
    )
elif PRECISION == "bf16":
    jit_model = InferenceOptimizer.quantize(
        model_ft,
        precision="bf16",
        accelerator="jit",
        use_ipex=True,
        input_sample=torch.rand(1, 3, 224, 224),
        thread_num=1,
    )
else:
    print("Error: The PRECISION must be 'fp32' or 'bf16'")

x = torch.rand(3, 3, 224, 224)
#%%
with torch.no_grad():
    with torch.jit.optimized_execution(USE_JIT):
        %time y_hat = jit_model(x)

#%%
with torch.no_grad():
    with torch.jit.optimized_execution(USE_JIT):
        %time y_hat = jit_model(x)
#%%
with torch.no_grad():
    with torch.jit.optimized_execution(USE_JIT):
        %time y_hat = jit_model(x)
#%%
with torch.no_grad():
    with torch.jit.optimized_execution(USE_JIT):
        %time y_hat = jit_model(x)
#%%
with torch.no_grad():
    with torch.jit.optimized_execution(USE_JIT):
        %time y_hat = jit_model(x)
#%%
with torch.no_grad():
    with torch.jit.optimized_execution(USE_JIT):
        %time y_hat = jit_model(x)
#%%

5. Result of test

  • Result without warmup before(bf16)
    MicrosoftTeams-image
  • Result after warmup
    MicrosoftTeams-image (1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants