|
| 1 | +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Classes to modify TensorFlow Serving code to be compatible with SageMaker Python SDK v2.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import ast |
| 17 | + |
| 18 | +from sagemaker.cli.compatibility.v2.modifiers.modifier import Modifier |
| 19 | + |
| 20 | + |
| 21 | +class TensorFlowServingConstructorRenamer(Modifier): |
| 22 | + """A class to rename TensorFlow Serving classes.""" |
| 23 | + |
| 24 | + def node_should_be_modified(self, node): |
| 25 | + """Checks if the ``ast.Call`` node instantiates a TensorFlow Serving class. |
| 26 | +
|
| 27 | + This looks for the following calls: |
| 28 | +
|
| 29 | + - ``sagemaker.tensorflow.serving.Model`` |
| 30 | + - ``sagemaker.tensorflow.serving.Predictor`` |
| 31 | + - ``Predictor`` |
| 32 | +
|
| 33 | + Because ``Model`` can refer to either ``sagemaker.tensorflow.serving.Model`` |
| 34 | + or :class:`~sagemaker.model.Model`, ``Model`` on its own is not sufficient |
| 35 | + for indicating a TFS Model object. |
| 36 | +
|
| 37 | + Args: |
| 38 | + node (ast.Call): a node that represents a function call. For more, |
| 39 | + see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + bool: If the ``ast.Call`` instantiates a TensorFlow Serving class. |
| 43 | + """ |
| 44 | + if isinstance(node.func, ast.Name): |
| 45 | + return node.func.id == "Predictor" |
| 46 | + |
| 47 | + if not (isinstance(node.func, ast.Attribute) and node.func.attr in ("Model", "Predictor")): |
| 48 | + return False |
| 49 | + |
| 50 | + return ( |
| 51 | + isinstance(node.func.value, ast.Attribute) |
| 52 | + and node.func.value.attr == "serving" |
| 53 | + and isinstance(node.func.value.value, ast.Attribute) |
| 54 | + and node.func.value.value.attr == "tensorflow" |
| 55 | + and isinstance(node.func.value.value.value, ast.Name) |
| 56 | + and node.func.value.value.value.id == "sagemaker" |
| 57 | + ) |
| 58 | + |
| 59 | + def modify_node(self, node): |
| 60 | + """Modifies the ``ast.Call`` node to use the v2 classes for TensorFlow Serving: |
| 61 | +
|
| 62 | + - ``sagemaker.tensorflow.TensorFlowModel`` |
| 63 | + - ``sagemaker.tensorflow.TensorFlowPredictor`` |
| 64 | +
|
| 65 | + Args: |
| 66 | + node (ast.Call): a node that represents a TensorFlow Serving constructor. |
| 67 | + """ |
| 68 | + if isinstance(node.func, ast.Name): |
| 69 | + node.func.id = self._new_cls_name(node.func.id) |
| 70 | + else: |
| 71 | + node.func.attr = self._new_cls_name(node.func.attr) |
| 72 | + node.func.value = node.func.value.value |
| 73 | + |
| 74 | + def _new_cls_name(self, cls_name): |
| 75 | + """Returns the v2 class name.""" |
| 76 | + return "TensorFlow{}".format(cls_name) |
| 77 | + |
| 78 | + |
| 79 | +class TensorFlowServingImportFromRenamer(Modifier): |
| 80 | + """A class to update import statements starting with ``from sagemaker.tensorflow.serving``.""" |
| 81 | + |
| 82 | + def node_should_be_modified(self, node): |
| 83 | + """Checks if the import statement imports from the ``sagemaker.tensorflow.serving`` module. |
| 84 | +
|
| 85 | + Args: |
| 86 | + node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement. |
| 87 | + For more, see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + bool: If the ``ast.ImportFrom`` uses the ``sagemaker.tensorflow.serving`` module. |
| 91 | + """ |
| 92 | + return node.module == "sagemaker.tensorflow.serving" |
| 93 | + |
| 94 | + def modify_node(self, node): |
| 95 | + """Changes the ``ast.ImportFrom`` node's module to ``sagemaker.tensorflow`` and updates the |
| 96 | + imported class names to ``TensorFlowModel`` and ``TensorFlowPredictor``, as applicable. |
| 97 | +
|
| 98 | + Args: |
| 99 | + node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement. |
| 100 | + For more, see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 101 | + """ |
| 102 | + node.module = "sagemaker.tensorflow" |
| 103 | + |
| 104 | + for cls in node.names: |
| 105 | + cls.name = "TensorFlow{}".format(cls.name) |
| 106 | + |
| 107 | + |
| 108 | +class TensorFlowServingImportRenamer(Modifier): |
| 109 | + """A class to update ``import sagemaker.tensorflow.serving``.""" |
| 110 | + |
| 111 | + def check_and_modify_node(self, node): |
| 112 | + """Checks if the ``ast.Import`` node imports the ``sagemaker.tensorflow.serving`` module |
| 113 | + and, if so, changes it to ``sagemaker.tensorflow``. |
| 114 | +
|
| 115 | + Args: |
| 116 | + node (ast.Import): a node that represents an import statement. For more, |
| 117 | + see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 118 | + """ |
| 119 | + for module in node.names: |
| 120 | + if module.name == "sagemaker.tensorflow.serving": |
| 121 | + module.name = "sagemaker.tensorflow" |
0 commit comments