{"action":"created","issue":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/24236","repository_url":"https://api.github.com/repos/tensorflow/tensorflow","labels_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/24236/labels{/name}","comments_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/24236/comments","events_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/24236/events","html_url":"https://github.com/tensorflow/tensorflow/issues/24236","id":388862847,"node_id":"MDU6SXNzdWUzODg4NjI4NDc=","number":24236,"title":"XLA support for Python  instance, class, and static methods","user":{"login":"subrataambient","id":42156923,"node_id":"MDQ6VXNlcjQyMTU2OTIz","avatar_url":"https://avatars0.githubusercontent.com/u/42156923?v=4","gravatar_id":"","url":"https://api.github.com/users/subrataambient","html_url":"https://github.com/subrataambient","followers_url":"https://api.github.com/users/subrataambient/followers","following_url":"https://api.github.com/users/subrataambient/following{/other_user}","gists_url":"https://api.github.com/users/subrataambient/gists{/gist_id}","starred_url":"https://api.github.com/users/subrataambient/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/subrataambient/subscriptions","organizations_url":"https://api.github.com/users/subrataambient/orgs","repos_url":"https://api.github.com/users/subrataambient/repos","events_url":"https://api.github.com/users/subrataambient/events{/privacy}","received_events_url":"https://api.github.com/users/subrataambient/received_events","type":"User","site_admin":false},"labels":[{"id":1133285679,"node_id":"MDU6TGFiZWwxMTMzMjg1Njc5","url":"https://api.github.com/repos/tensorflow/tensorflow/labels/comp:xla","name":"comp:xla","color":"0052cc","default":false},{"id":473173272,"node_id":"MDU6TGFiZWw0NzMxNzMyNzI=","url":"https://api.github.com/repos/tensorflow/tensorflow/labels/type:feature","name":"type:feature","color":"159b2e","default":false}],"state":"open","locked":false,"assignee":{"login":"jlebar","id":150663,"node_id":"MDQ6VXNlcjE1MDY2Mw==","avatar_url":"https://avatars1.githubusercontent.com/u/150663?v=4","gravatar_id":"","url":"https://api.github.com/users/jlebar","html_url":"https://github.com/jlebar","followers_url":"https://api.github.com/users/jlebar/followers","following_url":"https://api.github.com/users/jlebar/following{/other_user}","gists_url":"https://api.github.com/users/jlebar/gists{/gist_id}","starred_url":"https://api.github.com/users/jlebar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jlebar/subscriptions","organizations_url":"https://api.github.com/users/jlebar/orgs","repos_url":"https://api.github.com/users/jlebar/repos","events_url":"https://api.github.com/users/jlebar/events{/privacy}","received_events_url":"https://api.github.com/users/jlebar/received_events","type":"User","site_admin":false},"assignees":[{"login":"jlebar","id":150663,"node_id":"MDQ6VXNlcjE1MDY2Mw==","avatar_url":"https://avatars1.githubusercontent.com/u/150663?v=4","gravatar_id":"","url":"https://api.github.com/users/jlebar","html_url":"https://github.com/jlebar","followers_url":"https://api.github.com/users/jlebar/followers","following_url":"https://api.github.com/users/jlebar/following{/other_user}","gists_url":"https://api.github.com/users/jlebar/gists{/gist_id}","starred_url":"https://api.github.com/users/jlebar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jlebar/subscriptions","organizations_url":"https://api.github.com/users/jlebar/orgs","repos_url":"https://api.github.com/users/jlebar/repos","events_url":"https://api.github.com/users/jlebar/events{/privacy}","received_events_url":"https://api.github.com/users/jlebar/received_events","type":"User","site_admin":false}],"milestone":null,"comments":3,"created_at":"2018-12-08T00:35:26Z","updated_at":"2018-12-11T01:48:19Z","closed_at":null,"author_association":"NONE","body":"\r\n\r\n**System information**\r\n- TensorFlow version (you are using): 1.12\r\n- Are you willing to contribute it (Yes/No): Yes\r\n\r\n\r\n\r\n**Describe the feature and the current behavior/state.**\r\n\r\nThe current XLA compile API does not support  instance methods.  Existing  code that uses Python objects along with TF api codes needs lot of refactoring to enable XLA. \r\n\r\n**Will this change the current api? How?**\r\nNo change in the  current API (e.g. xla.compile ). \r\n\r\n**Who will benefit with this feature?**\r\nAny  Python developer using XLA\r\n\r\n**Any Other info.**\r\n"},"comment":{"url":"https://api.github.com/repos/tensorflow/tensorflow/issues/comments/446042145","html_url":"https://github.com/tensorflow/tensorflow/issues/24236#issuecomment-446042145","issue_url":"https://api.github.com/repos/tensorflow/tensorflow/issues/24236","id":446042145,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0NjA0MjE0NQ==","user":{"login":"subrataambient","id":42156923,"node_id":"MDQ6VXNlcjQyMTU2OTIz","avatar_url":"https://avatars0.githubusercontent.com/u/42156923?v=4","gravatar_id":"","url":"https://api.github.com/users/subrataambient","html_url":"https://github.com/subrataambient","followers_url":"https://api.github.com/users/subrataambient/followers","following_url":"https://api.github.com/users/subrataambient/following{/other_user}","gists_url":"https://api.github.com/users/subrataambient/gists{/gist_id}","starred_url":"https://api.github.com/users/subrataambient/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/subrataambient/subscriptions","organizations_url":"https://api.github.com/users/subrataambient/orgs","repos_url":"https://api.github.com/users/subrataambient/repos","events_url":"https://api.github.com/users/subrataambient/events{/privacy}","received_events_url":"https://api.github.com/users/subrataambient/received_events","type":"User","site_admin":false},"created_at":"2018-12-11T01:48:19Z","updated_at":"2018-12-11T01:48:19Z","author_association":"NONE","body":"Thanks for  the quick response. Here are my findings of your suggested workarounds.\r\n\r\n1. works with the following \r\n```\r\n    def build_mnist_model_meth(self, x, y_):\r\n        y = tf.keras.layers.Dense(MNIST.NUM_CLASSES).apply(x)\r\n\r\n        cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y_, logits=y)\r\n        train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\r\n\r\n        return y, train_step\r\n    \r\n    def train (self, ):  \r\n        def  build_mnist_model_xla_helper(images, labels):\r\n            return self.build_mnist_model_meth(images, labels) \r\n        # works \r\n        #[y] = xla.compile(build_mnist_model_func, inputs=[self.images, self.labels])\r\n        [y] = xla.compile(build_mnist_model_xla_helper, inputs=[self.images, self.labels])\r\n```\r\n\r\n\r\n3.   Does not work \r\n```\r\nTypeError: Failed to convert object of type <type 'instance'> to Tensor. \r\n```\r\n\r\nMight  help to mention the above workaround in XLA documentation.  \r\n\r\nThe  sample MNIST code as of now with the mods in 1 is as follows. \r\n\r\n```\r\nimport tensorflow as tf\r\nfrom tensorflow.contrib.compiler import xla\r\nfrom tensorflow.python.client import timeline\r\n\r\n\r\ndef build_mnist_model_func(x, y_):\r\n    y = tf.keras.layers.Dense(MNIST.NUM_CLASSES).apply(x)\r\n    cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y_, logits=y)\r\n    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\r\n    return y, train_step\r\n\r\nclass MNIST :\r\n\r\n    # Size of each input image, 28 x 28 pixels\r\n    IMAGE_SIZE = 28 * 28\r\n    # Number of distinct number labels, [0..9]\r\n    NUM_CLASSES = 10\r\n    # Number of examples in each training batch (step)\r\n    TRAIN_BATCH_SIZE = 100\r\n    # Number of training steps to run\r\n    TRAIN_STEPS = 1000\r\n\r\n    def data_ready(self,):\r\n        # Loads MNIST dataset.\r\n        self.traind, self.testd = tf.keras.datasets.mnist.load_data()\r\n        self.train_ds = tf.data.Dataset.from_tensor_slices(self.traind).batch(MNIST.TRAIN_BATCH_SIZE).repeat()\r\n        self.test_ds = tf.data.Dataset.from_tensor_slices(self.testd).batch(MNIST.TRAIN_BATCH_SIZE)\r\n\r\n        self.iterator = tf.data.Iterator.from_structure(self.train_ds.output_types, self.train_ds.output_shapes)\r\n        self.images, self.labels = self.iterator.get_next()\r\n        self.images = tf.reshape(self.images, [-1, MNIST.IMAGE_SIZE])\r\n        self.images, self.labels = tf.cast(self.images, tf.float32), tf.cast(self.labels, tf.int64)\r\n\r\n    def build_mnist_model_meth(self, x, y_):\r\n        y = tf.keras.layers.Dense(MNIST.NUM_CLASSES).apply(x)\r\n\r\n        cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y_, logits=y)\r\n        train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\r\n\r\n        return y, train_step\r\n\r\n    def train (self, ):\r\n        def  build_mnist_model_xla_helper(images, labels):\r\n            return self.build_mnist_model_meth(images, labels)\r\n        # works \r\n        #[y] = xla.compile(build_mnist_model_func, inputs=[self.images, self.labels])\r\n        [y] = xla.compile(build_mnist_model_xla_helper, inputs=[self.images, self.labels])\r\n        # does not work\r\n        #[y] = xla.compile(self.build_mnist_model_meth, inputs=[self.images, self.labels])\r\n        #[y] = xla.compile(self.build_mnist_model_meth, inputs=[self, self.images, self.labels])\r\n\r\n        # Creates session and initialize all variables.\r\n        # xla.compile() doesn't work with Keras model.fit() API or TF eager mode yet.\r\n        self.sess = tf.Session()\r\n        options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)\r\n        run_metadata = tf.RunMetadata()\r\n        self.sess.run(tf.global_variables_initializer())\r\n\r\n        # Feeds training dataset\r\n        self.sess.run(self.iterator.make_initializer(self.train_ds))\r\n\r\n        # Runs TRAIN_STEPS steps\r\n        for i in range(MNIST.TRAIN_STEPS):\r\n            self.sess.run(y, options=options, run_metadata=run_metadata)\r\n\r\n        print(\"Model trained for %s steps.\" % MNIST.TRAIN_STEPS)\r\n        return y\r\n\r\n    def test (self, y) :\r\n        # Tests trained model\r\n\r\n        # Feeds testing dataset\r\n        self.sess.run(self.iterator.make_initializer(self.test_ds))\r\n\r\n        # Calculates accuracy\r\n        correct_prediction = tf.equal(tf.argmax(y, 1), self.labels)\r\n        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\r\n        options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)\r\n        run_metadata = tf.RunMetadata()\r\n        print(\"Prediction accuracy after training: %s\" % self.sess.run(accuracy, options=options, run_metadata=run_metadata))\r\n\r\n        # Create the Timeline object, and write it to a json file\r\n        fetched_timeline = timeline.Timeline(run_metadata.step_stats)\r\n        chrome_trace = fetched_timeline.generate_chrome_trace_format()\r\n        with open('mnist-xla-test-timeline.json', 'w') as f:\r\n            f.write(chrome_trace)\r\n\r\n        # Cleans up session\r\n        self.sess.close()\r\n\r\nif __name__ == '__main__' :\r\n    mnist = MNIST()\r\n    mnist.data_ready()\r\n    y = mnist.train()\r\n    mnist.test(y)\r\n\r\n```"}}
{"actor":{"display_login":"subrataambient"}}