prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  [clean up] Should check tf is running eagerly early
  so the program can fail early if tf is configured wrong.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `elasticdl/worker/worker.py`
  Content:
  ```
  1 import tensorflow as tf
  2 from tensorflow.python.ops import math_ops
  3 from proto import master_pb2
  4 from proto import master_pb2_grpc
  5 
  6 
  7 class Worker(object):
  8     """ElasticDL worker"""
  9 
  10     def __init__(self, model_cls, input_fn, opt_fn, channel=None):
  11         """
  12         Arguments:
  13             model_cls: A class to define the model, which contains funcs
  14                 get_keras_model: return the keras model defined in the class, with a tf dataset as its input
  15                 output(data): get model ouput from data as input, either a single output of a dict of outputs
  16                 loss(output, data): get model loss from output and data as input
  17             input_fn: a func to to get a dataset, which can be used as the keras model input
  18                       dataset = input_fn(dict_of_params)
  19                       dict_of_params from GetTask for DistributedTrain, from kwargs for LocalTrain
  20             opt_fn: a func to get the optimizer 
  21             channel: grpc channel
  22         """
  23 
  24         self._model_cls = model_cls()
  25         self._keras_model = self._model_cls.get_keras_model()
  26         self._input_fn = input_fn
  27         self._opt_fn = opt_fn
  28         if channel is None:
  29             self._stub = None
  30         else:
  31             self._stub = master_pb2_grpc.MasterStub(channel)
  32         self._model_version = -1
  33 
  34     def get_task(self):
  35         # TODO: get task from master
  36         pass
  37 
  38     def get_model(self):
  39         # TODO: get model from master
  40         pass
  41 
  42     def report_task_result(self):
  43         # TODO: report task result to master
  44         pass
  45 
  46     def report_gradient(self):
  47         # TODO: report gradient to ps
  48         pass
  49 
  50     def distributed_train(self):
  51         # TODO: distributed training
  52         pass
  53 
  54     def local_train(self, batch_size, epoch=1, kwargs=None):
  55         """
  56         Local training for local testing. Must in eager mode.
  57         Argments:
  58             batch_size: batch size in training
  59             epoch: the number of epoch in training
  60             kwargs: contains a dict of parameters used in training
  61         """
  62 
  63         if not tf.executing_eagerly():
  64             raise ValueError('Eager mode is required for LocalTrain')
  65 
  66         dataset = self._input_fn(kwargs)
  67         dataset = dataset.repeat(epoch).batch(batch_size)
  68         optimizer = self._opt_fn()
  69 
  70         for data in dataset:
  71             with tf.GradientTape() as tape:
  72                 output = self._model_cls.output(data)
  73                 loss = self._model_cls.loss(output, data)
  74                 # Add regularization loss if any.
  75                 # Note: for distributed training, the regularization loss should
  76                 #       be divided by the number of contributing workers, which
  77                 #       might be difficult for elasticdl.
  78                 if self._keras_model.losses:
  79                     loss += math_ops.add_n(self._keras_model.losses)
  80             grads = tape.gradient(loss, self._keras_model.variables)
  81             optimizer.apply_gradients(zip(grads, self._keras_model.variables))
  82             print('Loss is ', loss.numpy())
  83         pass
  84
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/elasticdl/worker/worker.py b/elasticdl/worker/worker.py
  --- a/elasticdl/worker/worker.py
  +++ b/elasticdl/worker/worker.py
  @@ -1,4 +1,6 @@
   import tensorflow as tf
  +assert tf.executing_eagerly()
  +
   from tensorflow.python.ops import math_ops
   from proto import master_pb2
   from proto import master_pb2_grpc
  @@ -60,9 +62,6 @@
               kwargs: contains a dict of parameters used in training
           """
   
  -        if not tf.executing_eagerly():
  -            raise ValueError('Eager mode is required for LocalTrain')
  -
           dataset = self._input_fn(kwargs)
           dataset = dataset.repeat(epoch).batch(batch_size)
           optimizer = self._opt_fn()
  
verification_info: {"golden_diff": "diff --git a/elasticdl/worker/worker.py b/elasticdl/worker/worker.py\n--- a/elasticdl/worker/worker.py\n+++ b/elasticdl/worker/worker.py\n@@ -1,4 +1,6 @@\n import tensorflow as tf\n+assert tf.executing_eagerly()\n+\n from tensorflow.python.ops import math_ops\n from proto import master_pb2\n from proto import master_pb2_grpc\n@@ -60,9 +62,6 @@\n             kwargs: contains a dict of parameters used in training\n         \"\"\"\n \n-        if not tf.executing_eagerly():\n-            raise ValueError('Eager mode is required for LocalTrain')\n-\n         dataset = self._input_fn(kwargs)\n         dataset = dataset.repeat(epoch).batch(batch_size)\n         optimizer = self._opt_fn()\n", "issue": "[clean up] Should check tf is running eagerly early\nso the program can fail early if tf is configured wrong.\n", "before_files": [{"content": "import tensorflow as tf\nfrom tensorflow.python.ops import math_ops\nfrom proto import master_pb2\nfrom proto import master_pb2_grpc\n\n\nclass Worker(object):\n    \"\"\"ElasticDL worker\"\"\"\n\n    def __init__(self, model_cls, input_fn, opt_fn, channel=None):\n        \"\"\"\n        Arguments:\n            model_cls: A class to define the model, which contains funcs\n                get_keras_model: return the keras model defined in the class, with a tf dataset as its input\n                output(data): get model ouput from data as input, either a single output of a dict of outputs\n                loss(output, data): get model loss from output and data as input\n            input_fn: a func to to get a dataset, which can be used as the keras model input\n                      dataset = input_fn(dict_of_params)\n                      dict_of_params from GetTask for DistributedTrain, from kwargs for LocalTrain\n            opt_fn: a func to get the optimizer \n            channel: grpc channel\n        \"\"\"\n\n        self._model_cls = model_cls()\n        self._keras_model = self._model_cls.get_keras_model()\n        self._input_fn = input_fn\n        self._opt_fn = opt_fn\n        if channel is None:\n            self._stub = None\n        else:\n            self._stub = master_pb2_grpc.MasterStub(channel)\n        self._model_version = -1\n\n    def get_task(self):\n        # TODO: get task from master\n        pass\n\n    def get_model(self):\n        # TODO: get model from master\n        pass\n\n    def report_task_result(self):\n        # TODO: report task result to master\n        pass\n\n    def report_gradient(self):\n        # TODO: report gradient to ps\n        pass\n\n    def distributed_train(self):\n        # TODO: distributed training\n        pass\n\n    def local_train(self, batch_size, epoch=1, kwargs=None):\n        \"\"\"\n        Local training for local testing. Must in eager mode.\n        Argments:\n            batch_size: batch size in training\n            epoch: the number of epoch in training\n            kwargs: contains a dict of parameters used in training\n        \"\"\"\n\n        if not tf.executing_eagerly():\n            raise ValueError('Eager mode is required for LocalTrain')\n\n        dataset = self._input_fn(kwargs)\n        dataset = dataset.repeat(epoch).batch(batch_size)\n        optimizer = self._opt_fn()\n\n        for data in dataset:\n            with tf.GradientTape() as tape:\n                output = self._model_cls.output(data)\n                loss = self._model_cls.loss(output, data)\n                # Add regularization loss if any.\n                # Note: for distributed training, the regularization loss should\n                #       be divided by the number of contributing workers, which\n                #       might be difficult for elasticdl.\n                if self._keras_model.losses:\n                    loss += math_ops.add_n(self._keras_model.losses)\n            grads = tape.gradient(loss, self._keras_model.variables)\n            optimizer.apply_gradients(zip(grads, self._keras_model.variables))\n            print('Loss is ', loss.numpy())\n        pass\n", "path": "elasticdl/worker/worker.py"}], "after_files": [{"content": "import tensorflow as tf\nassert tf.executing_eagerly()\n\nfrom tensorflow.python.ops import math_ops\nfrom proto import master_pb2\nfrom proto import master_pb2_grpc\n\n\nclass Worker(object):\n    \"\"\"ElasticDL worker\"\"\"\n\n    def __init__(self, model_cls, input_fn, opt_fn, channel=None):\n        \"\"\"\n        Arguments:\n            model_cls: A class to define the model, which contains funcs\n                get_keras_model: return the keras model defined in the class, with a tf dataset as its input\n                output(data): get model ouput from data as input, either a single output of a dict of outputs\n                loss(output, data): get model loss from output and data as input\n            input_fn: a func to to get a dataset, which can be used as the keras model input\n                      dataset = input_fn(dict_of_params)\n                      dict_of_params from GetTask for DistributedTrain, from kwargs for LocalTrain\n            opt_fn: a func to get the optimizer \n            channel: grpc channel\n        \"\"\"\n\n        self._model_cls = model_cls()\n        self._keras_model = self._model_cls.get_keras_model()\n        self._input_fn = input_fn\n        self._opt_fn = opt_fn\n        if channel is None:\n            self._stub = None\n        else:\n            self._stub = master_pb2_grpc.MasterStub(channel)\n        self._model_version = -1\n\n    def get_task(self):\n        # TODO: get task from master\n        pass\n\n    def get_model(self):\n        # TODO: get model from master\n        pass\n\n    def report_task_result(self):\n        # TODO: report task result to master\n        pass\n\n    def report_gradient(self):\n        # TODO: report gradient to ps\n        pass\n\n    def distributed_train(self):\n        # TODO: distributed training\n        pass\n\n    def local_train(self, batch_size, epoch=1, kwargs=None):\n        \"\"\"\n        Local training for local testing. Must in eager mode.\n        Argments:\n            batch_size: batch size in training\n            epoch: the number of epoch in training\n            kwargs: contains a dict of parameters used in training\n        \"\"\"\n\n        dataset = self._input_fn(kwargs)\n        dataset = dataset.repeat(epoch).batch(batch_size)\n        optimizer = self._opt_fn()\n\n        for data in dataset:\n            with tf.GradientTape() as tape:\n                output = self._model_cls.output(data)\n                loss = self._model_cls.loss(output, data)\n                # Add regularization loss if any.\n                # Note: for distributed training, the regularization loss should\n                #       be divided by the number of contributing workers, which\n                #       might be difficult for elasticdl.\n                if self._keras_model.losses:\n                    loss += math_ops.add_n(self._keras_model.losses)\n            grads = tape.gradient(loss, self._keras_model.variables)\n            optimizer.apply_gradients(zip(grads, self._keras_model.variables))\n            print('Loss is ', loss.numpy())\n        pass\n", "path": "elasticdl/worker/worker.py"}]}