Skip to content

[dynamo] fix functools.reduce() function with None as initial#116398

Closed
XuehaiPan wants to merge 2 commits intopytorch:mainfrom
XuehaiPan:fix-dynamo-reduce-with-initial
Closed

[dynamo] fix functools.reduce() function with None as initial#116398
XuehaiPan wants to merge 2 commits intopytorch:mainfrom
XuehaiPan:fix-dynamo-reduce-with-initial

Conversation

@XuehaiPan
Copy link
Copy Markdown
Collaborator

@XuehaiPan XuehaiPan commented Dec 25, 2023

The initial argument in functools.reduce can be None.

initial_missing = object()

def reduce(function, iterable, initial=initial_missing, /):
    it = iter(iterable)
    if initial is initial_missing:
        value = next(it)
    else:
        value = initial
    for element in it:
        value = function(value, element)
    return value

Reference:

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @aakhundov @kadeng

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot bot commented Dec 25, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/116398

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 116948d with merge base c7e9c15 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

)

def call_reduce(self, tx, function, iterable, initializer=None):
def call_reduce(self, tx, function, iterable, initial=_SENTINEL):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, gosh this seems really hacky, but it is unfortunately, the best way to do it... Sigh...

@XuehaiPan
Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 25, 2023
@pytorchmergebot
Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

facebook-github-bot pushed a commit to pytorch/benchmark that referenced this pull request Jan 2, 2024
Summary:
The `initial` argument in `functools.reduce` can be `None`.

```python
initial_missing = object()

def reduce(function, iterable, initial=initial_missing, /):
    it = iter(iterable)
    if initial is initial_missing:
        value = next(it)
    else:
        value = initial
    for element in it:
        value = function(value, element)
    return value
```

Reference:

- python/cpython#102759

X-link: pytorch/pytorch#116398
Approved by: https://github.com/Skylion007

Reviewed By: izaitsevfb, jeanschmidt

Differential Revision: D52424773

fbshipit-source-id: 36d3a6f5f5786350fe8c325773deb135ccbd48e9
@XuehaiPan XuehaiPan deleted the fix-dynamo-reduce-with-initial branch April 30, 2024 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants