- 
                Notifications
    You must be signed in to change notification settings 
- Fork 11
Allow empty sub-containers in reductions #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            majosm
  wants to merge
  1
  commit into
  inducer:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
majosm:empty-subcontainers
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -464,7 +464,7 @@ def rec_map_reduce_array_container( | |
|  | ||
| or any other such traversal. | ||
| """ | ||
| def rec(_ary: ArrayOrContainerT) -> ArrayOrContainerT: | ||
| def rec(_ary: ArrayOrContainerT) -> Optional[ArrayOrContainerT]: | ||
| if type(_ary) is leaf_class: | ||
| return map_func(_ary) | ||
| else: | ||
|  | @@ -473,11 +473,22 @@ def rec(_ary: ArrayOrContainerT) -> ArrayOrContainerT: | |
| except NotAnArrayContainerError: | ||
| return map_func(_ary) | ||
| else: | ||
| return reduce_func([ | ||
| rec(subary) for _, subary in iterable | ||
| ]) | ||
| subary_results = [ | ||
| rec(subary) for _, subary in iterable] | ||
| filtered_subary_results = [ | ||
| result for result in subary_results | ||
| if result is not None] | ||
| if len(filtered_subary_results) > 0: | ||
| return reduce_func(filtered_subary_results) | ||
| else: | ||
| return None | ||
|  | ||
| return rec(ary) | ||
| result = rec(ary) | ||
|  | ||
| if result is None: | ||
| raise ValueError("cannot reduce empty array container") | ||
|  | ||
| return result | ||
|  | ||
|  | ||
| def rec_multimap_reduce_array_container( | ||
|  | @@ -503,12 +514,23 @@ def rec_multimap_reduce_array_container( | |
| # NOTE: this wrapper matches the signature of `deserialize_container` | ||
| # to make plugging into `_multimap_array_container_impl` easier | ||
| def _reduce_wrapper(ary: ContainerT, iterable: Iterable[Tuple[Any, Any]]) -> Any: | ||
| return reduce_func([subary for _, subary in iterable]) | ||
| filtered_subary_results = [ | ||
| result for _, result in iterable | ||
| if result is not None] | ||
| if len(filtered_subary_results) > 0: | ||
| return reduce_func(filtered_subary_results) | ||
| else: | ||
| return None | ||
|  | ||
| return _multimap_array_container_impl( | ||
| result = _multimap_array_container_impl( | ||
| map_func, *args, | ||
| reduce_func=_reduce_wrapper, leaf_cls=leaf_class, recursive=True) | ||
|  | ||
| if result is None: | ||
| raise ValueError("cannot reduce empty array container") | ||
| 
      Comment on lines
    
      +529
     to 
      +530
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above. | ||
|  | ||
| return result | ||
|  | ||
| # }}} | ||
|  | ||
|  | ||
|  | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I think reducing over an empty container is well-defined: just return the neutral element.
reduce_funcshould be trusted to do the right thing. What was the reasoning for putting in this error message? Basically, what I'm pushing for is to get rid of it and returnreduce_func([])instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
reduce_funcworked for an empty list, I think this PR wouldn't be needed, right?The motivation for this was for things like
actx.np.maxwherereduce_funcdoesn't (currently) behave nicely for empty inputs. Should that maybe be fixed instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, calling
np.maxon an empty list raisesand requires specifying an initial value with
np.max([], initial=np.inf). We should probably do something like that inactx.nptoo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great point. I had not thought far enough to realize that, but I agree now.
I think that's exactly what we should do.
I agree. While I wasn't loving numpy's interface of "no implicit neutral" element at first, I think it makes sense from the perspective of integers. I think we should aim to replicate this, both at the level of
actx.np, and for the array container reductions under discussion here.One subtlety is the
ValueErrorfor empty arrays. For symbolically-shapedpytatoarrays, it may not be possible to do a perfect job.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some work towards that:
ReductionOperationclass, accept 'initial' in reductions pytato#238That latter PR needs some more work done to it. @majosm, could you take a look?