# Error using CuIterator with Flux.train!

**URL:** https://discourse.julialang.org/t/error-using-cuiterator-with-flux-train/86436
**Category:** GPU
**Tags:** cuda
**Created:** [August 27, 2022, 3:58pm UTC](https://discourse.julialang.org/t/error-using-cuiterator-with-flux-train/86436 "2022-08-27T15:58:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)
#### Post date: [August 27, 2022, 3:58pm UTC](https://discourse.julialang.org/t/error-using-cuiterator-with-flux-train/86436/1 "2022-08-27T15:58:15Z")

</div>

I’m trying to use CuIterator and Flux.train!. Attempting to do so gives an error because [train! tries to get the length](https://github.com/FluxML/Flux.jl/blob/master/src/optimise/train.jl#L116) of the CuIterator. So this:

```julia
iter = CuIterator(batch(i) for i in 1:BATCH_COUNT)
Flux.train!(loss, ps, iter, opt)

```

results in an error:

`MethodError: no method matching length(::CuIterator{Base.Generator{UnitRange{Int64}, var"#17#18"}})`

It appears that [HasLength() is the default for Base.IteratorSize](https://github.com/JuliaLang/julia/blob/master/base/generator.jl#L94) and CuIterator doesn’t specify it, so it uses the default.

I can workaround it by setting it, but… should this be in CUDA.jl, or is there some other way I should be doing this?

```julia
 Base.IteratorSize(::Type{<:CuIterator}) = Base.SizeUnknown()

```

---

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [August 27, 2022, 5:31pm UTC](https://discourse.julialang.org/t/error-using-cuiterator-with-flux-train/86436/2 "2022-08-27T17:31:32Z")

</div>

I have seen the same but forgot to make an issue/pr. I think the correct(er) fix is to delegate both `HasLength` and `length` to the wrapped iterator.

---

<div class="post-metadata">

### Author: ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)
#### Post date: [August 29, 2022, 1:54pm UTC](https://discourse.julialang.org/t/error-using-cuiterator-with-flux-train/86436/3 "2022-08-29T13:54:59Z")

</div>

> [@DrChainsaw](#):
>
> delegate both `HasLength` and `length` to the wrapped iterator.

Right. Thanks for your help! I [created an issue](https://github.com/JuliaGPU/CUDA.jl/issues/1583).
