For these specific input cases you can use
import tensorflow as tf
x = tf.range(1,8)
out_frame_2_1 = tf.signal.frame(x, 2, 1)
out_frame_2_2 = tf.signal.frame(x, 2, 2)
print(out_frame_2_1)
print(out_frame_2_2)

But more in general for pytorch unfold see

  

      github.com/tensorflow/tensorflow
  

  
    
  
	  
  

  
    
      torch.unfold function is needed..
    

    
      
        opened 
        
          
        
        Jul 26, 2019
      
      

        
          closed 
        
          
        
        Aug 6, 2019
      
        

      
        
          
          seolhokim
        
      
    

    
        API review
        TF 1.14
        comp:apis
        type:feature
    
  


  
    <em>Please make sure that this is a feature request. As per our [GitHub Policy](…https://github.com/tensorflow/tensorflow/blob/master/ISSUES.md), we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:feature_template</em>


**System information**
- TensorFlow version (you are using):1.14
- Are you willing to contribute it (Yes/No):Yes



**Describe the feature and the current behavior/state.**
I think sending the function page in torch is the best. https://pytorch.org/docs/stable/_modules/torch/nn/modules/fold.html
**Will this change the current api? How?**
I guess it doesn't.
**Who will benefit with this feature?**
I think it will be popular in Vision Models, cause self attention is arising now to find out relationship in input pixels.(Stand-Alone Self-Attention in Vision Models)


**Any Other info.**
  

  

  
    
    
  

  



  
      

      TensorFlow
  

  
    

tf.image.extract_patches  |  TensorFlow Core v2.5.0

  Extract patches from images.


  

  
    
    
  

  


For more complex use cases you could have some performance issues to solve. See:

  

      github.com/pytorch/xla
  

  
    
  
	  
  

  
    
      Lowering `unfold`
    

    
      
        opened 
        
          
        
        Jun 18, 2020
      
      


      
        
          
          ibeltagy
        
      
    

    
        nostale
        op lowering
    
  


  
    ## 🚀 Feature
Add a lowering for `unfold`. 

## Motivation

I want to run Lo…ngformer ([model code on HF repo](https://github.com/huggingface/transformers/blob/master/src/transformers/modeling_longformer.py)) on pytroch-xla, and this requires an overlapping sliding window operation which needs a lowering for `unfold`.

## Pitch

Add a lowering for `unfold`

## Alternatives

Use `as_strided` but the current implementation is limited as discussed in [this issue](https://github.com/pytorch/xla/issues/2238).


## Additional context

Below is the metric report for the forward pass of Longformer with `unfold`. It has entries for `aten::unfold`.


```
Metric: CompileTime                                                                                                                                                                             [194/1996]
  TotalSamples: 40                                                                                                                                                                                        
  Accumulator: 06m12s060ms761.186us                                                                                                                                                                       
  ValueRate: 985ms703.787us / second                                                                                                                                                                      
  Rate: 0.105865 / second                                                                                                                                                                                 
  Percentiles: 1%=002ms604.019us; 5%=002ms103.276us; 10%=002ms209.085us; 20%=031ms487.158us; 50%=11s482ms222.482us; 80%=14s789ms136.836us; 90%=14s427ms259.848us; 95%=15s075ms200.017us; 99%=15s212ms201.$
81us                                                                                                                                                                                                      
Metric: DeviceLockWait                                                                                                                                                                                    
  TotalSamples: 73                                                                                                                                                                                        
  Accumulator: 277.621us                                                                                                                                                                                  
  ValueRate: 000.765us / second                                                                                                                                                                           
  Rate: 0.201229 / second                                                                                                                                                                                 
  Percentiles: 1%=002.159us; 5%=002.515us; 10%=002.707us; 20%=002.944us; 50%=003.671us; 80%=004.275us; 90%=004.708us; 95%=004.854us; 99%=015.004us                                                        
Metric: ExecuteTime                                                                                                                                                                                       
  TotalSamples: 73                                                                                                                                                                                        
  Accumulator: 03s919ms069.706us                                                                                                                                                                          
  ValueRate: 008ms722.713us / second                                                                                                                                                                      
  Rate: 0.193129 / second                                                                                                                                                                                 
  Percentiles: 1%=001ms485.104us; 5%=002ms714.332us; 10%=002ms000.342us; 20%=002ms237.048us; 50%=003ms337.952us; 80%=098ms610.960us; 90%=126ms721.599us; 95%=139ms781.481us; 99%=154ms800.680us           
Metric: InboundData                                                                                                                                                                                       
  TotalSamples: 72                                                                                                                                                                                        
  Accumulator: 234.19MB                                                                                                                                                                                   
  ValueRate: 634.49KB / second                                                                                                                                                                            
  Rate: 0.190499 / second                                                                                                                                                                                 
  Percentiles: 1%=1.00B; 5%=1.00B; 10%=1.00B; 20%=8.00KB; 50%=6.00MB; 80%=6.00MB; 90%=7.50MB; 95%=7.50MB; 99%=7.50MB                                                                                      
Metric: InputOutputAliasCount                                                                                                                                                                             
  TotalSamples: 1                                                                                                                                                                                         
  Accumulator: 271.00                                                                                                                                                                                     
  Percentiles: 1%=271.00; 5%=271.00; 10%=271.00; 20%=271.00; 50%=271.00; 80%=271.00; 90%=271.00; 95%=271.00; 99%=271.00                                                                                   
Metric: IrValueTensorToXlaData                                                                                                                                                                            
  TotalSamples: 331                                                                                                                                                                                       
  Accumulator: 03s006ms264.150us                                                                                                                                                                          
  ValueRate: 008ms922.872us / second                                                                                                                                                                      
  Rate: 0.872335 / second                                                                                                                                                                                 
  Percentiles: 1%=863.555us; 5%=967.491us; 10%=001ms069.569us; 20%=001ms215.703us; 50%=002ms606.635us; 80%=007ms211.581us; 90%=022ms513.355us; 95%=029ms074.835us; 99%=067ms409.847us                     
Metric: OutboundData                                                                                                                                                                                      
  TotalSamples: 335                                                                                                                                                                                       
  Accumulator: 1.01GB                                                                                                                                                                                     
  ValueRate: 2.73MB / second
  Rate: 0.881721 / second
  Percentiles: 1%=3.00KB; 5%=3.00KB; 10%=3.00KB; 20%=3.00KB; 50%=14.00KB; 80%=2.25MB; 90%=10.50MB; 95%=10.50MB; 99%=18.00MB
Metric: ReleaseDataHandlesTime
  TotalSamples: 81
  Accumulator: 333ms705.496us
  ValueRate: 880.219us / second
  Rate: 0.214297 / second
  Percentiles: 1%=382.511us; 5%=474.639us; 10%=522.986us; 20%=611.054us; 50%=001ms050.138us; 80%=001ms216.637us; 90%=003ms012.896us; 95%=031ms474.989us; 99%=038ms143.816us
Metric: TensorsGraphSize
  TotalSamples: 73
  Accumulator: 83903.00
  ValueRate: 222.06 / second
  Rate: 0.193203 / second
  Percentiles: 1%=4.00; 5%=4.00; 10%=4.00; 20%=23.00; 50%=67.00; 80%=2874.00; 90%=3673.00; 95%=4075.00; 99%=4474.00
Metric: TransferFromServerTime                                                                                                                                                                  [141/1996]
  TotalSamples: 72
  Accumulator: 850ms040.762us
  ValueRate: 002ms249.054us / second
  Rate: 0.190499 / second
  Percentiles: 1%=850.857us; 5%=001ms079.063us; 10%=001ms135.278us; 20%=001ms285.135us; 50%=015ms444.166us; 80%=021ms375.969us; 90%=027ms938.459us; 95%=030ms432.630us; 99%=046ms339.680us
Metric: TransferToServerTime
  TotalSamples: 335
  Accumulator: 03s025ms272.057us
  ValueRate: 008ms972.967us / second
  Rate: 0.882877 / second
  Percentiles: 1%=857.302us; 5%=959.191us; 10%=001ms060.268us; 20%=001ms210.822us; 50%=002ms606.569us; 80%=007ms260.753us; 90%=021ms492.181us; 95%=029ms982.476us; 99%=067ms384.995us
Metric: TransferToServerTransformTime
  TotalSamples: 335
  Accumulator: 460ms996.455us
  ValueRate: 001ms210.712us / second
  Rate: 0.881721 / second
  Percentiles: 1%=087.734us; 5%=094.554us; 10%=099.654us; 20%=107.230us; 50%=268.367us; 80%=612.733us; 90%=003ms313.737us; 95%=006ms138.063us; 99%=009ms517.447us
Counter: CachedCompile
  Value: 33
Counter: CreateCompileHandles
  Value: 40
Counter: CreateDataHandles
  Value: 692
Counter: CreateXlaTensor
  Value: 3897
Counter: DestroyDataHandles
  Value: 343
Counter: DestroyXlaTensor
  Value: 3608
Counter: MarkStep
  Value: 1
Counter: ReleaseDataHandles
  Value: 343
Counter: UncachedCompile
  Value: 40
Counter: XRTAllocateFromTensor_Empty
  Value: 20
Counter: XrtCompile_Empty
  Value: 144
Counter: XrtExecuteChained_Empty
  Value: 144
Counter: XrtExecute_Empty
  Value: 144
Counter: XrtRead_Empty
  Value: 144
Counter: XrtReleaseAllocationHandle_Empty
  Value: 144
Counter: XrtReleaseCompileHandle_Empty
  Value: 144
Counter: XrtSessionCount
  Value: 10
Counter: XrtSubTuple_Empty
  Value: 144
Counter: aten::_local_scalar_dense
  Value: 12
Counter: aten::unfold
  Value: 60
Counter: xla::_softmax
  Value: 12
Counter: xla::_unsafe_view
  Value: 72
Counter: xla::add
  Value: 27
Counter: xla::add_
  Value: 84
Counter: xla::addcmul
  Value: 25
Counter: xla::addmm
  Value: 1
Counter: xla::as_strided
  Value: 271
Counter: xla::bmm
  Value: 36
Counter: xla::clone
  Value: 24
Counter: xla::constant_pad_nd
  Value: 48
Counter: xla::copy_
  Value: 394
Counter: xla::cumsum
  Value: 1
Counter: xla::div_
  Value: 12
Counter: xla::embedding
  Value: 3
Counter: xla::empty
  Value: 359
Counter: xla::empty_strided
  Value: 271
Counter: xla::eq
  Value: 48
Counter: xla::expand
  Value: 120
Counter: xla::fill_
  Value: 36
Counter: xla::flip
  Value: 48
Counter: xla::gelu
  Value: 12
Counter: xla::gt
  Value: 12
Counter: xla::index_select
  Value: 3
Counter: xla::le
  Value: 12
Counter: xla::lt
  Value: 12
Counter: xla::masked_fill_
  Value: 72
Counter: xla::max
  Value: 12
Counter: xla::mm
 Value: 72
Counter: xla::mul
  Value: 2
Counter: xla::native_batch_norm
  Value: 25
Counter: xla::native_layer_norm
  Value: 25
Counter: xla::ne
  Value: 13
Counter: xla::permute
  Value: 180
Counter: xla::rsub
  Value: 1
Counter: xla::select
  Value: 97
Counter: xla::slice
  Value: 999
Counter: xla::squeeze
  Value: 24
Counter: xla::sum
  Value: 12
Counter: xla::t
  Value: 73
Counter: xla::tanh
  Value: 1
Counter: xla::transpose
  Value: 240
Counter: xla::tril
  Value: 24
Counter: xla::unsqueeze
  Value: 170
Counter: xla::view
  Value: 644
Counter: xla::zero_
  Value: 1
Metric: XrtAllocateFromTensor
  TotalSamples: 48135
  Accumulator: 01m10s487ms137.203us
  Mean: 002ms504.791us
  StdDev: 006ms961.073us
  Rate: 1.03083 / second
  Percentiles: 25%=295.798us; 50%=458.079us; 80%=002ms686.172us; 90%=003ms916.758us; 95%=004ms695.148us; 99%=008ms407.314us
Metric: XrtCompile
  TotalSamples: 2122
  Accumulator: 10m56s974ms699.040us
  Mean: 505ms763.352us
  StdDev: 02s338ms482.396us
  Rate: 0.114957 / second
  Percentiles: 25%=008ms570.206us; 50%=008ms862.980us; 80%=008ms259.798us; 90%=009ms638.784us; 95%=611ms324.713us; 99%=13s233ms291.015us
Metric: XrtExecute
  TotalSamples: 20796
  Accumulator: 02m59s103ms661.768us
  Mean: 004ms131.993us
  StdDev: 017ms650.704us
  Rate: 0.114971 / second
  Percentiles: 25%=851.542us; 50%=956.518us; 80%=001ms210.393us; 90%=002ms377.763us; 95%=006ms024.523us; 99%=110ms012.002us
Metric: XrtExecutorEvict
  TotalSamples: 0
  Accumulator: nanB
  Mean: nanB
  StdDev: nanB
  Percentiles:
Metric: XrtReadLiteral
  TotalSamples: 10335
  Accumulator: 05s641ms262.404us
  Mean: 774.616us
  StdDev: 002ms725.146us
  Rate: 0.114966 / second
  Percentiles: 25%=269.442us; 50%=343.087us; 80%=470.896us; 90%=583.015us; 95%=005ms062.565us; 99%=010ms496.053us
Metric: XrtReleaseAllocation
  TotalSamples: 34172
  Accumulator: 02s911ms410.970us
  Mean: 185.145us
  StdDev: 322.759us
  Rate: 0.115061 / second
  Percentiles: 25%=020.634us; 50%=033.172us; 80%=338.061us; 90%=648.733us; 95%=861.389us; 99%=002ms549.868us
Metric: XrtReleaseCompilation
  TotalSamples: 518
  Accumulator: 002ms770.287us
  Mean: 003.418us
  StdDev: 002.299us
  Rate: 81.2152 / second
  Percentiles: 25%=002.889us; 50%=003.118us; 80%=003.383us; 90%=003.659us; 95%=003.945us; 99%=019.823us
```