prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Port ensembling HOWTO from old diff based system
  And instead, use a standalone doc with tests like in #771
  
  Here is the old (pre-Linen) HOWTO diff, for reference:
  https://github.com/google/flax/blob/master/howtos/diffs/ensembling.diff
  
  </issue>
  <code>
  [start of docs/_ext/codediff.py]
  1 # Copyright 2020 The Flax Authors.
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # you may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at
  6 #
  7 #      http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 # Unless required by applicable law or agreed to in writing, software
  10 # distributed under the License is distributed on an "AS IS" BASIS,
  11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 # See the License for the specific language governing permissions and
  13 # limitations under the License.
  14 import dataclasses
  15 from typing import Optional, Sequence
  16 import itertools
  17 
  18 from docutils import nodes
  19 from docutils.parsers.rst import directives
  20 from docutils.statemachine import ViewList
  21 
  22 import sphinx
  23 from sphinx.util.docutils import SphinxDirective
  24 """Sphinx directive for creating code diff tables.
  25 
  26 Use directive as follows:
  27 
  28 .. codediff::
  29   :title-left: <LEFT_CODE_BLOCK_TITLE>
  30   :title-right: <RIGHT_CODE_BLOCK_TITLE>
  31   :highlight-left: <LINES_TO_HIGHLIGHT_LEFT>
  32   :highlight-right: <LINES_TO_HIGHLIGHT_RIGHT>
  33   
  34   <CODE_BLOCK_LEFT>
  35   ---
  36   <CODE_BLOCK_RIGHT>
  37 """
  38 
  39 class CodeDiffParser:
  40   def parse(self, lines, title_left='Base', title_right='Diff', code_sep='---'):
  41     if code_sep not in lines:
  42       raise ValueError('Code separator not found! Code snippets should be '
  43                        f'separated by {code_sep}.')
  44     idx = lines.index(code_sep)
  45     code_left = self._code_block(lines[0: idx])
  46     code_right = self._code_block(lines[idx+1:])
  47     
  48     self.max_left = max(len(x) for x in code_left + [title_left])
  49     self.max_right = max(len(x) for x in code_right + [title_right])
  50 
  51     output = [
  52       self._hline(),
  53       self._table_row(title_left, title_right),
  54       self._hline(),
  55     ]
  56 
  57     for l, r in itertools.zip_longest(code_left, code_right, fillvalue=''):
  58       output += [self._table_row(l, r)]
  59 
  60     return output + [self._hline()]
  61 
  62   def _code_block(self, lines):
  63     # Remove right trailing whitespace so we can detect the comments.
  64     lines = [x.rstrip() for x in lines]
  65     highlight = lambda x : x.endswith('#!')
  66     code = map(lambda x : x[:-2].rstrip() if highlight(x) else x, lines)
  67     highlights = [i+1 for i in range(len(lines)) if highlight(lines[i])]
  68     highlights = ','.join(str(i) for i in highlights)
  69 
  70     directive = ['.. code-block:: python']
  71     if highlights:
  72       directive += [f'  :emphasize-lines: {highlights}']
  73 
  74     # Indent code and add empty line so the code is picked up by the directive.
  75     return directive + [''] + list(map(lambda x: '  ' + x, code))
  76 
  77   def _hline(self):
  78     return '+' + '-'*(self.max_left+2) + '+' + '-'*(self.max_right+2) + '+'
  79 
  80   def _rfill(self, text, max_len):
  81     return text + ' ' * (max_len-len(text))
  82 
  83   def _table_row(self, left, right):
  84     text_left = self._rfill(left, self.max_left)
  85     text_right = self._rfill(right, self.max_right)
  86     return '| ' + text_left + ' | ' + text_right + ' |'
  87 
  88 
  89 class CodeDiffDirective(SphinxDirective):
  90   has_content = True
  91   option_spec = {
  92     'title_left': directives.unchanged,
  93     'title_right': directives.unchanged,
  94     'code_sep': directives.unchanged,
  95     }
  96 
  97   def run(self):    
  98     new_content = CodeDiffParser().parse(list(self.content), **self.options)
  99 
  100     node = nodes.paragraph()
  101     self.content = ViewList(new_content, self.content.parent)
  102     self.state.nested_parse(self.content, self.content_offset, node)
  103     return [node]
  104 
  105 def setup(app):
  106     app.add_directive('codediff', CodeDiffDirective)
  107 
  108     return {
  109         'version': sphinx.__display_version__,
  110         'parallel_read_safe': True,
  111         'parallel_write_safe': True,
  112     }
  113
  [end of docs/_ext/codediff.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/docs/_ext/codediff.py b/docs/_ext/codediff.py
  --- a/docs/_ext/codediff.py
  +++ b/docs/_ext/codediff.py
  @@ -26,14 +26,14 @@
   Use directive as follows:
   
   .. codediff::
  -  :title-left: <LEFT_CODE_BLOCK_TITLE>
  -  :title-right: <RIGHT_CODE_BLOCK_TITLE>
  -  :highlight-left: <LINES_TO_HIGHLIGHT_LEFT>
  -  :highlight-right: <LINES_TO_HIGHLIGHT_RIGHT>
  +  :title_left: <LEFT_CODE_BLOCK_TITLE>
  +  :title_right: <RIGHT_CODE_BLOCK_TITLE>
     
     <CODE_BLOCK_LEFT>
     ---
     <CODE_BLOCK_RIGHT>
  +
  +In order to highlight a line of code, prepend it with "#!".
   """
   
   class CodeDiffParser:
  @@ -94,7 +94,7 @@
       'code_sep': directives.unchanged,
       }
   
  -  def run(self):    
  +  def run(self):
       new_content = CodeDiffParser().parse(list(self.content), **self.options)
   
       node = nodes.paragraph()
  
verification_info: {"golden_diff": "diff --git a/docs/_ext/codediff.py b/docs/_ext/codediff.py\n--- a/docs/_ext/codediff.py\n+++ b/docs/_ext/codediff.py\n@@ -26,14 +26,14 @@\n Use directive as follows:\n \n .. codediff::\n-  :title-left: <LEFT_CODE_BLOCK_TITLE>\n-  :title-right: <RIGHT_CODE_BLOCK_TITLE>\n-  :highlight-left: <LINES_TO_HIGHLIGHT_LEFT>\n-  :highlight-right: <LINES_TO_HIGHLIGHT_RIGHT>\n+  :title_left: <LEFT_CODE_BLOCK_TITLE>\n+  :title_right: <RIGHT_CODE_BLOCK_TITLE>\n   \n   <CODE_BLOCK_LEFT>\n   ---\n   <CODE_BLOCK_RIGHT>\n+\n+In order to highlight a line of code, prepend it with \"#!\".\n \"\"\"\n \n class CodeDiffParser:\n@@ -94,7 +94,7 @@\n     'code_sep': directives.unchanged,\n     }\n \n-  def run(self):    \n+  def run(self):\n     new_content = CodeDiffParser().parse(list(self.content), **self.options)\n \n     node = nodes.paragraph()\n", "issue": "Port ensembling HOWTO from old diff based system\nAnd instead, use a standalone doc with tests like in #771\r\n\r\nHere is the old (pre-Linen) HOWTO diff, for reference:\r\nhttps://github.com/google/flax/blob/master/howtos/diffs/ensembling.diff\n", "before_files": [{"content": "# Copyright 2020 The Flax Authors.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport dataclasses\nfrom typing import Optional, Sequence\nimport itertools\n\nfrom docutils import nodes\nfrom docutils.parsers.rst import directives\nfrom docutils.statemachine import ViewList\n\nimport sphinx\nfrom sphinx.util.docutils import SphinxDirective\n\"\"\"Sphinx directive for creating code diff tables.\n\nUse directive as follows:\n\n.. codediff::\n  :title-left: <LEFT_CODE_BLOCK_TITLE>\n  :title-right: <RIGHT_CODE_BLOCK_TITLE>\n  :highlight-left: <LINES_TO_HIGHLIGHT_LEFT>\n  :highlight-right: <LINES_TO_HIGHLIGHT_RIGHT>\n  \n  <CODE_BLOCK_LEFT>\n  ---\n  <CODE_BLOCK_RIGHT>\n\"\"\"\n\nclass CodeDiffParser:\n  def parse(self, lines, title_left='Base', title_right='Diff', code_sep='---'):\n    if code_sep not in lines:\n      raise ValueError('Code separator not found! Code snippets should be '\n                       f'separated by {code_sep}.')\n    idx = lines.index(code_sep)\n    code_left = self._code_block(lines[0: idx])\n    code_right = self._code_block(lines[idx+1:])\n    \n    self.max_left = max(len(x) for x in code_left + [title_left])\n    self.max_right = max(len(x) for x in code_right + [title_right])\n\n    output = [\n      self._hline(),\n      self._table_row(title_left, title_right),\n      self._hline(),\n    ]\n\n    for l, r in itertools.zip_longest(code_left, code_right, fillvalue=''):\n      output += [self._table_row(l, r)]\n\n    return output + [self._hline()]\n\n  def _code_block(self, lines):\n    # Remove right trailing whitespace so we can detect the comments.\n    lines = [x.rstrip() for x in lines]\n    highlight = lambda x : x.endswith('#!')\n    code = map(lambda x : x[:-2].rstrip() if highlight(x) else x, lines)\n    highlights = [i+1 for i in range(len(lines)) if highlight(lines[i])]\n    highlights = ','.join(str(i) for i in highlights)\n\n    directive = ['.. code-block:: python']\n    if highlights:\n      directive += [f'  :emphasize-lines: {highlights}']\n\n    # Indent code and add empty line so the code is picked up by the directive.\n    return directive + [''] + list(map(lambda x: '  ' + x, code))\n\n  def _hline(self):\n    return '+' + '-'*(self.max_left+2) + '+' + '-'*(self.max_right+2) + '+'\n\n  def _rfill(self, text, max_len):\n    return text + ' ' * (max_len-len(text))\n\n  def _table_row(self, left, right):\n    text_left = self._rfill(left, self.max_left)\n    text_right = self._rfill(right, self.max_right)\n    return '| ' + text_left + ' | ' + text_right + ' |'\n\n\nclass CodeDiffDirective(SphinxDirective):\n  has_content = True\n  option_spec = {\n    'title_left': directives.unchanged,\n    'title_right': directives.unchanged,\n    'code_sep': directives.unchanged,\n    }\n\n  def run(self):    \n    new_content = CodeDiffParser().parse(list(self.content), **self.options)\n\n    node = nodes.paragraph()\n    self.content = ViewList(new_content, self.content.parent)\n    self.state.nested_parse(self.content, self.content_offset, node)\n    return [node]\n\ndef setup(app):\n    app.add_directive('codediff', CodeDiffDirective)\n\n    return {\n        'version': sphinx.__display_version__,\n        'parallel_read_safe': True,\n        'parallel_write_safe': True,\n    }\n", "path": "docs/_ext/codediff.py"}]}