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

Update export doc (fixes + add torch jit) #1074

Merged
merged 7 commits into from
Sep 30, 2022
Merged

Update export doc (fixes + add torch jit) #1074

merged 7 commits into from
Sep 30, 2022

Conversation

araffin
Copy link
Member

@araffin araffin commented Sep 26, 2022

Description

Related to #836 by @Gregwar

Motivation and Context

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist:

  • I've read the CONTRIBUTION guide (required)
  • I have updated the changelog accordingly (required).
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.
  • I have reformatted the code using make format (required)
  • I have checked the codestyle using make check-codestyle and make lint (required)
  • I have ensured make pytest and make type both pass. (required)
  • I have checked that the documentation builds using make doc (required)

Note: You can run most of the checks using make commit-checks.

Note: we are using a maximum length of 127 characters per line

Comment on lines 164 to 172
import torch as th
from stable_baselines3 import SAC
model = SAC.load("PathToTrainedModel.zip")
# model = SAC("MlpPolicy", "Pendulum-v1")
onnxable_model = OnnxablePolicy(model.policy.actor).to("cpu")

observation_size = model.observation_space.shape
dummy_input = th.randn(1, *observation_size)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, it is not possible to copy and paste, because OnnxablePolicy is not defined. I understand that you don't want to rewrite the class to avoid overloading the example. So I would say to just delete everything that has been written before:

jit_path = "sac_traced.pt"

# Trace and optimize the module
traced_module = th.jit.trace(onnxable_model.eval(), dummy_input)
frozen_module = th.jit.freeze(traced_module)
frozen_module = th.jit.optimize_for_inference(frozen_module)
th.jit.save(frozen_module, jit_path)

##### Load and test with onnx

import torch as th

dummy_input = th.randn(1, *observation_size)
loaded_module = th.jit.load(jit_path)
action_jit = loaded_module(dummy_input)

@araffin araffin requested a review from qgallouedec September 29, 2022 09:39
@araffin araffin merged commit 537a82a into master Sep 30, 2022
@araffin araffin deleted the docs/update-onnx branch September 30, 2022 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants