prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  [PS-1] RPC service `push_model` implementation
  
  
  </issue>
  <code>
  [start of elasticdl/python/ps/parameters.py]
  1 import tensorflow as tf
  2 
  3 from elasticdl.python.common.ndarray import tensor_to_ndarray
  4 from elasticdl.python.ps.embedding_table import create_embedding_table
  5 
  6 
  7 class Parameters(object):
  8     """
  9     There are two kinds of parameters:
  10 
  11     1. non-embedding parameters, or dense tensor parameters. We save it
  12        in a hashmap `non-embedding_params`, the key is the parameter name,
  13        the value is a tf.Variable` object.
  14     2. embedding parameters, or row-sparse parameters. We save it in a
  15        hashmap `embedding_params`, the key is the embedding layer name,
  16        the value is an `EmbeddingTable` object.
  17 
  18     """
  19 
  20     def __init__(self):
  21         self.init_status = False
  22         self.non_embedding_params = {}
  23         self.embedding_params = {}
  24 
  25     def get_embedding_param(self, name, indices):
  26         if name not in self.embedding_params:
  27             raise ValueError(
  28                 "Please initialize embedding param %s first!", name
  29             )
  30         return self.embedding_params[name].get(indices)
  31 
  32     def set_embedding_param(self, name, indices, values):
  33         if name not in self.embedding_params:
  34             raise ValueError(
  35                 "Please initialize embedding param %s first!", name
  36             )
  37         self.embedding_params[name].set(indices, values)
  38 
  39     def init_from_model_pb(self, model_pb):
  40         if not self.init_status:
  41             tensors_pb = model_pb.param
  42             embeddings_pb = model_pb.embedding_table_info
  43             self._init_non_embedding_params(tensors_pb)
  44             self._init_embedding_params(embeddings_pb)
  45             self.init_status = True
  46 
  47     def _init_non_embedding_params(self, tensors_pb):
  48         for pb in tensors_pb:
  49             name = pb.name
  50             arr = tensor_to_ndarray(pb)
  51             var = tf.Variable(name=name, initial_value=arr, trainable=True)
  52             self.non_embedding_params[name] = var
  53 
  54     def _init_embedding_params(self, embeddings_pb):
  55         for pb in embeddings_pb:
  56             self.embedding_params[pb.name] = create_embedding_table(pb)
  57
  [end of elasticdl/python/ps/parameters.py]
  [start of elasticdl/python/ps/servicer.py]
  1 from google.protobuf import empty_pb2
  2 
  3 from elasticdl.proto import elasticdl_pb2, elasticdl_pb2_grpc
  4 
  5 
  6 class PserverServicer(elasticdl_pb2_grpc.PserverServicer):
  7     """PS service implementation"""
  8 
  9     def __init__(
  10         self,
  11         parameters,
  12         grads_to_wait,
  13         optimizer,
  14         lr_staleness_modulation=False,
  15         use_async=False,
  16     ):
  17         self._parameters = parameters
  18         self._grads_to_wait = grads_to_wait
  19         self._optimizer = optimizer
  20         self._lr_staleness_modulation = lr_staleness_modulation
  21         self._use_async = use_async
  22         self._version = 0
  23 
  24     def pull_variable(self, request, _):
  25         # TODO: implement this RPC service
  26         return elasticdl_pb2.PullVariableResponse()
  27 
  28     def pull_embedding_vector(self, request, _):
  29         # TODO: implement this RPC service
  30         return elasticdl_pb2.Tensor()
  31 
  32     def push_model(self, request, _):
  33         # TODO: implement this RPC service
  34         return empty_pb2.Empty()
  35 
  36     def push_gradient(self, request, _):
  37         # TODO: implement this RPC service
  38         return elasticdl_pb2.PushGradientResponse()
  39
  [end of elasticdl/python/ps/servicer.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/elasticdl/python/ps/parameters.py b/elasticdl/python/ps/parameters.py
  --- a/elasticdl/python/ps/parameters.py
  +++ b/elasticdl/python/ps/parameters.py
  @@ -18,6 +18,7 @@
       """
   
       def __init__(self):
  +        self.version = 0
           self.init_status = False
           self.non_embedding_params = {}
           self.embedding_params = {}
  @@ -42,6 +43,7 @@
               embeddings_pb = model_pb.embedding_table_info
               self._init_non_embedding_params(tensors_pb)
               self._init_embedding_params(embeddings_pb)
  +            self.version = model_pb.version
               self.init_status = True
   
       def _init_non_embedding_params(self, tensors_pb):
  diff --git a/elasticdl/python/ps/servicer.py b/elasticdl/python/ps/servicer.py
  --- a/elasticdl/python/ps/servicer.py
  +++ b/elasticdl/python/ps/servicer.py
  @@ -1,3 +1,5 @@
  +import threading
  +
   from google.protobuf import empty_pb2
   
   from elasticdl.proto import elasticdl_pb2, elasticdl_pb2_grpc
  @@ -20,6 +22,7 @@
           self._lr_staleness_modulation = lr_staleness_modulation
           self._use_async = use_async
           self._version = 0
  +        self._lock = threading.Lock()
   
       def pull_variable(self, request, _):
           # TODO: implement this RPC service
  @@ -30,7 +33,8 @@
           return elasticdl_pb2.Tensor()
   
       def push_model(self, request, _):
  -        # TODO: implement this RPC service
  +        with self._lock:
  +            self._parameters.init_from_model_pb(request)
           return empty_pb2.Empty()
   
       def push_gradient(self, request, _):
  
verification_info: {"golden_diff": "diff --git a/elasticdl/python/ps/parameters.py b/elasticdl/python/ps/parameters.py\n--- a/elasticdl/python/ps/parameters.py\n+++ b/elasticdl/python/ps/parameters.py\n@@ -18,6 +18,7 @@\n     \"\"\"\n \n     def __init__(self):\n+        self.version = 0\n         self.init_status = False\n         self.non_embedding_params = {}\n         self.embedding_params = {}\n@@ -42,6 +43,7 @@\n             embeddings_pb = model_pb.embedding_table_info\n             self._init_non_embedding_params(tensors_pb)\n             self._init_embedding_params(embeddings_pb)\n+            self.version = model_pb.version\n             self.init_status = True\n \n     def _init_non_embedding_params(self, tensors_pb):\ndiff --git a/elasticdl/python/ps/servicer.py b/elasticdl/python/ps/servicer.py\n--- a/elasticdl/python/ps/servicer.py\n+++ b/elasticdl/python/ps/servicer.py\n@@ -1,3 +1,5 @@\n+import threading\n+\n from google.protobuf import empty_pb2\n \n from elasticdl.proto import elasticdl_pb2, elasticdl_pb2_grpc\n@@ -20,6 +22,7 @@\n         self._lr_staleness_modulation = lr_staleness_modulation\n         self._use_async = use_async\n         self._version = 0\n+        self._lock = threading.Lock()\n \n     def pull_variable(self, request, _):\n         # TODO: implement this RPC service\n@@ -30,7 +33,8 @@\n         return elasticdl_pb2.Tensor()\n \n     def push_model(self, request, _):\n-        # TODO: implement this RPC service\n+        with self._lock:\n+            self._parameters.init_from_model_pb(request)\n         return empty_pb2.Empty()\n \n     def push_gradient(self, request, _):\n", "issue": "[PS-1] RPC service `push_model` implementation\n\n", "before_files": [{"content": "import tensorflow as tf\n\nfrom elasticdl.python.common.ndarray import tensor_to_ndarray\nfrom elasticdl.python.ps.embedding_table import create_embedding_table\n\n\nclass Parameters(object):\n    \"\"\"\n    There are two kinds of parameters:\n\n    1. non-embedding parameters, or dense tensor parameters. We save it\n       in a hashmap `non-embedding_params`, the key is the parameter name,\n       the value is a tf.Variable` object.\n    2. embedding parameters, or row-sparse parameters. We save it in a\n       hashmap `embedding_params`, the key is the embedding layer name,\n       the value is an `EmbeddingTable` object.\n\n    \"\"\"\n\n    def __init__(self):\n        self.init_status = False\n        self.non_embedding_params = {}\n        self.embedding_params = {}\n\n    def get_embedding_param(self, name, indices):\n        if name not in self.embedding_params:\n            raise ValueError(\n                \"Please initialize embedding param %s first!\", name\n            )\n        return self.embedding_params[name].get(indices)\n\n    def set_embedding_param(self, name, indices, values):\n        if name not in self.embedding_params:\n            raise ValueError(\n                \"Please initialize embedding param %s first!\", name\n            )\n        self.embedding_params[name].set(indices, values)\n\n    def init_from_model_pb(self, model_pb):\n        if not self.init_status:\n            tensors_pb = model_pb.param\n            embeddings_pb = model_pb.embedding_table_info\n            self._init_non_embedding_params(tensors_pb)\n            self._init_embedding_params(embeddings_pb)\n            self.init_status = True\n\n    def _init_non_embedding_params(self, tensors_pb):\n        for pb in tensors_pb:\n            name = pb.name\n            arr = tensor_to_ndarray(pb)\n            var = tf.Variable(name=name, initial_value=arr, trainable=True)\n            self.non_embedding_params[name] = var\n\n    def _init_embedding_params(self, embeddings_pb):\n        for pb in embeddings_pb:\n            self.embedding_params[pb.name] = create_embedding_table(pb)\n", "path": "elasticdl/python/ps/parameters.py"}, {"content": "from google.protobuf import empty_pb2\n\nfrom elasticdl.proto import elasticdl_pb2, elasticdl_pb2_grpc\n\n\nclass PserverServicer(elasticdl_pb2_grpc.PserverServicer):\n    \"\"\"PS service implementation\"\"\"\n\n    def __init__(\n        self,\n        parameters,\n        grads_to_wait,\n        optimizer,\n        lr_staleness_modulation=False,\n        use_async=False,\n    ):\n        self._parameters = parameters\n        self._grads_to_wait = grads_to_wait\n        self._optimizer = optimizer\n        self._lr_staleness_modulation = lr_staleness_modulation\n        self._use_async = use_async\n        self._version = 0\n\n    def pull_variable(self, request, _):\n        # TODO: implement this RPC service\n        return elasticdl_pb2.PullVariableResponse()\n\n    def pull_embedding_vector(self, request, _):\n        # TODO: implement this RPC service\n        return elasticdl_pb2.Tensor()\n\n    def push_model(self, request, _):\n        # TODO: implement this RPC service\n        return empty_pb2.Empty()\n\n    def push_gradient(self, request, _):\n        # TODO: implement this RPC service\n        return elasticdl_pb2.PushGradientResponse()\n", "path": "elasticdl/python/ps/servicer.py"}]}