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

Turn command import into optional #936

Merged
merged 9 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
optional
  • Loading branch information
JingyaHuang committed Mar 30, 2023
commit e173677932e9cacfe0c16da72ff91524586d586e
16 changes: 14 additions & 2 deletions optimum/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from transformers.utils import is_tf_available

from ..utils import is_onnx_available, is_onnxruntime_available
from .base import BaseOptimumCLICommand, CommandInfo, RootOptimumCLICommand
from .env import EnvironmentCommand
from .export import ExportCommand, ONNXExportCommand, TFLiteExportCommand
from .onnxruntime import ONNXRuntimeCommand, ONNXRuntimmeOptimizeCommand, ONNXRuntimmeQuantizeCommand
from .export import ExportCommand
from .optimum_cli import register_optimum_cli_subcommand


if is_onnx_available():
from .export import ONNXExportCommand

if is_tf_available():
from .export import TFLiteExportCommand

if is_onnxruntime_available():
from .onnxruntime import ONNXRuntimeCommand, ONNXRuntimmeOptimizeCommand, ONNXRuntimmeQuantizeCommand
10 changes: 8 additions & 2 deletions optimum/commands/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from transformers.utils import is_tf_available

from ...utils import is_onnx_available
from .base import ExportCommand
from .onnx import ONNXExportCommand
from .tflite import TFLiteExportCommand


if is_onnx_available():
from .onnx import ONNXExportCommand
if is_tf_available():
from .tflite import TFLiteExportCommand
39 changes: 25 additions & 14 deletions optimum/commands/export/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,36 @@
# limitations under the License.
"""optimum.exporters command-line interface base classes."""

from transformers.utils import is_tf_available

from ...utils import is_onnx_available
from .. import BaseOptimumCLICommand, CommandInfo
from .onnx import ONNXExportCommand
from .tflite import TFLiteExportCommand


class ExportCommand(BaseOptimumCLICommand):
COMMAND = CommandInfo(
name="export",
help="Export PyTorch and TensorFlow models to several format.",
)
SUBCOMMANDS = (
CommandInfo(
name="onnx",
help="Export PyTorch and TensorFlow to ONNX.",
subcommand_class=ONNXExportCommand,
),
CommandInfo(
name="tflite",
help="Export TensorFlow to TensorFlow Lite.",
subcommand_class=TFLiteExportCommand,
),
)
SUBCOMMANDS = ()
if is_onnx_available():
from .onnx import ONNXExportCommand

SUBCOMMANDS += (
CommandInfo(
name="onnx",
help="Export PyTorch and TensorFlow to ONNX.",
subcommand_class=ONNXExportCommand,
),
)

if is_tf_available():
from .tflite import TFLiteExportCommand

SUBCOMMANDS += (
CommandInfo(
name="tflite",
help="Export TensorFlow to TensorFlow Lite.",
subcommand_class=TFLiteExportCommand,
),
)
9 changes: 6 additions & 3 deletions optimum/commands/optimum_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Type, Union

from ..utils import logging
from ..utils import is_onnxruntime_available, logging
from .base import BaseOptimumCLICommand, CommandInfo, RootOptimumCLICommand
from .env import EnvironmentCommand
from .export import ExportCommand
from .onnxruntime import ONNXRuntimeCommand


logger = logging.get_logger()

OPTIMUM_CLI_SUBCOMMANDS = [
ExportCommand,
EnvironmentCommand,
ONNXRuntimeCommand,
]

if is_onnxruntime_available():
from .onnxruntime import ONNXRuntimeCommand

OPTIMUM_CLI_SUBCOMMANDS.append(ONNXRuntimeCommand)


def resolve_command_to_command_instance(
root: RootOptimumCLICommand, commands: List[Type[BaseOptimumCLICommand]]
Expand Down
1 change: 1 addition & 0 deletions optimum/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
check_if_transformers_greater,
is_accelerate_available,
is_diffusers_available,
is_onnx_available,
is_onnxruntime_available,
is_pydantic_available,
is_torch_onnx_support_available,
Expand Down